Very simple library for controlling small graphic LCDs
bitmap.cpp@0:573607103077, 2011-03-03 (annotated)
- Committer:
- HBP
- Date:
- Thu Mar 03 00:28:24 2011 +0000
- Revision:
- 0:573607103077
- Child:
- 2:183b861fba00
Initial release
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
HBP | 0:573607103077 | 1 | #include "bitmap.h" |
HBP | 0:573607103077 | 2 | |
HBP | 0:573607103077 | 3 | #define X_MAX 160 |
HBP | 0:573607103077 | 4 | #define Y_MAX 32 |
HBP | 0:573607103077 | 5 | |
HBP | 0:573607103077 | 6 | extern Serial pc(USBTX, USBRX); |
HBP | 0:573607103077 | 7 | |
HBP | 0:573607103077 | 8 | bitmapImage::bitmapImage(char *inputFilename) |
HBP | 0:573607103077 | 9 | { |
HBP | 0:573607103077 | 10 | filename = inputFilename; |
HBP | 0:573607103077 | 11 | |
HBP | 0:573607103077 | 12 | pc.printf("Opening file %s...", filename); |
HBP | 0:573607103077 | 13 | fp = fopen(filename, "rb"); |
HBP | 0:573607103077 | 14 | if(fp != NULL) |
HBP | 0:573607103077 | 15 | pc.printf(" OK\n\r"); |
HBP | 0:573607103077 | 16 | else { |
HBP | 0:573607103077 | 17 | fclose(fp); |
HBP | 0:573607103077 | 18 | error(""); |
HBP | 0:573607103077 | 19 | } |
HBP | 0:573607103077 | 20 | |
HBP | 0:573607103077 | 21 | // check to see if it is a valid bitmap file |
HBP | 0:573607103077 | 22 | if(fgetc(fp)!='B' || fgetc(fp)!='M') |
HBP | 0:573607103077 | 23 | { |
HBP | 0:573607103077 | 24 | fclose(fp); |
HBP | 0:573607103077 | 25 | error(""); |
HBP | 0:573607103077 | 26 | } |
HBP | 0:573607103077 | 27 | |
HBP | 0:573607103077 | 28 | //fseek(fp, 0L, SEEK_END); |
HBP | 0:573607103077 | 29 | |
HBP | 0:573607103077 | 30 | // Get BMP info |
HBP | 0:573607103077 | 31 | fileInfo.cols = readImageInfo(0x12, 4); |
HBP | 0:573607103077 | 32 | fileInfo.rows = readImageInfo(0x16, 4); |
HBP | 0:573607103077 | 33 | fileSize = readImageInfo(2, 4); |
HBP | 0:573607103077 | 34 | fileInfo.nColors = readImageInfo(0x2E, 4); |
HBP | 0:573607103077 | 35 | // Assume we are working with a 8-bit file |
HBP | 0:573607103077 | 36 | if (fileInfo.nColors == 0) fileInfo.nColors = 256; |
HBP | 0:573607103077 | 37 | fileInfo.offset = readImageInfo(0x0A, 4); |
HBP | 0:573607103077 | 38 | fileInfo.bitsPP = (uint16_t)readImageInfo(0x1C, 2); |
HBP | 0:573607103077 | 39 | |
HBP | 0:573607103077 | 40 | // Print info |
HBP | 0:573607103077 | 41 | pc.printf("\n\rBMP file %s:\n\r", filename); |
HBP | 0:573607103077 | 42 | pc.printf("Width: %d\n\r", fileInfo.cols); |
HBP | 0:573607103077 | 43 | pc.printf("Height: %d\n\r", fileInfo.rows); |
HBP | 0:573607103077 | 44 | pc.printf("File size: %ld\n\r", fileSize); |
HBP | 0:573607103077 | 45 | pc.printf("# Colors: %d\n\r", fileInfo.nColors); |
HBP | 0:573607103077 | 46 | pc.printf("Offset: %d\n\r", fileInfo.offset); |
HBP | 0:573607103077 | 47 | pc.printf("Bits per pixel: %d\n\r", fileInfo.bitsPP); |
HBP | 0:573607103077 | 48 | //pc.printf("Vector size: %d\n\r", vectorSize); |
HBP | 0:573607103077 | 49 | } |
HBP | 0:573607103077 | 50 | |
HBP | 0:573607103077 | 51 | bitmapImage::~bitmapImage() |
HBP | 0:573607103077 | 52 | { |
HBP | 0:573607103077 | 53 | fclose(fp); |
HBP | 0:573607103077 | 54 | } |
HBP | 0:573607103077 | 55 | |
HBP | 0:573607103077 | 56 | void bitmapImage::drawImage(gfxLcd *glcd, int xOffset, int yOffset) |
HBP | 0:573607103077 | 57 | { |
HBP | 0:573607103077 | 58 | // Start at beginning of raster data |
HBP | 0:573607103077 | 59 | fseek(fp, fileInfo.offset, SEEK_SET); |
HBP | 0:573607103077 | 60 | int32_t offsettedX, offsettedY; |
HBP | 0:573607103077 | 61 | |
HBP | 0:573607103077 | 62 | uint32_t x=0, y=fileInfo.rows-1; |
HBP | 0:573607103077 | 63 | for(uint32_t index=0; index < fileInfo.rows*(fileInfo.cols/8); index++) |
HBP | 0:573607103077 | 64 | { |
HBP | 0:573607103077 | 65 | char c=fgetc(fp); |
HBP | 0:573607103077 | 66 | for(char b=0; b < 8; b++) |
HBP | 0:573607103077 | 67 | { |
HBP | 0:573607103077 | 68 | char mask = 0x80 >> b; |
HBP | 0:573607103077 | 69 | offsettedX = (int32_t)x+xOffset; |
HBP | 0:573607103077 | 70 | offsettedY = (int32_t)y+yOffset; |
HBP | 0:573607103077 | 71 | |
HBP | 0:573607103077 | 72 | if((offsettedX <= X_MAX) && (offsettedY <= Y_MAX) && (offsettedX >= 0) && (offsettedY >= 0)) |
HBP | 0:573607103077 | 73 | { |
HBP | 0:573607103077 | 74 | glcd->putPixel(offsettedX, offsettedY, ~c&mask); |
HBP | 0:573607103077 | 75 | } |
HBP | 0:573607103077 | 76 | x++; |
HBP | 0:573607103077 | 77 | |
HBP | 0:573607103077 | 78 | if (x == fileInfo.cols) |
HBP | 0:573607103077 | 79 | { |
HBP | 0:573607103077 | 80 | x=0; |
HBP | 0:573607103077 | 81 | y--; |
HBP | 0:573607103077 | 82 | } |
HBP | 0:573607103077 | 83 | if (y == 0) |
HBP | 0:573607103077 | 84 | return; |
HBP | 0:573607103077 | 85 | } |
HBP | 0:573607103077 | 86 | } |
HBP | 0:573607103077 | 87 | } |
HBP | 0:573607103077 | 88 | |
HBP | 0:573607103077 | 89 | uint32_t bitmapImage::readImageInfo(long offset, int n) |
HBP | 0:573607103077 | 90 | { |
HBP | 0:573607103077 | 91 | long value = 0L; |
HBP | 0:573607103077 | 92 | char c; |
HBP | 0:573607103077 | 93 | int i; |
HBP | 0:573607103077 | 94 | |
HBP | 0:573607103077 | 95 | fseek(fp, offset, SEEK_SET); |
HBP | 0:573607103077 | 96 | |
HBP | 0:573607103077 | 97 | for(i=0; i < n; i++) |
HBP | 0:573607103077 | 98 | { |
HBP | 0:573607103077 | 99 | fread(&c, sizeof(char), 1, fp); |
HBP | 0:573607103077 | 100 | /* calculate value based on adding bytes */ |
HBP | 0:573607103077 | 101 | value = (long)(value + (c)*((long)pow((long double)256, (int)(i)))); |
HBP | 0:573607103077 | 102 | } |
HBP | 0:573607103077 | 103 | |
HBP | 0:573607103077 | 104 | return value; |
HBP | 0:573607103077 | 105 | } |