convert JPEG stream data to bitmap, BaseJpegDecode example program

Dependencies:   BaseJpegDecode BaseUsbHost FATFileSystem mbed-rtos mbed

JPEGデコードのサンプルプログラムです。
JPEGのMCU単位で逐次デコード出力していますので少ないRAMメモリで動かすことが出来ます。

#include "USBHostMSD.h"
#include "SimpleJpegDecode.h"
#include "bmp24.h"

const char* INPUT_FILE  = "/usb/input.jpg";
const char* OUTPUT_FILE = "/usb/output.bmp";

bmp24 bmp;
RawSerial pc(USBTX, USBRX);

void callbackRGB(int x, int y, uint8_t* rgb) {
    bmp.point(x, y, rgb);
    pc.printf("x=%d, y=%d, RGB=(0x%02x,0x%02x,0x%02x)\n", x, y, rgb[0], rgb[1], rgb[2]);
}

int main() {
    pc.baud(115200);
    
    USBHostMSD* msd = new USBHostMSD("usb");
    if (!msd->connect()) {
        error("USB Flash drive not found.\n");
    } 
    SimpleJpegDecode* decode = new SimpleJpegDecode(RGB24);

    decode->setOnResult(callbackRGB);
    decode->clear();
    pc.printf("input: %s\n", INPUT_FILE);
    FILE* fp = fopen(INPUT_FILE, "rb");
    if (fp == NULL) {
         error("open error\n");
    }
    while(1) {
        int c = fgetc(fp);
        if (c == EOF) {
            break;
        }
        decode->input(c);
    }
    fclose(fp);
    pc.printf("output: %s\n", OUTPUT_FILE);
    if (!bmp.writeFile(OUTPUT_FILE)) {
        error("write error\n");
    }
    exit(1);     
}
Revision:
0:98f918e1d528
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/bmp24.h	Sat Feb 02 01:25:25 2013 +0000
@@ -0,0 +1,66 @@
+#ifndef BMP24_H
+#define BMP24_H
+
+#define BMP24_WIDTH  (16*4)
+#define BMP24_HEIGHT (16*3)
+
+class bmp24 {
+    uint8_t m_bitmap[BMP24_WIDTH*BMP24_HEIGHT*3];
+public:
+    int width;
+    int height;
+
+    bmp24() {
+        width = BMP24_WIDTH;
+        height = BMP24_HEIGHT;
+    }
+    
+    void clear() {
+        memset(m_bitmap, 0, sizeof(m_bitmap));
+    }
+    
+    void point(int x, int y, uint8_t* rgb) {
+        if (x >= 0 && x < width && y >= 0 && y < height) {
+            int pos = y*width*3+x*3;
+            m_bitmap[pos++] = rgb[0];
+            m_bitmap[pos++] = rgb[1];
+            m_bitmap[pos]   = rgb[2];
+        }
+    }
+    
+    void LE32write(uint8_t* buf, int value) {
+        *buf++ = value & 0xff;
+        *buf++ = (value>>8) & 0xff;
+        *buf++ = (value>>16) & 0xff;
+        *buf   = (value>>24) & 0xff;
+    }
+    
+    bool writeFile(const char *path) {
+        FILE *fp = fopen(path, "wb");
+        if (fp == NULL) {
+            return false;
+        }
+        uint8_t header[] = {
+0x42,0x4d,0x36,0xe1,0x00,0x00,0x00,0x00,0x00,0x00,0x36,0x00,0x00,0x00,0x28,0x00,
+0x00,0x00,0xa0,0x00,0x00,0x00,0x78,0x00,0x00,0x00,0x01,0x00,0x18,0x00,0x00,0x00,
+0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+0x00,0x00,0x00,0x00,0x00,0x00};
+        int file_size = sizeof(header) + sizeof(m_bitmap);
+        LE32write(header+2, file_size);
+        LE32write(header+18, width);
+        LE32write(header+22, height);
+        
+        fwrite(header, 1, sizeof(header), fp);
+        for(int y = height-1; y >=0; y--) {
+            for(int x = 0; x < width; x++) {
+                fputc(m_bitmap[y*width*3+x*3+2], fp);
+                fputc(m_bitmap[y*width*3+x*3+1], fp);
+                fputc(m_bitmap[y*width*3+x*3+0], fp);
+            }
+        }    
+        fclose(fp);
+        return true;
+    }
+};
+
+#endif // BMP24_H