Advertisement
Guest User

Untitled

a guest
Aug 24th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.45 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <cs50.h>
  3. #include <string.h>
  4. #include "bmp.h"
  5.  
  6. int main(int argc, char *argv[])
  7. {
  8. //Tests command line input for correct # of inputs
  9. if (argc != 4)
  10. {
  11. printf("Usage: ./resize n infile outfile\n");
  12. return 1;
  13. }
  14.  
  15. //Add pointers for factor & file names of infile and outfile
  16. int rFactor = atoi(argv[1]);
  17. char *infile = argv[2];
  18. char *outfile = argv[3];
  19.  
  20. //Tests rFactor input for integers between 1 and 100 inclusive
  21. if (rFactor < 1 || rFactor > 100)
  22. {
  23. printf("Resize factor needs to be a value between 1 and 100, inclusive.\n");
  24. return 1;
  25. }
  26. //Open infile and check for NULL
  27. FILE *inptr = fopen(infile, "r");
  28. if (inptr == NULL)
  29. {
  30. printf("Could not open %s.\n", infile);
  31. return 2;
  32. }
  33.  
  34. //Open or create output file
  35. FILE *outptr = fopen(outfile, "w");
  36. if (outptr == NULL)
  37. {
  38. printf("Could not create %s.\n", outfile);
  39. return 3;
  40. }
  41.  
  42. //Read infile BITMAPFILEHEADER
  43. BITMAPFILEHEADER bf;
  44. fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr);
  45.  
  46. //Read infile BITMAPINFOHEADER
  47. BITMAPINFOHEADER bi;
  48. fread(&bi, sizeof(BITMAPINFOHEADER), 1, inptr);
  49.  
  50. //Ensure file is 24-bit uncompressed BMP 4.0
  51. if (bf.bfType != 0x4d42 || bf.bfOffBits != 54 || bi.biSize != 40 || bi.biBitCount != 24 || bi.biCompression != 0)
  52. {
  53. fclose(inptr);
  54. fclose(outptr);
  55. printf("Unsupported File Format.\n");
  56. return 4;
  57. }
  58.  
  59. int inptWidth = bi.biWidth;
  60. int inptHeight = bi.biHeight;
  61.  
  62. //Modify width/height of BITMAPINFOHEADER
  63. bi.biWidth = bi.biWidth * rFactor;
  64. bi.biHeight = bi.biHeight * rFactor;
  65.  
  66. //Write outfile BITMAPFILEHEADER
  67. fwrite(&bf, sizeof(BITMAPFILEHEADER), 1, outptr);
  68.  
  69. //Write outfile BITMAPINFOHEADER
  70. fwrite(&bi, sizeof(BITMAPINFOHEADER), 1, outptr);
  71.  
  72. //Determine padding for input scanlines
  73. int inpt_padding = (4 - (inptWidth * sizeof(RGBTRIPLE)) % 4) % 4;
  74.  
  75. //Determine padding for output scanlines
  76. int outpt_padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
  77.  
  78. //iterate over infile height
  79. for (int h = 0; h < abs(inptHeight); h++)
  80. {
  81. //create temp array to store RGBTRIPLE
  82. RGBTRIPLE *row = malloc(bi.biWidth * sizeof(RGBTRIPLE));
  83.  
  84. //iterate over pixels in scanline
  85. for (int i = 0; i < bi.biWidth; i+= rFactor)
  86. {
  87. //Create temp var to store RGBTRIPLE
  88. RGBTRIPLE triple;
  89.  
  90. //Read RGBTRIPLE from infile and store in temp var
  91. fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
  92.  
  93. //Store temp var in array
  94. row[i] = triple;
  95.  
  96. //iterate pixel for rFactor scaling
  97. for (int j = i + 1; j < bi.biWidth; j++)
  98. row[j] = triple;
  99. }
  100.  
  101. for (int x = 0; x < bi.biWidth; x++)
  102. printf("%i, %i, %i | ", row[x].rgbtRed, row[x].rgbtGreen, row[x].rgbtBlue);
  103. printf("\n");
  104.  
  105. //write array to outfile
  106. for (int k = 0; k < rFactor; k++)
  107. {
  108. for (int l = 0; l < bi.biWidth; l++)
  109. fwrite(&row[l], sizeof(RGBTRIPLE), 1, outptr);
  110. for (int m = 0; m < outpt_padding; m++)
  111. fputc(0x00, outptr);
  112. }
  113.  
  114. //Skip infile padding
  115. fseek(inptr, inpt_padding, SEEK_CUR);
  116.  
  117. //free up memory
  118. free(row);
  119. }
  120.  
  121. //Close infile
  122. fclose(inptr);
  123.  
  124. //Close outfile
  125. fclose(outptr);
  126.  
  127. //Sucess
  128. return 0;
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement