Thread: resize image

  1. #16
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    You don't have to use an array of ints. You can create an array of your RGBTRIPLE structs:
    Code:
    RGBTRIPLE **image = malloc(rows * sizeof(*image));
    for (i = 0; i < rows; i++) {
        image[0] = malloc(cols * sizeof(*image[0]));
    }
    
    for (i = 0; i < rows; i++) {
        if (fread(image[i], sizeof(image[i][j]), cols, inptr) != cols * sizeof(image[i][j])) {
            perror("fread failed on original image");
            return -1;  // or whatever error code you want
        }
    }

  2. #17
    Registered User
    Join Date
    Jul 2011
    Posts
    44
    I've changed your code a bit to my vars
    Code:
        RGBTRIPLE **image = malloc(biHeight * sizeof(*image));
        for (i = 0; i < biHeight; i++)
            image[0] = malloc(bi.biWidth * sizeof(*image[0]));
    
        for (i = 0; i < biHeight; i++)
            for (j = 0; j < bi.biWidth; j++)
                if (fread(image[i], sizeof(image[i][j]), bi.biWidth, inptr) != bi.biWidth * sizeof(image[i][j]))
                {
                    perror("fread failed on original image");
                    return -1;  // or whatever error code you want
                }
    But the line perror gets excuted all the time. I've seen your fread statement and everything seem right.
    Both the height and the width get a value of 3. because my input bmp is 3 by 3.
    What you cause it?

  3. #18
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Did you look at the documentation to see what fread actually returns?

  4. #19
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by lamko View Post
    I've changed your code a bit to my vars
    Code:
        RGBTRIPLE **image = malloc(biHeight * sizeof(*image));
        for (i = 0; i < biHeight; i++)
            image[0] = malloc(bi.biWidth * sizeof(*image[0]));
    
        for (i = 0; i < biHeight; i++)
            for (j = 0; j < bi.biWidth; j++)
                if (fread(image[i], sizeof(image[i][j]), bi.biWidth, inptr) != bi.biWidth * sizeof(image[i][j]))
                {
                    perror("fread failed on original image");
                    return -1;  // or whatever error code you want
                }
    But the line perror gets excuted all the time. I've seen your fread statement and everything seem right.
    Both the height and the width get a value of 3. because my input bmp is 3 by 3.
    What you cause it?
    sizeof(image[i][j]) is going to return the size of the pointer to the array... not the size of the image in memory. You need to track that separately.

  5. #20
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    There are bugs in my code. Read it over carefully, and look at the documentation for malloc and fread. Pay attention to loop indices, and think about what makes sense and what doesn't. In particular:
    1. I have a loop that malloc's each time. Where do I store each result? Where should it be stored?
    2. When I read, I only have a single loop, but two indexes. Where did j come from and what's it's value?
    3. How much are you reading (how many RGBTRIPLE's), and how many times are you doing that?

  6. #21
    Registered User
    Join Date
    Jul 2011
    Posts
    44
    I've thought I was stuck but i've found this website : A Tutorial on Pointers and Arrays in C and it is explaining just the things I need to grasp all this stuff. Now of to master it

  7. #22
    Registered User
    Join Date
    Jul 2011
    Posts
    44
    Finally a working version after also reading this : Pointers Usage in C++: Beginners to Advanced - CodeProject

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <stdint.h>
    
    typedef uint8_t  BYTE;
    typedef uint32_t DWORD;
    typedef int32_t  LONG;
    typedef uint16_t WORD;
    
    typedef struct
    {
        BYTE  rgbtBlue;
        BYTE  rgbtGreen;
        BYTE  rgbtRed;
    } __attribute__((__packed__))
    RGBTRIPLE;
    
    int main( int argc, char* argv[] )
    {
    
        RGBTRIPLE TEST;
    
        RGBTRIPLE** aptr = (RGBTRIPLE**)malloc( sizeof(RGBTRIPLE*) * 10 );
    
        for( int i = 0; i < 10; ++i )
            aptr[ i ] = (RGBTRIPLE*)malloc( sizeof(RGBTRIPLE*) * i * 5 );
    
        for( int i = 0; i < 10; ++i )
        {
            for( int j = 0; j < i * 5; ++j )
                aptr[ i ][ j ] = TEST;
        }
        return 0;
    }
    Now off to code the rest
    Last edited by lamko; 09-01-2011 at 08:50 AM.

  8. #23
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Just one small point here... It's not really necessary to cast the return value of malloc() in a C program, that's a C++ thing...
    Malloc returns a void* that can be assigned to almost any pointer.

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