0

it works, but cant get past test 4

:) resize.c and bmp.h exist.
:) resize.c compiles.
:) doesn't resize 3x3-pixel BMP when n is 1
:( resizes 3x3-pixel BMP to 6x6 correctly when n is 2
    resized image does not match expected image
:( resizes 3x3-pixel BMP to 9x9 correctly when n is 3
    resized image does not match expected image
:( resizes 3x3-pixel BMP to 12x12 correctly when n is 4
    resized image does not match expected image
:( resizes 3x3-pixel BMP to 15x15 correctly when n is 5
    resized image does not match expected image
:( resizes 6x6-pixel BMP to 12x12 correctly when n is 2
    resized image does not match expected image
:( resizes 9x9-pixel BMP to 18x18 correctly when n is 2
    resized image does not match expected image
:( resizes 6x6-pixel BMP to 18x18 correctly when n is 3
    resized image does not match expected image
:( resizes 6x6-pixel BMP to 3x3 correctly when f is 0.5
    resized image does not match expected image
:( resizes 12x12-pixel BMP to 6x6 correctly when f is 0.5
    resized image does not match expected image
:( resizes 18x18-pixel BMP to 9x9 correctly when f is 0.5
    resized image does not match expected image

any help is greatly appreciated

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

#include "bmp.h"

void writeImg (int width, int scale, RGBTRIPLE *row, FILE *output, int padding);
void rescaleImg (int avg, int height, int width, FILE *output, int padding, int oHeight, int oWidth, RGBTRIPLE rowImg[abs(oHeight)][oWidth]);

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

    // remember filenames
    char *infile = argv[2];
    char *outfile = argv[3];
    float scale = strtof(argv[1], NULL);

    if (scale <= 0 || scale > 100) {
        fprintf(stderr, "Usage: scale: %f not valid range\n", scale);
        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;
    }

    // write outfile's BITMAPFILEHEADER
    bf.bfSize *= scale*scale;
    fwrite(&bf, sizeof(BITMAPFILEHEADER), 1, outptr);

    int orPadding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
    // write outfile's BITMAPINFOHEADER
    int orWidth = bi.biWidth;
    int orHeight = bi.biHeight;
    bi.biWidth *= scale;
    bi.biHeight *= scale;
    fwrite(&bi, sizeof(BITMAPINFOHEADER), 1, outptr);

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

    RGBTRIPLE rowImg[abs(orHeight)][orWidth];
    // iterate over infile's scanlines
    for (int i = 0, biHeight = abs(orHeight); i < biHeight; i++)
    {

        // iterate over pixels in scanline
        for (int j = 0; j < orWidth; j++)
        {
            // temporary storage

            // read RGB triple from infile
            fread(&rowImg[i][j], sizeof(RGBTRIPLE), 1, inptr);
            // printf("R:%i G:%i B:%i ", rowImg[i][j].rgbtRed, rowImg[i][j].rgbtGreen, rowImg[i][j].rgbtBlue);
            // write RGB triple to outfile
        }
        // printf("\n");

        if (scale >= 1) writeImg(bi.biWidth, scale, rowImg[i], outptr, padding);

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

        // then add it back (to demonstrate how)
    }

    if (scale < 1) rescaleImg(1/scale, abs(bi.biHeight), bi.biWidth, outptr, padding, orHeight, orWidth, rowImg);

    // close infile
    fclose(inptr);

    // close outfile
    fclose(outptr);

    // success
    return 0;
}

void writeImg (int width, int scale, RGBTRIPLE *row, FILE *output, int padding) {
    for (int i = 0; i < scale; i++) {
        for (int j = 0; j < width; j++) {
            fwrite(&row[j/scale], sizeof(RGBTRIPLE), 1, output);
            // printf("R:%i G:%i B:%i ", row[i].rgbtRed, row[i].rgbtGreen, row[i].rgbtBlue);
        }
        for (int k = 0; k < padding; k++)
        {
            fputc(0x00, output);
        }
        // printf("\n");

    }
}

void rescaleImg (int avg, int height, int width, FILE *output, int padding, int oHeight, int oWidth, RGBTRIPLE rowImg[abs(oHeight)][oWidth]) {
    eprintf("height: %i width: %i",height,width);
    for (int k = 0; k < height; k++) {
        for (int l = 0; l < width; l++) {
            RGBTRIPLE avgRGB;
            // int avgBlue = 0;
            // int avgRed = 0;
            // int avgGreen = 0;
            // for (int i = 0; i < avg; i++) {
            //     for (int j =0; j < avg; j++) {
            //         avgBlue += rowImg[i+(k*avg)][j+(l*avg)].rgbtBlue;
            //         avgRed += rowImg[i+(k*avg)][j+(l*avg)].rgbtRed;
            //         avgGreen += rowImg[i+(k*avg)][j+(l*avg)].rgbtGreen;
            //     }
            // }
            // avgRGB.rgbtBlue = avgBlue/(avg*avg);
            // avgRGB.rgbtRed = avgRed/(avg*avg);
            // avgRGB.rgbtGreen = avgGreen/(avg*avg);
            avgRGB.rgbtBlue = rowImg[k*avg][l*avg].rgbtBlue;
            avgRGB.rgbtRed = rowImg[k*avg][l*avg].rgbtRed;
            avgRGB.rgbtGreen = rowImg[k*avg][l*avg].rgbtGreen;
            printf("r: %i, b: %i, g: %i", avgRGB.rgbtRed, avgRGB.rgbtBlue, avgRGB.rgbtGreen);
            fwrite(&avgRGB, sizeof(RGBTRIPLE), 1, output);
        }
        printf("\n");
        for (int i = 0; i < padding; i++)
        {
            fputc(0x00, output);
        }
    }

}
1

Have you checked the header data of the output files when scale is not 1? Run smiley.bmp through your program and through the staff sample sith scale = 2 and compare the headers using peek. I'm sure you'll see the problem.

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 .