0

I am attempting pset4 resize right now and I am running into a bit of problems on the part where you have to vertically size the image by a factor of n. So, my logic is to declear a dynamically allocated array by using malloc and then copy the RGBTRIPLE values into the array continuously. After, I put the array into a loop and make it repeat it self over again n times to represent the vertical sizing.

Below is my code and I need your guys help to correct me on where I went wrong. Thanks!

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

#include "bmp.h"

//declear functions
void horizontalFactorAndStoring(RGBTRIPLE triple, int factor, FILE *outptr, int *temp);

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

    //declear factor variable
    int factor = atoi(argv[1]);

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

    //store new BITMAPINFOHEADER
    BITMAPINFOHEADER newBi;
    newBi = bi;
    newBi.biWidth = (bi.biWidth)*factor;

    // determine padding for scanlines and if needed
    int padding = 0;
    if(((newBi.biWidth) % 4) != 0){
        padding =  (4 - (newBi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
    }else{
        padding = 0;
        }

    newBi.biHeight = (bi.biHeight)*factor;

    newBi.biSizeImage = (((newBi.biWidth)*(abs(newBi.biHeight)))*3)+(padding*(newBi.biHeight)*3); 

    //store new BITMAPFILEHEADER
    BITMAPFILEHEADER newBf;
    newBf = bf;
    newBf.bfSize = (newBi.biSizeImage)+54; //hardcode 54 first

    // write outfile's BITMAPFILEHEADER
    fwrite(&newBf, sizeof(BITMAPFILEHEADER), 1, outptr);

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

    //temporary storage for scanlines
    int *temp = malloc((newBi.biWidth)*3);

    //old padding
    int oldPadding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;


    // iterate over infile's scanlines
    for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
    {
        // 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
            horizontalFactorAndStoring(triple, factor, outptr, temp);


        }

        //write RGB triple vertically n times to outfile
     if(factor != 1){
     RGBTRIPLE triple2;
        for(int x = 0; x < factor; x++){
            for(int h = 0; h < (newBi.biWidth)*3; h++){
                triple2.rgbtBlue = *(temp + h);
                triple2.rgbtGreen = *(temp + (h+1));
                triple2.rgbtRed = *(temp + (h+2));
                fwrite(&triple2, sizeof(RGBTRIPLE), 1, outptr);
                  // add padding
                for (int k = 0; k < padding; k++)
                {
                    fputc(0x00, outptr);
                    }           
            }
          }
          }else{
              // add padding
        for (int k = 0; k < padding; k++)
        {
            fputc(0x00, outptr);
        }

        }


        // skip over old padding, if any
        fseek(inptr, oldPadding, SEEK_CUR);

    }


    // close infile
    fclose(inptr);

    // close outfile
    fclose(outptr);

    //free allocated memory
    free(temp);

    // that's all folks
    return 0;
}

void horizontalFactorAndStoring(RGBTRIPLE triple, int factor, FILE *outptr, int *temp){
int counter = 0;
    if(factor != 1){
        for(int k=0; k<factor;k++){
        // replicates each pixel n times (horizontal sizing)
            fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
        // copies each RGB triple values to malloc array, temp
            *(temp + counter) = triple.rgbtBlue;
            *(temp + (counter+1)) = triple.rgbtGreen;
            *(temp + (counter+2)) = triple.rgbtRed;
            counter++;
            }
   }else{
        fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
        }

   }
0

1 Answer 1

0

To be frank with you I don't understand why you are using the temp array to store the RGBTRIPLE by it's 3 values separately. Moreover you have a function to write the pixels horizontally but you write the vertically inside main. Here's what I would suggest:

  1. Read each line pixel by pixel
  2. Write each pixel n times
  3. Do the above for each line n times
  4. Move to next line
2
  • ok, thanks for the tip. I think I now have another way to approach it. Thanks, I will try it.
    – Xihai Luo
    Jul 7, 2015 at 18:12
  • Ok. Hope I helped!
    – ChrisG
    Jul 7, 2015 at 19:48

You must log in to answer this question.

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