GUI_BMPfile.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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_RGB_7Color(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. UDOUBLE Image_Byte = bmpInfoHeader.biWidth * bmpInfoHeader.biHeight * 3;
  204. UBYTE Image[Image_Byte];
  205. memset(Image, 0xFF, Image_Byte);
  206. // Determine if it is a monochrome bitmap
  207. int readbyte = bmpInfoHeader.biBitCount;
  208. if(readbyte != 24){
  209. Debug("Bmp image is not 24 bitmap!\n");
  210. exit(0);
  211. }
  212. // Read image data into the cache
  213. UWORD x, y;
  214. UBYTE Rdata[3];
  215. fseek(fp, bmpFileHeader.bOffset, SEEK_SET);
  216. for(y = 0; y < bmpInfoHeader.biHeight; y++) {//Total display column
  217. for(x = 0; x < bmpInfoHeader.biWidth ; x++) {//Show a line in the line
  218. if(fread((char *)Rdata, 1, 1, fp) != 1) {
  219. perror("get bmpdata:\r\n");
  220. break;
  221. }
  222. if(fread((char *)Rdata+1, 1, 1, fp) != 1) {
  223. perror("get bmpdata:\r\n");
  224. break;
  225. }
  226. if(fread((char *)Rdata+2, 1, 1, fp) != 1) {
  227. perror("get bmpdata:\r\n");
  228. break;
  229. }
  230. if(Rdata[0] == 0 && Rdata[1] == 0 && Rdata[2] == 0){
  231. Image[x+(y* bmpInfoHeader.biWidth )] = 0;//Black
  232. }else if(Rdata[0] == 255 && Rdata[1] == 255 && Rdata[2] == 255){
  233. Image[x+(y* bmpInfoHeader.biWidth )] = 1;//White
  234. }else if(Rdata[0] == 0 && Rdata[1] == 255 && Rdata[2] == 0){
  235. Image[x+(y* bmpInfoHeader.biWidth )] = 2;//Green
  236. }else if(Rdata[0] == 255 && Rdata[1] == 0 && Rdata[2] == 0){
  237. Image[x+(y* bmpInfoHeader.biWidth )] = 3;//Blue
  238. }else if(Rdata[0] == 0 && Rdata[1] == 0 && Rdata[2] == 255){
  239. Image[x+(y* bmpInfoHeader.biWidth )] = 4;//Red
  240. }else if(Rdata[0] == 0 && Rdata[1] == 255 && Rdata[2] == 255){
  241. Image[x+(y* bmpInfoHeader.biWidth )] = 5;//Yellow
  242. }else if(Rdata[0] == 0 && Rdata[1] == 128 && Rdata[2] == 255){
  243. Image[x+(y* bmpInfoHeader.biWidth )] = 6;//Orange
  244. }
  245. }
  246. }
  247. fclose(fp);
  248. // Refresh the image to the display buffer based on the displayed orientation
  249. for(y = 0; y < bmpInfoHeader.biHeight; y++) {
  250. for(x = 0; x < bmpInfoHeader.biWidth; x++) {
  251. if(x > Paint.Width || y > Paint.Height) {
  252. break;
  253. }
  254. Paint_SetPixel(Xstart + x, Ystart + y, Image[bmpInfoHeader.biHeight * bmpInfoHeader.biWidth - 1 -(bmpInfoHeader.biWidth-x-1+(y* bmpInfoHeader.biWidth))]);
  255. }
  256. }
  257. return 0;
  258. }
  259. UBYTE GUI_ReadBmp_RGB_4Color(const char *path, UWORD Xstart, UWORD Ystart)
  260. {
  261. FILE *fp; //Define a file pointer
  262. BMPFILEHEADER bmpFileHeader; //Define a bmp file header structure
  263. BMPINFOHEADER bmpInfoHeader; //Define a bmp info header structure
  264. // Binary file open
  265. if((fp = fopen(path, "rb")) == NULL) {
  266. Debug("Cann't open the file!\n");
  267. exit(0);
  268. }
  269. // Set the file pointer from the beginning
  270. fseek(fp, 0, SEEK_SET);
  271. fread(&bmpFileHeader, sizeof(BMPFILEHEADER), 1, fp); //sizeof(BMPFILEHEADER) must be 14
  272. fread(&bmpInfoHeader, sizeof(BMPINFOHEADER), 1, fp); //sizeof(BMPFILEHEADER) must be 50
  273. printf("pixel = %d * %d\r\n", bmpInfoHeader.biWidth, bmpInfoHeader.biHeight);
  274. UDOUBLE Image_Byte = bmpInfoHeader.biWidth * bmpInfoHeader.biHeight * 3;
  275. UBYTE Image[Image_Byte];
  276. memset(Image, 0xFF, Image_Byte);
  277. // Determine if it is a monochrome bitmap
  278. int readbyte = bmpInfoHeader.biBitCount;
  279. if(readbyte != 24){
  280. Debug("Bmp image is not 24 bitmap!\n");
  281. exit(0);
  282. }
  283. // Read image data into the cache
  284. UWORD x, y;
  285. UBYTE Rdata[3];
  286. fseek(fp, bmpFileHeader.bOffset, SEEK_SET);
  287. for(y = 0; y < bmpInfoHeader.biHeight; y++) {//Total display column
  288. for(x = 0; x < bmpInfoHeader.biWidth ; x++) {//Show a line in the line
  289. if(fread((char *)Rdata, 1, 1, fp) != 1) {
  290. perror("get bmpdata:\r\n");
  291. break;
  292. }
  293. if(fread((char *)Rdata+1, 1, 1, fp) != 1) {
  294. perror("get bmpdata:\r\n");
  295. break;
  296. }
  297. if(fread((char *)Rdata+2, 1, 1, fp) != 1) {
  298. perror("get bmpdata:\r\n");
  299. break;
  300. }
  301. if(Rdata[0] < 128 && Rdata[1] < 128 && Rdata[2] < 128){
  302. Image[x+(y* bmpInfoHeader.biWidth )] = 0;//Black
  303. }else if(Rdata[0] > 127 && Rdata[1] > 127 && Rdata[2] > 127){
  304. Image[x+(y* bmpInfoHeader.biWidth )] = 1;//White
  305. }else if(Rdata[0] < 128 && Rdata[1] > 127 && Rdata[2] > 127){
  306. Image[x+(y* bmpInfoHeader.biWidth )] = 2;//Yellow
  307. }else if(Rdata[0] < 128 && Rdata[1] < 128 && Rdata[2] > 127){
  308. Image[x+(y* bmpInfoHeader.biWidth )] = 3;//Red
  309. }
  310. }
  311. }
  312. fclose(fp);
  313. // Refresh the image to the display buffer based on the displayed orientation
  314. for(y = 0; y < bmpInfoHeader.biHeight; y++) {
  315. for(x = 0; x < bmpInfoHeader.biWidth; x++) {
  316. if(x > Paint.Width || y > Paint.Height) {
  317. break;
  318. }
  319. Paint_SetPixel(Xstart + x, Ystart + y, Image[bmpInfoHeader.biHeight * bmpInfoHeader.biWidth - 1 -(bmpInfoHeader.biWidth-x-1+(y* bmpInfoHeader.biWidth))]);
  320. }
  321. }
  322. return 0;
  323. }