BaseJpegDeocde exampe program

Dependencies:   BaseJpegDecode Terminal BaseUsbHost mbed mbed-rtos

Fork of BaseJpegDecode by Norimasa Okamoto

Revision:
4:7d88de31c55a
Child:
5:033432f9baf3
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/example_SimpleJpegDecode.cpp	Tue Oct 30 15:35:36 2012 +0000
@@ -0,0 +1,92 @@
+#if 1
+//
+// split jpeg to bmp files
+//
+#include "mbed.h"
+#include "SimpleJpegDecode.h"
+#include "bmp24.h"
+#include "uvc.h"
+#include "msc.h"
+
+#define ASSERT(A) while(!(A)){fprintf(stderr,"\n\n%s@%d %s ASSERT!\n\n",__PRETTY_FUNCTION__,__LINE__,#A);exit(1);};
+
+DigitalOut led1(LED1), led2(LED2), led3(LED3), led4(LED4);
+Serial pc(USBTX, USBRX);
+
+SimpleJpegDecode* decode = NULL;
+bmp24* bmp = NULL;
+
+int offset_x = 0;
+int offset_y = 0;
+void callback(int x, int y, uint8_t* yuv)
+{
+    led1 = !led1;
+    if (bmp) {
+        uint8_t rgb[3];
+        int r = yuv[0] + (yuv[2]-128) * 1.4020;
+        if (r < 0) {
+            r = 0;
+        } else if (r > 255) {
+            r = 255;
+        }
+        rgb[0] = r;
+        int g = yuv[0] - (yuv[1]-128) * 0.3441 - (yuv[2]-128) * 0.7139;
+        if (g < 0) {
+            g = 0;
+        } else if (g > 255) {
+            g = 255;
+        }
+        rgb[1] = g;
+        int b = yuv[0] + (yuv[1]-128) * 1.7718 - (yuv[2]-128) * 0.0012;
+        if (b < 0) {
+            b = 0;
+        } else if (b > 255) {
+            b = 255;
+        }
+        rgb[2] = b;
+        bmp->point(x - offset_x, y - offset_y, rgb);
+    }
+}
+
+int main() {
+    pc.baud(921600);
+    printf("%s\n", __FILE__);
+    
+    msc* usb = new msc("usb");
+    int r = usb->setup();
+    ASSERT(r == 0);
+
+    bmp = new bmp24;
+    ASSERT(bmp);
+
+    decode = new SimpleJpegDecode;
+    ASSERT(decode);
+    decode->setOnResult(callback);
+
+    const char* input_file = "/usb/input.jpg";
+    printf("input: %s\n", input_file);
+    int n = 0;
+    for(offset_y = 0; offset_y < 240; offset_y += 48) {
+        for(offset_x = 0; offset_x < 320; offset_x += 64) {
+            bmp->clear();
+            decode->clear();
+            FILE *fp = fopen(input_file, "rb");
+            ASSERT(fp != NULL);
+            while(!feof(fp)) {
+                int c = fgetc(fp);
+                decode->input(c);
+                led2 = !led2;
+            }
+            fclose(fp);
+            char path[32];
+            sprintf(path, "/usb/output%02d.bmp", n++);
+            printf("offset: (%3d,%3d) %s\n", offset_x, offset_y, path);
+            bmp->writeFile(path);
+            led3 = !led3;
+        }
+        led4 = !led4;
+    }
+    printf("width: %d, height: %d, yblock: %d\n", decode->width, decode->height, decode->m_yblocks);
+    exit(1);     
+}
+#endif