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); }
SimpleJpegDecode/SimpleJpegDecode.h@0:98f918e1d528, 2013-02-02 (annotated)
- Committer:
- va009039
- Date:
- Sat Feb 02 01:25:25 2013 +0000
- Revision:
- 0:98f918e1d528
first commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
va009039 | 0:98f918e1d528 | 1 | #ifndef SIMPLE_JPEG_DECODE_H |
va009039 | 0:98f918e1d528 | 2 | #define SIMPLE_JPEG_DECODE_H |
va009039 | 0:98f918e1d528 | 3 | |
va009039 | 0:98f918e1d528 | 4 | #include "BaseJpegDecode.h" |
va009039 | 0:98f918e1d528 | 5 | #include "inverseDCT.h" |
va009039 | 0:98f918e1d528 | 6 | |
va009039 | 0:98f918e1d528 | 7 | #define YUV 0 |
va009039 | 0:98f918e1d528 | 8 | #define RGB24 1 |
va009039 | 0:98f918e1d528 | 9 | |
va009039 | 0:98f918e1d528 | 10 | class SimpleJpegDecode : public BaseJpegDecode, public inverseDCT { |
va009039 | 0:98f918e1d528 | 11 | public: |
va009039 | 0:98f918e1d528 | 12 | SimpleJpegDecode(uint8_t output_mode=RGB24); |
va009039 | 0:98f918e1d528 | 13 | |
va009039 | 0:98f918e1d528 | 14 | void format_YUV(int mcu, int block, int8_t* values); |
va009039 | 0:98f918e1d528 | 15 | void format_RGB24(int mcu, int block, int8_t* values); |
va009039 | 0:98f918e1d528 | 16 | |
va009039 | 0:98f918e1d528 | 17 | void output(int mcu, int block, int scan, int value); |
va009039 | 0:98f918e1d528 | 18 | virtual void outputDC(int mcu, int block, int value); |
va009039 | 0:98f918e1d528 | 19 | virtual void outputAC(int mcu, int block, int scan, int value); |
va009039 | 0:98f918e1d528 | 20 | virtual void outputMARK(uint8_t c); |
va009039 | 0:98f918e1d528 | 21 | virtual void outputBLOCK(int muc, int block, int8_t* values); // iDCT |
va009039 | 0:98f918e1d528 | 22 | |
va009039 | 0:98f918e1d528 | 23 | int8_t m_block_data[5][64]; |
va009039 | 0:98f918e1d528 | 24 | int DC_count; |
va009039 | 0:98f918e1d528 | 25 | int AC_count; |
va009039 | 0:98f918e1d528 | 26 | int BLOCK_count; |
va009039 | 0:98f918e1d528 | 27 | |
va009039 | 0:98f918e1d528 | 28 | ///Setups the result callback |
va009039 | 0:98f918e1d528 | 29 | /** |
va009039 | 0:98f918e1d528 | 30 | @param pMethod : callback function |
va009039 | 0:98f918e1d528 | 31 | */ |
va009039 | 0:98f918e1d528 | 32 | void setOnResult( void (*pMethod)(int, int, uint8_t*) ); |
va009039 | 0:98f918e1d528 | 33 | |
va009039 | 0:98f918e1d528 | 34 | ///Setups the result callback |
va009039 | 0:98f918e1d528 | 35 | /** |
va009039 | 0:98f918e1d528 | 36 | @param pItem : instance of class on which to execute the callback method |
va009039 | 0:98f918e1d528 | 37 | @param pMethod : callback method |
va009039 | 0:98f918e1d528 | 38 | */ |
va009039 | 0:98f918e1d528 | 39 | class CDummy; |
va009039 | 0:98f918e1d528 | 40 | template<class T> |
va009039 | 0:98f918e1d528 | 41 | void setOnResult( T* pItem, void (T::*pMethod)(int, int, uint8_t*) ) |
va009039 | 0:98f918e1d528 | 42 | { |
va009039 | 0:98f918e1d528 | 43 | m_pCb = NULL; |
va009039 | 0:98f918e1d528 | 44 | m_pCbItem = (CDummy*) pItem; |
va009039 | 0:98f918e1d528 | 45 | m_pCbMeth = (void (CDummy::*)(int, int, uint8_t*)) pMethod; |
va009039 | 0:98f918e1d528 | 46 | } |
va009039 | 0:98f918e1d528 | 47 | void clearOnResult(); |
va009039 | 0:98f918e1d528 | 48 | protected: |
va009039 | 0:98f918e1d528 | 49 | void onResult(int x, int y, uint8_t* yuv); |
va009039 | 0:98f918e1d528 | 50 | CDummy* m_pCbItem; |
va009039 | 0:98f918e1d528 | 51 | void (CDummy::*m_pCbMeth)(int, int, uint8_t*); |
va009039 | 0:98f918e1d528 | 52 | void (*m_pCb)(int, int, uint8_t*); |
va009039 | 0:98f918e1d528 | 53 | uint8_t m_output_mode; |
va009039 | 0:98f918e1d528 | 54 | }; |
va009039 | 0:98f918e1d528 | 55 | |
va009039 | 0:98f918e1d528 | 56 | #endif // SIMPLE_JPEG_DECODE_H |