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); }
bmp24.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 BMP24_H |
va009039 | 0:98f918e1d528 | 2 | #define BMP24_H |
va009039 | 0:98f918e1d528 | 3 | |
va009039 | 0:98f918e1d528 | 4 | #define BMP24_WIDTH (16*4) |
va009039 | 0:98f918e1d528 | 5 | #define BMP24_HEIGHT (16*3) |
va009039 | 0:98f918e1d528 | 6 | |
va009039 | 0:98f918e1d528 | 7 | class bmp24 { |
va009039 | 0:98f918e1d528 | 8 | uint8_t m_bitmap[BMP24_WIDTH*BMP24_HEIGHT*3]; |
va009039 | 0:98f918e1d528 | 9 | public: |
va009039 | 0:98f918e1d528 | 10 | int width; |
va009039 | 0:98f918e1d528 | 11 | int height; |
va009039 | 0:98f918e1d528 | 12 | |
va009039 | 0:98f918e1d528 | 13 | bmp24() { |
va009039 | 0:98f918e1d528 | 14 | width = BMP24_WIDTH; |
va009039 | 0:98f918e1d528 | 15 | height = BMP24_HEIGHT; |
va009039 | 0:98f918e1d528 | 16 | } |
va009039 | 0:98f918e1d528 | 17 | |
va009039 | 0:98f918e1d528 | 18 | void clear() { |
va009039 | 0:98f918e1d528 | 19 | memset(m_bitmap, 0, sizeof(m_bitmap)); |
va009039 | 0:98f918e1d528 | 20 | } |
va009039 | 0:98f918e1d528 | 21 | |
va009039 | 0:98f918e1d528 | 22 | void point(int x, int y, uint8_t* rgb) { |
va009039 | 0:98f918e1d528 | 23 | if (x >= 0 && x < width && y >= 0 && y < height) { |
va009039 | 0:98f918e1d528 | 24 | int pos = y*width*3+x*3; |
va009039 | 0:98f918e1d528 | 25 | m_bitmap[pos++] = rgb[0]; |
va009039 | 0:98f918e1d528 | 26 | m_bitmap[pos++] = rgb[1]; |
va009039 | 0:98f918e1d528 | 27 | m_bitmap[pos] = rgb[2]; |
va009039 | 0:98f918e1d528 | 28 | } |
va009039 | 0:98f918e1d528 | 29 | } |
va009039 | 0:98f918e1d528 | 30 | |
va009039 | 0:98f918e1d528 | 31 | void LE32write(uint8_t* buf, int value) { |
va009039 | 0:98f918e1d528 | 32 | *buf++ = value & 0xff; |
va009039 | 0:98f918e1d528 | 33 | *buf++ = (value>>8) & 0xff; |
va009039 | 0:98f918e1d528 | 34 | *buf++ = (value>>16) & 0xff; |
va009039 | 0:98f918e1d528 | 35 | *buf = (value>>24) & 0xff; |
va009039 | 0:98f918e1d528 | 36 | } |
va009039 | 0:98f918e1d528 | 37 | |
va009039 | 0:98f918e1d528 | 38 | bool writeFile(const char *path) { |
va009039 | 0:98f918e1d528 | 39 | FILE *fp = fopen(path, "wb"); |
va009039 | 0:98f918e1d528 | 40 | if (fp == NULL) { |
va009039 | 0:98f918e1d528 | 41 | return false; |
va009039 | 0:98f918e1d528 | 42 | } |
va009039 | 0:98f918e1d528 | 43 | uint8_t header[] = { |
va009039 | 0:98f918e1d528 | 44 | 0x42,0x4d,0x36,0xe1,0x00,0x00,0x00,0x00,0x00,0x00,0x36,0x00,0x00,0x00,0x28,0x00, |
va009039 | 0:98f918e1d528 | 45 | 0x00,0x00,0xa0,0x00,0x00,0x00,0x78,0x00,0x00,0x00,0x01,0x00,0x18,0x00,0x00,0x00, |
va009039 | 0:98f918e1d528 | 46 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, |
va009039 | 0:98f918e1d528 | 47 | 0x00,0x00,0x00,0x00,0x00,0x00}; |
va009039 | 0:98f918e1d528 | 48 | int file_size = sizeof(header) + sizeof(m_bitmap); |
va009039 | 0:98f918e1d528 | 49 | LE32write(header+2, file_size); |
va009039 | 0:98f918e1d528 | 50 | LE32write(header+18, width); |
va009039 | 0:98f918e1d528 | 51 | LE32write(header+22, height); |
va009039 | 0:98f918e1d528 | 52 | |
va009039 | 0:98f918e1d528 | 53 | fwrite(header, 1, sizeof(header), fp); |
va009039 | 0:98f918e1d528 | 54 | for(int y = height-1; y >=0; y--) { |
va009039 | 0:98f918e1d528 | 55 | for(int x = 0; x < width; x++) { |
va009039 | 0:98f918e1d528 | 56 | fputc(m_bitmap[y*width*3+x*3+2], fp); |
va009039 | 0:98f918e1d528 | 57 | fputc(m_bitmap[y*width*3+x*3+1], fp); |
va009039 | 0:98f918e1d528 | 58 | fputc(m_bitmap[y*width*3+x*3+0], fp); |
va009039 | 0:98f918e1d528 | 59 | } |
va009039 | 0:98f918e1d528 | 60 | } |
va009039 | 0:98f918e1d528 | 61 | fclose(fp); |
va009039 | 0:98f918e1d528 | 62 | return true; |
va009039 | 0:98f918e1d528 | 63 | } |
va009039 | 0:98f918e1d528 | 64 | }; |
va009039 | 0:98f918e1d528 | 65 | |
va009039 | 0:98f918e1d528 | 66 | #endif // BMP24_H |