How to write a C Program Copies a BMP piece by piece in C Programming Language ?
Solution:
/*Copies a BMP piece by piece*/ #include <stdio.h> #include <stdlib.h> #include "bmp.h" int main(int argc, char* argv[]) { // ensure proper usage if (argc != 4) { printf("Usage: ./resize n infile outfile\n"); return 1; } if (atoi(argv[1]) < 1 || atoi(argv[1]) > 100) { printf("size has to be between 1 and 100\n"); return 1; } // remember filenames int multiple = atoi(argv[1]); 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; } // read infile's BITMAPFILEHEADER BITMAPFILEHEADER bf, bf_r; fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr); bf_r = bf; // read infile's BITMAPINFOHEADER BITMAPINFOHEADER bi, bi_r; fread(&bi, sizeof(BITMAPINFOHEADER), 1, inptr); bi_r = 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; } // resize header details bi_r.biWidth = bi.biWidth * multiple; bi_r.biHeight = bi.biHeight * multiple; // determine padding for scanlines int padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4; int r_padding = (4 - (bi_r.biWidth * sizeof(RGBTRIPLE)) % 4) % 4; // determine imageSize bi_r.biSizeImage = (bi_r.biWidth + r_padding) * abs(bi_r.biHeight) * sizeof(RGBTRIPLE); bf_r.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + bi_r.biSizeImage; // write outfile's BITMAPFILEHEADER fwrite(&bf_r, sizeof(BITMAPFILEHEADER), 1, outptr); // write outfile's BITMAPINFOHEADER fwrite(&bi_r, sizeof(BITMAPINFOHEADER), 1, outptr); // iterate over infile's scanlines for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++) { // 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); // create temporary buffer for resized image int* buffer = malloc(bi_r.biSizeImage); // write each pixel multiple times for (int k = 0; k < multiple; k ++) { // write RGB triple to outfile fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr); } } // skip over padding, if any fseek(inptr, padding, SEEK_CUR); // then add it back (to demonstrate how) for (int k = 0; k < padding; k++) { fputc(0x00, outptr); } } // close infile fclose(inptr); // close outfile fclose(outptr); // that's all folks return 0; }