C programming -fread. I am having trouble with stacking a 3-D image from 2-D images?

This is a discussion on C programming -fread. I am having trouble with stacking a 3-D image from 2-D images? within the C Programming forums, part of the General Programming Boards category; The problem that I am having is that I am trying to pass in a very large value into the ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    2

    C programming -fread. I am having trouble with stacking a 3-D image from 2-D images?

    The problem that I am having is that I am trying to pass in a very large value into the 3rd parameter of the fread(~,~,~,~) function. The third parameter is "_In_ size_t _Count" as defined in stdio.h. Here is my function prototype:

    Code:
    static void read_image_lines(int width, int height, int numbands,
    I16 ***image, int swapsamplebytes)
    Now inside of the above function I call the function:
    Code:
    myfread(image[0][0], sizeof(I16), 5*width*numbands, file);
    in which myfread(~,~,~,~) is:
    Code:
    void myfread(void *ptr, sizet size, sizet noobj, FILE *rfile)
    {
    int hold = 0;
    hold = fread(ptr,size,noobj,rfile);
    if (noobj != hold) {
    fprintf(stderr,"Error reading file\n");
    exit(1);
    }
    }

    There is a problem because passing in noobj is to large. Notice above in myfread I have : 5*width*numbands. When I try to change the number 5 to the number variable "height", I guess my overall height*width*numbands is just to big and I have to then just use 5 instead. So I need to figure out how to do a while loop or something of the like so that I can stack the original 3-D "image" from a bunch of smaller 2-D images. It is a bit difficult because of the pointers involved though. Any ideas would be very much appreciated.


    Thank you,

    Brian

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,834
    Show us how you initialise whatever you pass to this function for the I16 ***image parameter.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    The problem is what? You get "error reading file" I assume?

    (Two quick notes. I guess you mean size_t not sizet. And hold should probably be const int rather than int, though that makes no difference for the error).

    Maybe the file is too short and you that is why you get an error, meaning you reached the EOF and didn't have an error as you may think.

    You just do one fread() and check if it succeeded. There cannot possibly any other problem. Check how big is your file.

    So image is a ***I16. So image[0][0] is a *I16, so you have the pointers right here. My guess is though that image is a 3d array height*width*numbands. So image[x][y] is a pointer that has numbands elements not height*width*numbands! So it won't work like that.

    You should pass image[i][j] in a loop and set noobj = numbands. Then image will have what you want (or not). And ofcourse pass the sime file pointer (rfile)

  4. #4
    Registered User
    Join Date
    Nov 2008
    Posts
    2
    C_ntua,

    Thanks very much for your reply, it is helpful. I am stuck on a couple different things. First since image[0][0] is a *I16, am I correct at thinking that this is a pointer to an index, (in this case [0][0]) in the XY plane? So I therefore have numbands of the XY planes which I want to increment through one by one and stack them in to the final 3D image? I see that by reading something on fread(....) what it does is reads a bunch of elements from stream and puts in to a memory location specified by, in my case rfile.

    So do I somehow want to increment the 2D XY array slice of the image so that I go up one count in Z? If so, I am a bit confused on how to do this. While I do understand that I would use a loop, I don't quite get how to update the pointer(s) to the right places. For example, would I somehow do a

    Code:
    while (?????)
    {
     my_fread(image[y][0], sizeof(LshsSampleType), width*height, iofile);
    y++;
    }
    The reason being because I want to go to the next plane, (band), on each iteration. But then I know I would have an error because I would be getting data at the beginning of iofile. This is what I think you mean by updating rfile, but I am not sure how. Perhaps there is a way to just read how big the file is and have the program only put that in?

    Thanks for any suggestions and thanks for the help already,

    Brian

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,834
    Unless you have
    image[0][0] = malloc( width * height * sizeof(I16) );
    then you're going to be trashing memory in all probability.

    Is LshsSampleType the same as I16 ?

    You can't just declare ***array and start writing data all over the place and expect C to magically allocate all the space you need.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem reading tiff image files?
    By compz in forum C++ Programming
    Replies: 9
    Last Post: 10-30-2009, 05:17 AM
  2. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 02:42 AM
  3. Reading color tiff images?
    By compz in forum Game Programming
    Replies: 1
    Last Post: 11-21-2003, 12:48 AM
  4. Replies: 4
    Last Post: 03-02-2003, 09:12 AM

Tags for this Thread


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