Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <cs50.h>
- #include <string.h>
- #include "bmp.h"
- int main(int argc, char *argv[])
- {
- //Tests command line input for correct # of inputs
- if (argc != 4)
- {
- printf("Usage: ./resize n infile outfile\n");
- return 1;
- }
- //Add pointers for factor & file names of infile and outfile
- int rFactor = atoi(argv[1]);
- char *infile = argv[2];
- char *outfile = argv[3];
- //Tests rFactor input for integers between 1 and 100 inclusive
- if (rFactor < 1 || rFactor > 100)
- {
- printf("Resize factor needs to be a value between 1 and 100, inclusive.\n");
- return 1;
- }
- //Open infile and check for NULL
- FILE *inptr = fopen(infile, "r");
- if (inptr == NULL)
- {
- printf("Could not open %s.\n", infile);
- return 2;
- }
- //Open or create output file
- FILE *outptr = fopen(outfile, "w");
- if (outptr == NULL)
- {
- printf("Could not create %s.\n", outfile);
- return 3;
- }
- //Read infile BITMAPFILEHEADER
- BITMAPFILEHEADER bf;
- fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr);
- //Read infile BITMAPINFOHEADER
- BITMAPINFOHEADER bi;
- fread(&bi, sizeof(BITMAPINFOHEADER), 1, inptr);
- //Ensure file is 24-bit uncompressed BMP 4.0
- if (bf.bfType != 0x4d42 || bf.bfOffBits != 54 || bi.biSize != 40 || bi.biBitCount != 24 || bi.biCompression != 0)
- {
- fclose(inptr);
- fclose(outptr);
- printf("Unsupported File Format.\n");
- return 4;
- }
- int inptWidth = bi.biWidth;
- int inptHeight = bi.biHeight;
- //Modify width/height of BITMAPINFOHEADER
- bi.biWidth = bi.biWidth * rFactor;
- bi.biHeight = bi.biHeight * rFactor;
- //Write outfile BITMAPFILEHEADER
- fwrite(&bf, sizeof(BITMAPFILEHEADER), 1, outptr);
- //Write outfile BITMAPINFOHEADER
- fwrite(&bi, sizeof(BITMAPINFOHEADER), 1, outptr);
- //Determine padding for input scanlines
- int inpt_padding = (4 - (inptWidth * sizeof(RGBTRIPLE)) % 4) % 4;
- //Determine padding for output scanlines
- int outpt_padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
- //iterate over infile height
- for (int h = 0; h < abs(inptHeight); h++)
- {
- //create temp array to store RGBTRIPLE
- RGBTRIPLE *row = malloc(bi.biWidth * sizeof(RGBTRIPLE));
- //iterate over pixels in scanline
- for (int i = 0; i < bi.biWidth; i+= rFactor)
- {
- //Create temp var to store RGBTRIPLE
- RGBTRIPLE triple;
- //Read RGBTRIPLE from infile and store in temp var
- fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
- //Store temp var in array
- row[i] = triple;
- //iterate pixel for rFactor scaling
- for (int j = i + 1; j < bi.biWidth; j++)
- row[j] = triple;
- }
- for (int x = 0; x < bi.biWidth; x++)
- printf("%i, %i, %i | ", row[x].rgbtRed, row[x].rgbtGreen, row[x].rgbtBlue);
- printf("\n");
- //write array to outfile
- for (int k = 0; k < rFactor; k++)
- {
- for (int l = 0; l < bi.biWidth; l++)
- fwrite(&row[l], sizeof(RGBTRIPLE), 1, outptr);
- for (int m = 0; m < outpt_padding; m++)
- fputc(0x00, outptr);
- }
- //Skip infile padding
- fseek(inptr, inpt_padding, SEEK_CUR);
- //free up memory
- free(row);
- }
- //Close infile
- fclose(inptr);
- //Close outfile
- fclose(outptr);
- //Sucess
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement