Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**********************************************************
- * resize.c *
- * *
- * Made by Husam Malakwi *
- * *
- * This program risizes bmp files. *
- * *
- **********************************************************/
- #include <stdio.h>
- #include <stdlib.h>
- #include "bmp.h"
- #include "helper.h"
- // prototypes
- void create_new_file(char* infile, char* outfile);
- void edit_file_header(int scale);
- void draw_image(int scale);
- // struct that contains FILE* for input/output files
- ioptrs ioptr;
- // struct that contains both padding values
- pad padding;
- // sruct that contain file header info
- BITMAPFILEHEADER bf;
- BITMAPINFOHEADER bi;
- int main(int argc, char* argv[])
- {
- // ensure proper use
- if(argc != 4)
- {
- printf("Usage ./resize n infile outfile\n");
- return 1;
- }
- // store the scale factor "scale"
- int scale = atoi(argv[1]);
- // checks if the value is in range
- if (scale <= 0 || scale > 100)
- {
- printf("invalid number");
- return 2;
- }
- // remember filenames
- char* infile = argv[2];
- char* outfile = argv[3];
- create_new_file(infile, outfile);
- edit_file_header(scale);
- draw_image(scale);
- // close infile
- fclose(ioptr.inptr);
- //close outfile
- fclose(ioptr.outptr);
- return 0;
- }
- /*********************************************************************************
- * create_new_file: This function opens the infile for readin and creates an *
- * outfile for writing the new data according to the names *
- * specified by the user. *
- * *
- *********************************************************************************/
- void create_new_file(char* infile, char* outfile)
- {
- // open input file
- ioptr.inptr = fopen(infile, "r");
- if (ioptr.inptr == NULL)
- {
- printf("could not open %s.\n", infile);
- exit(3);
- }
- // open output file
- ioptr.outptr = fopen(outfile, "w");
- if (ioptr.outptr == NULL)
- {
- fclose(ioptr.inptr);
- fprintf(stderr, "could not create %s.\n", outfile);
- exit(4);
- }
- }
- /*********************************************************************************
- * edit_file_header: This function reads the old value if the BITMAPFILEHEADER *
- * and edits it according to the scale factor specified by the *
- * user. *
- * *
- *********************************************************************************/
- void edit_file_header(int scale)
- {
- // read the infile's BITMAPFILEHEADER
- fread (&bf, sizeof(BITMAPFILEHEADER), 1, ioptr.inptr);
- // read the infile's BITMAPINFOHEADER
- fread(&bi, sizeof(BITMAPINFOHEADER), 1, ioptr.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(ioptr.outptr);
- fclose(ioptr.inptr);
- printf("invalid file type\n");
- exit(5);
- }
- // determing the original padding value
- padding.oldPadding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
- // determine the new padding value
- padding.newPadding = (4 - ((bi.biWidth * scale) * sizeof(RGBTRIPLE)) % 4) % 4;
- // Edits the header file according to the scale
- bi.biWidth *= scale;
- bi.biHeight *= scale;
- bi.biSizeImage = (bi.biWidth * sizeof(RGBTRIPLE) + padding.newPadding) *
- abs(bi.biHeight);
- bf.bfSize = bi.biSizeImage + 54;
- // write outfile's BITMAPFILEHEADER
- fwrite(&bf, sizeof(BITMAPFILEHEADER), 1, ioptr.outptr);
- // write oufile's BITMAPINFOHEADER
- fwrite(&bi, sizeof(BITMAPINFOHEADER), 1, ioptr.outptr);
- }
- /*********************************************************************************
- * draw_image: This function scan pixels from the infile and then it writes an *
- * edited version of the pixels and padding according to the scale *
- * factor specified by the user. *
- * *
- *********************************************************************************/
- void draw_image(int scale)
- {
- //iterate over infile's scan lines "/ scale" is used to get teh original value of biHeight.
- for(int i = 0, biHeight = abs(bi.biHeight / scale); i < biHeight; i++)
- {
- //writes the line * scale factor
- for (int m = 0; m < scale; m++)
- {
- // iterate over pixels in scanline
- for(int j = 0; j < (bi.biWidth / scale); j++)
- {
- //temporary storage
- RGBTRIPLE triple;
- //read RGB triple from infile
- fread(&triple, sizeof(RGBTRIPLE), 1, ioptr.inptr);
- // write RGB triple to outfile * the scale factor
- fwrite(&triple, sizeof(RGBTRIPLE), scale, ioptr.outptr);
- }
- // skip over padding if any
- fseek(ioptr.inptr, padding.oldPadding, SEEK_CUR);
- // write the new padding
- for(int k = 0; k < padding.newPadding; k++)
- {
- fputc(0x00, ioptr.outptr);
- }
- }
- }
- }
- /*************************************************************************************************************************************/
- /**
- * bmp.h
- *
- * Computer Science 50
- * Problem Set 4
- *
- * BMP-related data types based on Microsoft's own.
- */
- #include <stdint.h>
- /**
- * Common Data Types
- *
- * The data types in this section are essentially aliases for C/C++
- * primitive data types.
- *
- * Adapted from http://msdn.microsoft.com/en-us/library/cc230309.aspx.
- * See http://en.wikipedia.org/wiki/Stdint.h for more on stdint.h.
- */
- typedef uint8_t BYTE;
- typedef uint32_t DWORD;
- typedef int32_t LONG;
- typedef uint16_t WORD;
- /**
- * BITMAPFILEHEADER
- *
- * The BITMAPFILEHEADER structure contains information about the type, size,
- * and layout of a file that contains a DIB [device-independent bitmap].
- *
- * Adapted from http://msdn.microsoft.com/en-us/library/dd183374(VS.85).aspx.
- */
- typedef struct
- {
- WORD bfType;
- DWORD bfSize;
- WORD bfReserved1;
- WORD bfReserved2;
- DWORD bfOffBits;
- } __attribute__((__packed__))
- BITMAPFILEHEADER;
- /**
- * BITMAPINFOHEADER
- *
- * The BITMAPINFOHEADER structure contains information about the
- * dimensions and color format of a DIB [device-independent bitmap].
- *
- * Adapted from http://msdn.microsoft.com/en-us/library/dd183376(VS.85).aspx.
- */
- typedef struct
- {
- DWORD biSize;
- LONG biWidth;
- LONG biHeight;
- WORD biPlanes;
- WORD biBitCount;
- DWORD biCompression;
- DWORD biSizeImage;
- LONG biXPelsPerMeter;
- LONG biYPelsPerMeter;
- DWORD biClrUsed;
- DWORD biClrImportant;
- } __attribute__((__packed__))
- BITMAPINFOHEADER;
- /**
- * RGBTRIPLE
- *
- * This structure describes a color consisting of relative intensities of
- * red, green, and blue.
- *
- * Adapted from http://msdn.microsoft.com/en-us/library/aa922590.aspx.
- */
- typedef struct
- {
- BYTE rgbtBlue;
- BYTE rgbtGreen;
- BYTE rgbtRed;
- } __attribute__((__packed__))
- RGBTRIPLE;
- /*************************************************************************************************************************************/
- /**
- * helper.h
- *
- * Husam Malkawi
- *
- * This contains structures that help resize.c
- */
- typedef struct
- {
- FILE* inptr;
- FILE* outptr;
- }
- ioptrs;
- typedef struct
- {
- int oldPadding;
- int newPadding;
- }
- pad;
Advertisement
Add Comment
Please, Sign In to add comment