frain8

Untitled

Nov 21st, 2019
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.21 KB | None | 0 0
  1. /* Dasprog C - 2019
  2. William Handi Wijaya
  3. 0087
  4.  
  5. Program untuk memperbesar suatu gambar berformat bmp.
  6. */
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10.  
  11. // Masukan header yang berisi custom data type dan struct
  12. #include "bmp.h"
  13.  
  14. int main(int argc, char *argv[])
  15. {
  16.     // ensure proper usage
  17.     if (argc != 4)
  18.     {
  19.         fprintf(stderr, "Usage: copy infile outfile\n");
  20.         return 1;
  21.     }
  22.  
  23.     // remember filenames
  24.     char *infile = argv[2];
  25.     char *outfile = argv[3];
  26.     int n = atoi(argv[1]);
  27.  
  28.     // open input file
  29.     FILE *inptr = fopen(infile, "r");
  30.     if (inptr == NULL)
  31.     {
  32.         fprintf(stderr, "Could not open %s.\n", infile);
  33.         return 2;
  34.     }
  35.  
  36.     // open output file
  37.     FILE *outptr = fopen(outfile, "w");
  38.     if (outptr == NULL)
  39.     {
  40.         fclose(inptr);
  41.         fprintf(stderr, "Could not create %s.\n", outfile);
  42.         return 3;
  43.     }
  44.  
  45.     // read infile's BITMAPFILEHEADER
  46.     BITMAPFILEHEADER bf, bf_old;
  47.     fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr);
  48.     bf_old = bf;
  49.  
  50.     // read infile's BITMAPINFOHEADER
  51.     BITMAPINFOHEADER bi, bi_old;
  52.     fread(&bi, sizeof(BITMAPINFOHEADER), 1, inptr);
  53.     bi_old = bi;
  54.  
  55.     // ensure infile is (likely) a 24-bit uncompressed BMP 4.0
  56.     if (bf.bfType != 0x4d42 || bf.bfOffBits != 54 || bi.biSize != 40 ||
  57.         bi.biBitCount != 24 || bi.biCompression != 0)
  58.     {
  59.         fclose(outptr);
  60.         fclose(inptr);
  61.         fprintf(stderr, "Unsupported file format.\n");
  62.         return 4;
  63.     }
  64.  
  65.     // Resize canvas
  66.     bi.biWidth *= n;
  67.     bi.biHeight *= n;
  68.  
  69.     // determine padding for scanlines
  70.     int old_padding = (4 - (bi_old.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
  71.     int padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
  72.  
  73.     // Change image size
  74.     bi.biSizeImage = ((sizeof(RGBTRIPLE) * bi.biWidth) + padding) * abs(bi.biHeight);
  75.  
  76.     // Change file size
  77.     bf.bfSize = bi.biSizeImage + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
  78.  
  79.     // write outfile's BITMAPFILEHEADER
  80.     fwrite(&bf, sizeof(BITMAPFILEHEADER), 1, outptr);
  81.  
  82.     // write outfile's BITMAPINFOHEADER
  83.     fwrite(&bi, sizeof(BITMAPINFOHEADER), 1, outptr);
  84.  
  85.     RGBTRIPLE temp[bi.biWidth];
  86.  
  87.     // iterate over infile's scanlines
  88.     for (int i = 0, biHeight = abs(bi_old.biHeight); i < biHeight; i++)
  89.     {
  90.         // iterate over pixels in scanline
  91.         for (int j = 0; j < bi_old.biWidth; j++)
  92.         {
  93.             // temporary storage
  94.             RGBTRIPLE triple;
  95.  
  96.             // read RGB triple from infile
  97.             fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
  98.  
  99.             // write RGB triple to outfile
  100.             for (int k = 0; k < n; k++)
  101.             {
  102.                 temp[(j * n) + k] = triple;
  103.             }
  104.         }
  105.  
  106.         // skip over infile padding, if any
  107.         fseek(inptr, old_padding, SEEK_CUR);
  108.  
  109.         for (int k = 0; k < n; k++)
  110.         {
  111.             fwrite(temp, sizeof(RGBTRIPLE), bi.biWidth, outptr);
  112.             for (int a = 0; a < padding; a++)
  113.             {
  114.                 fputc(0x00, outptr);
  115.             }
  116.         }
  117.     }
  118.  
  119.     // close infile
  120.     fclose(inptr);
  121.  
  122.     // close outfile
  123.     fclose(outptr);
  124.  
  125.     // success
  126.     return 0;
  127. }
Add Comment
Please, Sign In to add comment