Skip to main content
added 326 characters in body
Source Link
Cliff B
  • 69.3k
  • 4
  • 33
  • 56

int n = atoi(argv[1]);

// read infile's BITMAPFILEHEADER BITMAPFILEHEADER bf,bfR; fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr); bfR = bf;

// read infile's BITMAPINFOHEADER BITMAPINFOHEADER bi,biR; fread(&bi, sizeof(BITMAPINFOHEADER), 1, inptr); biR = bi;

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

// modify bitmapinfoheader for height an width biR.biWidth = bi.biWidthn; biR.biHeight = bi.biHeightn;

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

// modify bisizeimage biR.biSizeImage = (((biR.biWidth*(sizeof(RGBTRIPLE)))+outpadding)*abs(biR.biHeight));

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

// modify bfsize before writing it bfR.bfSize = (sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + sizeof(biR.biSizeImage));

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

// iterate over infile's scanlines for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++) { for(int m = 0; m < n; m++) { // iterate over pixels in scanline for (int j = 0; j < bi.biWidth; j++) { // temporary storage RGBTRIPLE triple;

int n = atoi(argv[1]);

// read infile's BITMAPFILEHEADER
BITMAPFILEHEADER bf,bfR;
fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr);
bfR = bf;

// read infile's BITMAPINFOHEADER
BITMAPINFOHEADER bi,biR;
fread(&bi, sizeof(BITMAPINFOHEADER), 1, inptr);
biR = bi;

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

// modify bitmapinfoheader for height an width
biR.biWidth = bi.biWidth*n;
biR.biHeight = bi.biHeight*n;

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

// modify bisizeimage
biR.biSizeImage = (((biR.biWidth*(sizeof(RGBTRIPLE)))+outpadding)*abs(biR.biHeight));

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

// modify bfsize before writing it
bfR.bfSize = (sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + sizeof(biR.biSizeImage));

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


// iterate over infile's scanlines
for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
{
    for(int m = 0; m < n; m++)
    {
        // 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
            for (int k = 0; k < n; k++)
            {
            fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
            }

        }

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

        //go back to reprint the same line
       if(m != n-1)
        {
            fseek(inptr, -bi.biWidth * sizeof(RGBTRIPLE),SEEK_CUR);
        }   



        // then add it back (to demonstrate how)
        for (int k = 0; k < padding; k++)
        {
            fputc(0x00, outptr);
        }
}

}

int n = atoi(argv[1]);

// read infile's BITMAPFILEHEADER BITMAPFILEHEADER bf,bfR; fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr); bfR = bf;

// read infile's BITMAPINFOHEADER BITMAPINFOHEADER bi,biR; fread(&bi, sizeof(BITMAPINFOHEADER), 1, inptr); biR = bi;

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

// modify bitmapinfoheader for height an width biR.biWidth = bi.biWidthn; biR.biHeight = bi.biHeightn;

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

// modify bisizeimage biR.biSizeImage = (((biR.biWidth*(sizeof(RGBTRIPLE)))+outpadding)*abs(biR.biHeight));

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

// modify bfsize before writing it bfR.bfSize = (sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + sizeof(biR.biSizeImage));

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

// iterate over infile's scanlines for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++) { for(int m = 0; m < n; m++) { // 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
        for (int k = 0; k < n; k++)
        {
        fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
        }

    }

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

    //go back to reprint the same line
   if(m != n-1)
    {
        fseek(inptr, -bi.biWidth * sizeof(RGBTRIPLE),SEEK_CUR);
    }   



    // then add it back (to demonstrate how)
    for (int k = 0; k < padding; k++)
    {
        fputc(0x00, outptr);
    }

}

int n = atoi(argv[1]);

// read infile's BITMAPFILEHEADER
BITMAPFILEHEADER bf,bfR;
fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr);
bfR = bf;

// read infile's BITMAPINFOHEADER
BITMAPINFOHEADER bi,biR;
fread(&bi, sizeof(BITMAPINFOHEADER), 1, inptr);
biR = bi;

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

// modify bitmapinfoheader for height an width
biR.biWidth = bi.biWidth*n;
biR.biHeight = bi.biHeight*n;

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

// modify bisizeimage
biR.biSizeImage = (((biR.biWidth*(sizeof(RGBTRIPLE)))+outpadding)*abs(biR.biHeight));

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

// modify bfsize before writing it
bfR.bfSize = (sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + sizeof(biR.biSizeImage));

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


// iterate over infile's scanlines
for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
{
    for(int m = 0; m < n; m++)
    {
        // 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
            for (int k = 0; k < n; k++)
            {
            fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
            }

        }

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

        //go back to reprint the same line
       if(m != n-1)
        {
            fseek(inptr, -bi.biWidth * sizeof(RGBTRIPLE),SEEK_CUR);
        }   



        // then add it back (to demonstrate how)
        for (int k = 0; k < padding; k++)
        {
            fputc(0x00, outptr);
        }
}
Source Link

CS50 pset3 i want to resize this image, code is working for some and not for others? Can anyone tell me whats the problem?

int n = atoi(argv[1]);

// read infile's BITMAPFILEHEADER BITMAPFILEHEADER bf,bfR; fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr); bfR = bf;

// read infile's BITMAPINFOHEADER BITMAPINFOHEADER bi,biR; fread(&bi, sizeof(BITMAPINFOHEADER), 1, inptr); biR = bi;

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

// modify bitmapinfoheader for height an width biR.biWidth = bi.biWidthn; biR.biHeight = bi.biHeightn;

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

// modify bisizeimage biR.biSizeImage = (((biR.biWidth*(sizeof(RGBTRIPLE)))+outpadding)*abs(biR.biHeight));

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

// modify bfsize before writing it bfR.bfSize = (sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + sizeof(biR.biSizeImage));

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

// iterate over infile's scanlines for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++) { for(int m = 0; m < n; m++) { // 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
        for (int k = 0; k < n; k++)
        {
        fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
        }

    }

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

    //go back to reprint the same line
   if(m != n-1)
    {
        fseek(inptr, -bi.biWidth * sizeof(RGBTRIPLE),SEEK_CUR);
    }   



    // then add it back (to demonstrate how)
    for (int k = 0; k < padding; k++)
    {
        fputc(0x00, outptr);
    }

}