Просмотр исходного кода

Add GUI_ReadBmp_16Gray function

mbartlett21 3 лет назад
Родитель
Сommit
dbcee5e5a2

+ 80 - 0
RaspberryPi_JetsonNano/c/lib/GUI/GUI_BMPfile.c

@@ -205,6 +205,86 @@ UBYTE GUI_ReadBmp_4Gray(const char *path, UWORD Xstart, UWORD Ystart)
     return 0;
 }
 
+UBYTE GUI_ReadBmp_16Gray(const char *path, UWORD Xstart, UWORD Ystart)
+{
+    FILE *fp;                     //Define a file pointer
+    BMPFILEHEADER bmpFileHeader;  //Define a bmp file header structure
+    BMPINFOHEADER bmpInfoHeader;  //Define a bmp info header structure
+
+    // Binary file open
+    if((fp = fopen(path, "rb")) == NULL) {
+        Debug("Cann't open the file!\n");
+        exit(0);
+    }
+
+    // Set the file pointer from the beginning
+    fseek(fp, 0, SEEK_SET);
+    fread(&bmpFileHeader, sizeof(BMPFILEHEADER), 1, fp);    //sizeof(BMPFILEHEADER) must be 14
+    fread(&bmpInfoHeader, sizeof(BMPINFOHEADER), 1, fp);    //sizeof(BMPFILEHEADER) must be 50
+    printf("pixel = %d * %d\r\n", bmpInfoHeader.biWidth, bmpInfoHeader.biHeight);
+
+    // They are both the same width in bytes
+    // round up to the next byte
+    UWORD Width_Byte = (bmpInfoHeader.biWidth + 1) / 2;
+    UBYTE Image[Width_Byte * bmpInfoHeader.biHeight];
+    memset(Image, 0xFF, Width_Byte * bmpInfoHeader.biHeight);
+
+    // Determine if it is a monochrome bitmap
+    int readbyte = bmpInfoHeader.biBitCount;
+    printf("biBitCount = %d\r\n",readbyte);
+    if(readbyte != 4) {
+        Debug("Bmp image is not a 4-bit bitmap!\n");
+        exit(0);
+    }
+
+    // Determine colors based on the palette
+
+    // A map from palette entry to color
+    UBYTE colors[16];
+    UBYTE i;
+    BMPRGBQUAD rgbData;
+
+    for (i = 0; i < 16; i++){
+        fread(&rgbData, sizeof(BMPRGBQUAD), 1, fp);
+
+        // Work out the closest colour
+        // 16 colours over 0-255 => 0-8 => 0, 9-25 => 1 (17), 26-42 => 2 (34), etc
+
+        // Base it on red
+        colors[i] = (rgbData.rgbRed + 8) / 17;
+    }
+
+    // Read image data into the cache
+    UWORD x, y;
+    UBYTE Rdata;
+    fseek(fp, bmpFileHeader.bOffset, SEEK_SET);
+
+    for (y = 0; y < bmpInfoHeader.biHeight; y++) {//Total display column
+        for (x = 0; x < Width_Byte; x++) {//Show a line in the line
+            if (fread((char *) &Rdata, 1, 1, fp) != 1) {
+                perror("get bmpdata:\r\n");
+                break;
+            }
+            Image[x + (bmpInfoHeader.biHeight - y - 1) * Width_Byte] = Rdata;
+        }
+    }
+    fclose(fp);
+
+    // Refresh the image to the display buffer based on the displayed orientation
+    UBYTE coloridx;
+    printf("bmpInfoHeader.biWidth = %d\r\n", bmpInfoHeader.biWidth);
+    printf("bmpInfoHeader.biHeight = %d\r\n", bmpInfoHeader.biHeight);
+    for (y = 0; y < bmpInfoHeader.biHeight; y++) {
+        for (x = 0; x < bmpInfoHeader.biWidth; x++) {
+            if (Xstart + x > Paint.Width || Ystart + y > Paint.Height)
+                break;
+
+            coloridx = (Image[x / 2 + y * Width_Byte] >> ((x % 2) ? 0 : 4)) & 15;
+            Paint_SetPixel(Xstart + x, Ystart + y, colors[coloridx]);
+        }
+    }
+    return 0;
+}
 
 UBYTE GUI_ReadBmp_RGB_7Color(const char *path, UWORD Xstart, UWORD Ystart)
 {

+ 1 - 0
RaspberryPi_JetsonNano/c/lib/GUI/GUI_BMPfile.h

@@ -87,6 +87,7 @@ typedef struct RGB_QUAD {
 
 UBYTE GUI_ReadBmp(const char *path, UWORD Xstart, UWORD Ystart);
 UBYTE GUI_ReadBmp_4Gray(const char *path, UWORD Xstart, UWORD Ystart);
+UBYTE GUI_ReadBmp_16Gray(const char *path, UWORD Xstart, UWORD Ystart);
 UBYTE GUI_ReadBmp_RGB_4Color(const char *path, UWORD Xstart, UWORD Ystart);
 UBYTE GUI_ReadBmp_RGB_7Color(const char *path, UWORD Xstart, UWORD Ystart);
 #endif