diff --git a/core/SConscript.firmware b/core/SConscript.firmware index d409bbfce..de5cdb934 100644 --- a/core/SConscript.firmware +++ b/core/SConscript.firmware @@ -179,7 +179,7 @@ SOURCE_MOD += [ 'embed/extmod/modtrezorui/fonts/font_bitmap.c', 'embed/extmod/modtrezorui/modtrezorui.c', 'embed/extmod/modtrezorui/qr-code-generator/qrcodegen.c', - 'embed/extmod/modtrezorui/tjpgd/src/tjpgd.c', + 'embed/extmod/modtrezorui/tjpgd/tjpgd.c', 'vendor/micropython/lib/uzlib/adler32.c', 'vendor/micropython/lib/uzlib/crc32.c', 'vendor/micropython/lib/uzlib/tinflate.c', diff --git a/core/SConscript.unix b/core/SConscript.unix index f10b9581b..045ed5770 100644 --- a/core/SConscript.unix +++ b/core/SConscript.unix @@ -175,7 +175,7 @@ SOURCE_MOD += [ 'embed/extmod/modtrezorui/fonts/font_bitmap.c', 'embed/extmod/modtrezorui/modtrezorui.c', 'embed/extmod/modtrezorui/qr-code-generator/qrcodegen.c', - 'embed/extmod/modtrezorui/tjpgd/src/tjpgd.c', + 'embed/extmod/modtrezorui/tjpgd/tjpgd.c', 'vendor/micropython/lib/uzlib/adler32.c', 'vendor/micropython/lib/uzlib/crc32.c', 'vendor/micropython/lib/uzlib/tinflate.c', diff --git a/core/embed/extmod/modtrezorui/display.c b/core/embed/extmod/modtrezorui/display.c index 723669099..98e404741 100644 --- a/core/embed/extmod/modtrezorui/display.c +++ b/core/embed/extmod/modtrezorui/display.c @@ -20,7 +20,7 @@ #define _GNU_SOURCE #include "qr-code-generator/qrcodegen.h" -#include "tjpgd/src/tjpgd.h" +#include "tjpgd/tjpgd.h" #include "uzlib.h" diff --git a/core/embed/extmod/modtrezorui/tjpgd/doc/00index.html b/core/embed/extmod/modtrezorui/tjpgd/doc/00index.html deleted file mode 100644 index bc0b960d8..000000000 --- a/core/embed/extmod/modtrezorui/tjpgd/doc/00index.html +++ /dev/null @@ -1,77 +0,0 @@ - - -
- - - - - - -TJpgDec is a generic JPEG image decompressor module that highly optimized for small embedded systems. It works with very low memory consumption, so that it can be incorporated into tiny microcontrollers, such as AVR, 8051, PIC, Z80, Cortex-M0 and etc.
- -There are two API functions to analyze and decompress the JPEG image.
-To input the JPEG data and output the decompressed image, TJpgDec requires two user defined I/O functions. These are called back from the TJpgDec module in the decompression process.
-The TJpgDec module is a free software opened for education, research and development. You can use, modify and/or redistribute it for personal projects or commercial products without any restriction under your responsibility.
-First of all, you should build and run the sample program shown below. This is a typical usage of TJpgDec module and it helps to narrow down the problem on debugging.
-The decompression session is divided in two stages. The first stage is to analyze the JPEG image and the second stage is to decompress it.
--/*------------------------------------------------*/ -/* TJpgDec Quick Evaluation Program for PCs */ -/*------------------------------------------------*/ - -#include <stdio.h> -#include <string.h> -#include "tjpgd.h" - -/* Bytes per pixel of image output */ -#define N_BPP (3 - JD_FORMAT) - - -/* Session identifier for input/output functions (name, members and usage are as user defined) */ -typedef struct { - FILE *fp; /* Input stream */ - uint8_t *fbuf; /* Output frame buffer */ - unsigned int wfbuf; /* Width of the frame buffer [pix] */ -} IODEV; - - -/*------------------------------*/ -/* User defined input funciton */ -/*------------------------------*/ - -size_t in_func ( /* Returns number of bytes read (zero on error) */ - JDEC* jd, /* Decompression object */ - uint8_t* buff, /* Pointer to the read buffer (null to remove data) */ - size_t nbyte /* Number of bytes to read/remove */ -) -{ - IODEV *dev = (IODEV*)jd->device; /* Session identifier (5th argument of jd_prepare function) */ - - - if (buff) { /* Raad data from imput stream */ - return fread(buff, 1, nbyte, dev->fp); - } else { /* Remove data from input stream */ - return fseek(dev->fp, nbyte, SEEK_CUR) ? 0 : nbyte; - } -} - - -/*------------------------------*/ -/* User defined output funciton */ -/*------------------------------*/ - -int out_func ( /* Returns 1 to continue, 0 to abort */ - JDEC* jd, /* Decompression object */ - void* bitmap, /* Bitmap data to be output */ - JRECT* rect /* Rectangular region of output image */ -) -{ - IODEV *dev = (IODEV*)jd->device; /* Session identifier (5th argument of jd_prepare function) */ - uint8_t *src, *dst; - uint16_t y, bws; - unsigned int bwd; - - - /* Progress indicator */ - if (rect->left == 0) { - printf("\r%lu%%", (rect->top << jd->scale) * 100UL / jd->height); - } - - /* Copy the output image rectangle to the frame buffer */ - src = (uint8_t*)bitmap; /* Output bitmap */ - dst = dev->fbuf + N_BPP * (rect->top * dev->wfbuf + rect->left); /* Left-top of rectangle in the frame buffer */ - bws = N_BPP * (rect->right - rect->left + 1); /* Width of the rectangle [byte] */ - bwd = N_BPP * dev->wfbuf; /* Width of the frame buffer [byte] */ - for (y = rect->top; y <= rect->bottom; y++) { - memcpy(dst, src, bws); /* Copy a line */ - src += bws; dst += bwd; /* Next line */ - } - - return 1; /* Continue to decompress */ -} - - -/*------------------------------*/ -/* Program Main */ -/*------------------------------*/ - -int main (int argc, char* argv[]) -{ - JRESULT res; /* Result code of TJpgDec API */ - JDEC jdec; /* Decompression object */ - void *work; /* Pointer to the work area */ - size_t sz_work = 3500; /* Size of work area */ - IODEV devid; /* Session identifier */ - - - /* Initialize input stream */ - if (argc < 2) return -1; - devid.fp = fopen(argv[1], "rb"); - if (!devid.fp) return -1; - - /* Prepare to decompress */ - work = (void*)malloc(sz_work); - res = jd_prepare(&jdec, in_func, work, sz_work, &devid); - if (res == JDR_OK) { - /* It is ready to dcompress and image info is available here */ - printf("Image size is %u x %u.\n%u bytes of work ares is used.\n", jdec.width, jdec.height, sz_work - jdec.sz_pool); - - /* Initialize output device */ - devid.fbuf = (uint8_t*)malloc(N_BPP * jdec.width * jdec.height); /* Create frame buffer for output image */ - devid.wfbuf = jdec.width; - - res = jd_decomp(&jdec, out_func, 0); /* Start to decompress with 1/1 scaling */ - if (res == JDR_OK) { - /* Decompression succeeded. You have the decompressed image in the frame buffer here. */ - printf("\rDecompression succeeded.\n"); - - } else { - printf("jd_decomp() failed (rc=%d)\n", res); - } - - free(devid.fbuf); /* Discard frame buffer */ - - } else { - printf("jd_prepare() failed (rc=%d)\n", res); - } - - free(work); /* Discard work area */ - - fclose(devid.fp); /* Close the JPEG file */ - - return res; -} -- -
These are the memory usage of some platforms at default configuration. Each compilations are optimized in code size.
-AVR | PIC24 | CM0 | IA-32 | |
---|---|---|---|---|
Compiler | GCC | C30 | GCC | MSC |
text+const | 6.1k | 5.1k | 3.1k | 3.7k |
TJpgDec requires a work area upto 3100 bytes for most JPEG images. It exactly depends on what parameter has been used to create the JPEG image to be decompressed. The 3100 bytes is the maximum memory requirement in default configuration and it varies depends on JD_SZBUF and JD_FASTDECODE.
-TJpgDec has some configuration options on output format, performance and memory usage. These options are in tjpgdcnf.h.
-This is a copy of the TJpgDec license document that included in the source codes.
--/*----------------------------------------------------------------------------/ -/ TJpgDec - Tiny JPEG Decompressor R0.xx (C)ChaN, 20xx -/-----------------------------------------------------------------------------/ -/ The TJpgDec is a generic JPEG decompressor module for tiny embedded systems. -/ This is a free software that opened for education, research and commercial -/ developments under license policy of following terms. -/ -/ Copyright (C) 20xx, ChaN, all right reserved. -/ -/ * The TJpgDec module is a free software and there is NO WARRANTY. -/ * No restriction on use. You can use, modify and redistribute it for -/ personal, non-profit or commercial products UNDER YOUR RESPONSIBILITY. -/ * Redistributions of source code must retain the above copyright notice. -/ -/----------------------------------------------------------------------------*/-
Therefore TJpgDec license is one of the BSD-style license but there is a significant difference. Because TJpgDec is for embedded projects, so that the conditions for redistributions in binary form, such as embedded code, hex file and binary library, are not specified in order to maximize its usability. The documentation of the distributions may or may not include about TJpgDec and its license document. Of course TJpgDec is compatible with the projects under GNU GPL. When redistribute TJpgDec with any modification, the license can also be changed to GNU GPL or any BSD-style license.
-The jd_decomp function decompress the JPEG image and output it as pixel data.
--JRESULT jd_decomp ( - JDEC* jdec, /* Pointer to valid decompression object */ - int(*outfunc)(JDEC*,void*,JRECT*), /* Pointer to output function */ - uint8_t scale /* Scaling factor */ -); --
The jd_decomp function is the second stage of a JPEG decompression session. It decompresses the input JPEG stream and outputs it via user defined output function. After this function, the decompressor object is no longer valid.
-The scaling factor can be specified on decompresson. It descales the size of the output image by 1/2, 1/4 or 1/8. For example, when decompress a JPEG image of 1024x768 in 1/4 scaling, the decmopressed image will be output in 256x192. The scaling ratio of 1/2 and 1/4 will slightly decrease the decompression speed compared to 1/1 due to averaging process. However scaling ratio of 1/8 will 2-3 times faster than 1/1, because IDCT and averaging process for each block can be skipped. This characteristic is sutable to create thumbnails.
-User defined input function to read data from input stream.
--size_t in_func ( - JDEC* jdec, /* Pointer to the decompression object */ - uint8_t* buff, /* Pointer to buffer to store the read data */ - size_t ndata /* Number of bytes to read/remove */ -); - --
Returns number of bytes read/removed. If any value not expected is returned, the jd_prepare and jd_decomp function aborts with JDR_INP.
-This function is the data input interface of the TJpgDec module. The corresponding decompression session can be identified by the session identifier jdec->device passed to the 5th argument of jd_prepare function.
-The JDEC structure holds the data, pointer and flags to manage the decompression session. Application program must not change any member in this structure.
--typedef struct JDEC JDEC; -struct JDEC { - size_t dctr; /* Number of bytes available in the input buffer */ - uint8_t* dptr; /* Current data read ptr */ - uint8_t* inbuf; /* Bit stream input buffer */ - uint8_t dmsk; /* Current bit in the current read byte */ - uint8_t scale; /* Output scaling ratio */ - uint8_t msx, msy; /* MCU size in unit of block (width, height) */ - uint8_t qtid[3]; /* Quantization table ID of each component */ - uint8_t ncomp; /* Number of color components (1:grayscale, 3:color) */ - int16_t dcv[3]; /* Previous DC element of each component */ - uint16_t nrst; /* Restart inverval */ - uint16_t width, height; /* Size of the input image (pixel) */ - uint8_t* huffbits[2][2]; /* Huffman bit distribution tables [yc][dcac] */ - uint16_t* huffcode[2][2]; /* Huffman code word tables [yc][dcac] */ - uint8_t* huffdata[2][2]; /* Huffman decoded data tables [yc][dcac] */ - int32_t* qttbl[4]; /* De-quaitizer tables [id] */ - void* workbuf; /* Working buffer for IDCT and RGB output */ - uint8_t* mcubuf; /* Working buffer for the MCU */ - void* pool; /* Pointer to available memory pool */ - size_t sz_pool; /* Size of momory pool (bytes available) */ - size_t (*infunc)(JDEC*, uint8_t*, size_t); /* Pointer to jpeg stream input function */ - void* device; /* Pointer to I/O device identifiler for the session */ -}; --
User defined output function to write decompressed pixels to the output device.
--int out_func ( - JDEC* jdec, /* Pointer to the decompression object */ - void* bitmap, /* Bitmap to be output */ - JRECT* rect /* Rectangle to output */ -); --
Normally returns 1. It lets TJpgDec to continue the decompressing process. If a 0 is returned, jd_decomp function aborts with JDR_INTR. This is useful to interrupt the decompression process.
-This function is the data output interface of the TJpgDec module. The corresponding decompression session can be identified by the session identifier jdec->device passed to the 5th argument of jd_prepare function.
-The bitmap is sent to the frame buffer or display device in this function. The first pixel in the bitmap is the left-top of the rectangular, the second one is next right and last pixel is the bottom-right of the rectangle. Because the JPEG image is compressed and streamed in unit of MCU (Minimum Coded Unit), TJpgDec outputs the image in MCU by MCU. The size of MCU depends on the sampling factor of JPEG compression, typically in 8x8, 16x8 or 16x16, but the rectangles on right end and bottom end of the image will be clipped.
-The output pixel format is defined by JD_FORMAT option in the tjpgdcnf.h as shown below.
-JD_FORMAT | Pixel Format |
---|---|
0 (RGB888) | uint8_t bitmap[] = {R1, G1, B1, R2, G2, B2, .... |
1 (RGB565) | uint16_t bitmap[] = {rrrrrggggggbbbbb, .... |
2 (Grayscale) | uint8_t bitmap[] = {Y1, Y2, Y3, .... |
The jd_prepare function analyzes the JPEG data and create a decompression object for subsequnet decompression process.
--JRESULT jd_prepare ( - JDEC* jdec, /* Pointer to blank decompression object */ - size_t (*infunc)(JDEC*, uint8_t*, size_t), /* Pointer to input function */ - void* work, /* Pointer to the work area */ - size_t sz_work, /* Size of the work area */ - void* device /* Session identifier for the session */ -); --
The jd_prepare function is the first stage of a JPEG decompression session. It analyzes the JPEG image and create parameter tables for decompression. After the function succeeded, the session gets ready to decompress the JPEG image by the jd_decomp fnction. The application program can refer the dimensions of the JPEG image stored in the decompression object. These information will be used to configure the output device and parameters for subsequent decompression stage.
-