Advertisement
Guest User

Untitled

a guest
May 23rd, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.37 KB | None | 0 0
  1. #include<stdio.h>
  2.  
  3. #pragma pack(push, 1)
  4.  
  5. typedef struct BITMAPFILEHEADER{
  6. unsigned short int bfType;
  7. unsigned long int bfSize;
  8. unsigned short int bfReserved1;
  9. unsigned short int bfReserved2;
  10. unsigned long int bfOffBits;
  11. } BITMAPFILEHEADER;
  12.  
  13. typedef struct BITMAPINFOHEADER{
  14. unsigned long int biSize;
  15. long int biWidth;
  16. long int biHeight;
  17. unsigned short int biPlanes;
  18. unsigned short int biBitCount;
  19. unsigned long int biCompression;
  20. unsigned long int biSizeImage;
  21. long int biXPelsPerMeter;
  22. long int biYPelsPerMeter;
  23. unsigned long int biClrUsed;
  24. unsigned long int biClrImportant;
  25. } BITMAPINFOHEADER;
  26.  
  27. typedef struct RGBTRIPLE {
  28. char rgbtBlue;
  29. char rgbtGreen;
  30. char rgbtRed;
  31. } RGBTRIPLE;
  32. #pragma pack(pop)
  33.  
  34. void printInfo(BITMAPFILEHEADER filehdr, BITMAPINFOHEADER infohdr){
  35.  
  36. printf("%x\n",filehdr.bfType);
  37. printf("%li\n",filehdr.bfSize);
  38. printf("%d\n",filehdr.bfReserved1);
  39. printf("%d\n",filehdr.bfReserved2);
  40. printf("%li\n\n",filehdr.bfOffBits);
  41.  
  42. printf("%li\n", infohdr.biSize);
  43. printf("%li\n", infohdr.biWidth);
  44. printf("%li\n", infohdr.biHeight);
  45. printf("%d\n", infohdr.biPlanes);
  46. printf("%d\n", infohdr.biBitCount);
  47. printf("%li\n", infohdr.biCompression);
  48. printf("%li\n", infohdr.biSizeImage);
  49. printf("%li\n", infohdr.biXPelsPerMeter);
  50. printf("%li\n", infohdr.biYPelsPerMeter);
  51. printf("%li\n", infohdr.biClrUsed);
  52. printf("%li\n", infohdr.biClrImportant);
  53. }
  54.  
  55. int checkCoordinates(int x0, int y0, int x1, int y1, int height, int width) { //возвращает 1, если координаты некорректны
  56. //возвращает 0, если данные введены верно
  57. if (x0 < 0 || y0 < 0 || x1 < 0|| y1 < 0) {
  58.  
  59. return 1;
  60. }
  61. if (x0 > width || y0 > height || x1 > width || y1 > height ) {
  62.  
  63. return 1;
  64. }
  65. return 0;
  66. }
  67.  
  68. void rot(long int x0, long int y0, long int x1, long int y1, RGBTRIPLE** rgb) {
  69.  
  70. RGBTRIPLE swapRgb [y1-y0+1][x1-x0+1];
  71. long int i, j, m = 0, k = 0;
  72.  
  73. for ( i = x0; i <= y1; i ++ , k ++) {
  74. for ( j = y0; j <= x1; j++, m ++) {
  75.  
  76. swapRgb[m][k] = rgb[i][j];
  77. }
  78. }
  79.  
  80. for ( i = x0; i < y1; i ++ , m ++) {
  81. for ( j = y0; j < x1; j++, k ++) {
  82.  
  83. rgb[i][j] = swapRgb[m][k];
  84. }
  85. }
  86. }
  87.  
  88. int main(){
  89.  
  90. BITMAPFILEHEADER filehdr;
  91. BITMAPINFOHEADER infohdr;
  92. RGBTRIPLE rgbChange;
  93. RGBTRIPLE rgbZero = {'0','0','0'};
  94.  
  95. char inputFile[10];
  96. long int x0, y0, x1, y1, height, width, i, j, k = 0;
  97. int emptyBytes = 0, flagChange = 0; // flagChange == 1, если строка была отражена
  98.  
  99. printf("Enter file name: ");
  100. scanf("%s", &inputFile);
  101. printf("Enter x0: ");
  102. scanf("%li", &x0);
  103. printf("Enter y0: ");
  104. scanf("%li", &y0);
  105. printf("Enter x1: ");
  106. scanf("%li", &x1);
  107. printf("Enter y1: ");
  108. scanf("%li", &y1);
  109. FILE* f = fopen(inputFile, "rb");
  110.  
  111. if (!f){
  112.  
  113. printf("Fail with file\n");
  114. return 0;
  115. }
  116.  
  117. fread(&filehdr, sizeof(struct BITMAPFILEHEADER), 1, f);
  118. fread(&infohdr, sizeof(struct BITMAPINFOHEADER), 1, f);
  119.  
  120. height = infohdr.biHeight;
  121. width = infohdr.biWidth;
  122.  
  123. if (checkCoordinates(x0, y0, x1, y1, height, width)) {
  124.  
  125. printf("Fail with coordinates\n");
  126. return 0;
  127. }
  128.  
  129. FILE* newf = fopen("new.bmp", "wb");
  130.  
  131. fwrite(&filehdr, sizeof(BITMAPFILEHEADER), 1, newf);
  132. fwrite(&infohdr, sizeof(BITMAPINFOHEADER), 1, newf);
  133.  
  134. if ( (width * 3) % 4 ){
  135.  
  136. emptyBytes = 4 - (width * 3) % 4;
  137. }
  138.  
  139. RGBTRIPLE rgb[height][width];
  140.  
  141. for (i = 0; i < height ; i++) {
  142.  
  143. for (j = 0; j < width; j++) {
  144.  
  145. fread(&rgb[i][j], sizeof(RGBTRIPLE), 1, f);
  146. }
  147.  
  148. if (emptyBytes) {
  149.  
  150. fread(&rgbChange, emptyBytes, 1, f);
  151. }
  152. }
  153.  
  154. rot(x0, height - y0, x1, height - y1 , rgb);
  155.  
  156. for (i = 0; i < height ; i++) {
  157.  
  158. for (j = 0; j < width; j++ ) {
  159.  
  160. fwrite(&rgb[i][j], sizeof(RGBTRIPLE), 1, newf);
  161. }
  162.  
  163. if (emptyBytes) {
  164.  
  165. fwrite(&rgbZero, emptyBytes, 1, newf);
  166. }
  167. }
  168.  
  169. printf("Look new.bmp");
  170. fclose(f);
  171. fclose(newf);
  172. return 0;
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement