//This program reads in a 24-bit bmp image file named test.tns in the directory bmpviewer #include #include "utils.h" #include "screen.h" #include "dirlist.h" #include "string.h" #define CONTRAST (*(volatile unsigned*) 0x900F0020) asm(".string \"PRG\"\n"); int main(void) { //bitmap header char* header = (char*) malloc(0x36); //screen buffer char* scrbuf = (char*) malloc(SCREEN_BYTES_SIZE); clearBuf(scrbuf); //image file FILE* img; //loop indices int i, x, y; //list the files int numfiles; char** filenames = (char**) malloc(1024); numfiles = dirlist("/documents/bmpviewer/", "*.bmp.tns", filenames); y = 0; for (i = 0; i < numfiles; i++) { drawStr(scrbuf, 8, y, filenames[i]); y += 12; } refresh(scrbuf); i = 0; while (!isKeyPressed(KEY_NSPIRE_CLICK)) { if (isKeyPressed(KEY_NSPIRE_UP)) { putChar(scrbuf, 0, 12 * i, 0); i--; if (i < 0) i = 0; } if (isKeyPressed(KEY_NSPIRE_DOWN)) { putChar(scrbuf, 0, 12 * i, 0); i++; if (i >= numfiles) i = numfiles - 1; } if (isKeyPressed(KEY_NSPIRE_ESC)) { return; } drawStr(scrbuf, 0, 12 * i, ">"); //refresh 3x to delay keypresses for (y = 0; y < 3; y++) { refresh(scrbuf); } } char* selected = filenames[i]; char* path = "/documents/bmpviewer/"; char* fullname = (char*) malloc(strlen(path) + strlen(selected)); strcpy(fullname, path); strcpy(fullname + strlen(path), selected); clearBuf(scrbuf); //read the header //img = fopen("/documents/bmpviewer/test.tns", "rb"); img = fopen(fullname, "rb"); fread(header, 1, 0x36, img); //compute some ints (image data size, width, and height) from the header int bmpsize = header[34] + 256 * header[35] + 65536 * header[36] + 65536 * 256 * header[37]; int w = header[18] + 256 * header[19] + 65536 * header[20] + 65536 * 256 * header[21]; int h = header[22] + 256 * header[23] + 65536 * header[24] + 65536 * 256 * header[25]; //number of pixels int pxsize = bmpsize / 3; //allocate byte data and pixel (grayscale) data char* data = (char*) malloc(bmpsize); char* pxdata = (char*) malloc(pxsize); //read in data and close image fread(data, 1, bmpsize, img); fclose(img); //convert RGB to grayscale for (i = 0; i < pxsize; i++) { pxdata[i] = (data[3 * i] + data[3 * i + 1] + data[3 * i + 2]) / 48; } //draw to the buffer and display it for (x = 0; x < w; x++) { for (y = 0; y < h; y++) { setPixelBuf(scrbuf, x, h - 1 - y, pxdata[w * y + x]); } } while (!isKeyPressed(KEY_NSPIRE_ESC)) { if (isKeyPressed(KEY_NSPIRE_PLUS)) { CONTRAST++; if (CONTRAST > 0x95) CONTRAST = 0x95; } if (isKeyPressed(KEY_NSPIRE_MINUS)) { CONTRAST--; if (CONTRAST < 0x6B) CONTRAST = 0x6B; } for (i = 0; i < 3; i++) { refresh(scrbuf); } } free(fullname); free(filenames); free(header); free(scrbuf); free(data); free(pxdata); return 0; }