Advertisement
Guest User

broken resize

a guest
Jul 22nd, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.54 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. // ensure proper usage
  11. if (argc != 4)
  12. {
  13. fprintf(stderr, "Usage: ./resize scalefactor infile outfile\n");
  14. return 1;
  15. }
  16. //sanity check for numbers
  17. int factor = atoi(argv[1]);
  18. if (factor == 0 || factor > 100)
  19. {
  20. printf("Factors equal to 0 or above 100 are invalid. Enter a different number. \n");
  21. return 2;
  22. }
  23. // remember filenames
  24. char *infile = argv[2];
  25. char *outfile = argv[3];
  26.  
  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 3;
  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 4;
  43. }
  44.  
  45. // read infile's BITMAPFILEHEADER
  46. BITMAPFILEHEADER bf;
  47. fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr);
  48.  
  49. // read infile's BITMAPINFOHEADER
  50. BITMAPINFOHEADER bi;
  51. fread(&bi, sizeof(BITMAPINFOHEADER), 1, inptr);
  52. //creating outfile's BITMAPINFOHEADER
  53. // ensure infile is (likely) a 24-bit uncompressed BMP 4.0
  54. if (bf.bfType != 0x4d42 || bf.bfOffBits != 54 || bi.biSize != 40 ||
  55. bi.biBitCount != 24 || bi.biCompression != 0)
  56. {
  57. fclose(outptr);
  58. fclose(inptr);
  59. fprintf(stderr, "Unsupported file format.\n");
  60. return 5;
  61. }
  62. double oldw = bi.biWidth;
  63. bi.biWidth = bi.biWidth * factor;
  64. bi.biHeight = bi.biHeight * factor;
  65.  
  66. // determine padding for scanlines
  67. int padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
  68.  
  69. if (bi.biWidth % 4 != 0)
  70. {
  71. bi.biWidth = bi.biWidth + padding;
  72. }
  73. //outfile's bi.bisizeim redef
  74. bi.biSizeImage = ((sizeof(RGBTRIPLE) * bi.biWidth) * abs(bi.biHeight));
  75.  
  76. // write outfile's BITMAPFILEHEADER
  77. fwrite(&bf, sizeof(BITMAPFILEHEADER), 1, outptr);
  78.  
  79. //outfile's bitinfo redef
  80. bf.bfSize = bi.biSizeImage + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
  81.  
  82. // write outfile's BITMAPINFOHEADER, need to create a variable to change the
  83. fwrite(&bi, sizeof(BITMAPINFOHEADER), 1, outptr);
  84.  
  85. printf("%i, %i", bi.biWidth, bi.biHeight);
  86. // iterate over infile's scanlines
  87. for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
  88. {
  89. for (int b = 0; b < factor; b++)
  90. {
  91. // iterate over pixels in scanline of initial file. Must be the old one because the old one is the one being manipulated, not the new width.
  92. for (int j = 0; j < oldw; j++)
  93. {
  94. // temporary storage
  95. RGBTRIPLE triple;
  96. RGBTRIPLE[] =
  97.  
  98. // read RGB triple from infile
  99. fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
  100.  
  101. // write RGB triple to outfile
  102. for (int a = 0; a < factor; a++)
  103. {
  104. fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
  105. }
  106.  
  107. }
  108. //adding padding
  109. for (int k = 0; k < padding; k++)
  110. {
  111. fputc(0x00, outptr);
  112. }
  113. }
  114.  
  115. // skip over padding, if any
  116. fseek(inptr, padding, SEEK_CUR);
  117. }
  118.  
  119. // close infile
  120. fclose(inptr);
  121.  
  122. // close outfile
  123. fclose(outptr);
  124.  
  125. // success
  126. return 0;
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement