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); }
Diff: SimpleJpegDecode/SimpleJpegDecode.h
- Revision:
- 0:98f918e1d528
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SimpleJpegDecode/SimpleJpegDecode.h Sat Feb 02 01:25:25 2013 +0000 @@ -0,0 +1,56 @@ +#ifndef SIMPLE_JPEG_DECODE_H +#define SIMPLE_JPEG_DECODE_H + +#include "BaseJpegDecode.h" +#include "inverseDCT.h" + +#define YUV 0 +#define RGB24 1 + +class SimpleJpegDecode : public BaseJpegDecode, public inverseDCT { +public: + SimpleJpegDecode(uint8_t output_mode=RGB24); + + void format_YUV(int mcu, int block, int8_t* values); + void format_RGB24(int mcu, int block, int8_t* values); + + void output(int mcu, int block, int scan, int value); + virtual void outputDC(int mcu, int block, int value); + virtual void outputAC(int mcu, int block, int scan, int value); + virtual void outputMARK(uint8_t c); + virtual void outputBLOCK(int muc, int block, int8_t* values); // iDCT + + int8_t m_block_data[5][64]; + int DC_count; + int AC_count; + int BLOCK_count; + + ///Setups the result callback + /** + @param pMethod : callback function + */ + void setOnResult( void (*pMethod)(int, int, uint8_t*) ); + + ///Setups the result callback + /** + @param pItem : instance of class on which to execute the callback method + @param pMethod : callback method + */ + class CDummy; + template<class T> + void setOnResult( T* pItem, void (T::*pMethod)(int, int, uint8_t*) ) + { + m_pCb = NULL; + m_pCbItem = (CDummy*) pItem; + m_pCbMeth = (void (CDummy::*)(int, int, uint8_t*)) pMethod; + } + void clearOnResult(); +protected: + void onResult(int x, int y, uint8_t* yuv); + CDummy* m_pCbItem; + void (CDummy::*m_pCbMeth)(int, int, uint8_t*); + void (*m_pCb)(int, int, uint8_t*); + uint8_t m_output_mode; +}; + +#endif // SIMPLE_JPEG_DECODE_H \ No newline at end of file