Advertisement
Guest User

Untitled

a guest
Aug 18th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.74 KB | None | 0 0
  1. // Copies a BMP file
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5.  
  6. #include "bmp.h"
  7.  
  8. int main(int argc, char *argv[])
  9. {
  10.  
  11.     // Resize factor
  12.     int n = atoi(argv[1]);
  13.  
  14.     // Ensure proper usage
  15.     if (argc != 4)
  16.     {
  17.         printf("Usage: ./resize n infile outfile\n");
  18.         return 1;
  19.     }
  20.     if (n < 0 || n > 100)
  21.     {
  22.         printf("Usage: ./resize n infile outfile\n");
  23.         return 1;
  24.     }
  25.  
  26.     // Remember filenames
  27.     char *infile = argv[2];
  28.     char *outfile = argv[3];
  29.  
  30.     // Open input file
  31.     FILE *inptr = fopen(infile, "r");
  32.     if (inptr == NULL)
  33.     {
  34.         printf("Could not open %s.\n", infile);
  35.         return 2;
  36.     }
  37.  
  38.     // Open output file
  39.     FILE *outptr = fopen(outfile, "w");
  40.     if (outptr == NULL)
  41.     {
  42.         fclose(inptr);
  43.         printf("Could not create %s.\n", outfile);
  44.         return 3;
  45.     }
  46.  
  47.  
  48.     // Read infile's BITMAPFILEHEADER
  49.     BITMAPFILEHEADER bf;
  50.     fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr);
  51.  
  52.     // Read infile's BITMAPINFOHEADER
  53.     BITMAPINFOHEADER bi;
  54.     fread(&bi, sizeof(BITMAPINFOHEADER), 1, inptr);
  55.  
  56.     // Adjust Height and Width
  57.     bi.biHeight = bi.biHeight * n;
  58.     bi.biWidth = bi.biWidth * n;
  59.  
  60.     // Determine padding for scanlines
  61.     int newPadding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
  62.     int oldPadding = (4 - (((bi.biWidth) / n) * sizeof(RGBTRIPLE)) % 4) % 4;
  63.  
  64.     // Adjust image and file size
  65.     bi.biSizeImage = ((bi.biWidth * sizeof(RGBTRIPLE)) + newPadding) * abs(bi.biHeight);
  66.     bf.bfSize = bi.biSizeImage + sizeof(BITMAPINFOHEADER) + sizeof(BITMAPFILEHEADER);
  67.  
  68.  
  69.     // Ensure infile is (likely) a 24-bit uncompressed BMP 4.0
  70.     if (bf.bfType != 0x4d42 || bf.bfOffBits != 54 || bi.biSize != 40 ||
  71.         bi.biBitCount != 24 || bi.biCompression != 0)
  72.     {
  73.         fclose(outptr);
  74.         fclose(inptr);
  75.         printf("Unsupported file format.\n");
  76.         return 4;
  77.     }
  78.  
  79.     // Write outfile's BITMAPFILEHEADER
  80.     fwrite(&bf, sizeof(BITMAPFILEHEADER), 1, outptr);
  81.  
  82.  
  83.     // Write outfile's BITMAPINFOHEADER
  84.     fwrite(&bi, sizeof(BITMAPINFOHEADER), 1, outptr);
  85.  
  86.     // Iterate over infile's scanlines
  87.     for (int i = 0, biHeight = abs((bi.biHeight) / n); i < biHeight; i++)
  88.     {
  89.  
  90.         // Make every row n times
  91.         for (int l = 0; l < n; l++)
  92.         {
  93.             // Iterate over pixels in scanline
  94.             for (int j = 0; j < (bi.biWidth) / n; j++)
  95.             {
  96.  
  97.                 // Make every pixel n times
  98.                 for (int m = 0; m < n; m++)
  99.                 {
  100.                     // Temporary storage
  101.                     RGBTRIPLE triple;
  102.  
  103.                     // Read RGB triple from infile
  104.                     fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
  105.  
  106.                     // Write RGB triple n times to outfile
  107.                     fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
  108.  
  109.                     // Move infile cursor 1 RGB triple back
  110.                     fseek(inptr, -(sizeof(RGBTRIPLE)), SEEK_CUR);
  111.                 }
  112.  
  113.                 // Move infile cursor 1 RGB triple forward
  114.                 fseek(inptr, sizeof(RGBTRIPLE), SEEK_CUR);
  115.             }
  116.  
  117.             // Add padding
  118.             for (int k = 0; k < newPadding; k++)
  119.             {
  120.                 fputc(0x00, outptr);
  121.             }
  122.  
  123.             // Send infile cursor back
  124.             fseek(inptr, -(bi.biWidth / n) * sizeof(RGBTRIPLE), SEEK_CUR);
  125.         }
  126.  
  127.         // Send infile cursor forward
  128.         fseek(inptr, (bi.biWidth / n) * sizeof(RGBTRIPLE), SEEK_CUR);
  129.  
  130.         // Skip over padding, if any
  131.         fseek(inptr, oldPadding, SEEK_CUR);
  132.  
  133.     }
  134.  
  135.     // Close infile
  136.     fclose(inptr);
  137.  
  138.     // Close outfile
  139.     fclose(outptr);
  140.  
  141.     // Success
  142.     return 0;
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement