0

I have a BMP format image that is archived in the following manner

  for (j = 0; j < 240; j++) {
    for(i=0;i<320;i++) { 
      data_temp = LCD_ReadRAM();
      image_buf[i*2+1] = (data_temp&0xff00) >> 8;
      image_buf[i*2+0] = data_temp & 0x00ff;

    }
    ret = f_write(&file, image_buf, 640, &bw);

where LCD_ReadRam function reads a pixel at a time from the LCD screen

I want to know, How could I get the pixel positions of this image file. And how to save the values of each pixel in an [320][240] matrix
Any help would be appreciated, Thanks.

Matthew G.
  • 1,298
  • 10
  • 24
Turcu Daniel
  • 1
  • 1
  • 2
  • 1
    What do you mean "get the pixel positions"? Can you not just use `i` and `j` as your X and Y (respectively)? – Drew McGowen Jul 25 '13 at 13:20
  • It's difficult to answer without looking at `LCD_Read_RAM()`. However normally in this type of application the pixel values are returned from top left to right. Assuming this is correct, you can assign the pixel value to a two dimensional array in side the inner loop `img[i][j] = data_temp;`. – Thanushan Jul 25 '13 at 13:21
  • the bmp is saved corect . it can be viewed on a pc .. i want a function that does the exact oposite – Turcu Daniel Jul 25 '13 at 13:48

3 Answers3

1

A BMP file reader does what you want. You can get any good BMP file reader and tweak it to your purposes. For example: this question and answer gives a BMP file reader that assumes 24-bit BMP format. Your format is 16-bit, so it requires some tweaking.

Here is my attempt at doing that (didn't test, so you should take the hard-coded details with a grain of salt).

int i;
FILE* f = fopen(filename, "rb");
unsigned char info[54];
fread(info, sizeof(unsigned char), 54, f); // read the 54-byte header

int width = 320, height = 240; // might want to extract that info from BMP header instead

int size_in_file = 2 * width * height;
unsigned char* data_from_file = new unsigned char[size_in_file];
fread(data_from_file, sizeof(unsigned char), size_in_file, f); // read the rest
fclose(f);

unsigned char pixels[240 * 320][3];

for(i = 0; i < width * height; ++i)
{
    unsigned char temp0 = data_from_file[i * 2 + 0];
    unsigned char temp1 = data_from_file[i * 2 + 1];
    unsigned pixel_data = temp1 << 8 | temp0;

    // Extract red, green and blue components from the 16 bits
    pixels[i][0] = pixel_data >> 11;
    pixels[i][1] = (pixel_data >> 5) & 0x3f;
    pixels[i][2] = pixel_data & 0x1f;
}

Note: this assumes that your LCD_ReadRAM function (presumably, reading stuff from your LCD memory) gives the pixels in the standard 5-6-5 format.

The name 5-6-5 signifies the number of bits in each 16-bit number allocated for each colour component (red, green, blue). There exist other allocations like 5-5-5, but i have never seen them in practice.

Community
  • 1
  • 1
anatolyg
  • 26,506
  • 9
  • 60
  • 134
  • the LCD_ReadRAM function is just return LCD_Data; where LCD_Data is a define to (*(vu16 *)LCD_BASE_Data) and LCD_BASE_Addr is a define to ((u32)(0x60000000|0x00000000)) – Turcu Daniel Jul 26 '13 at 06:35
0

If you are talking about BMP image, then there is existing BMP format.

In BMP image all pixels are written in the reversed order (starting from the last line in image) sequentially. Size is defined in the BMP header, so you will have to read it.

One more point is that each line in image has padding in order to make it multiply of 4.

Alex
  • 9,891
  • 11
  • 53
  • 87
0

You can use gimp. Open the image in gimp, use a plugin to export C code using the 16-bit mode, and its put into an array in exported C code :-).

Karibe
  • 155
  • 1
  • 10