Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

decompose image into pixels array

Status
Not open for further replies.

electroRF

Member
Hi,

I started off with a (C-Language) project which composes:

PIC18F4520 <--I2C--> GLCD-FLEXEL

I got it working, and I can draw pixels / text on the GLCD.

I wanna present different images on the GLCD display.

I'm interested in breaking down an image into an array, in which each organ will contain the color of each pixel.

Are you familiar with such method for that?

I found via google some C# methods, but since I lack experience with C# / C++, I didn't quite understand how it works.

Thanks folks.
 
Could you post a link to the C# methods.. Could give us some detail about what you are looking for.
 
...I wanna present different images on the GLCD display.

I'm interested in breaking down an image into an array, in which each organ will contain the color of each pixel.
...

Google (or Wiki) for RGB565, RGB323, or terms like "16bit colour" "24bit colour" etc.

They are all standard ways of using a single variable in the array to store all colour info for one pixel.
 
Hi mates,
Thanks.

I'll explain what I want.
Say that I have an image, PNG / JPEG / BMP, it doesn't really matter as I can manually convert the image to the format I want (i.e. PNG -> JPEG using painter).

I'd like to convert this image to an array, each organ contains RGB565 of each pixel of the image.

I'll send this array to the GLCD and it will show it on its display.


RB
I looked for RGB565 over google, but still haven't found any code implying to how to do it.
 
If you know VB it would be quite easy to load a picture in a picture box and use the point function to get the RGB values. A little shifting (dividing) would give you 565 and you can then write it out in any format you like. I assume source code would be best.

Mike.
 
Hi guys, thanks for your support :)

Pommie
I'm not familiar with Visual Basic.

misterT
I read the link you posted, which is great.
I think you missed my point.
I need help with taking an image (JPEG / BMP / PNG, doesn't matter to me), and extracting from it an array which looks like that:
array[0] = RGB of pixel row 1, column 1
array [1] = RGB of pixel row 1, column 2
etc..

What I didn't manage to find yet it a function that does that.
 
you will never find it, this is simple data manipulation. those functions you write yourself.

i guess by "organ" you mean pixel.

you may make a structure for one pixel then make an array of that type...
don't know, maybe something like

Code:
// create custom data type for image, smallest image element is one RGB pixel
typedef struct image {
	byte r;
	byte g;
	byte b;
} image;
 
// specify image size so we can adjust it in only one place when needed
#define img_row 32
#define img_col 32
#define img_size img_row*img_col

// now create an array to hold the image 
image pixel[img_size];
 
// somewhere in program manipulate content of the image pixel by pixel
for (int i=0; i<img_size;i++){ 
	pixel[i].r= 160;
	pixel[i].g= 50;
	pixel[i].b= 71 + i % 16;
}
 
Hi,
Thanks Panic.

Unfortunately, I guess I did not explain myself well again.

I'm looking for a function as follows:
- Input: Image file (any format you like - JPEG / PNG / GIF / BMP)
- Output: RGB Array (I'll handle any type of array structure).

In your function, I did not see where you transform the image file to an array
i.e. I see you already assumed an array which represents the image file, and that is exactly that Part i'm missing.
 
just use any uncompressed format and you already have RGB values in order you ask. for example BMP format is very simple to use. header is tiny, you only need few values from it, after header you get data byte by byte:
R0,G0,B0,R1,G1,B1,R2,G2,B2...
where 0,1,2 etc represent pixel number.
if you need to compact this into RGB565 for example, you just shift values around to strip least significant bits.

for example
https://cboard.cprogramming.com/c-programming/50007-reading-bitmap-image.html
 
Hi Panic,
Thanks a lot mate.

How do you get the head and data, that is my main difficulty.

I mean, say that you got a simple BMP image, as attached.
**broken link removed**

How do you get all the header bytes and data bytes?

From that point, it'd be very simple to write a C program to get the RGB565 values.
 
Last edited:
How do you get the head and data, that is my main difficulty.

How do you get all the header bytes and data bytes?

You read the BMP file as a binary format. The header is at the beginning of the data and has a well defined structure. Google for bmp file structure. How you read the BMP file as binary depends on your hardware.. are you using SD card or something else to store the image?

Here is an image of that structure:
**broken link removed**
 
Last edited:
Hi T,
sorry for the late response, I was studying the great inputs you mentioned here.

I came up with a simple code that reads bmp file (code is below and attached).
This is a 24-bit bmp file I created in MSPAINT (windows painter).

for some reason, I get the following reading:

READING.JPG

i expected to get 0x424d.

But I don't understand why I get all these F's.

The file size is (642,806 bytes), meaning 0x09CEF6.

Shouldn't the reading outcome 0x09 0xCE 0xF6?


#include <stdio.h>
#include <stdlib.h>

int main()
{
char ch, file_name[25];
FILE *fp;

printf("Enter the name of file you wish to see\n");
gets(file_name);

fp = fopen(file_name,"rb"); // read mode

if( fp == NULL )
{
perror("Error while opening the file.\n");
getch(); // for pause
exit(EXIT_FAILURE);
}

printf("\nThe content of file %s in hex is:\n", file_name);

while( ( ch = fgetc(fp) ) != EOF )
{
printf("%x",ch);
}

fclose(fp);

getch(); // for pause

return 0;


}
 
you can install IrfanView, it is a tiny program and reads tons of file formats.
as a bonus you get hex viewer (just press F3).

here is BMP header is used for my DSP:
 

Attachments

  • Spec_BMP.pdf
    21.2 KB · Views: 246
Hi Panic,
Thanks!
This application is great! :)

I just don't understand, how come when I read the bmp file via C code, I get different strange outcome.
 
Hi Ian,
Thank you.

I read the great link you provided.

I read the format the guy showed there, however, the problem is that when I read the BMP image (using the code I attached above), I get for example several F's (i.e. FFFF) between the File type. Set to "BM" and the Size in BYTES of the file.

I don't get what is the problem.

That is the reading I get:
READING.JPG

The file size is (642,806 bytes), meaning 0x09CEF6.

But what I read was: 424dfffffff6ffffffce900000...
 
I read the format the guy showed there, however, the problem is that when I read the BMP image (using the code I attached above), I get for example several F's (i.e. FFFF) between the File type. Set to "BM" and the Size in BYTES of the file.

I don't get what is the problem.

The file size is (642,806 bytes), meaning 0x09CEF6.

But what I read was: 424dfffffff6ffffffce900000...

File size is not the same as image size. File size is image size + header.

EDIT: I tried to track those hex values and could not come to a sane conclusion (I got image width of 4294967173 pixels [0xffffff85]). Maybe there is padding so that all the values are 32 bits wide, but that is only a guess. According to the link Ian posted, the third field should be a "reserved word set to zero".. and in your output I can't see eight zeros at that place..
 
Last edited:
Hi T,
According to the link Ian posted, the third field should be a "reserved word set to zero".. and in your output I can't see eight zeros at that place..
Yes, you're right, something might be wrong with the way I read.

Do you have a C Code that simply reads hex values of BMP image?
 
electro...Can you post the bitmap? I would like to see the hex file for myself

The other thing that maybe screwing it up... Are you reading the file size as a signed long?

0xFFFFFFF6 & 0xFFFFFCE9 seem like signed long's to me..
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top