Skip to main content
hid code behind edit flag (per academic honesty guidelines)
Source Link
curiouskiwi
  • 18.7k
  • 2
  • 18
  • 43
// remember resizer
int ResizeFactor = atoi(argv[1]);

// ensure proper range of n
if( (0>=ResizeFactor)||(ResizeFactor>=100) )
{
    printf("n has to be between 1 and 100");
    return 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;
}

//2.UPDATE HEADER INTO OUTFILE.

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

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

//test1
//printf("%d %d %d\n",bi.biHeight,bi.biWidth,bi.biSizeImage);

// 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;
}
//Define the padding of the small image
int oldpadding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
//printf("%i\n",oldpadding);

//Ajust the width and height of image
bi.biWidth= bi.biWidth*ResizeFactor;
bi.biHeight= bi.biHeight*ResizeFactor;

// Padding of the new image
int padding =  (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
//printf("%i\n",padding);

//Ajust the biSizeImage using padding and new dimensions
bi.biSizeImage= (bi.biWidth*BYTEPERPIXEL + padding)*abs(bi.biHeight);
bf.bfSize = bf.bfOffBits + bi.biSizeImage;

//test2
//printf("%d %d %d\n",bi.biHeight,bi.biWidth,bi.biSizeImage);

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

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

// 3.RESIZE MAGIC

RGBTRIPLE PixelRow[(bi.biWidth/ResizeFactor)];

// iterate over infile's scanlines  
for (int i = 0, biHeight =abs(bi.biHeight/ResizeFactor); i < biHeight; i++)
{
    // iterate over pixels in scanline
        for (int j = 0; j < (bi.biWidth/ResizeFactor); j++)
        {
            // temporary storage
            RGBTRIPLE pixel;
            // read RGB triple from infile
            fread(&pixel, sizeof(RGBTRIPLE), 1, inptr);
            
            PixelRow[j]=pixel;
            
            //resize horizontally
            for (int k=0; k< ResizeFactor;k++)
            {
                fwrite(&pixel, sizeof(RGBTRIPLE), 1, outptr);
            }
        }
        // skip over padding, if any
        fseek(inptr, oldpadding, SEEK_CUR);
        // then add it back (to demonstrate how)
        for (int k=0; k < padding; k++)
        {
            fputc(0x00, outptr);
        }
        
        //resize vertically
        for (int c=0; c<((ResizeFactor)-1);c++)
        {
            //Each pixel in the row should be copied
            for (int y=0, h=(bi.biWidth/ResizeFactor); y<h;y++)
            {
                //horizontal resize in row copies.
                for (int k=0; k< ResizeFactor;k++)
                {
                    fwrite(&PixelRow[y], sizeof(RGBTRIPLE), 1, outptr);
                }
            }
            //padding in copied rows
            for (int k = 0; k < padding; k++)
            {
                fputc(0xff, outptr);
            }
        }
}

// close infile
fclose(inptr);

// close outfile
fclose(outptr);

// that's all folks
return 0;

} [removed code per academic honesty guidelines]

// remember resizer
int ResizeFactor = atoi(argv[1]);

// ensure proper range of n
if( (0>=ResizeFactor)||(ResizeFactor>=100) )
{
    printf("n has to be between 1 and 100");
    return 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;
}

//2.UPDATE HEADER INTO OUTFILE.

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

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

//test1
//printf("%d %d %d\n",bi.biHeight,bi.biWidth,bi.biSizeImage);

// 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;
}
//Define the padding of the small image
int oldpadding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
//printf("%i\n",oldpadding);

//Ajust the width and height of image
bi.biWidth= bi.biWidth*ResizeFactor;
bi.biHeight= bi.biHeight*ResizeFactor;

