Advertisement
Guest User

Untitled

a guest
Jul 19th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.77 KB | None | 0 0
  1. #include <Windows.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4.  
  5. //typedef struct _BITMAPFILEHEADER
  6. //{
  7. // WORD bfType; //specifies the file type
  8. // DWORD bfSize; //specifies the size in bytes of the bitmap file
  9. // WORD bfReserved1; //reserved; must be 0
  10. // WORD bfReserved2; //reserved; must be 0
  11. // DWORD bfOffBits; //species the offset in bytes from the bitmapfileheader to the bitmap bits
  12. //}BITMAPFILEHEADER, *PBITMAPFILEHEADER;
  13. //
  14. //typedef struct _BITMAPINFOHEADER
  15. //{
  16. // DWORD biSize; //specifies the number of bytes required by the struct
  17. // LONG biWidth; //specifies width in pixels
  18. // LONG biHeight; //species height in pixels
  19. // WORD biPlanes; //specifies the number of color planes, must be 1
  20. // WORD biBitCount; //specifies the number of bit per pixel
  21. // DWORD biCompression;//spcifies the type of compression
  22. // DWORD biSizeImage; //size of image in bytes
  23. // LONG biXPelsPerMeter; //number of pixels per meter in x axis
  24. // LONG biYPelsPerMeter; //number of pixels per meter in y axis
  25. // DWORD biClrUsed; //number of colors used by th ebitmap
  26. // DWORD biClrImportant; //number of colors that are important
  27. //}BITMAPINFOHEADER, *PBITMAPINFOHEADER;
  28.  
  29. typedef struct _RGB {
  30. BYTE Red;
  31. BYTE Green;
  32. BYTE Blue;
  33. } RGB, *PRGB;
  34.  
  35. #define FILE_NAME "pr19.bmp"
  36. #define SIGNATURE 0x4D42
  37.  
  38. HANDLE getDataPointer(
  39. PBYTE copyFileName,
  40. int *width,
  41. int *height,
  42. PRGB *startingPixel
  43. )
  44. {
  45. HANDLE fileHandle = INVALID_HANDLE_VALUE;
  46. HANDLE fileMappingHandle = INVALID_HANDLE_VALUE;
  47. PBITMAPFILEHEADER data = NULL;
  48. PBITMAPINFOHEADER bitmapInfoHeader = NULL;
  49. DWORD fileSize = 0;
  50.  
  51. BOOL copied = CopyFileA(
  52. FILE_NAME,
  53. copyFileName,
  54. FALSE
  55. );
  56.  
  57. if (copied == FALSE)
  58. {
  59. printf("Could not copy file %s", copyFileName);
  60. return INVALID_HANDLE_VALUE;
  61. }
  62.  
  63. fileHandle = CreateFileA(
  64. copyFileName, //lpFileName
  65. GENERIC_READ | GENERIC_WRITE, //dwDesiredAccess
  66. 0, //dwShareMode
  67. NULL, //lpSecurityAttributes
  68. OPEN_EXISTING, //dwCreationDisposition
  69. FILE_ATTRIBUTE_NORMAL, //dwFlagsAndAttributes
  70. NULL //hTemplateFile
  71. );
  72. if (INVALID_HANDLE_VALUE == fileHandle)
  73. {
  74. printf("Error: could not open file %d", GetLastError());
  75. system("pause");
  76. exit(1);
  77. }
  78.  
  79. fileMappingHandle = CreateFileMappingA(
  80. fileHandle, //hFile
  81. NULL, //lpFileMappingAttributes
  82. PAGE_READWRITE, //flProtect
  83. 0, //dwMaximumSizeHigh
  84. 0, //dwMaximumSizeLow
  85. NULL //lpName
  86. );
  87. if (NULL == fileMappingHandle)
  88. {
  89. printf("Error: mapping file %d", GetLastError());
  90. goto cleanup;
  91. }
  92.  
  93. fileSize = GetFileSize(
  94. fileHandle,
  95. NULL
  96. );
  97.  
  98. data = (PBITMAPFILEHEADER)MapViewOfFile(
  99. fileMappingHandle, //hFileMappingObject
  100. FILE_MAP_WRITE, //dwDesiredAccess
  101. 0, //dwFileOffsetHigh
  102. 0, //dwFileOffsetLow
  103. fileSize //dwNumberOfBytesToMap
  104. );
  105. if (NULL == data)
  106. {
  107. printf("Error: mapping file %d", GetLastError());
  108. goto cleanup;
  109. }
  110.  
  111. if (SIGNATURE != data->bfType)
  112. {
  113. printf("Error: signature");
  114. goto cleanup;
  115. }
  116.  
  117.  
  118. bitmapInfoHeader = (PBITMAPINFOHEADER)((PBYTE)data + sizeof(BITMAPFILEHEADER));
  119. *width = bitmapInfoHeader->biWidth;
  120. *height = bitmapInfoHeader->biHeight;
  121.  
  122. *startingPixel = (PBYTE)data + data->bfOffBits;
  123. return fileHandle;
  124.  
  125. cleanup:
  126. if (NULL != data)
  127. {
  128. UnmapViewOfFile(data);
  129. }
  130. if (NULL != fileMappingHandle)
  131. {
  132. CloseHandle(fileMappingHandle);
  133. }
  134.  
  135. return INVALID_HANDLE_VALUE;
  136. }
  137.  
  138. void solveA()
  139. {
  140. int width, height;
  141. PRGB pixel;
  142.  
  143. HANDLE h = getDataPointer("pr191.bmp", &width, &height, &pixel);
  144. int pad = (4 - width * 3 % 4) % 4;
  145. if (INVALID_HANDLE_VALUE != h)
  146. {
  147. for (int j = 0; j < height; ++j)
  148. {
  149. for (int i = 0; i < width; ++i)
  150. {
  151. pixel->Red = 255 - pixel->Red;
  152. pixel->Green = 255 - pixel->Green;
  153. pixel->Blue = 255 - pixel->Blue;
  154. pixel += 1;
  155. }
  156. pixel = (PBYTE)pixel + pad;
  157. }
  158. CloseHandle(h);
  159. }
  160. }
  161.  
  162. void solveB()
  163. {
  164. int width, height;
  165. PRGB pixel;
  166.  
  167. HANDLE h = getDataPointer("pr193.bmp", &width, &height, &pixel);
  168. int pad = (4 - width * 3 % 4) % 4;
  169. if (INVALID_HANDLE_VALUE != h)
  170. {
  171. for (int j = 0; j < height; ++j)
  172. {
  173. for (int i = 0; i < width; ++i)
  174. {
  175. int av = (pixel->Red + pixel->Green + pixel->Blue) / 3;
  176. pixel->Red = av;
  177. pixel->Green = av;
  178. pixel->Blue = av;
  179. pixel += 1;
  180. }
  181. pixel = (PBYTE)pixel + pad;
  182. }
  183. CloseHandle(h);
  184. }
  185. }
  186.  
  187. void solveC()
  188. {
  189. int width, height;
  190. PRGB pixel;
  191. PRGB pixelAux;
  192. RGB aux;
  193.  
  194. HANDLE h = getDataPointer("pr192.bmp", &width, &height, &pixel);
  195. int pad = (4 - width * 3 % 4) % 4;
  196. pixelAux = pixel + width * (height - 1);
  197. pixelAux = (PBYTE)pixelAux + (height - 1) * pad;
  198. if (INVALID_HANDLE_VALUE != h)
  199. {
  200. for (int i = 0; i < height / 2; ++i)
  201. {
  202. for (int j = 0; j < width; ++j)
  203. {
  204. //swap
  205. //////////////////////////////////
  206. aux.Red = pixel->Red;
  207. aux.Blue = pixel->Blue;
  208. aux.Green = pixel->Green;
  209.  
  210. pixel->Red = pixelAux->Red;
  211. pixel->Green = pixelAux->Green;
  212. pixel->Blue = pixelAux->Blue;
  213.  
  214. pixelAux->Red = aux.Red;
  215. pixelAux->Green = aux.Green;
  216. pixelAux->Blue = aux.Blue;
  217. //////////////////////////////////
  218. pixel += 1;
  219. pixelAux += 1;
  220. }
  221. pixel = (PBYTE)pixel + pad;
  222. pixelAux -= width * 2;
  223. pixelAux = (PBYTE)pixelAux - pad;
  224. }
  225. CloseHandle(h);
  226. }
  227. }
  228.  
  229. int main()
  230. {
  231. solveA();
  232. solveB();
  233. solveC();
  234. system("pause");
  235. return 0;
  236. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement