Guest User

Untitled

a guest
Jan 23rd, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.88 KB | None | 0 0
  1. // Scans a BMP file and increases its size by a factor of n
  2.  
  3. //open infile
  4. //update outfield header info
  5. //Read infile scanline pixel x pixel
  6. //Resize horizontally
  7. //remember padding
  8. //Resize vertically
  9.  
  10.  
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13.  
  14. #include "bmp.h"
  15.  
  16. int main(int argc, char *argv[])
  17. {
  18. // ensure proper usage
  19. if (argc != 3)
  20. {
  21. fprintf(stderr, "Usage: copy infile outfile\n");
  22. return 1;
  23. }
  24.  
  25. // remember filenames
  26. char *infile = argv[1];
  27. char *outfile = argv[2];
  28.  
  29. // open input file
  30. FILE *inptr = fopen(infile, "r");
  31. if (inptr == NULL)
  32. {
  33. fprintf(stderr, "Could not open %s.\n", infile);
  34. return 2;
  35. }
  36.  
  37. // open output file
  38. FILE *outptr = fopen(outfile, "w");
  39. if (outptr == NULL)
  40. {
  41. fclose(inptr);
  42. fprintf(stderr, "Could not create %s.\n", outfile);
  43. return 3;
  44. }
  45.  
  46. // read infile's BITMAPFILEHEADER
  47. BITMAPFILEHEADER bf;
  48. fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr);
  49.  
  50. // read infile's BITMAPINFOHEADER
  51. BITMAPINFOHEADER bi;
  52. fread(&bi, sizeof(BITMAPINFOHEADER), 1, inptr);
  53.  
  54. // ensure infile is (likely) a 24-bit uncompressed BMP 4.0
  55. if (bf.bfType != 0x4d42 || bf.bfOffBits != 54 || bi.biSize != 40 ||
  56. bi.biBitCount != 24 || bi.biCompression != 0)
  57. {
  58. fclose(outptr);
  59. fclose(inptr);
  60. fprintf(stderr, "Unsupported file format.\n");
  61. return 4;
  62. }
  63.  
  64.  
  65.  
  66. // write outfile's BITMAPFILEHEADER
  67. fwrite(&bf, sizeof(BITMAPFILEHEADER), 1, outptr);
  68.  
  69. // write outfile's BITMAPINFOHEADER
  70. fwrite(&bi, sizeof(BITMAPINFOHEADER), 1, outptr);
  71.  
  72. //modify outfile BITMAPFILEHEADER to match the increse in size...
  73.  
  74. //my sandbox....
  75. printf ("Size of BMFIH: %li\n", sizeof(BITMAPINFOHEADER));
  76. printf ("Size of BMIH: %li\n", sizeof(BITMAPFILEHEADER));
  77. bi.biWidth = 256;
  78. printf ("size of biWidth = %i\n", bi.biWidth);
  79. bf.bfReserved1 = 256;
  80. printf ("size of bfReserved1 = %i\n", bf.bfReserved1);
  81.  
  82.  
  83.  
  84. // determine padding for scanlines
  85. int padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
  86.  
  87. // iterate over infile's scanlines
  88. for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
  89. {
  90. // iterate over pixels in scanline
  91. for (int j = 0; j < bi.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. fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
  101. }
  102.  
  103. // skip over padding, if any
  104. fseek(inptr, padding, SEEK_CUR);
  105.  
  106. // then add it back (to demonstrate how)
  107. for (int k = 0; k < padding; k++)
  108. {
  109. fputc(0x00, outptr);
  110. }
  111. }
  112.  
  113. // close infile
  114. fclose(inptr);
  115.  
  116. // close outfile
  117. fclose(outptr);
  118.  
  119. // success
  120. return 0;
  121. }
Add Comment
Please, Sign In to add comment