Facebook
From kiran, 1 Month ago, written in C.
Embed
Download Paste or View Raw
Hits: 26
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. //bu verilmiş hazır kodlar bunda 2. parametre olarak vereceğim bitmap ı yeniden boyutlandırmaya çalşyrm
  4. // ./resize 2 small.bmp larger.bmp ----> ilk parametrede kaç la ölçeklendreceğmi ikinci parametre ölçeklendrlcek resmi 3. prmetre de ölçeklediğim resmin dosyası
  5. //olcak sekilde görev tanımı var
  6. #include "bmp.h"
  7.  
  8. int main(int argc, char *argv[])
  9. {
  10.     // ensure proper usage
  11.     if (argc != 3)
  12.     {
  13.         fprintf(stderr, "Usage: copy infile outfile\n");
  14.         return 1;
  15.     }
  16.  
  17.     // remember filenames
  18.     char *infile = argv[1];
  19.     char *outfile = argv[2];
  20.  
  21.     // open input file
  22.     FILE *inptr = fopen(infile, "r");
  23.     if (inptr == NULL)
  24.     {
  25.         fprintf(stderr, "Could not open %s.\n", infile);
  26.         return 2;
  27.     }
  28.  
  29.     // open output file
  30.     FILE *outptr = fopen(outfile, "w");
  31.     if (outptr == NULL)
  32.     {
  33.         fclose(inptr);
  34.         fprintf(stderr, "Could not create %s.\n", outfile);
  35.         return 3;
  36.     }
  37.  
  38.     // read infile's BITMAPFILEHEADER
  39.     BITMAPFILEHEADER bf;
  40.     fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr);
  41.  
  42.     // read infile's BITMAPINFOHEADER
  43.     BITMAPINFOHEADER bi;
  44.     fread(&bi, sizeof(BITMAPINFOHEADER), 1, inptr);
  45.  
  46.     // ensure infile is (likely) a 24-bit uncompressed BMP 4.0
  47.     if (bf.bfType != 0x4d42 || bf.bfOffBits != 54 || bi.biSize != 40 ||
  48.         bi.biBitCount != 24 || bi.biCompression != 0)
  49.     {
  50.         fclose(outptr);
  51.         fclose(inptr);
  52.         fprintf(stderr, "Unsupported file format.\n");
  53.         return 4;
  54.     }
  55.  
  56.     // write outfile's BITMAPFILEHEADER
  57.     fwrite(&bf, sizeof(BITMAPFILEHEADER), 1, outptr);
  58.  
  59.     // write outfile's BITMAPINFOHEADER
  60.     fwrite(&bi, sizeof(BITMAPINFOHEADER), 1, outptr);
  61.  
  62.     // determine padding for scanlines
  63.     int padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
  64.  
  65.     // iterate over infile's scanlines
  66.     for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
  67.     {
  68.         // iterate over pixels in scanline
  69.         for (int j = 0; j < bi.biWidth; j++)
  70.         {
  71.             // temporary storage
  72.             RGBTRIPLE triple;
  73.  
  74.             // read RGB triple from infile
  75.             fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
  76.  
  77.             // write RGB triple to outfile
  78.             fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
  79.         }
  80.  
  81.         // skip over padding, if any
  82.         fseek(inptr, padding, SEEK_CUR);
  83.  
  84.         // then add it back (to demonstrate how)
  85.         for (int k = 0; k < padding; k++)
  86.         {
  87.             fputc(0x00, outptr);
  88.         }
  89.     }
  90.  
  91.     // close infile
  92.     fclose(inptr);
  93.  
  94.     // close outfile
  95.     fclose(outptr);
  96.  
  97.     // success
  98.     return 0;
  99. }
  100.