Update tjpgd_example.c

This commit is contained in:
kyt_2002 2023-10-05 19:16:56 +08:00
parent a5ad91fc1d
commit e280812ee8
1 changed files with 148 additions and 151 deletions

View File

@ -1,151 +1,148 @@
#include <transform.h> #include <transform.h>
#include <tjpgd.h> #include <tjpgd.h>
#define WORK_POOL_SIZE (4*1024+32)//This value depends on the resolution of the image #define WORK_POOL_SIZE (4*1024+32)//This value depends on the resolution of the image
/* User defined device identifier */ /* User defined device identifier */
typedef struct { typedef struct {
FILE *fp; /* File pointer for input function */ FILE *fp; /* File pointer for input function */
uint8_t *fbuf; /* Pointer to the frame buffer for output function */ uint8_t *fbuf; /* Pointer to the frame buffer for output function */
uint16_t wfbuf; /* Width of the frame buffer [pix] */ uint16_t wfbuf; /* Width of the frame buffer [pix] */
} IODEV; } IODEV;
/*------------------------------*/ /*------------------------------*/
/* User defined input funciton */ /* User defined input funciton */
/*------------------------------*/ /*------------------------------*/
unsigned int in_func (JDEC* jd, uint8_t* buff, unsigned int nbyte) unsigned int in_func (JDEC* jd, uint8_t* buff, unsigned int nbyte)
{ {
IODEV *dev = (IODEV*)jd->device; /* Device identifier for the session (5th argument of jd_prepare function) */ IODEV *dev = (IODEV*)jd->device; /* Device identifier for the session (5th argument of jd_prepare function) */
if (buff) {
if (buff) { /* Read bytes from input stream */
/* Read bytes from input stream */ return (uint16_t)fread(buff, 1, nbyte, dev->fp);
return (uint16_t)fread(buff, 1, nbyte, dev->fp); } else {
} else { /* Remove bytes from input stream */
/* Remove bytes from input stream */ return fseek(dev->fp, nbyte, SEEK_CUR) ? 0 : nbyte;
return fseek(dev->fp, nbyte, SEEK_CUR) ? 0 : nbyte; }
} }
}
/*------------------------------*/
/*------------------------------*/ /* User defined output funciton */
/* User defined output funciton */ /*------------------------------*/
/*------------------------------*/
int out_func (JDEC* jd, void* bitmap, JRECT* rect)
int out_func (JDEC* jd, void* bitmap, JRECT* rect) {
{ IODEV *dev = (IODEV*)jd->device;
IODEV *dev = (IODEV*)jd->device; uint8_t *src, *dst;
uint8_t *src, *dst; uint16_t y, bws, bwd;
uint16_t y, bws, bwd;
/* Put progress indicator */
if (rect->left == 0) {
/* Put progress indicator */ printf("\r%lu%%", (rect->top << jd->scale) * 100UL / jd->height);
if (rect->left == 0) { }
printf("\r%lu%%", (rect->top << jd->scale) * 100UL / jd->height);
} /* Copy the decompressed RGB rectanglar to the frame buffer (assuming RGB888 cfg) */
src = (uint8_t*)bitmap;
/* Copy the decompressed RGB rectanglar to the frame buffer (assuming RGB888 cfg) */ dst = dev->fbuf + 3 * (rect->top * dev->wfbuf + rect->left); /* Left-top of destination rectangular */
src = (uint8_t*)bitmap; bws = 3 * (rect->right - rect->left + 1); /* Width of source rectangular [byte] */
dst = dev->fbuf + 3 * (rect->top * dev->wfbuf + rect->left); /* Left-top of destination rectangular */ bwd = 3 * dev->wfbuf; /* Width of frame buffer [byte] */
bws = 3 * (rect->right - rect->left + 1); /* Width of source rectangular [byte] */ for (y = rect->top; y <= rect->bottom; y++) {
bwd = 3 * dev->wfbuf; /* Width of frame buffer [byte] */ memcpy(dst, src, bws); /* Copy a line */
for (y = rect->top; y <= rect->bottom; y++) { src += bws; dst += bwd; /* Next line */
memcpy(dst, src, bws); /* Copy a line */ }
src += bws; dst += bwd; /* Next line */
} return 1; /* Continue to decompress */
}
return 1; /* Continue to decompress */
}
/*------------------------------*/
/* Program Jpeg_Dec */
/*------------------------------*/ /*------------------------------*/
/* Program Jpeg_Dec */
/*------------------------------*/ int Jpeg_Dec (int argc, char* argv[])
{
int Jpeg_Dec (int argc, char* argv[]) void *work; /* Pointer to the decompressor work area */
{ JDEC jdec; /* Decompression object */
void *work; /* Pointer to the decompressor work area */ JRESULT res; /* Result code of TJpgDec API */
JDEC jdec; /* Decompression object */ IODEV devid; /* User defined device identifier */
JRESULT res; /* Result code of TJpgDec API */
IODEV devid; /* User defined device identifier */ /* Open a JPEG file */
if (argc < 2)
/* Open a JPEG file */ {
if (argc < 2) printf("Jpeg_Dec illegal arguments ...\n");
{ return -1;
printf("Jpeg_Dec illegal arguments ...\n"); }
return -1;
} devid.fp = fopen(argv[1], "r+");
if (!devid.fp)
devid.fp = fopen(argv[1], "r+"); {
if (!devid.fp) printf("Jpeg_Dec open the file failed...\n");
{ return -1;
printf("Jpeg_Dec open the file failed...\n"); }
return -1;
} /* Allocate a work area for TJpgDec */
work = malloc(WORK_POOL_SIZE);
/* Allocate a work area for TJpgDec */ if(work == NULL)
work = malloc(WORK_POOL_SIZE); {
if(work == NULL) printf("Jpeg_Dec work malloc failed...\n");
{ res = -1;
printf("Jpeg_Dec work malloc failed...\n"); goto __exit;
res = -1; }
goto __exit;
} /* Prepare to decompress */
res = jd_prepare(&jdec, in_func, work, WORK_POOL_SIZE, &devid);
/* Prepare to decompress */ if (res == JDR_OK)
res = jd_prepare(&jdec, in_func, work, WORK_POOL_SIZE, &devid); {
if (res == JDR_OK) /* Ready to dcompress. Image info is available here. */
{ printf("Image dimensions: %u by %u. %u bytes used.\n", jdec.width, jdec.height, 3100 - jdec.sz_pool);
/* Ready to dcompress. Image info is available here. */
printf("Image dimensions: %u by %u. %u bytes used.\n", jdec.width, jdec.height, 3100 - jdec.sz_pool); devid.fbuf = malloc(3 * jdec.width * jdec.height); /* Frame buffer for output image (assuming RGB888 cfg) */
if(devid.fbuf == RT_NULL)
devid.fbuf = malloc(3 * jdec.width * jdec.height); /* Frame buffer for output image (assuming RGB888 cfg) */ {
if(devid.fbuf == RT_NULL) printf("Jpeg_Dec devid.fbuf malloc failed, need to use %d Bytes ...\n", 3 * jdec.width * jdec.height);
{ res = -1;
printf("Jpeg_Dec devid.fbuf malloc failed, need to use %d Bytes ...\n", 3 * jdec.width * jdec.height); goto __exit;
res = -1; }
goto __exit; devid.wfbuf = jdec.width;
}
devid.wfbuf = jdec.width; res = jd_decomp(&jdec, out_func, 0); /* Start to decompress with 1/1 scaling */
if (res == JDR_OK) {
res = jd_decomp(&jdec, out_func, 0); /* Start to decompress with 1/1 scaling */ /* Decompression succeeded. You have the decompressed image in the frame buffer here. */
if (res == JDR_OK) { printf("\rOK \n");
/* Decompression succeeded. You have the decompressed image in the frame buffer here. */ // for(int j = 0; j<3 * jdec.width * jdec.height;j++)
printf("\rOK \n"); // {
// for(int j = 0; j<3 * jdec.width * jdec.height;j++) // printf("%d,",*(devid.fbuf+j));
// { // }
// printf("%d,",*(devid.fbuf+j));
// } }
else
} {
else printf("Failed to decompress: rc=%d\n", res);
{ }
printf("Failed to decompress: rc=%d\n", res);
} if(devid.fbuf != NULL)
{
if(devid.fbuf != NULL) free(devid.fbuf); /* Discard frame buffer */
{ }
free(devid.fbuf); /* Discard frame buffer */
} }
else
} {
else printf("Failed to prepare: rc=%d\n", res);
{ }
printf("Failed to prepare: rc=%d\n", res);
} __exit:
if(work != NULL)
__exit: {
if(work != NULL) free(work); /* Discard work area */
{ }
free(work); /* Discard work area */
} fclose(devid.fp); /* Close the JPEG file */
fclose(devid.fp); /* Close the JPEG file */ return res;
}
return res; #ifdef __RT_THREAD_H__
} MSH_CMD_EXPORT(Jpeg_Dec, Jpeg Decode Test);
#ifdef __RT_THREAD_H__ #endif
MSH_CMD_EXPORT(Jpeg_Dec, Jpeg Decode Test);
#endif