0

enter image description hereFirst time on this so be gentle. I have been at a standstill on this for hours. Below is my code and I can't seem to find out why these black bars(padding)show up. Any help?

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

#include "bmp.h"

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

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

    if (scale <= 0)
    {
        fprintf(stderr, "posititve integer please:\n");
        return 1;
    }
    // open input file 
    FILE *inptr = fopen(infile, "r");
    if (inptr == NULL)
    {
        fprintf(stderr, "Could not open %s.\n", infile);
        return 1;
    }

    // open output file
    FILE *outptr = fopen(outfile, "w");
    if (outptr == NULL)
    {
        fclose(inptr);
        fprintf(stderr, "Could not create %s.\n", outfile);
        return 1;
    }

    // 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;
    }
    BITMAPFILEHEADER bf_new;
    bf_new = bf;

    BITMAPINFOHEADER bi_new;
    bi_new = bi;
    bi_new.biHeight = bi.biHeight *= scale;
    bi_new.biWidth = bi.biWidth *= scale;

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


    bi_new.biSizeImage = ((sizeof(RGBTRIPLE) * bi_new.biWidth) +         padding_new) * abs(bi_new.biHeight);
    bf_new.bfSize = bi_new.biSizeImage + sizeof(BITMAPINFOHEADER) + sizeof(BITMAPFILEHEADER);
    // write outfile's BITMAPFILEHEADER
    fwrite(&bf_new, sizeof(BITMAPFILEHEADER), 1, outptr);

    // write outfile's BITMAPINFOHEADER
    fwrite(&bi_new, sizeof(BITMAPINFOHEADER), 1, outptr);

    // iterate over infile's scanlines
    for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
    {
        for (int n = 0; n < scale; n++)
        {
            // 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 "scale" # of times
                for(int m = 0; m < scale; m++)
                {
                    fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
                }
            }
            // add padding 
            for (int k = 0; k < padding_new; k++)
            {
                fputc(0x00, outptr);
            }
            // skip padding
            fseek(inptr, padding, SEEK_CUR);
            fseek(inptr, -bi.biWidth * 3 + padding_new, SEEK_CUR);
        }

    }
    // close infile
   fclose(inptr);

   // close outfile
   fclose(outptr);

   // success
   return 0;
[![enter image description here][1]][1]}
0

The pseudocode from the walkthrough:

psuedocode: "recopy" method

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

The pseudocode for this program:

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

There's also a problem with fseek(inptr, -bi.biWidth * 3 + padding_new, SEEK_CUR);. You want to send the infile cursor back to the beginning of the row. How could that include the new padding?

0

Attempt using old height and old width in your loops.

Multiplying new height and new width by scale is not correct.

You want to loop old values reading and writing them by scale to match new values.

oh = 2, ow = 2, s = 2,    
nh = 4, nw = 4, s = 2, 

fwrite oh * s = 4,  // correct == nh
fwrite nh * s = 8,  // wrong   != nh

fwrite ow * s = 4,  // correct == nw
fwrite nw * s = 8,  // wrong   != nw

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

You must log in to answer this question.

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