0

Finally I have this code, it worked scaling up correctly but apparently it has problems with writing the pixels. Here are the code and the result I get by command (./resize 10 small.bmp large.bmp ) My guess is the fseek functions I use,but still can't know what's the problem specifically enter image description here

#include <stdio.h>
#include <stdlib.h>

#include "bmp.h"

int main(int argc, char* argv[])
{
    // ensure proper usage
    if (argc != 4)
    {
        printf("Usage: ./resize n infile outfile\n");
        return 1;
    }

    // remember filenames
    int factor = atoi(argv[1]);
    char* infile = argv[2];
    char* outfile = argv[3];

     // check available scale/factor number
    if ((factor <= 0) || (factor >= 100))
    {
        printf("valid scale number is between 0 and 100\n");
        return 10 ; 
    }

    // 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;
    }


    // update header info for outfile  - change these values of the height and width 
    // keeping track of both original and new files
    BITMAPFILEHEADER out_bf = bf;
    BITMAPINFOHEADER out_bi = bi;

    // change file size 
    out_bi.biWidth = bi.biWidth * factor ; 
    out_bi.biHeight = bi.biHeight * factor ;

    // padding difference formulas
    int padding =  (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
    int out_padding =  (4 - (out_bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;

    // continue adjusting header files 
    out_bi.biSizeImage = (out_bi.biWidth * sizeof(RGBTRIPLE) + out_padding) * abs(out_bi.biHeight) ; 
    out_bf.bfSize = out_bi.biSizeImage + 54 ;   

    // write outfile's headers
    fwrite(&out_bf, sizeof(BITMAPFILEHEADER), 1, outptr);
    fwrite(&out_bi, sizeof(BITMAPINFOHEADER), 1, outptr);

    // iterate over infile's scanlines - iterate over infile's scanlines (rows
    for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
    {
        int current_position = ftell(inptr);

        // every row needs to be copied (n) times = factor 
        for (int v = 0 ; v < factor ; v++)
        {
            fseek(inptr, current_position, SEEK_SET);

            // iterate over pixels in scanline
            for (int j = 0; j < bi.biWidth; j++)
            {
                RGBTRIPLE triple;
                fread(&triple, sizeof(RGBTRIPLE), 1, inptr);

                // resize it horizontally 
                for (int h = 0 ; h < factor ; h++)
                    fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
            }

            // skip padding
            fseek(inptr, padding, SEEK_CUR);

            // add it back  
            for (int k = 0; k < out_padding; k++)
            {
                fputc(0x00, outptr);
            }

            // Re-copy method 
            fseek(inptr, -(bi.biWidth * sizeof(RGBTRIPLE) + padding), SEEK_CUR);
        }
    }



    // close infile
    fclose(inptr);

    // close outfile
    fclose(outptr);

    // that's all folks
    return 0;
} 
0

It may be producing an output file of the right size, but it isn't scaling it up correctly.

        // Re-copy method 
        fseek(inptr, -(bi.biWidth * sizeof(RGBTRIPLE) + padding), SEEK_CUR);

If you reposition the pointer to the beginning of the current input line every time you process a line, how will it ever advance to the next line?

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

1
  • wooo, my code works ! ur comment helped my code pass the check50 now Oct 4 '16 at 8:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .