ketan18710

Untitled

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