GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #41  
Old 07-Mar-2009, 18:57
Silvermaul Silvermaul is offline
New Member
 
Join Date: Mar 2009
Posts: 4
Silvermaul is on a distinguished road

Re: How to get the pixels' value of a bmp image? (Pelles C)


Ι have found the errors in my program. Since the header file is 40 it means that it is not a V5 infoheader. I commented out the unecessary structs and variables from the header file. Now it works. However my new output is this one :

Size of File Header is : 14
Size of File Header is : 40

Bmp File Type is : 4d42
Bmp Size is : 263222
Bmp Reserved One is : 0
Bmp Reserved Two is : 0
Bmp Header Offset is : 1078
Bmp Header Size is : 40
Image Width is : 512
Image Height is :512
Image bpp is : 8
Image Compression is : 0
Bytes in pixel data section are : 262144

File Pointer at the beginning of storing to the 2D array is : 1078
File Pointer at the end of storing to the 2D array is : 263222
Total Number of bytes read into the array is : 262144

Why is the Header Offset 1078? What happens from byte 54 to 1078? Empty bytes with junk?
  #42  
Old 08-Mar-2009, 07:24
Silvermaul Silvermaul is offline
New Member
 
Join Date: Mar 2009
Posts: 4
Silvermaul is on a distinguished road

Re: How to get the pixels' value of a bmp image? (Pelles C)


Ιt was the pallete that I was missing! Now everything works!
  #43  
Old 16-Jun-2009, 03:43
vk10 vk10 is offline
New Member
 
Join Date: Jun 2009
Posts: 7
vk10 is on a distinguished road
Arrow

Re: How to get the pixels' value of a bmp image? (Pelles C)


hi silvermaul/dave,

could you please guide me as to how those pallettes were corrected?

appreciate your help very much...

regards,
vk
  #44  
Old 16-Jun-2009, 06:30
Silvermaul Silvermaul is offline
New Member
 
Join Date: Mar 2009
Posts: 4
Silvermaul is on a distinguished road

Re: How to get the pixels' value of a bmp image? (Pelles C)


I used a grayscale to save the pallete into a different .raw file and then is used it as a reference with all my bmps. The pallete is the same in all bmps. I also used it to convert 24bmp to 8bit grayscale bmp and it works.

Hope this helps.
  #45  
Old 25-Jun-2009, 04:16
vk10 vk10 is offline
New Member
 
Join Date: Jun 2009
Posts: 7
vk10 is on a distinguished road
Arrow

Re: How to get the pixels' value of a bmp image? (Pelles C)


Quote:
Originally Posted by Silvermaul
I used a grayscale to save the pallete into a different .raw file and then is used it as a reference with all my bmps. The pallete is the same in all bmps. I also used it to convert 24bmp to 8bit grayscale bmp and it works.

Hope this helps.

Hi Silvermaul,

Thank you very much for the reply. When I used a standard grey scale picture such as the one below,



I get the following output

Code:
sizeof: fheader: 14 iheader: 40 fheader.type = 4d42 fheader.size = 263222 fheader.reserved1 = 0 fheader.reserved2 = 0 fheader.offset = 1078 iheader.size = 40 iheader.width = 512 iheader.height = 512 iheader.planes = 1 iheader.bits = 8 iheader.compression = 0 iheader.imagesize = 262144 iheader.xresolution = 0 iheader.yresolution = 0 iheader.ncolors = 256 iheader.importantcolors = 0 Couldn't read colors for element [171][170] Press any key to continue . . .

please suggest a method to overcome this problem. Thanks a lot in advance.
  #46  
Old 25-Jun-2009, 11:07
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: How to get the pixels' value of a bmp image? (Pelles C)


Quote:
Originally Posted by vk10
...
I get the following output...

How about showing us the program that gave that output? See Footnote.

I have a program that reads that bitmap just fine.
Code:
sizeof: fheader: 14 iheader: 40 fheader.type = 4d42 fheader.size = 263222 fheader.reserved1 = 0 fheader.reserved2 = 0 fheader.offset = 1078 iheader.size = 40 iheader.width = 512 iheader.height = 512 iheader.planes = 1 iheader.bits = 8 iheader.compression = 0 iheader.imagesize = 262144 iheader.xresolution = 3780 iheader.yresolution = 3780 iheader.ncolors = 0 iheader.importantcolors = 0 Color table index value of element [171][170] = 195 (0xc3) RGB values from the color table are 0xc3c3c3


Bottom line: I respectfully suggest that in order to get help, you show us exactly what code you are using. This is always important.

Also, tell us what compiler and what version of compiler you are using. Sometimes it makes a difference to people who would like to help.

Regards,

Dave

Footnote:
I am going to guess what happened when you wanted to make a program to read the bitmap in your post.


Now, if you didn't do what I am about to show, then I apologize for wasting everyone's time (and board bandwidth).

I hate to repeat myself, but the point remains: Show us what you are working with. Maybe someone can help.

Here's my guess:

If you used my example program from post number 28 in this thread as a starting point note the following
  1. That example could read color values for a 24-bit 512x512 bitmap only. It is not a general-purpose bitmap reader. My purpose in posting the code was to help people learn from specific examples, not to give a cookbook method for reading all kinds of bitmap files. I did mention in the post that that example works for a certain specific size and shape bitmap file.

  2. If you ran that program with the 8-bit grayscale 512x512 bitmap file in your post you might get the following output:
    Code:
    sizeof: fheader: 14 iheader: 40 fheader.type = 4d42 fheader.size = 263222 fheader.reserved1 = 0 fheader.reserved2 = 0 fheader.offset = 1078 iheader.size = 40 iheader.width = 512 iheader.height = 512 iheader.planes = 1 iheader.bits = 8 iheader.compression = 0 iheader.imagesize = 262144 iheader.xresolution = 3780 iheader.yresolution = 3780 iheader.ncolors = 0 iheader.importantcolors = 0 This only works for 512x512 24-color bitmaps

    Hmmm---That's annoying.


  3. If you comment out the test that creates the "This only works for..." message you might see
    Code:
    . Header stuff is the same as before . This only works if the offset is equal to 54

    Hmmm---That's annoying.


  4. If you comment out the test that causes the "This only works if.." message, you might see
    Code:
    . Header stuff is the same as before . Couldn't read colors for element [171][170]

The moral of the story: Eliminating annoying messages doesn't always lead to a satisfactory solution.

I gave some links to places with information about bitmap file formats In post number three of this thread. They are all still live but Microsoft has moved theirs to mdsn Bitmap Storage Reference, and some day they may delete the original link.
Last edited by davekw7x : 25-Jun-2009 at 11:47.
  #47  
Old 25-Jun-2009, 17:53
vk10 vk10 is offline
New Member
 
Join Date: Jun 2009
Posts: 7
vk10 is on a distinguished road
Arrow

Re: How to get the pixels' value of a bmp image? (Pelles C)


Hi Dave,

Thanks for your reply.

As a staratup, i am using the code that you suggested in Thread 28.

I would post the code here:

CPP / C++ / C Code:
#include <stdio.h>
#include <stdio.h>
#include <math.h>
#include <conio.h>
#include <stdlib.h>
#include <windows.h>

#include "bmp_headers.h"

