Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Copies a BMP file
- #include <stdio.h>
- #include <stdlib.h>
- #include "bmp.h"
- int main(int argc, char *argv[])
- {
- // ensure proper usage
- if (argc != 4)
- {
- fprintf(stderr, "Usage: copy infile outfile\n");
- return 1;
- }
- // ensure 'n' is between 0 and 100
- int num;
- if (atoi(argv[1]) > 0 && atoi(argv[1]) <= 100)
- {
- num = atoi(argv[1]);
- }
- else
- {
- return 1;
- }
- // remember filenames
- int n = num;
- 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 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;
- fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr);
- BITMAPFILEHEADER bfNew; //new
- bfNew = bf; //now fread's data is workable from bfNew
- // read infile's BITMAPINFOHEADER
- BITMAPINFOHEADER bi;
- fread(&bi, sizeof(BITMAPINFOHEADER), 1, inptr);
- BITMAPINFOHEADER biNew;
- biNew = bi; //now fread's data is workable from biNew
- // 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;
- }
- // updated height & width
- biNew.biWidth = bi.biWidth * n;
- biNew.biHeight = bi.biHeight * n;
- // determine padding for scanlines
- int padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
- int paddingNew = (4 - (biNew.biWidth * sizeof(RGBTRIPLE)) % 4) % 4; //new padding
- // update dimension
- biNew.biSizeImage = ((sizeof(RGBTRIPLE) * biNew.biWidth) + paddingNew) * abs(biNew.biHeight);
- bfNew.bfSize = biNew.biSizeImage + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
- // write outfile's BITMAPFILEHEADER
- fwrite(&bfNew, sizeof(BITMAPFILEHEADER), 1, outptr);
- // write outfile's BITMAPINFOHEADER
- fwrite(&biNew, sizeof(BITMAPINFOHEADER), 1, outptr);
- /*
- //RE-COPY METHOD
- // iterate over each scanline
- for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
- {
- //FIRST TIME
- // for n-1 times
- for (int a = 0; a < n - 1; a++)
- {
- //read, write, padding, cursor_BACK
- //iterate over each pixel
- for (int b = 0; b < bi.biWidth; b++)
- {
- // temporary storage
- RGBTRIPLE triple;
- // read RGB triple from infile
- fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
- //// for n-times
- // write RGB triple to outfile
- for (int c = 0; c < n; c++)
- {
- fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
- }
- //fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
- // write new padding
- for (int k = 0; k < paddingNew; k++)
- {
- fputc(0x00, outptr);
- }
- // send infile cursor back
- fseek(inptr, -(bi.biWidth * sizeof(RGBTRIPLE)), SEEK_CUR);
- }
- }
- //LAST TIME
- //read, write, padding, padding_SKIP
- // 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);
- // write RGB trip to outfile
- fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
- // write new padding
- for (int k = 0; k < paddingNew; k++)
- {
- fputc(0x00, outptr);
- }
- // skip over padding, if any (infile)
- fseek(inptr, padding, SEEK_CUR);
- }
- }
- */
- //REWRITE METHOD
- // iterate over infile's scanlines
- for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
- {
- // temporary storage
- RGBTRIPLE pixelArr[abs(biNew.biWidth)]; //RGBTRIPLE pixelArr[abs(biNew.biWidth) * sizeof(RGBTRIPLE)];
- // Array index counter
- int count = 0;
- // iterate over pixels in scanline
- for (int j = 0; j < bi.biWidth; j++)
- {
- //make an array, fill up and at the end of this loop send values to upper loop
- // temporary storage
- RGBTRIPLE triple;
- //RGBTRIPLE pixelArr[n];
- // read RGB triple from infile
- fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
- // write pixel in array n times
- for (int l = 0; l < n; l++, count++)
- {
- pixelArr[count] = triple;
- }
- }
- // for n times scanline
- for (int m = 0; m < n; m++) //for (int m = 0; m < n; m++)
- {
- // write RGB triple array to outfile
- fwrite(&pixelArr, sizeof(biNew.biWidth), 1, outptr);
- // write new padding
- for (int k = 0; k < paddingNew; k++)
- {
- fputc(0x00, outptr);
- }
- }
- // skip over padding, if any
- fseek(inptr, padding, SEEK_CUR);
- // fseek?
- }
- // close infile
- fclose(inptr);
- // close outfile
- fclose(outptr);
- // success
- return 0;
- }
Add Comment
Please, Sign In to add comment