// Padding of the new image
int padding =  (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
//printf("%i\n",padding);

//Ajust the biSizeImage using padding and new dimensions
bi.biSizeImage= (bi.biWidth*BYTEPERPIXEL + padding)*abs(bi.biHeight);
bf.bfSize = bf.bfOffBits + bi.biSizeImage;

//test2
//printf("%d %d %d\n",bi.biHeight,bi.biWidth,bi.biSizeImage);

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

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

// 3.RESIZE MAGIC

RGBTRIPLE PixelRow[(bi.biWidth/ResizeFactor)];

// iterate over infile's scanlines  
for (int i = 0, biHeight =abs(bi.biHeight/ResizeFactor); i < biHeight; i++)
{
    // iterate over pixels in scanline
        for (int j = 0; j < (bi.biWidth/ResizeFactor); j++)
        {
            // temporary storage
            RGBTRIPLE pixel;
            // read RGB triple from infile
            fread(&pixel, sizeof(RGBTRIPLE), 1, inptr);
            
            PixelRow[j]=pixel;
            
            //resize horizontally
            for (int k=0; k< ResizeFactor;k++)
            {
                fwrite(&pixel, sizeof(RGBTRIPLE), 1, outptr);
            }
        }
        // skip over padding, if any
        fseek(inptr, oldpadding, SEEK_CUR);
        // then add it back (to demonstrate how)
        for (int k=0; k < padding; k++)
        {
            fputc(0x00, outptr);
        }
        
        //resize vertically
        for (int c=0; c<((ResizeFactor)-1);c++)
        {
            //Each pixel in the row should be copied
            for (int y=0, h=(bi.biWidth/ResizeFactor); y<h;y++)
            {
                //horizontal resize in row copies.
                for (int k=0; k< ResizeFactor;k++)
                {
                    fwrite(&PixelRow[y], sizeof(RGBTRIPLE), 1, outptr);
                }
            }
            //padding in copied rows
            for (int k = 0; k < padding; k++)
            {
                fputc(0xff, outptr);
            }
        }
}

// close infile
fclose(inptr);

// close outfile
fclose(outptr);

// that's all folks
return 0;

}

[removed code per academic honesty guidelines]

Source Link
italo
  • 117
  • 6

Resize fails in Check50 but 1x1 pixel images work

Resize fails in Check50 but 1x1 pixel images work. I've compared the outcome with the staff implementation and it's the same.

// remember resizer
int ResizeFactor = atoi(argv[1]);

// ensure proper range of n
if( (0>=ResizeFactor)||(ResizeFactor>=100) )
{
    printf("n has to be between 1 and 100");
    return 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;
}

//2.UPDATE HEADER INTO OUTFILE.

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

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

//test1
//printf("%d %d %d\n",bi.biHeight,bi.biWidth,bi.biSizeImage);

// 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;
}
//Define the padding of the small image
int oldpadding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
//printf("%i\n",oldpadding);

//Ajust the width and height of image
bi.biWidth= bi.biWidth*ResizeFactor;
bi.biHeight= bi.biHeight*ResizeFactor;

// Padding of the new image
int padding =  (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
//printf("%i\n",padding);

//Ajust the biSizeImage using padding and new dimensions
bi.biSizeImage= (bi.biWidth*BYTEPERPIXEL + padding)*abs(bi.biHeight);
bf.bfSize = bf.bfOffBits + bi.biSizeImage;

//test2
//printf("%d %d %d\n",bi.biHeight,bi.biWidth,bi.biSizeImage);

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

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

// 3.RESIZE MAGIC

RGBTRIPLE PixelRow[(bi.biWidth/ResizeFactor)];

// iterate over infile's scanlines  
for (int i = 0, biHeight =abs(bi.biHeight/ResizeFactor); i < biHeight; i++)
{
    // iterate over pixels in scanline
        for (int j = 0; j < (bi.biWidth/ResizeFactor); j++)
        {
            // temporary storage
            RGBTRIPLE pixel;
            // read RGB triple from infile
            fread(&pixel, sizeof(RGBTRIPLE), 1, inptr);
            
            PixelRow[j]=pixel;
            
            //resize horizontally
            for (int k=0; k< ResizeFactor;k++)
            {
                fwrite(&pixel, sizeof(RGBTRIPLE), 1, outptr);
            }
        }
        // skip over padding, if any
        fseek(inptr, oldpadding, SEEK_CUR);
        // then add it back (to demonstrate how)
        for (int k=0; k < padding; k++)
        {
            fputc(0x00, outptr);
        }
        
        //resize vertically
        for (int c=0; c<((ResizeFactor)-1);c++)
        {
            //Each pixel in the row should be copied
            for (int y=0, h=(bi.biWidth/ResizeFactor); y<h;y++)
            {
                //horizontal resize in row copies.
                for (int k=0; k< ResizeFactor;k++)
                {
                    fwrite(&PixelRow[y], sizeof(RGBTRIPLE), 1, outptr);
                }
            }
            //padding in copied rows
            for (int k = 0; k < padding; k++)
            {
                fputc(0xff, outptr);
            }
        }
}

// close infile
fclose(inptr);

// close outfile
fclose(outptr);

// that's all folks
return 0;

}