Reading the file, and filling the array of structures

This is a discussion on Reading the file, and filling the array of structures within the C Programming forums, part of the General Programming Boards category; Hello, i have created a structure holding pixel information, now i am going to make up a memory for the ...

  1. #1
    Registered User BlackOps's Avatar
    Join Date
    Jul 2009
    Location
    AZERBAIJAN
    Posts
    78

    Question Reading the file, and filling the array of structures

    Hello,

    i have created a structure holding pixel information, now i am going to make up a memory for the image, my program will read image from file, and then convert its data to the appropriate structure, and fill the array of such structures...

    well...everything works just fine...i also made my program to print every value of colors etc in another statistical file...just for my information...

    BUT... when my cycle was at the stage of 18682... it did not fill any structures into memory array... as you see from the pice of code below, the end variable in my primary for loop is 786432... but beginning from 18682 it did not write data anymore...

    i decided to choose the more little picture... (this one was 1024 x 768), and it worked just fine!

    but with this big picture it does such BUG...any ideas why?

    as you see in my code, i included to inform me if there is no memory, but it didnt happen... so i wonder why it does not fill array of structures...

    Code:
        RGBQUAD *pixel = (RGBQUAD *)calloc(786432, 3);
    
        if (pixel == NULL)
            printf("No memory available!\n");
        else
    {
    loading structures into memory
    }

  2. #2
    Registered User BlackOps's Avatar
    Join Date
    Jul 2009
    Location
    AZERBAIJAN
    Posts
    78
    well another interesting detail is, that another image which was processed good has dimensions 500x500, and the for variable was 249999 there! but it didnt do such bug! and saved all structures to memory as i want... so thats strange... any ideas?

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    What? What for loop? What memory array? What structures? What "end variable"? Just, in general, what?

    Bonus hint: the second number in a call to calloc should probably never be an actual literal number (except, perhaps, the literal number 1). In this case it needs to be "sizeof(RGBQUAD)". (Maybe that's three, I don't know.)

  4. #4
    Registered User BlackOps's Avatar
    Join Date
    Jul 2009
    Location
    AZERBAIJAN
    Posts
    78
    yes yes know, i did a size of... i just simplified it so that not to bore u with much of stuff...

    loop is just a FOR loop with one variable, memory array begins from pixel as you see... structure is a structure which contains 3 one byte elements, by saying end variable i mean that do this FOR loop until i is 786431, in general i already described my issue. thank u for attention!

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Since you're having trouble with a for loop, and you are apparently categorically refusing to tell anybody anything whatsoever about your for loop, I don't know how much help any of us can provide.

    What goes wrong inside your for loop? Does your file-reading function give an error? Does it return erroneous values? How do you know you aren't getting what you want?

  6. #6
    Registered User BlackOps's Avatar
    Join Date
    Jul 2009
    Location
    AZERBAIJAN
    Posts
    78
    no its ok! i just wanted to keep it easy as possible, so that it would be easier to help.

    ok here it is... as u see in that code i have allocated a memory for 1024x768 structures, each structure is 3 bytes.

    so, when i begin to fill that big array of structures with values which i read from file...it goes good until the structure number: 18682...after that, all remaining structures in my big array are filled with zeros! no errors or crashes....

    and the interesting thing is..when i tried to copy more little image from file... so i allocated 500 x 500 elements each is 3 bytes... then it makes up even 250 000 structures in that big array! and i could easily fill them with values! so it doesnt make sense isnt it? i could fill 250 000 structures in an array... but i got stucked on 18682 on previous case... when i wanted to fill the bigger array...

    now i hope my issue is being understood somehow anyway thank u

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by BlackOps View Post
    no its ok! i just wanted to keep it easy as possible, so that it would be easier to help.

    ok here it is... as u see in that code i have allocated a memory for 1024x768 structures, each structure is 3 bytes.

    so, when i begin to fill that big array of structures with values which i read from file...it goes good until the structure number: 18682...after that, all remaining structures in my big array are filled with zeros! no errors or crashes....

    and the interesting thing is..when i tried to copy more little image from file... so i allocated 500 x 500 elements each is 3 bytes... then it makes up even 250 000 structures in that big array! and i could easily fill them with values! so it doesnt make sense isnt it? i could fill 250 000 structures in an array... but i got stucked on 18682 on previous case... when i wanted to fill the bigger array...

    now i hope my issue is being understood somehow anyway thank u
    Do you believe there are more than 18682 sets of numbers in your file? Why? Are you sure? And what does your file-reading function tell you? Does it tell you that it's reading zeroes from the file?

  8. #8
    Registered User BlackOps's Avatar
    Join Date
    Jul 2009
    Location
    AZERBAIJAN
    Posts
    78
    yes i do beleive, because the other numbers are zero in file ( i just opened it and saw )

    well... i have worked a little more on my problem... and discovered very strange thing... ok here i am showing my code:

    Code:
        long i,k,j;
        
        ReadImageInfo(&imageheader, &imageinfo, image1);
    
        RGBQUAD *pixel = (RGBQUAD *)calloc(imageinfo.bwidth * imageinfo.bheight, sizeof(RGBQUAD));
    
        if (pixel == NULL)
            printf("No memory available!\n");
        else
            {
    
        for (i = imageinfo.bwidth*imageinfo.bheight; i>=imageinfo.bwidth-1; i-=imageinfo.bwidth)
                for (j = i-imageinfo.bwidth; j<=i-1; j++)
                    {
                    fread(&(pixel+j)->blue, 1, 1, image1);
                    fread(&(pixel+j)->green, 1, 1, image1);
                    fread(&(pixel+j)->red, 1, 1, image1);
                    }
    
            }
    /* This piece of code saves RGB colors of each pixel(or structure in an array) in the text file
    and it also saves the offset position from the pixel near the RGB data. */  
    for (j = 0; j <= imageinfo.bwidth * imageinfo.bheight - 1; j++)
        {
          fprintf(imsnap, "%d ", (pixel+j)->red);
          fprintf(imsnap, "%d ", (pixel+j)->green);
          fprintf(imsnap, "%d\t%d\n", (pixel+j)->blue, j);
    
            
        }
    All the structures are being saved in a big array which was allocated by the calloc in the above code, and the structure has the following type:
    Code:
    typedef struct rgbquad
    {
        unsigned char red;
        unsigned char green;
        unsigned char blue;
    } RGBQUAD;
    well, who worked with BMP files, probably know that they are reversed, so i am reading the file and simultaneously putting the files in the appropriate position in a memory convenient for me.. just reverse it..

    interesting thing is... when i try to open different images...the program does not read the correct values from the file! and does not save correct values in structure arrays!!!

    but... i have tried same with 500x500 bmp image size...and i saw that it worked perfectly! and saved all the data in a statistical file as i want!! very strange!

    later...i decided to load some another BMP picture with a dimension of 500x500... and it didnt work! ....i think code works very good! but...

    what a problem could be? any ideas? thank you very much for an attention
    Last edited by BlackOps; 07-12-2009 at 01:04 PM.

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    What values does it read, then. How do you know they're not the correct values? Are they just plain the wrong numbers, or are they in the wrong place, both, or neither? Be specific.

  10. #10
    Registered User BlackOps's Avatar
    Join Date
    Jul 2009
    Location
    AZERBAIJAN
    Posts
    78
    okey... then let me say what i do,for example i have an image... i open this image with image viewer...and point to any pixel, obtain its coordinates and colors...

    then i covert those coordinates to the pointer offset of the pixel , open the statistical file, and see does that record in a file has same color data or not... with one image which is 500x500 i always get correct results for any pixel on the picture!!! but with others no! even with some others who have the same dimension!

    and... when those other pics are being processed by my program, i see that the statistical file is being filled with the values 0 0 0 and with an offset value to the right...

    for some files...i saw that wrong numbers are being read...they look correct but they are wrong...a different color... not the one i obtain with image viewwer...

    ok...so an idea now?

    i say again, strange thing is that my algorithm works good, and program works perfect with some kind fo image, but wrong with other kind of images... it reads wrong values, and fills array of pointers with wrong values! so?

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So I would have thought that at some point you would check whether your fread is returning an error. Why haven't you?

    And what does "offset to the right" mean? Does that mean you are reading the numbers you expect to read, but they are not in the correct location? (As 0 0 0 cannot itself be offset to the right, I'm assuming the phrase must refer to something else.)

  12. #12
    Registered User BlackOps's Avatar
    Join Date
    Jul 2009
    Location
    AZERBAIJAN
    Posts
    78
    well the information file which i am writing data to has the following structure:

    R G B offset(integer value of variable j from second FOR loop)

    thats it... you can see what i do in the code above...you asked me to show code so that people may know what i do, so i showed the code...its clear and straightforward what i do..

    i checked the fread...it returns number of elements read... i always read one element as was supposed to when i was opening image which worked good.... BUT... for another image...with same dimension it returned one...but after 190 elements it return value of 0...

    strange thing is... i did not touch the code...i just changed name of file in the #define... and it doesnt work with another file...why....

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Soooooooooooooooooooooo, if fread returns zero, that means you ran out of data in your file or there is some other read error. fread doesn't really have format errors to speak of. Consequently, presumably, there is not as much data in the file as you think there is. Malformed image file? Not reading height and width correctly?

  14. #14
    Registered User BlackOps's Avatar
    Join Date
    Jul 2009
    Location
    AZERBAIJAN
    Posts
    78
    No... both image have same characteristics! here my program prints them:

    Code:
    imageheader . htype = 19778
    imageheader . hsize = 750054
    imageheader . hreserved1 = 0
    imageheader . hreserved2 = 0
    imageheader . hoffsetbits = 54
    imageinfo . bsize = 40
    imageinfo . bwidth = 500
    imageinfo . bheight = 500
    imageinfo . bplanes = 1
    imageinfo . bbitcount = 24
    imageinfo . bcompression = 0
    imageinfo . bimagesize = 750000
    imageinfo . bxpermeter = 0
    imageinfo . bypermeter = 0
    imageinfo . bclrused = 0
    imageinfo . bclrimportant = 0
    both files are same dimension BMP images with 24bpp colors, both can be opened and viewed with image viewer so strange huh?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Filling Array Of Structures From A File
    By mesmer in forum C Programming
    Replies: 4
    Last Post: 11-26-2008, 08:59 AM
  2. filling an array of structures?
    By voodoo3182 in forum C Programming
    Replies: 9
    Last Post: 08-06-2005, 05:29 PM
  3. filling an array from a file using fscanf...
    By stodd04 in forum C Programming
    Replies: 2
    Last Post: 03-16-2005, 07:55 AM
  4. C Filling and Printing a 2-d array
    By Smoot in forum C Programming
    Replies: 3
    Last Post: 11-13-2003, 07:42 PM
  5. Filling an Array of Structures
    By Zildjian in forum C Programming
    Replies: 5
    Last Post: 11-12-2003, 04:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21