0

I think i made a breakthrough in this one with the rewrite method but i need to know how to write the triple to the array i tried a couple of things but didn't work out here's my code :

int main(int argc, char *argv[])

{

// ensure proper usage
if (argc != 4)
{
    fprintf(stderr, "Usage: ./resize n infile outfile\n");
    return 1;
}

// remember filenames
char *infile = argv[2];
char *outfile = argv[3];

// making argv[1] is factor number
int n = atoi(argv[1]);

//ensure n for valid input
if (n <= 0 || n >= 100)
{
    printf("scale Must be between 0 and 100\n");
    return 1;

}

// open input file
FILE *inptr = fopen(infile, "r");
if (inptr == NULL)
{
    fprintf(stderr, "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;
}


//coping the infile headers to new variables
BITMAPINFOHEADER new_bi = bi;
BITMAPFILEHEADER new_bf = bf;

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


new_bi.biWidth *= n;
new_bi.biHeight *= n;

//printf("%d\n", bi.biWidth); // print 4 bytes with sizeof and without sizeof is value 3
//printf("%d\n", bi.biHeight); // print 4 bytes with sizeof and without sizeof is value -3
//printf("%lu\n", sizeof(RGBTRIPLE)); // print 3
//creating new padding with the new width
int new_padding = (4 - (new_bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;

//checking if padding works
//printf("%i\n", new_padding); // prints 3 from the equation

//printf("%lu\n", sizeof(bi.biSizeImage));
//printf("%lu\n", sizeof(bf.bfSize));


// calculating the new sizes for the image and the file
new_bi.biSizeImage = ((sizeof(RGBTRIPLE) * new_bi.biWidth) + new_padding) * abs(new_bi.biHeight); // equals 36 in total and have 4 bytes
//printf("%d\n", bi.biSizeImage);
new_bf.bfSize = new_bi.biSizeImage + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER); // equals 90 in total and have 4 bytes
// printf("%lu\n", sizeof(bf.bfSize));
//printf("%lu\n", sizeof(BITMAPFILEHEADER));
//printf("%lu\n", sizeof(BITMAPINFOHEADER));


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

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

RGBTRIPLE array[new_bi.biWidth * sizeof(RGBTRIPLE)];

// 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 to array n times
        fwrite(&triple , sizeof(RGBTRIPLE), n, array);

    }

    // for n times write array to outfile
    for (int r = 0; r < n; r++)
    {
        // write RGB triple to outfile
        fwrite(array, sizeof(RGBTRIPLE), 1, outptr);

        // then add new padding if needed
        for (int k = 0; k < new_padding; k++)
        {
            fputc(0x00, outptr);
        }
    }

    // skip over old padding
    fseek(inptr, padding, SEEK_CUR);
}

// close infile
fclose(inptr);

// close outfile
fclose(outptr);

// success
return 0;

}

and thanks guys for your help

0

You should store each pixel n times into an array after reading them from infile. Then, write that array n times to outfile. Remember to use an independent counter to increment your array. Hope this help.

You must log in to answer this question.

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