int main()
{
    FILE *fp;

    int i, j, k;
    char *filename = "lena512.bmp";

    unsigned char databuff[512][512][3];

    BmpFileHeader fheader;
    BmpInfoHeader iheader;

    printf("sizeof: fheader: %d   iheader: %d\n\n", sizeof(fheader),
           sizeof(iheader));
    if ((sizeof(fheader) != 14) || (sizeof(iheader) != 40)) {
        printf("Header structs are not properly packed\n");
        return 0;
    }

    if ((fp = fopen(filename, "rb")) == NULL) {
        printf("Coulsn't open file %s for reading.\n", filename);
        return 1;
    };
    if (fread(&fheader, sizeof(fheader), 1, fp) != 1) {
        printf("Couldn't read fheader.\n");
        return 0;
    }
    if (fread(&iheader, sizeof(iheader), 1, fp) != 1) {
        printf("Couldn't read iheader.\n");
        return 0;
    }
    printf("           fheader.type = %04x \n", fheader.type);
    printf("           fheader.size = %u   \n", fheader.size);
    printf("      fheader.reserved1 = %u   \n", fheader.reserved1);
    printf("      fheader.reserved2 = %u   \n", fheader.reserved2);
    printf("         fheader.offset = %u   \n", fheader.offset);
    printf("           iheader.size = %u   \n", iheader.size);
    printf("          iheader.width = %u   \n", iheader.width);
    printf("         iheader.height = %u   \n", iheader.height);
    printf("         iheader.planes = %u   \n", iheader.planes);
    printf("           iheader.bits = %u   \n", iheader.bits);
    printf("    iheader.compression = %u   \n", iheader.compression);
    printf("      iheader.imagesize = %u   \n", iheader.imagesize);
    printf("    iheader.xresolution = %u   \n", iheader.xresolution);
    printf("    iheader.yresolution = %u   \n", iheader.yresolution);
    printf("        iheader.ncolors = %u   \n", iheader.ncolors);
    printf("iheader.importantcolors = %u   \n", iheader.importantcolors);
    printf("\n");


    if ((iheader.height != 512) || (iheader.width != 512) || (iheader.bits != 8)) {
        printf("This only works for 512x512 8-color bitmaps\n");
		system("pause");
        return 0;
    }
    if (fheader.offset != 1078) {
        printf("This only works if the offset is equal to 1078\n");
		system("pause");
        return 0;
    }

    for (i = 0; i < iheader.height; i++) {
        for (j = 0; j < iheader.width; j++) {
            if (fread(&databuff[i][j][0], 3, 1, fp) != 1) {
                printf("Couldn't read colors for element [%d][%d]\n", i, j);
				system("pause");
                return 0;
            }
        }
    }
    for (i = 0; i < 12; i++) { /* look at the first 12 rows */
        printf("Row %2d:\n", i);
        for (j = 0; j < 12; j++) { /* look at the first 12 on each row */
            printf("  ");
            for (k = 0; k < 3; k++) {
                printf("%02x ", databuff[i][j][k]);
            }
            printf("\n");
        }
        printf("\n\n");
    }
	system("pause");
    fclose(fp);
    return 0;
}


Quote:
..
I have a program that reads that bitmap just fine.
...
Code:
.... Color table index value of element [171][170] = 195 (0xc3) RGB values from the color table are 0xc3c3c3

As I am a beginner in this area, I am still not able to understand the pallete differences. What I understand is the differences in the pallete and the bit count of the bitmap could be the reason. How were you able to read the index value at [171][170]. I am using Visual Studio 2008.

Dave,Please help me.Appreciate your help very much.

Regards
Last edited by admin : 26-Jun-2009 at 01:47. Reason: Please insert your example C/C++ codes between [CPP] and [/CPP] tags
  #48  
Old 25-Jun-2009, 19:41
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: How to get the pixels' value of a bmp image? (Pelles C)


Quote:
Originally Posted by vk10

I would post the code here:

Hmmm... When I compile that with Visual C++ version 8, I get the following:
Code:
sizeof: fheader: 14 iheader: 40 fheader.type = 4d42 fheader.size = 263222 fheader.reserved1 = 0 fheader.reserved2 = 0 fheader.offset = 1078 iheader.size = 40 iheader.width = 512 iheader.height = 512 iheader.planes = 1 iheader.bits = 8 iheader.compression = 0 iheader.imagesize = 262144 iheader.xresolution = 3780 iheader.yresolution = 3780 iheader.ncolors = 0 iheader.importantcolors = 0 Couldn't read colors for element [171][170]

This is consistent with my "real" bitmap reader, and is not exactly what you posted earlier.

Oh, well...
Quote:
Originally Posted by vk10
...As I am a beginner in this area, I am still not able to understand...
Well, no one was born knowing this stuff, you know.

Instead of talking endlessly about all of the things that could happen in a bitmap file, I will try to explain exactly what this file contains:

1. The two headers have a total length of 54 bytes.

2. Since the number of bits per pixel is equal to 8, there is a color table. Each entry in the color table consists of four bytes. The four bytes are the green, blue and red intensity values and there is a zero-byte. Each of the color bytes has a value from zero through 255. The color table itself could have as many as 1024 entries.

3. After the color table, there is one unsigned character for each pixel. The value of the unsigned char is used as an index into the color table, where the rgb values can be found for that pixel.

4. The actual length of the color table is obtained by subtracting the length of the two headers from the fheader.offset value. In this particular picture, fheader.offset is equal to 1078 - 54 = 1024 bytes.

5. So: After reading the headers, you know the length of the color table and you read that many bytes into the table. Then and only then do you start reading pixel values. One byte per pixel. The pixels occur sequentially a row at a time. The very first pixel is at the lower left hand corner of the picture.

6. One minor additional complication: The number of data bytes on each row in the file must always be a multiple of four. Your picture has 512 pixels per row, and this is a multiple of four, so there are no extra bytes. If your picture had 511 bytes, then each data row would consist of 511 pixel bytes and one zero "padding" byte.

7. Anyhow, you can read the data bytes one at a time for each row and use the values as follows:

CPP / C++ / C Code:
#include <stdio.h>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>

#include "bmp_headers.h"

void keep_window_open(void)
{
    printf("\nPress 'Enter' to continue . . .");
    getchar();
}

int main()
{
    FILE *fp;

    int i, j;
    char *filename = "lena512.bmp";

    unsigned char databuff[512][512];
    unsigned int color_table[1024];
    unsigned int num_bytes_in_table;

    BmpFileHeader fheader;
    BmpInfoHeader iheader;

    printf("sizeof: fheader: %d   iheader: %d\n\n", sizeof(fheader),
           sizeof(iheader));
    if ((sizeof(fheader) != 14) || (sizeof(iheader) != 40)) {
        printf("Header structs are not properly packed\n");
        return 0;
    }

    if ((fp = fopen(filename, "rb")) == NULL) {
        printf("Couldn't open file %s for reading.\n", filename);
        return 1;
    };
    if (fread(&fheader, sizeof(fheader), 1, fp) != 1) {
        printf("Couldn't read fheader.\n");
        return 0;
    }
    if (fread(&iheader, sizeof(iheader), 1, fp) != 1) {
        printf("Couldn't read iheader.\n");
        return 0;
    }
    printf("           fheader.type = %04x \n", fheader.type);
    printf("           fheader.size = %u   \n", fheader.size);
    printf("      fheader.reserved1 = %u   \n", fheader.reserved1);
    printf("      fheader.reserved2 = %u   \n", fheader.reserved2);
    printf("         fheader.offset = %u   \n", fheader.offset);
    printf("           iheader.size = %u   \n", iheader.size);
    printf("          iheader.width = %u   \n", iheader.width);
    printf("         iheader.height = %u   \n", iheader.height);
    printf("         iheader.planes = %u   \n", iheader.planes);
    printf("           iheader.bits = %u   \n", iheader.bits);
    printf("    iheader.compression = %u   \n", iheader.compression);
    printf("      iheader.imagesize = %u   \n", iheader.imagesize);
    printf("    iheader.xresolution = %u   \n", iheader.xresolution);
    printf("    iheader.yresolution = %u   \n", iheader.yresolution);
    printf("        iheader.ncolors = %u   \n", iheader.ncolors);
    printf("iheader.importantcolors = %u   \n", iheader.importantcolors);
    printf("\n");


    if ((iheader.height != 512) || (iheader.width != 512)
        || (iheader.bits != 8)) {
        printf("This only works for 512x512 8-color bitmaps\n");
        keep_window_open();
        return 0;
    }
    if (fheader.offset != 1078) {
        printf("This only works if the offset is equal to 1078\n");
        keep_window_open();
        return 0;
    }
    /* read the color table */
    num_bytes_in_table =
        fheader.offset - sizeof(fheader) - sizeof(iheader);
    printf("Length of the color table = %d\n", num_bytes_in_table);
    if (num_bytes_in_table > 1024) {
        printf("Color table can not be larger than 1024 bytes.\n");
        keep_window_open();
        return 0;
    }
    for (i = 0; i < num_bytes_in_table / 4; i++) {
        if (fread((char *) &color_table[i], 4, 1, fp) != 1) {
            printf("Couldn't read color table entry %d\n", i);
            keep_window_open();
            return 0;
        }
    }

    /* databuf contains values of index into color table */
    for (i = 0; i < iheader.height; i++) {
        for (j = 0; j < iheader.width; j++) {
            if (fread(&databuff[i][j], 1, 1, fp) != 1) {
                printf("Couldn't read colors for element [%d][%d]\n", i, j);
                keep_window_open();
                return 0;
            }
        }
    }
    i = 171;
    j = 170;
    printf("Color table index falue of element [%d][%d] = %d (0x%02x)\n",
           i, j, databuff[i][j], databuff[i][j]);

    printf("The rgb values from the color table are 0x%6x\n",
           color_table[databuff[i][j]]);

    keep_window_open();
    fclose(fp);
    return 0;
}

