0

One more "Wrong colors" problem :) I tried to write my code according to Zamyla's pseudo:

for each row
   for each pixel in row
      write to outfile n times
   write padding_outfile
   skip over infile padding

for each row
  for n-1 times
     write pixels, padding to outfile
     send infile cursor back
  write pixels, padding to outfile
  skip over infile padding

So I got correct size but crazy colors. Last two days I was trying to figure it out without success. Thanks for help!

 #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 n  = atoi(argv[1]);

        if (n < 1 || n > 100)
        {
               printf("Enter integer between 1 and 100.");
               return 1;
        }

        char* infile = argv[2];
        char* outfile = argv[3];


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

        // write outfile's HEADERs
        BITMAPFILEHEADER bf_outfile;
        BITMAPINFOHEADER bi_outfile;

        bf_outfile = bf;
        bi_outfile = bi;
        bi_outfile.biWidth = bi.biWidth * n;
        bi_outfile.biHeight = bi.biHeight * n; 

        // determine padding for scanlines
        int padding =  (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
        int padding_outfile = (4 - (bi_outfile.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;

        bi_outfile.biSizeImage =  (bi_outfile.biWidth * sizeof(RGBTRIPLE) + padding_outfile) * abs(bi_outfile.biHeight);
        bf_outfile.bfSize = bi_outfile.biSizeImage + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);

        fwrite(&bf_outfile, sizeof(BITMAPFILEHEADER), 1, outptr);
        fwrite(&bi_outfile, sizeof(BITMAPINFOHEADER), 1, outptr);


        // iterate over infile's scanlines
    for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++) //each row
    {
        int j, k, l, m;
        RGBTRIPLE triple;

       for (m = 0; m < n - 1; m++) //n-1 times for rows
       {
            for (j = 0; j < bi.biWidth; j++) //each pixel in row
            {
             fread(&triple, sizeof(RGBTRIPLE), 1, inptr);

                for (l = 0; l < n; l++) //n times for pixels
                {
                    fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr); //write pixels to outfile 
                }
            }

            for (k = 0; k < padding_outfile; k++) //write padding to outfile 
            {
                fputc(0x00, outptr);
            } 

            fseek(inptr, -(bi.biWidth * 3 + padding ), SEEK_CUR); //send infile cursor back
       }

       //write pixels to outfile 
       for (j = 0; j < bi.biWidth; j++)
       {
          fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
          for (l = 0; l < n; l++)
            {
                fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
            }
       }
       //write padding to outfile
       for (k = 0; k < padding_outfile; k++) 
       {
          fputc(0x00, outptr);
       } 

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

    }


        // close infile
        fclose(inptr);

        // close outfile
        fclose(outptr);

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

There is a problem here fseek(inptr, -(bi.biWidth * 3 + padding ), SEEK_CUR); //send infile cursor back.

Has the program read the (infile) padding?

2
  • He's right. Wrong colors usually indicates a padding problem.
    – Cliff B
    Jun 17 '17 at 2:11
  • Thanks a lot! It works!
    – Ivanka D.
    Jun 17 '17 at 6:39

You must log in to answer this question.

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