Thread: Help with a program to resize an image

  1. #16
    Noobin to the max
    Join Date
    Mar 2013
    Posts
    73
    Any Ideas as to what might be wrong???

  2. #17
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Quote Originally Posted by Dude22 View Post
    Any Ideas as to what might be wrong???
    Not without seeing the latest code.
    Anything else would be wild speculation.
    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.

  3. #18
    Noobin to the max
    Join Date
    Mar 2013
    Posts
    73
    Quote Originally Posted by Salem View Post
    Not without seeing the latest code.
    Anything else would be wild speculation.
    Whoops, forgot to add that on the last post... here it is:

    Code:
    /****************************************************************************
     * resize.c
     *
     * Computer Science 50
     * Problem Set 4
     *
     * Resizes a BMP piece by piece, just because.
     ***************************************************************************/
           
    #include <stdio.h>
    #include <stdlib.h>
    
    
    #include "bmp.h"
    
    
    int main(int argc, char* argv[])
    {
        // ensure proper usage
        if (argc != 4)
        {
            printf("Usage: copy N infile outfile\n");
            return 1;
        }
    
    
        // remember filenames
        char* nchar = argv[1];
        char* infile = argv[2];
        char* outfile = argv[3];
        
        int timeswriten = 0;
        
        //convert nchar to an int
        int n = atoi (nchar);
    
    
    
    
        
        if (n < 1 || n > 100)
        {
        printf("Please try again with a positive number less then 100\n");
        return 1;
        }
    
    
        // open input file 
        FILE* inptr = fopen(infile, "r");
        if (inptr == NULL)
        {
            printf("Could not open %s.\n", infile);
            return 2;
        }
    
    
        // open output file
        FILE* outptr = fopen(outfile, "w");
        if (outptr == NULL)
        {
            fclose(inptr);
            fprintf(stderr, "Could not create %s.\n", outfile);
            return 3;
        }
    
    
        // read infile's BITMAPFILEHEADER
        BITMAPFILEHEADER bf;
        fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr);
    
    
        // read infile's BITMAPINFOHEADER
        BITMAPINFOHEADER bi;
        fread(&bi, sizeof(BITMAPINFOHEADER), 1, inptr);
    
    
        // ensure infile is (likely) a 24-bit uncompressed BMP 4.0
        if (bf.bfType != 0x4d42 || bf.bfOffBits != 54 || bi.biSize != 40 || 
            bi.biBitCount != 24 || bi.biCompression != 0)
        {
            fclose(outptr);
            fclose(inptr);
            fprintf(stderr, "Unsupported file format.\n");
            return 4;
        }
        
        //save original image headder
        int oldheight = bi.biHeight;
        int oldwidth = bi.biWidth;
        int oldpadding = (4-(oldwidth*sizeof(RGBTRIPLE)) % 4) % 4;
        
        
        bi.biHeight = oldheight *n;
        bi.biWidth = oldwidth *n;
        int padding = (4 -(bi.biWidth * sizeof(RGBTRIPLE))%4)%4;
        bi.biSizeImage = (bi.biWidth * sizeof(RGBTRIPLE) + padding) * abs(bi.biHeight);
        bf.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + bi.biSizeImage;
    
    
    
    
        // write outfile's BITMAPFILEHEADER and BITMAPINFOHEADER
        fwrite(&bf, sizeof(BITMAPFILEHEADER), 1, outptr);
        fwrite(&bi, sizeof(BITMAPINFOHEADER), 1, outptr);
    
    
        RGBTRIPLE *buffer = malloc(sizeof(RGBTRIPLE) * bi.biWidth);
    
    
        // iterate over infile's scanlines
        for (int i = 0; i < abs(oldheight); i++)
        {
    
    
            // iterate over pixels in scanline
            for (int j = 0; j < oldwidth; j++)
            {
                // temporary storage
                RGBTRIPLE triple;
    
    
                // read RGB triple from infile and store in buffer
                fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
    
    
                //iterate over each pixel factor times
                for (int k = 0; k < n; k++)
                {
                    buffer[timeswriten++] = triple;
                }
            timeswriten = 0;
            }
    
    
            // skip over any input padding
            fseek(inptr, oldpadding, SEEK_CUR);
    
    
    
    
            // print each row from buffer factor times
            for (int r = 0; r < n; r++)
            {
                // write RGB triple to outfile
                fwrite(buffer, sizeof(RGBTRIPLE), bi.biWidth, outptr);
    
    
                // write padding to outfile
                for (int p = 0; p < padding; p++)
                fputc(0x00, outptr);
    
    
            }
        }
    
    
        // close infile
        fclose(inptr);
    
    
        // close outfile
        fclose(outptr);
    
    
        // that's all folks
        return 0;
    }

  4. #19
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Code:
    // iterate over pixels in scanline
    for (int j = 0; j < oldwidth; j++)
    {
        // temporary storage
        RGBTRIPLE triple;
    
        // read RGB triple from infile and store in buffer
        fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
    
        //iterate over each pixel factor times
        for (int k = 0; k < n; k++)
        {
            buffer[timeswriten++] = triple;
        }
    timeswriten = 0;
    }
    You read one pixel, fill up the buffer and then reset the buffer counter to zero, thus overwriting the buffer at the next iteration.

    Bye, Andreas

  5. #20
    Noobin to the max
    Join Date
    Mar 2013
    Posts
    73
    Quote Originally Posted by AndiPersti View Post
    Code:
    // iterate over pixels in scanline
    for (int j = 0; j < oldwidth; j++)
    {
        // temporary storage
        RGBTRIPLE triple;
    
        // read RGB triple from infile and store in buffer
        fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
    
        //iterate over each pixel factor times
        for (int k = 0; k < n; k++)
        {
            buffer[timeswriten++] = triple;
        }
    timeswriten = 0;
    }
    You read one pixel, fill up the buffer and then reset the buffer counter to zero, thus overwriting the buffer at the next iteration.

    Bye, Andreas
    Oh.... I shouldn't be resetting the buffer counter should I..?..?... I have removed the reset, and it is even closer to working (so I assume I made the correct change)

    Now when I input a very small image of a red smiley face and give it a resize factor of 100, It outputs a red band down the middle of a white canvas, although It is definitely a LOT larger, as it is supposed to be. It is not a smiley face however, so there is still something not quite correct..... Again, any help would be GREAT!

    NOTE: I have not reattached my code as all I changed was removing the "timeswriten = 0" line

  6. #21
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by Dude22 View Post
    Oh.... I shouldn't be resetting the buffer counter should I..?..?...
    You have to reset the counter but you did it at the wrong place. The indentation of that line is right but it should be outside the for-loop. IMHO the best place would be before the for-loop instead of after it. (I'm talking about the outer for-loop with the "j" counter in case that's not clear).

    Bye, Andreas

  7. #22
    Noobin to the max
    Join Date
    Mar 2013
    Posts
    73

    Talking It works

    IT WORKS!!!! PUTTING THE RESET OUTSIDE OF THE LOOP MADE IT WORK!!!!!!!!!!!!!!!!!!!!!!!

    Thanks, everyone so much for all the help you have given me!!!

    Now, If you could take a look at my other thread, there is another program I am working on : Recovering jpeg images from a .raw file

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. resize image
    By lamko in forum C Programming
    Replies: 22
    Last Post: 09-01-2011, 09:59 AM
  2. C program for text to image
    By karthiks551985 in forum C Programming
    Replies: 7
    Last Post: 10-10-2008, 09:36 AM
  3. png Image Resize
    By bhupesh.kec in forum C Programming
    Replies: 3
    Last Post: 12-06-2007, 07:52 AM
  4. Resize Image File???
    By stickman in forum C++ Programming
    Replies: 4
    Last Post: 09-19-2004, 11:46 AM
  5. how ot add an image in a c program
    By nisharoopa in forum C Programming
    Replies: 3
    Last Post: 07-13-2002, 02:06 PM