Output:

Code:
sizeof: fheader: 14 iheader: 40 fheader.type = 4d42 fheader.size = 263222 fheader.reserved1 = 0 fheader.reserved2 = 0 fheader.offset = 1078 iheader.size = 40 iheader.width = 512 iheader.height = 512 iheader.planes = 1 iheader.bits = 8 iheader.compression = 0 iheader.imagesize = 262144 iheader.xresolution = 3780 iheader.yresolution = 3780 iheader.ncolors = 0 iheader.importantcolors = 0 Length of the color table = 1024 Color table index falue of element [171][170] = 195 (0xc3) The rgb values from the color table are 0xc3c3c3 Press 'Enter' to continue . . .

Now, it's possible that the color table could have been created differently.

Maybe it's a real color picture, not a gray-scale, in spite of its appearance.

By examining the file itself, we can see that this particular color table has rgb values of 0x000000, 0x010101, 0x020202, ... 0xffffff, which is a linear "gray ramp", but sometimes the values are not exactly linear, as might happen if someone played with the "curves" (in Photoshop for example) to alter contrast and other visual characteristics. You could even do stuff like make the picture come out negative by just changing the color table and not the pixel data values themselves.


What I'm trying to say is that for this particular picture's color table the pixel byte value is actual equal to the gray-scale intensity, but that's not always the case.


Regards,

Dave

Footnote: This program is for illustration purposes. If I were doing some heavy-duty picture processing, I would probably not just make an array of integers for the color table, since accessing the individual color values is kind of compute-intensive. I'll leave refinements to people who are interested (if anyone out there is still awake).

Also, of course, the number of rows and columns are generally not known ahead of time, so dynamic allocation of the data buffer would, undoubtedly, be used in a more generally useful program.

I have personally dealt with bitmaps that have one bit per pixel, two bits per pixel, and four bits per pixel as well as eight and 24 bits per pixel. Everything with eight or fewer bits per pixel will have a color table. For the smaller numbers, the pixel data are stuffed into the bytes and have to be extracted by shift/mask operations.

This is the C++ board, but I have shown how one might do it in C. For C++ (and, actually for C) I would probably make a "bitmap structure" or class that would allow me to treat the bitmap as an object. User programs could call functions to read and write files without user having to struggle with individual fields of the headers. Stuff like that.
  #49  
Old 26-Jun-2009, 09:06
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: How to get the pixels' value of a bmp image? (Pelles C)


Quote:
Originally Posted by davekw7x
2. Since the number of bits per pixel is equal to 8, there is a color table. Each entry in the color table consists of four bytes. The four bytes are the green, blue and red intensity values and there is a zero-byte. Each of the color bytes has a value from zero through 255. The color table itself could have as many as 1024 entries
That is a mistake. In general, a color table may have as many as 256 entries. Each entry has four bytes, so the color table size may be as large as 1024 bytes.

In my sample code I declared the color table to be an array of 1024 integers. It could have been 256 integers.

Dave
  #50  
Old 26-Jun-2009, 15:12
vk10 vk10 is offline
New Member
 
Join Date: Jun 2009
Posts: 7
vk10 is on a distinguished road

Re: How to get the pixels' value of a bmp image? (Pelles C)


Thank you very much Dave. You are awfully fast in responding.The method you provided me was really helpful. I have some more problems which I ll try it out myself and then come to you if I am not able to solve it.

cheers!
 
 

Recent GIDBlogOnce again, no time for hobbies by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Question about locking surfaces to directly access pixels, SDL. george89 C++ Forum 0 18-Jun-2006 22:16
Pixels dan_ielle20 C++ Forum 0 23-Mar-2005 09:54
Checking source codes of image, audio and video files onauc C Programming Language 5 26-Feb-2005 22:47

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 00:42.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.