2
0

GUI_BMPfile.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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.2
  10. * | Date : 2020-07-08
  11. * | Info :
  12. * -----------------------------------------------------------------------------
  13. * V2.2(2020-07-08):
  14. * 1.Add GUI_ReadBmp_RGB_7Color()
  15. * V2.1(2019-10-10):
  16. * 1.Add GUI_ReadBmp_4Gray()
  17. * V2.0(2018-11-12):
  18. * 1.Change file name: GUI_BMP.h -> GUI_BMPfile.h
  19. * 2.fix: GUI_ReadBmp()
  20. * Now Xstart and Xstart can control the position of the picture normally,
  21. * and support the display of images of any size. If it is larger than
  22. * the actual display range, it will not be displayed.
  23. #
  24. # Permission is hereby granted, free of charge, to any person obtaining a copy
  25. # of this software and associated documnetation files (the "Software"), to deal
  26. # in the Software without restriction, including without limitation the rights
  27. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  28. # copies of the Software, and to permit persons to whom the Software is
  29. # furished to do so, subject to the following conditions:
  30. #
  31. # The above copyright notice and this permission notice shall be included in
  32. # all copies or substantial portions of the Software.
  33. #
  34. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  35. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  36. # FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  37. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  38. # LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  39. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  40. # THE SOFTWARE.
  41. #
  42. ******************************************************************************/
  43. #include "GUI_BMPfile.h"
  44. #include "GUI_Paint.h"
  45. #include "Debug.h"
  46. #include <fcntl.h>
  47. #include <unistd.h>
  48. #include <stdint.h>
  49. #include <stdlib.h> //exit()
  50. #include <string.h> //memset()
  51. #include <math.h> //memset()
  52. #include <stdio.h>
  53. UBYTE GUI_ReadBmp(const char *path, UWORD Xstart, UWORD Ystart)
  54. {
  55. FILE *fp; //Define a file pointer
  56. BMPFILEHEADER bmpFileHeader; //Define a bmp file header structure
  57. BMPINFOHEADER bmpInfoHeader; //Define a bmp info header structure
  58. // Binary file open
  59. if((fp = fopen(path, "rb")) == NULL) {
  60. Debug("Cann't open the file!\n");
  61. exit(0);
  62. }
  63. // Set the file pointer from the beginning
  64. fseek(fp, 0, SEEK_SET);
  65. fread(&bmpFileHeader, sizeof(BMPFILEHEADER), 1, fp); //sizeof(BMPFILEHEADER) must be 14
  66. fread(&bmpInfoHeader, sizeof(BMPINFOHEADER), 1, fp); //sizeof(BMPFILEHEADER) must be 50
  67. printf("pixel = %d * %d\r\n", bmpInfoHeader.biWidth, bmpInfoHeader.biHeight);
  68. UWORD Image_Width_Byte = (bmpInfoHeader.biWidth % 8 == 0)? (bmpInfoHeader.biWidth / 8): (bmpInfoHeader.biWidth / 8 + 1);
  69. UWORD Bmp_Width_Byte = (Image_Width_Byte % 4 == 0) ? Image_Width_Byte: ((Image_Width_Byte / 4 + 1) * 4);
  70. UBYTE Image[Image_Width_Byte * bmpInfoHeader.biHeight];
  71. memset(Image, 0xFF, Image_Width_Byte * bmpInfoHeader.biHeight);
  72. // Determine if it is a monochrome bitmap
  73. int readbyte = bmpInfoHeader.biBitCount;
  74. if(readbyte != 1) {
  75. Debug("the bmp Image is not a monochrome bitmap!\n");
  76. exit(0);
  77. }
  78. // Determine black and white based on the palette
  79. UWORD i;
  80. UWORD Bcolor, Wcolor;
  81. UWORD bmprgbquadsize = pow(2, bmpInfoHeader.biBitCount);// 2^1 = 2
  82. BMPRGBQUAD bmprgbquad[bmprgbquadsize]; //palette
  83. // BMPRGBQUAD bmprgbquad[2]; //palette
  84. for(i = 0; i < bmprgbquadsize; i++){
  85. // for(i = 0; i < 2; i++) {
  86. fread(&bmprgbquad[i], sizeof(BMPRGBQUAD), 1, fp);
  87. }
  88. if(bmprgbquad[0].rgbBlue == 0xff && bmprgbquad[0].rgbGreen == 0xff && bmprgbquad[0].rgbRed == 0xff) {
  89. Bcolor = BLACK;
  90. Wcolor = WHITE;
  91. } else {
  92. Bcolor = WHITE;
  93. Wcolor = BLACK;
  94. }
  95. // Read image data into the cache
  96. UWORD x, y;
  97. UBYTE Rdata;
  98. fseek(fp, bmpFileHeader.bOffset, SEEK_SET);
  99. for(y = 0; y < bmpInfoHeader.biHeight; y++) {//Total display column
  100. for(x = 0; x < Bmp_Width_Byte; x++) {//Show a line in the line
  101. if(fread((char *)&Rdata, 1, readbyte, fp) != readbyte) {
  102. perror("get bmpdata:\r\n");
  103. break;
  104. }
  105. if(x < Image_Width_Byte) { //bmp
  106. Image[x + (bmpInfoHeader.biHeight - y - 1) * Image_Width_Byte] = Rdata;
  107. // printf("rdata = %d\r\n", Rdata);
  108. }
  109. }
  110. }
  111. fclose(fp);
  112. // Refresh the image to the display buffer based on the displayed orientation
  113. UBYTE color, temp;
  114. for(y = 0; y < bmpInfoHeader.biHeight; y++) {
  115. for(x = 0; x < bmpInfoHeader.biWidth; x++) {
  116. if(x > Paint.Width || y > Paint.Height) {
  117. break;
  118. }
  119. temp = Image[(x / 8) + (y * Image_Width_Byte)];
  120. color = (((temp << (x%8)) & 0x80) == 0x80) ?Bcolor:Wcolor;
  121. Paint_SetPixel(Xstart + x, Ystart + y, color);
  122. }
  123. }
  124. return 0;
  125. }
  126. /*************************************************************************
  127. *************************************************************************/
  128. UBYTE GUI_ReadBmp_4Gray(const char *path, UWORD Xstart, UWORD Ystart)
  129. {
  130. FILE *fp; //Define a file pointer
  131. BMPFILEHEADER bmpFileHeader; //Define a bmp file header structure
  132. BMPINFOHEADER bmpInfoHeader; //Define a bmp info header structure
  133. // Binary file open
  134. if((fp = fopen(path, "rb")) == NULL) {
  135. Debug("Cann't open the file!\n");
  136. exit(0);
  137. }
  138. // Set the file pointer from the beginning
  139. fseek(fp, 0, SEEK_SET);
  140. fread(&bmpFileHeader, sizeof(BMPFILEHEADER), 1, fp); //sizeof(BMPFILEHEADER) must be 14
  141. fread(&bmpInfoHeader, sizeof(BMPINFOHEADER), 1, fp); //sizeof(BMPFILEHEADER) must be 50
  142. printf("pixel = %d * %d\r\n", bmpInfoHeader.biWidth, bmpInfoHeader.biHeight);
  143. UWORD Image_Width_Byte = (bmpInfoHeader.biWidth % 4 == 0)? (bmpInfoHeader.biWidth / 4): (bmpInfoHeader.biWidth / 4 + 1);
  144. UWORD Bmp_Width_Byte = (bmpInfoHeader.biWidth % 2 == 0)? (bmpInfoHeader.biWidth / 2): (bmpInfoHeader.biWidth / 2 + 1);
  145. UBYTE Image[Image_Width_Byte * bmpInfoHeader.biHeight * 2];
  146. memset(Image, 0xFF, Image_Width_Byte * bmpInfoHeader.biHeight * 2);
  147. // Determine if it is a monochrome bitmap
  148. int readbyte = bmpInfoHeader.biBitCount;
  149. printf("biBitCount = %d\r\n",readbyte);
  150. if(readbyte != 4){
  151. Debug("Bmp image is not a 4-color bitmap!\n");
  152. exit(0);
  153. }
  154. // Read image data into the cache
  155. UWORD x, y;
  156. UBYTE Rdata;
  157. fseek(fp, bmpFileHeader.bOffset, SEEK_SET);
  158. for(y = 0; y < bmpInfoHeader.biHeight; y++) {//Total display column
  159. for(x = 0; x < Bmp_Width_Byte; x++) {//Show a line in the line
  160. if(fread((char *)&Rdata, 1, 1, fp) != 1) {
  161. perror("get bmpdata:\r\n");
  162. break;
  163. }
  164. if(x < Image_Width_Byte*2) { //bmp
  165. Image[x + (bmpInfoHeader.biHeight - y - 1) * Image_Width_Byte*2] = Rdata;
  166. }
  167. }
  168. }
  169. fclose(fp);
  170. // Refresh the image to the display buffer based on the displayed orientation
  171. UBYTE color, temp;
  172. printf("bmpInfoHeader.biWidth = %d\r\n",bmpInfoHeader.biWidth);
  173. printf("bmpInfoHeader.biHeight = %d\r\n",bmpInfoHeader.biHeight);
  174. for(y = 0; y < bmpInfoHeader.biHeight; y++) {
  175. for(x = 0; x < bmpInfoHeader.biWidth; x++) {
  176. if(x > Paint.Width || y > Paint.Height) {
  177. break;
  178. }
  179. temp = Image[x/2 + y * bmpInfoHeader.biWidth/2] >> ((x%2)? 0:4);//0xf 0x8 0x7 0x0
  180. color = temp>>2; //11 10 01 00
  181. Paint_SetPixel(Xstart + x, Ystart + y, color);
  182. }
  183. }
  184. return 0;
  185. }
  186. UBYTE GUI_ReadBmp_RGB_7Color(const char *path, UWORD Xstart, UWORD Ystart)
  187. {
  188. FILE *fp; //Define a file pointer
  189. BMPFILEHEADER bmpFileHeader; //Define a bmp file header structure
  190. BMPINFOHEADER bmpInfoHeader; //Define a bmp info header structure
  191. // Binary file open
  192. if((fp = fopen(path, "rb")) == NULL) {
  193. Debug("Cann't open the file!\n");
  194. exit(0);
  195. }
  196. // Set the file pointer from the beginning
  197. fseek(fp, 0, SEEK_SET);
  198. fread(&bmpFileHeader, sizeof(BMPFILEHEADER), 1, fp); //sizeof(BMPFILEHEADER) must be 14
  199. fread(&bmpInfoHeader, sizeof(BMPINFOHEADER), 1, fp); //sizeof(BMPFILEHEADER) must be 50
  200. printf("pixel = %d * %d\r\n", bmpInfoHeader.biWidth, bmpInfoHeader.biHeight);
  201. UDOUBLE Image_Byte = bmpInfoHeader.biWidth * bmpInfoHeader.biHeight * 3;
  202. UBYTE Image[Image_Byte];
  203. memset(Image, 0xFF, Image_Byte);
  204. // Determine if it is a monochrome bitmap
  205. int readbyte = bmpInfoHeader.biBitCount;
  206. if(readbyte != 24){
  207. Debug("Bmp image is not 24 bitmap!\n");
  208. exit(0);
  209. }
  210. // Read image data into the cache
  211. UWORD x, y;
  212. UBYTE Rdata[3];
  213. fseek(fp, bmpFileHeader.bOffset, SEEK_SET);
  214. for(y = 0; y < bmpInfoHeader.biHeight; y++) {//Total display column
  215. for(x = 0; x < bmpInfoHeader.biWidth ; x++) {//Show a line in the line
  216. if(fread((char *)Rdata, 1, 1, fp) != 1) {
  217. perror("get bmpdata:\r\n");
  218. break;
  219. }
  220. if(fread((char *)Rdata+1, 1, 1, fp) != 1) {
  221. perror("get bmpdata:\r\n");
  222. break;
  223. }
  224. if(fread((char *)Rdata+2, 1, 1, fp) != 1) {
  225. perror("get bmpdata:\r\n");
  226. break;
  227. }
  228. if(Rdata[0] == 0 && Rdata[1] == 0 && Rdata[2] == 0){
  229. Image[x+(y* bmpInfoHeader.biWidth )] = 0;//Black
  230. }else if(Rdata[0] == 255 && Rdata[1] == 255 && Rdata[2] == 255){
  231. Image[x+(y* bmpInfoHeader.biWidth )] = 1;//White
  232. }else if(Rdata[0] == 0 && Rdata[1] == 255 && Rdata[2] == 0){
  233. Image[x+(y* bmpInfoHeader.biWidth )] = 2;//Green
  234. }else if(Rdata[0] == 255 && Rdata[1] == 0 && Rdata[2] == 0){
  235. Image[x+(y* bmpInfoHeader.biWidth )] = 3;//Blue
  236. }else if(Rdata[0] == 0 && Rdata[1] == 0 && Rdata[2] == 255){
  237. Image[x+(y* bmpInfoHeader.biWidth )] = 4;//Red
  238. }else if(Rdata[0] == 0 && Rdata[1] == 255 && Rdata[2] == 255){
  239. Image[x+(y* bmpInfoHeader.biWidth )] = 5;//Yellow
  240. }else if(Rdata[0] == 0 && Rdata[1] == 128 && Rdata[2] == 255){
  241. Image[x+(y* bmpInfoHeader.biWidth )] = 6;//Orange
  242. }
  243. }
  244. }
  245. fclose(fp);
  246. // Refresh the image to the display buffer based on the displayed orientation
  247. for(y = 0; y < bmpInfoHeader.biHeight; y++) {
  248. for(x = 0; x < bmpInfoHeader.biWidth; x++) {
  249. if(x > Paint.Width || y > Paint.Height) {
  250. break;
  251. }
  252. Paint_SetPixel(Xstart + x, Ystart + y, Image[bmpInfoHeader.biHeight * bmpInfoHeader.biWidth - 1 -(bmpInfoHeader.biWidth-x-1+(y* bmpInfoHeader.biWidth))]);
  253. }
  254. }
  255. return 0;
  256. }