Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Copies a BMP piece by piece, just because.
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <cs50.h>
- #include <string.h>
- #include <ctype.h>
- #include <errno.h>
- #include <math.h>
- #include "bmp.h"
- int main(int argc, char *argv[])
- {
- bool testFloat = true;
- float scaleFactor;
- // ensure proper usage
- if (argc != 4)
- {
- fprintf(stderr, "Usage: ./resize float infile outfile\n");
- return 1;
- }
- while (testFloat)
- {
- // get line of text, returning FLT_MAX on failure
- string line = argv[1];
- if (line == NULL)
- {
- fprintf(stderr, "Float is NULL %f\n", FLT_MAX);
- return 1;
- }
- // return a float if only a float was provided
- if (strlen(line) > 0 && !isspace(line[0]))
- {
- char *tail;1b.bmp
- errno = 0;
- float f = strtof(line, &tail);
- if (errno == 0 && *tail == '\0' && isfinite(f) != 0 && f < FLT_MAX)
- {
- // disallow hexadecimal and exponents
- if (strcspn(line, "XxEePp") == strlen(line))
- {
- scaleFactor = f;
- if (scaleFactor <= 0){
- fprintf(stderr, "Float less then 0 %f\n", scaleFactor);
- return 1;
- }
- testFloat = false;
- }
- }
- }
- if (testFloat){
- fprintf(stderr, "Float not entered %s\n", line);
- return 1;
- }
- }
- // remember filenames
- char *infile = argv[2];
- char *outfile = argv[3];
- // 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, oldbi;
- 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;
- }
- oldbi.biWidth = bi.biWidth;
- oldbi.biHeight = bi.biHeight;
- int oldpadding = (4 - (oldbi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
- bi.biWidth *= scaleFactor;
- bi.biHeight *= scaleFactor;
- // determine padding for scanlines
- int padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
- bi.biSizeImage = bi.biWidth * abs(bi.biHeight) * sizeof(RGBTRIPLE) + padding;
- bf.bfSize = bi.biSizeImage + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
- // write outfile's BITMAPFILEHEADER
- fwrite(&bf, sizeof(BITMAPFILEHEADER), 1, outptr);
- // write outfile's BITMAPINFOHEADER
- fwrite(&bi, sizeof(BITMAPINFOHEADER), 1, outptr);
- float hsum,vsum;
- int jsum,isum,rewindfile;
- vsum = scaleFactor;
- isum = 0;
- // iterate over infile's scanlines
- for (int i = 0, biHeight = abs(oldbi.biHeight); i < biHeight; i++)
- {
- while((int)vsum > isum){
- hsum = scaleFactor;
- jsum = 0;
- // iterate over pixels in scanline
- for (int j = 0; j < oldbi.biWidth; j++)
- {
- // temporary storage
- RGBTRIPLE triple;
- // read RGB triple from infile
- fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
- while((int)hsum > jsum){
- // write RGB triple to outfile
- fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
- jsum++;
- }
- hsum += scaleFactor;
- }
- // 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);
- }
- isum++;
- // printf("%lu %d %d\n",sizeof(RGBTRIPLE), oldbi.biWidth, oldpadding);
- rewindfile = ((int)sizeof(RGBTRIPLE) * oldbi.biWidth + oldpadding) * (-1);
- fseek(inptr,rewindfile,SEEK_CUR);
- }
- vsum+= scaleFactor;
- fseek(inptr,-rewindfile,SEEK_CUR);
- }
- // close infile
- fclose(inptr);
- // close outfile
- fclose(outptr);
- // success
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement