GUI_BMPfile.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*****************************************************************************
  2. * | File : GUI_BMPfile.c
  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.0
  10. * | Date : 2018-11-12
  11. * | Info :
  12. * 1.Change file name: GUI_BMP.c -> GUI_BMPfile.c
  13. * 2.fix: GUI_ReadBmp()
  14. * Now Xstart and Xstart can control the position of the picture normally,
  15. * and support the display of images of any size. If it is larger than
  16. * the actual display range, it will not be displayed.
  17. #
  18. # Permission is hereby granted, free of charge, to any person obtaining a copy
  19. # of this software and associated documnetation files (the "Software"), to deal
  20. # in the Software without restriction, including without limitation the rights
  21. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  22. # copies of the Software, and to permit persons to whom the Software is
  23. # furished to do so, subject to the following conditions:
  24. #
  25. # The above copyright notice and this permission notice shall be included in
  26. # all copies or substantial portions of the Software.
  27. #
  28. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  29. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  30. # FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  31. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  32. # LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  33. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  34. # THE SOFTWARE.
  35. #
  36. ******************************************************************************/
  37. #include "GUI_BMPfile.h"
  38. #include "GUI_Paint.h"
  39. #include "Debug.h"
  40. #include <fcntl.h>
  41. #include <unistd.h>
  42. #include <stdint.h>
  43. #include <stdlib.h> //exit()
  44. #include <string.h> //memset()
  45. #include <math.h> //memset()
  46. UBYTE GUI_ReadBmp(const char *path, UWORD Xstart, UWORD Ystart)
  47. {
  48. FILE *fp; //Define a file pointer
  49. BMPFILEHEADER bmpFileHeader; //Define a bmp file header structure
  50. BMPINFOHEADER bmpInfoHeader; //Define a bmp info header structure
  51. // Binary file open
  52. if((fp = fopen(path, "rb")) == NULL) {
  53. Debug("Cann't open the file!\n");
  54. exit(0);
  55. }
  56. // Set the file pointer from the beginning
  57. fseek(fp, 0, SEEK_SET);
  58. fread(&bmpFileHeader, sizeof(BMPFILEHEADER), 1, fp); //sizeof(BMPFILEHEADER) must be 14
  59. fread(&bmpInfoHeader, sizeof(BMPINFOHEADER), 1, fp); //sizeof(BMPFILEHEADER) must be 50
  60. printf("pixel = %d * %d\r\n", bmpInfoHeader.biWidth, bmpInfoHeader.biHeight);
  61. UWORD Image_Width_Byte = (bmpInfoHeader.biWidth % 8 == 0)? (bmpInfoHeader.biWidth / 8): (bmpInfoHeader.biWidth / 8 + 1);
  62. UWORD Bmp_Width_Byte = (Image_Width_Byte % 4 == 0) ? Image_Width_Byte: ((Image_Width_Byte / 4 + 1) * 4);
  63. UBYTE Image[Image_Width_Byte * bmpInfoHeader.biHeight];
  64. memset(Image, 0xFF, Image_Width_Byte * bmpInfoHeader.biHeight);
  65. // Determine if it is a monochrome bitmap
  66. int readbyte = bmpInfoHeader.biBitCount;
  67. if(readbyte != 1) {
  68. Debug("the bmp Image is not a monochrome bitmap!\n");
  69. exit(0);
  70. }
  71. // Determine black and white based on the palette
  72. UWORD i;
  73. UWORD Bcolor, Wcolor;
  74. // UWORD bmprgbquadsize = pow(2, bmpInfoHeader.biBitCount);// 2^1 = 2
  75. // BMPRGBQUAD bmprgbquad[bmprgbquadsize]; //palette
  76. BMPRGBQUAD bmprgbquad[2]; //palette
  77. // for(i = 0; i < bmprgbquadsize; i++){
  78. for(i = 0; i < 2; i++) {
  79. fread(&bmprgbquad[i * 4], sizeof(BMPRGBQUAD), 1, fp);
  80. }
  81. if(bmprgbquad[0].rgbBlue == 0xff && bmprgbquad[0].rgbGreen == 0xff && bmprgbquad[0].rgbRed == 0xff) {
  82. Bcolor = BLACK;
  83. Wcolor = WHITE;
  84. } else {
  85. Bcolor = WHITE;
  86. Wcolor = BLACK;
  87. }
  88. // Read image data into the cache
  89. UWORD x, y;
  90. UBYTE Rdata;
  91. fseek(fp, bmpFileHeader.bOffset, SEEK_SET);
  92. for(y = 0; y < bmpInfoHeader.biHeight; y++) {//Total display column
  93. for(x = 0; x < Bmp_Width_Byte; x++) {//Show a line in the line
  94. if(fread((char *)&Rdata, 1, readbyte, fp) != readbyte) {
  95. perror("get bmpdata:\r\n");
  96. break;
  97. }
  98. if(x < Image_Width_Byte) { //bmp
  99. Image[x + (bmpInfoHeader.biHeight - y - 1) * Image_Width_Byte] = Rdata;
  100. // printf("rdata = %d\r\n", Rdata);
  101. }
  102. }
  103. }
  104. fclose(fp);
  105. // Refresh the image to the display buffer based on the displayed orientation
  106. UBYTE color, temp;
  107. for(y = 0; y < bmpInfoHeader.biHeight; y++) {
  108. for(x = 0; x < bmpInfoHeader.biWidth; x++) {
  109. if(x > Paint.Width || y > Paint.Height) {
  110. break;
  111. }
  112. temp = Image[(x / 8) + (y * Image_Width_Byte)];
  113. color = (((temp << (x%8)) & 0x80) == 0x80) ?Bcolor:Wcolor;
  114. Paint_SetPixel(Xstart + x, Ystart + y, color);
  115. }
  116. }
  117. return 0;
  118. }