Thread: resize image

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    44

    resize image

    I need some guidance on how to resize a bmp image at run time. I thought I should use a 2 dimensional dynamic array.
    What steps do I need to take to do something like this. fread and fwrite are working.
    I now use a for loop to read the file row by row. But how to go any further.

    In the mean time I found the steps:
    1. Open the file
    2. Start reading the header information
    3. Allocate memory to hold the image
    4. continue reading and decode the image from the file format to your memory
    format
    5. close the file
    6. resize the image in memory
    7. open a new file for writing
    8. write the header
    9. write the image data, encoding the memory image according to the file
    format
    10. close the output file

    Now to see how I can use a dynamic array with fread as input, someone any idea ?

    Code:
    int **array = malloc(biHeight * sizeof(int *));
    	array[0] = malloc(biHeight * bi.biWidth * sizeof(int));
    	for(i = 1; i < biHeight; i++)
    		array[i] = array[0] + i * bi.biWidth;
    
        // iterate over infile's scanlines
    
        for (int i = 0 ; i < biHeight; i++)
        {
            // iterate over pixels in scanline
            for (int j = 0; j < bi.biWidth; j++)
            {
                // temporary storage
                RGBTRIPLE triple;
    
                // read RGB triple from infile
                fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
    
    
                // write RGB triple to outfile
                fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
            }
    Last edited by lamko; 08-26-2011 at 08:45 AM.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    BMP file format - Wikipedia, the free encyclopedia has good details about the bitmap file format. Read it.

    Read in the header info in to determine how many rows and columns you have. Then, read this c-faq article to learn about dynamic 2-d arrays: Question 6.16.

  3. #3
    Registered User
    Join Date
    Jul 2011
    Posts
    44
    Bmp format is no problem, i'm at step 4. But now to understand all the stuff.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So if you want to fread into your array, why not ... fread into your array?
    Code:
    fread(&array[i][j], sizeof(array[i][j]), 1, inptr);
    Even better:
    Code:
    fread(array[i], sizeof(array[i][0]), j, inptr);
    since that is what fread is supposed to be for in the first place.

  5. #5
    Registered User
    Join Date
    Jul 2011
    Posts
    44
    Thx, I think I just got overwhelmed with all the stuff, didn't understand al the pointer stuff and syntax of fread.

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Are you sure you are going to be able to use int arrays for the image. The last time I messed around with bmp files the pixels where packed in 3 bytes side by side with each row ending with padding, if necessary.

  7. #7
    Registered User
    Join Date
    Jul 2011
    Posts
    44
    You mean something like this :
    Code:
    int **array = malloc(biHeight * sizeof(RGBTRIPLE *));  <- why do you need here a RGBTRIPLE * ?
    	array[0] = malloc(biHeight * bi.biWidth * sizeof(RGBTRIPLE)); <- and here you don't
    	for(i = 1; i < biHeight; i++)
    		array[i] = array[0] + i * bi.biWidth;
    Last edited by lamko; 08-26-2011 at 11:49 AM.

  8. #8
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    It might depend on the format, but the files I used (just random bmp files), where all 24 bit bmps. But in your example that is still an int array, meaning any indexing you do on it will move ahead 4 bytes, not 3. I ended up making a pixel struct consisting of red, blue and green char members, which I then made an array of.

  9. #9
    Registered User
    Join Date
    Jul 2011
    Posts
    44
    That I'm also doing : 24 bit and my struct is RGBTRIPLE

    But I need to learn some stuff on pointers to pointers for me to get whats going on here :
    int **array <- how to change this ?
    (RGBTRIPLE *)); <- why do you need here a RGBTRIPLE * ?
    array[0] = malloc(biHeight * bi.biWidth * sizeof(RGBTRIPLE)); <- and here you don't

  10. #10
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Not sure about that second question, where is this from? But you set your type to be RGBTRIPLE instead of int then. I did like this:

    Code:
    Pixel (*img)[width] = malloc(width * height * 3);
    where img is the name of the array.

  11. #11
    Registered User
    Join Date
    Jul 2011
    Posts
    44
    copy paste from the 1st example Question 6.16 I thought about what you suggested it makes that clear to me now. Didn't know you could use structures like that.
    Last edited by lamko; 08-26-2011 at 12:37 PM.

  12. #12
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Ok you mean the two malloc calls where the first is allocating * sizeof(int*) but the second * sizeof(int). That is because a 2d array is an array of arrays, so the first call is there to make room for an array of pointers (the base of the array if you will), and the second malloc allocates each int array which are sizeof(int) * array_size, (a pointer might not be the same size as an int).
    Last edited by Subsonics; 08-26-2011 at 12:54 PM.

  13. #13
    Registered User
    Join Date
    Jul 2011
    Posts
    44
    so the first call is there to make room for an array of pointers
    But it is here a int right ! So you could leave the *
    Last edited by lamko; 08-26-2011 at 01:03 PM.

  14. #14
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    I don't know what you mean now, an int, what is an int? If you are talking about example 6.16 then the graphics illustrates the point well IMO. The vertical block is what is allocated in the first call to malloc, this is not an int array but an array of pointers which is why sizeof(int*) is used.

  15. #15
    Registered User
    Join Date
    Jul 2011
    Posts
    44
    So int * is the pointer array, So it's using a array that is created by them self. That also explains the int **array.
    Last edited by lamko; 08-26-2011 at 01:18 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 03-13-2010, 05:10 AM
  2. Replies: 13
    Last Post: 11-20-2009, 04:43 PM
  3. png Image Resize
    By bhupesh.kec in forum C Programming
    Replies: 3
    Last Post: 12-06-2007, 07:52 AM
  4. Problems with Image Magick [Unable to Quantisize Image]
    By Maragato in forum C Programming
    Replies: 1
    Last Post: 09-18-2006, 10:41 PM
  5. Resize Image File???
    By stickman in forum C++ Programming
    Replies: 4
    Last Post: 09-19-2004, 11:46 AM