GUI_BMPfile.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. /*****************************************************************************
  2. * | File : GUI_BMPfile.h
  3. * | Author : Waveshare team
  4. * | Function : Hardware underlying interface
  5. * | Info :
  6. * Used to shield the underlying layers of each master
  7. * and enhance portability
  8. *----------------
  9. * | This version: V2.3
  10. * | Date : 2022-07-27
  11. * | Info :
  12. * -----------------------------------------------------------------------------
  13. * V2.3(2022-07-27):
  14. * 1.Add GUI_ReadBmp_RGB_4Color()
  15. * V2.2(2020-07-08):
  16. * 1.Add GUI_ReadBmp_RGB_7Color()
  17. * V2.1(2019-10-10):
  18. * 1.Add GUI_ReadBmp_4Gray()
  19. * V2.0(2018-11-12):
  20. * 1.Change file name: GUI_BMP.h -> GUI_BMPfile.h
  21. * 2.fix: GUI_ReadBmp()
  22. * Now Xstart and Xstart can control the position of the picture normally,
  23. * and support the display of images of any size. If it is larger than
  24. * the actual display range, it will not be displayed.
  25. #
  26. # Permission is hereby granted, free of charge, to any person obtaining a copy
  27. # of this software and associated documnetation files (the "Software"), to deal
  28. # in the Software without restriction, including without limitation the rights
  29. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  30. # copies of the Software, and to permit persons to whom the Software is
  31. # furished to do so, subject to the following conditions:
  32. #
  33. # The above copyright notice and this permission notice shall be included in
  34. # all copies or substantial portions of the Software.
  35. #
  36. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  37. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  38. # FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  39. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  40. # LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  41. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  42. # THE SOFTWARE.
  43. #
  44. ******************************************************************************/
  45. #include "GUI_BMPfile.h"
  46. #include "GUI_Paint.h"
  47. #include "Debug.h"
  48. #include <fcntl.h>
  49. #include <unistd.h>
  50. #include <stdint.h>
  51. #include <stdlib.h> //exit()
  52. #include <string.h> //memset()
  53. #include <math.h> //memset()
  54. #include <stdio.h>
  55. UBYTE GUI_ReadBmp(const char *path, UWORD Xstart, UWORD Ystart)
  56. {
  57. FILE *fp; //Define a file pointer
  58. BMPFILEHEADER bmpFileHeader; //Define a bmp file header structure
  59. BMPINFOHEADER bmpInfoHeader; //Define a bmp info header structure
  60. // Binary file open
  61. if((fp = fopen(path, "rb")) == NULL) {
  62. Debug("Cann't open the file!\n");
  63. exit(0);
  64. }
  65. // Set the file pointer from the beginning
  66. fseek(fp, 0, SEEK_SET);
  67. fread(&bmpFileHeader, sizeof(BMPFILEHEADER), 1, fp); //sizeof(BMPFILEHEADER) must be 14
  68. fread(&bmpInfoHeader, sizeof(BMPINFOHEADER), 1, fp); //sizeof(BMPFILEHEADER) must be 50
  69. printf("pixel = %d * %d\r\n", bmpInfoHeader.biWidth, bmpInfoHeader.biHeight);
  70. UWORD Image_Width_Byte = (bmpInfoHeader.biWidth % 8 == 0)? (bmpInfoHeader.biWidth / 8): (bmpInfoHeader.biWidth / 8 + 1);
  71. UWORD Bmp_Width_Byte = (Image_Width_Byte % 4 == 0) ? Image_Width_Byte: ((Image_Width_Byte / 4 + 1) * 4);
  72. UBYTE Image[Image_Width_Byte * bmpInfoHeader.biHeight];
  73. memset(Image, 0xFF, Image_Width_Byte * bmpInfoHeader.biHeight);
  74. // Determine if it is a monochrome bitmap
  75. int readbyte = bmpInfoHeader.biBitCount;
  76. if(readbyte != 1) {
  77. Debug("the bmp Image is not a monochrome bitmap!\n");
  78. exit(0);
  79. }
  80. // Determine black and white based on the palette
  81. UWORD i;
  82. UWORD Bcolor, Wcolor;
  83. UWORD bmprgbquadsize = pow(2, bmpInfoHeader.biBitCount);// 2^1 = 2
  84. BMPRGBQUAD bmprgbquad[bmprgbquadsize]; //palette
  85. // BMPRGBQUAD bmprgbquad[2]; //palette
  86. for(i = 0; i < bmprgbquadsize; i++){
  87. // for(i = 0; i < 2; i++) {
  88. fread(&bmprgbquad[i], sizeof(BMPRGBQUAD), 1, fp);
  89. }
  90. if(bmprgbquad[0].rgbBlue == 0xff && bmprgbquad[0].rgbGreen == 0xff && bmprgbquad[0].rgbRed == 0xff) {
  91. Bcolor = BLACK;
  92. Wcolor = WHITE;
  93. } else {
  94. Bcolor = WHITE;
  95. Wcolor = BLACK;
  96. }
  97. // Read image data into the cache
  98. UWORD x, y;
  99. UBYTE Rdata;
  100. fseek(fp, bmpFileHeader.bOffset, SEEK_SET);
  101. for(y = 0; y < bmpInfoHeader.biHeight; y++) {//Total display column
  102. for(x = 0; x < Bmp_Width_Byte; x++) {//Show a line in the line
  103. if(fread((char *)&Rdata, 1, readbyte, fp) != readbyte) {
  104. perror("get bmpdata:\r\n");
  105. break;
  106. }
  107. if(x < Image_Width_Byte) { //bmp
  108. Image[x + (bmpInfoHeader.biHeight - y - 1) * Image_Width_Byte] = Rdata;
  109. // printf("rdata = %d\r\n", Rdata);
  110. }
  111. }
  112. }
  113. fclose(fp);
  114. // Refresh the image to the display buffer based on the displayed orientation
  115. UBYTE color, temp;
  116. for(y = 0; y < bmpInfoHeader.biHeight; y++) {
  117. for(x = 0; x < bmpInfoHeader.biWidth; x++) {
  118. if(x > Paint.Width || y > Paint.Height) {
  119. break;
  120. }
  121. temp = Image[(x / 8) + (y * Image_Width_Byte)];
  122. color = (((temp << (x%8)) & 0x80) == 0x80) ?Bcolor:Wcolor;
  123. Paint_SetPixel(Xstart + x, Ystart + y, color);
  124. }
  125. }
  126. return 0;
  127. }
  128. /*************************************************************************
  129. *************************************************************************/
  130. UBYTE GUI_ReadBmp_4Gray(const char *path, UWORD Xstart, UWORD Ystart)
  131. {
  132. FILE *fp; //Define a file pointer
  133. BMPFILEHEADER bmpFileHeader; //Define a bmp file header structure
  134. BMPINFOHEADER bmpInfoHeader; //Define a bmp info header structure
  135. // Binary file open
  136. if((fp = fopen(path, "rb")) == NULL) {
  137. Debug("Cann't open the file!\n");
  138. exit(0);
  139. }
  140. // Set the file pointer from the beginning
  141. fseek(fp, 0, SEEK_SET);
  142. fread(&bmpFileHeader, sizeof(BMPFILEHEADER), 1, fp); //sizeof(BMPFILEHEADER) must be 14
  143. fread(&bmpInfoHeader, sizeof(BMPINFOHEADER), 1, fp); //sizeof(BMPFILEHEADER) must be 50
  144. printf("pixel = %d * %d\r\n", bmpInfoHeader.biWidth, bmpInfoHeader.biHeight);
  145. UWORD Image_Width_Byte = (bmpInfoHeader.biWidth % 4 == 0)? (bmpInfoHeader.biWidth / 4): (bmpInfoHeader.biWidth / 4 + 1);
  146. UWORD Bmp_Width_Byte = (bmpInfoHeader.biWidth % 2 == 0)? (bmpInfoHeader.biWidth / 2): (bmpInfoHeader.biWidth / 2 + 1);
  147. UBYTE Image[Image_Width_Byte * bmpInfoHeader.biHeight * 2];
  148. memset(Image, 0xFF, Image_Width_Byte * bmpInfoHeader.biHeight * 2);
  149. // Determine if it is a monochrome bitmap
  150. int readbyte = bmpInfoHeader.biBitCount;
  151. printf("biBitCount = %d\r\n",readbyte);
  152. if(readbyte != 4){
  153. Debug("Bmp image is not a 4-color bitmap!\n");
  154. exit(0);
  155. }
  156. // Read image data into the cache
  157. UWORD x, y;
  158. UBYTE Rdata;
  159. fseek(fp, bmpFileHeader.bOffset, SEEK_SET);
  160. for(y = 0; y < bmpInfoHeader.biHeight; y++) {//Total display column
  161. for(x = 0; x < Bmp_Width_Byte; x++) {//Show a line in the line
  162. if(fread((char *)&Rdata, 1, 1, fp) != 1) {
  163. perror("get bmpdata:\r\n");
  164. break;
  165. }
  166. if(x < Image_Width_Byte*2) { //bmp
  167. Image[x + (bmpInfoHeader.biHeight - y - 1) * Image_Width_Byte*2] = Rdata;
  168. }
  169. }
  170. }
  171. fclose(fp);
  172. // Refresh the image to the display buffer based on the displayed orientation
  173. UBYTE color, temp;
  174. printf("bmpInfoHeader.biWidth = %d\r\n",bmpInfoHeader.biWidth);
  175. printf("bmpInfoHeader.biHeight = %d\r\n",bmpInfoHeader.biHeight);
  176. for(y = 0; y < bmpInfoHeader.biHeight; y++) {
  177. for(x = 0; x < bmpInfoHeader.biWidth; x++) {
  178. if(x > Paint.Width || y > Paint.Height) {
  179. break;
  180. }
  181. temp = Image[x/2 + y * bmpInfoHeader.biWidth/2] >> ((x%2)? 0:4);//0xf 0x8 0x7 0x0
  182. color = temp>>2; //11 10 01 00
  183. Paint_SetPixel(Xstart + x, Ystart + y, color);
  184. }
  185. }
  186. return 0;
  187. }
  188. UBYTE GUI_ReadBmp_16Gray(const char *path, UWORD Xstart, UWORD Ystart)
  189. {
  190. FILE *fp; //Define a file pointer
  191. BMPFILEHEADER bmpFileHeader; //Define a bmp file header structure
  192. BMPINFOHEADER bmpInfoHeader; //Define a bmp info header structure
  193. // Binary file open
  194. if((fp = fopen(path, "rb")) == NULL) {
  195. Debug("Cann't open the file!\n");
  196. exit(0);
  197. }
  198. // Set the file pointer from the beginning
  199. fseek(fp, 0, SEEK_SET);
  200. fread(&bmpFileHeader, sizeof(BMPFILEHEADER), 1, fp); //sizeof(BMPFILEHEADER) must be 14
  201. fread(&bmpInfoHeader, sizeof(BMPINFOHEADER), 1, fp); //sizeof(BMPFILEHEADER) must be 50
  202. printf("pixel = %d * %d\r\n", bmpInfoHeader.biWidth, bmpInfoHeader.biHeight);
  203. // They are both the same width in bytes
  204. // round up to the next byte
  205. UWORD Width_Byte = (bmpInfoHeader.biWidth + 1) / 2;
  206. UBYTE Image[Width_Byte * bmpInfoHeader.biHeight];
  207. memset(Image, 0xFF, Width_Byte * bmpInfoHeader.biHeight);
  208. // Determine if it is a monochrome bitmap
  209. int readbyte = bmpInfoHeader.biBitCount;
  210. printf("biBitCount = %d\r\n",readbyte);
  211. if(readbyte != 4) {
  212. Debug("Bmp image is not a 4-bit bitmap!\n");
  213. exit(0);
  214. }
  215. // Determine colors based on the palette
  216. // A map from palette entry to color
  217. UBYTE colors[16];
  218. UBYTE i;
  219. BMPRGBQUAD rgbData;
  220. for (i = 0; i < 16; i++){
  221. fread(&rgbData, sizeof(BMPRGBQUAD), 1, fp);
  222. // Work out the closest colour
  223. // 16 colours over 0-255 => 0-8 => 0, 9-25 => 1 (17), 26-42 => 2 (34), etc
  224. // Base it on red
  225. colors[i] = (rgbData.rgbRed + 8) / 17;
  226. }
  227. // Read image data into the cache
  228. UWORD x, y;
  229. UBYTE Rdata;
  230. fseek(fp, bmpFileHeader.bOffset, SEEK_SET);
  231. for (y = 0; y < bmpInfoHeader.biHeight; y++) {//Total display column
  232. for (x = 0; x < Width_Byte; x++) {//Show a line in the line
  233. if (fread((char *) &Rdata, 1, 1, fp) != 1) {
  234. perror("get bmpdata:\r\n");
  235. break;
  236. }
  237. Image[x + (bmpInfoHeader.biHeight - y - 1) * Width_Byte] = Rdata;
  238. }
  239. }
  240. fclose(fp);
  241. // Refresh the image to the display buffer based on the displayed orientation
  242. UBYTE coloridx;
  243. printf("bmpInfoHeader.biWidth = %d\r\n", bmpInfoHeader.biWidth);
  244. printf("bmpInfoHeader.biHeight = %d\r\n", bmpInfoHeader.biHeight);
  245. for (y = 0; y < bmpInfoHeader.biHeight; y++) {
  246. for (x = 0; x < bmpInfoHeader.biWidth; x++) {
  247. if (Xstart + x > Paint.Width || Ystart + y > Paint.Height)
  248. break;
  249. coloridx = (Image[x / 2 + y * Width_Byte] >> ((x % 2) ? 0 : 4)) & 15;
  250. Paint_SetPixel(Xstart + x, Ystart + y, colors[coloridx]);
  251. }
  252. }
  253. return 0;
  254. }
  255. UBYTE GUI_ReadBmp_RGB_7Color(const char *path, UWORD Xstart, UWORD Ystart)
  256. {
  257. FILE *fp; //Define a file pointer
  258. BMPFILEHEADER bmpFileHeader; //Define a bmp file header structure
  259. BMPINFOHEADER bmpInfoHeader; //Define a bmp info header structure
  260. // Binary file open
  261. if((fp = fopen(path, "rb")) == NULL) {
  262. Debug("Cann't open the file!\n");
  263. exit(0);
  264. }
  265. // Set the file pointer from the beginning
  266. fseek(fp, 0, SEEK_SET);
  267. fread(&bmpFileHeader, sizeof(BMPFILEHEADER), 1, fp); //sizeof(BMPFILEHEADER) must be 14
  268. fread(&bmpInfoHeader, sizeof(BMPINFOHEADER), 1, fp); //sizeof(BMPFILEHEADER) must be 50
  269. printf("pixel = %d * %d\r\n", bmpInfoHeader.biWidth, bmpInfoHeader.biHeight);
  270. UDOUBLE Image_Byte = bmpInfoHeader.biWidth * bmpInfoHeader.biHeight * 3;
  271. UBYTE Image[Image_Byte];
  272. memset(Image, 0xFF, Image_Byte);
  273. // Determine if it is a monochrome bitmap
  274. int readbyte = bmpInfoHeader.biBitCount;
  275. if(readbyte != 24){
  276. Debug("Bmp image is not 24 bitmap!\n");
  277. exit(0);
  278. }
  279. // Read image data into the cache
  280. UWORD x, y;
  281. UBYTE Rdata[3];
  282. fseek(fp, bmpFileHeader.bOffset, SEEK_SET);
  283. for(y = 0; y < bmpInfoHeader.biHeight; y++) {//Total display column
  284. for(x = 0; x < bmpInfoHeader.biWidth ; x++) {//Show a line in the line
  285. if(fread((char *)Rdata, 1, 1, fp) != 1) {
  286. perror("get bmpdata:\r\n");
  287. break;
  288. }
  289. if(fread((char *)Rdata+1, 1, 1, fp) != 1) {
  290. perror("get bmpdata:\r\n");
  291. break;
  292. }
  293. if(fread((char *)Rdata+2, 1, 1, fp) != 1) {
  294. perror("get bmpdata:\r\n");
  295. break;
  296. }
  297. if(Rdata[0] == 0 && Rdata[1] == 0 && Rdata[2] == 0){
  298. Image[x+(y* bmpInfoHeader.biWidth )] = 0;//Black
  299. }else if(Rdata[0] == 255 && Rdata[1] == 255 && Rdata[2] == 255){
  300. Image[x+(y* bmpInfoHeader.biWidth )] = 1;//White
  301. }else if(Rdata[0] == 0 && Rdata[1] == 255 && Rdata[2] == 0){
  302. Image[x+(y* bmpInfoHeader.biWidth )] = 2;//Green
  303. }else if(Rdata[0] == 255 && Rdata[1] == 0 && Rdata[2] == 0){
  304. Image[x+(y* bmpInfoHeader.biWidth )] = 3;//Blue
  305. }else if(Rdata[0] == 0 && Rdata[1] == 0 && Rdata[2] == 255){
  306. Image[x+(y* bmpInfoHeader.biWidth )] = 4;//Red
  307. }else if(Rdata[0] == 0 && Rdata[1] == 255 && Rdata[2] == 255){
  308. Image[x+(y* bmpInfoHeader.biWidth )] = 5;//Yellow
  309. }else if(Rdata[0] == 0 && Rdata[1] == 128 && Rdata[2] == 255){
  310. Image[x+(y* bmpInfoHeader.biWidth )] = 6;//Orange
  311. }
  312. }
  313. }
  314. fclose(fp);
  315. // Refresh the image to the display buffer based on the displayed orientation
  316. for(y = 0; y < bmpInfoHeader.biHeight; y++) {
  317. for(x = 0; x < bmpInfoHeader.biWidth; x++) {
  318. if(x > Paint.Width || y > Paint.Height) {
  319. break;
  320. }
  321. Paint_SetPixel(Xstart + x, Ystart + y, Image[bmpInfoHeader.biHeight * bmpInfoHeader.biWidth - 1 -(bmpInfoHeader.biWidth-x-1+(y* bmpInfoHeader.biWidth))]);
  322. }
  323. }
  324. return 0;
  325. }
  326. UBYTE GUI_ReadBmp_RGB_4Color(const char *path, UWORD Xstart, UWORD Ystart)
  327. {
  328. FILE *fp; //Define a file pointer
  329. BMPFILEHEADER bmpFileHeader; //Define a bmp file header structure
  330. BMPINFOHEADER bmpInfoHeader; //Define a bmp info header structure
  331. // Binary file open
  332. if((fp = fopen(path, "rb")) == NULL) {
  333. Debug("Cann't open the file!\n");
  334. exit(0);
  335. }
  336. // Set the file pointer from the beginning
  337. fseek(fp, 0, SEEK_SET);
  338. fread(&bmpFileHeader, sizeof(BMPFILEHEADER), 1, fp); //sizeof(BMPFILEHEADER) must be 14
  339. fread(&bmpInfoHeader, sizeof(BMPINFOHEADER), 1, fp); //sizeof(BMPFILEHEADER) must be 50
  340. printf("pixel = %d * %d\r\n", bmpInfoHeader.biWidth, bmpInfoHeader.biHeight);
  341. UDOUBLE Image_Byte = bmpInfoHeader.biWidth * bmpInfoHeader.biHeight * 3;
  342. UBYTE Image[Image_Byte];
  343. memset(Image, 0xFF, Image_Byte);
  344. // Determine if it is a monochrome bitmap
  345. int readbyte = bmpInfoHeader.biBitCount;
  346. if(readbyte != 24){
  347. Debug("Bmp image is not 24 bitmap!\n");
  348. exit(0);
  349. }
  350. // Read image data into the cache
  351. UWORD x, y;
  352. UBYTE Rdata[3];
  353. fseek(fp, bmpFileHeader.bOffset, SEEK_SET);
  354. for(y = 0; y < bmpInfoHeader.biHeight; y++) {//Total display column
  355. for(x = 0; x < bmpInfoHeader.biWidth ; x++) {//Show a line in the line
  356. if(fread((char *)Rdata, 1, 1, fp) != 1) {
  357. perror("get bmpdata:\r\n");
  358. break;
  359. }
  360. if(fread((char *)Rdata+1, 1, 1, fp) != 1) {
  361. perror("get bmpdata:\r\n");
  362. break;
  363. }
  364. if(fread((char *)Rdata+2, 1, 1, fp) != 1) {
  365. perror("get bmpdata:\r\n");
  366. break;
  367. }
  368. if(Rdata[0] < 128 && Rdata[1] < 128 && Rdata[2] < 128){
  369. Image[x+(y* bmpInfoHeader.biWidth )] = 0;//Black
  370. }else if(Rdata[0] > 127 && Rdata[1] > 127 && Rdata[2] > 127){
  371. Image[x+(y* bmpInfoHeader.biWidth )] = 1;//White
  372. }else if(Rdata[0] < 128 && Rdata[1] > 127 && Rdata[2] > 127){
  373. Image[x+(y* bmpInfoHeader.biWidth )] = 2;//Yellow
  374. }else if(Rdata[0] < 128 && Rdata[1] < 128 && Rdata[2] > 127){
  375. Image[x+(y* bmpInfoHeader.biWidth )] = 3;//Red
  376. }
  377. }
  378. if(bmpInfoHeader.biWidth % 4 != 0)
  379. {
  380. for (UWORD i = 0; i < (bmpInfoHeader.biWidth % 4); i++)
  381. {
  382. if(fread((char *)Rdata, 1, 1, fp) != 1) {
  383. perror("get bmpdata:\r\n");
  384. break;
  385. }
  386. }
  387. }
  388. }
  389. fclose(fp);
  390. // Refresh the image to the display buffer based on the displayed orientation
  391. for(y = 0; y < bmpInfoHeader.biHeight; y++) {
  392. for(x = 0; x < bmpInfoHeader.biWidth; x++) {
  393. if(x > Paint.Width || y > Paint.Height) {
  394. break;
  395. }
  396. Paint_SetPixel(Xstart + x, Ystart + y, Image[bmpInfoHeader.biHeight * bmpInfoHeader.biWidth - 1 -(bmpInfoHeader.biWidth-x-1+(y* bmpInfoHeader.biWidth))]);
  397. }
  398. }
  399. return 0;
  400. }
  401. UBYTE GUI_ReadBmp_RGB_6Color(const char *path, UWORD Xstart, UWORD Ystart)
  402. {
  403. FILE *fp; //Define a file pointer
  404. BMPFILEHEADER bmpFileHeader; //Define a bmp file header structure
  405. BMPINFOHEADER bmpInfoHeader; //Define a bmp info header structure
  406. // Binary file open
  407. if((fp = fopen(path, "rb")) == NULL) {
  408. Debug("Cann't open the file!\n");
  409. exit(0);
  410. }
  411. // Set the file pointer from the beginning
  412. fseek(fp, 0, SEEK_SET);
  413. fread(&bmpFileHeader, sizeof(BMPFILEHEADER), 1, fp); //sizeof(BMPFILEHEADER) must be 14
  414. fread(&bmpInfoHeader, sizeof(BMPINFOHEADER), 1, fp); //sizeof(BMPFILEHEADER) must be 50
  415. printf("pixel = %d * %d\r\n", bmpInfoHeader.biWidth, bmpInfoHeader.biHeight);
  416. UDOUBLE Image_Byte = bmpInfoHeader.biWidth * bmpInfoHeader.biHeight * 3;
  417. UBYTE Image[Image_Byte];
  418. memset(Image, 0xFF, Image_Byte);
  419. // Determine if it is a monochrome bitmap
  420. int readbyte = bmpInfoHeader.biBitCount;
  421. if(readbyte != 24){
  422. Debug("Bmp image is not 24 bitmap!\n");
  423. exit(0);
  424. }
  425. // Read image data into the cache
  426. UWORD x, y;
  427. UBYTE Rdata[3];
  428. fseek(fp, bmpFileHeader.bOffset, SEEK_SET);
  429. for(y = 0; y < bmpInfoHeader.biHeight; y++) {//Total display column
  430. for(x = 0; x < bmpInfoHeader.biWidth ; x++) {//Show a line in the line
  431. if(fread((char *)Rdata, 1, 1, fp) != 1) {
  432. perror("get bmpdata:\r\n");
  433. break;
  434. }
  435. if(fread((char *)Rdata+1, 1, 1, fp) != 1) {
  436. perror("get bmpdata:\r\n");
  437. break;
  438. }
  439. if(fread((char *)Rdata+2, 1, 1, fp) != 1) {
  440. perror("get bmpdata:\r\n");
  441. break;
  442. }
  443. if(Rdata[0] == 0 && Rdata[1] == 0 && Rdata[2] == 0){
  444. Image[x+(y* bmpInfoHeader.biWidth )] = 0;//Black
  445. }else if(Rdata[0] == 255 && Rdata[1] == 255 && Rdata[2] == 255){
  446. Image[x+(y* bmpInfoHeader.biWidth )] = 1;//White
  447. }else if(Rdata[0] == 0 && Rdata[1] == 255 && Rdata[2] == 255){
  448. Image[x+(y* bmpInfoHeader.biWidth )] = 2;//Yellow
  449. }else if(Rdata[0] == 0 && Rdata[1] == 0 && Rdata[2] == 255){
  450. Image[x+(y* bmpInfoHeader.biWidth )] = 3;//Red
  451. // }else if(Rdata[0] == 0 && Rdata[1] == 128 && Rdata[2] == 255){
  452. // Image[x+(y* bmpInfoHeader.biWidth )] = 4;//Orange
  453. }else if(Rdata[0] == 255 && Rdata[1] == 0 && Rdata[2] == 0){
  454. Image[x+(y* bmpInfoHeader.biWidth )] = 5;//Blue
  455. }else if(Rdata[0] == 0 && Rdata[1] == 255 && Rdata[2] == 0){
  456. Image[x+(y* bmpInfoHeader.biWidth )] = 6;//Green
  457. }
  458. }
  459. }
  460. fclose(fp);
  461. // Refresh the image to the display buffer based on the displayed orientation
  462. for(y = 0; y < bmpInfoHeader.biHeight; y++) {
  463. for(x = 0; x < bmpInfoHeader.biWidth; x++) {
  464. if(x > Paint.Width || y > Paint.Height) {
  465. break;
  466. }
  467. Paint_SetPixel(Xstart + x, Ystart + y, Image[bmpInfoHeader.biHeight * bmpInfoHeader.biWidth - 1 -(bmpInfoHeader.biWidth-x-1+(y* bmpInfoHeader.biWidth))]);
  468. }
  469. }
  470. return 0;
  471. }