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);     
}
Committer:
va009039
Date:
Sat Feb 02 01:25:25 2013 +0000
Revision:
0:98f918e1d528
first commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
va009039 0:98f918e1d528 1 // SimpleJpegDecode_example/main.cpp 2013/2/2
va009039 0:98f918e1d528 2 // convert jpeg to bmp
va009039 0:98f918e1d528 3 //
va009039 0:98f918e1d528 4 #include "mbed.h"
va009039 0:98f918e1d528 5 #include "rtos.h"
va009039 0:98f918e1d528 6 #include "BaseUsbHost.h"
va009039 0:98f918e1d528 7 #include "SimpleJpegDecode.h"
va009039 0:98f918e1d528 8 #include "UsbFlashDrive.h"
va009039 0:98f918e1d528 9 #include "bmp24.h"
va009039 0:98f918e1d528 10
va009039 0:98f918e1d528 11 #define INPUT_FILE "/usb/input.jpg"
va009039 0:98f918e1d528 12 #define OUTPUT_FILE "/usb/output.bmp"
va009039 0:98f918e1d528 13
va009039 0:98f918e1d528 14 Serial pc(USBTX, USBRX);
va009039 0:98f918e1d528 15
va009039 0:98f918e1d528 16 SimpleJpegDecode decode;
va009039 0:98f918e1d528 17 bmp24 bmp;
va009039 0:98f918e1d528 18
va009039 0:98f918e1d528 19 void callbackRGB(int x, int y, uint8_t* rgb)
va009039 0:98f918e1d528 20 {
va009039 0:98f918e1d528 21 bmp.point(x, y, rgb);
va009039 0:98f918e1d528 22 }
va009039 0:98f918e1d528 23
va009039 0:98f918e1d528 24 int main() {
va009039 0:98f918e1d528 25 pc.baud(921600);
va009039 0:98f918e1d528 26 printf("%s\n", __FILE__);
va009039 0:98f918e1d528 27
va009039 0:98f918e1d528 28 BaseUsbHost* usbHost = new BaseUsbHost();
va009039 0:98f918e1d528 29 ControlEp* ctlEp = new ControlEp; // root hub
va009039 0:98f918e1d528 30 if (UsbHub::check(ctlEp)) {
va009039 0:98f918e1d528 31 UsbHub* hub = new UsbHub(ctlEp);
va009039 0:98f918e1d528 32 ctlEp = hub->search<UsbFlashDrive>();
va009039 0:98f918e1d528 33 }
va009039 0:98f918e1d528 34 if (!UsbFlashDrive::check(ctlEp)) {
va009039 0:98f918e1d528 35 error("USB flash drive is not connected.\n");
va009039 0:98f918e1d528 36 }
va009039 0:98f918e1d528 37 UsbFlashDrive* drive = new UsbFlashDrive("usb", ctlEp);
va009039 0:98f918e1d528 38
va009039 0:98f918e1d528 39 bmp.clear();
va009039 0:98f918e1d528 40 decode.setOnResult(callbackRGB);
va009039 0:98f918e1d528 41 decode.clear();
va009039 0:98f918e1d528 42 printf("input: %s\n", INPUT_FILE);
va009039 0:98f918e1d528 43 FILE* fp = fopen(INPUT_FILE, "rb");
va009039 0:98f918e1d528 44 if (fp == NULL) {
va009039 0:98f918e1d528 45 error("open error\n");
va009039 0:98f918e1d528 46 }
va009039 0:98f918e1d528 47 while(!feof(fp)) {
va009039 0:98f918e1d528 48 int c = fgetc(fp);
va009039 0:98f918e1d528 49 decode.input(c);
va009039 0:98f918e1d528 50 }
va009039 0:98f918e1d528 51 fclose(fp);
va009039 0:98f918e1d528 52 printf("output: %s\n", OUTPUT_FILE);
va009039 0:98f918e1d528 53 if (!bmp.writeFile(OUTPUT_FILE)) {
va009039 0:98f918e1d528 54 error("write error\n");
va009039 0:98f918e1d528 55 }
va009039 0:98f918e1d528 56 exit(1);
va009039 0:98f918e1d528 57 }