int spr_load(int id, char *name, int descale) { char work_buf[8192]; JDEC jdec; JRESULT r; jdecomp_handle jh; struct sprite *sp; if (fb_mode > 1) return (-1); jh.fh = open(name, O_RDONLY); if (jh.fh < 0) return (-1); r = jd_prepare(&jdec, jpeg_fetch_encoded, work_buf, sizeof(work_buf), &jh); if (r == JDR_OK) { sp = spr_alloc(id, jdec.width * jdec.height * (fb_mode +1) >> (descale * 2)); sp->spr_size_x = jdec.width >> descale; sp->spr_size_y = jdec.height >> descale; jh.sp = sp; r = jd_decomp(&jdec, jpeg_dump_decoded, descale); if (r != JDR_OK) { spr_free(id); printf("Failed to decompress: rc=%d\n", r); } } else {
int spr_load(int id, char *name, int descale) { char work_buf[8192]; JDEC jdec; JRESULT jr; jdecomp_handle jh; struct sprite *sp; upng_t *up; uint8_t *rgbsrc; uint16_t *u16dst; uint8_t *u8dst; int i, sx, sy, r, g, b; if (fb_mode > 1) return (-1); /* Attempt JPEG decoding first */ jh.fh = open(name, O_RDONLY); if (jh.fh < 0) return (-1); jr = jd_prepare(&jdec, jpeg_fetch_encoded, work_buf, sizeof(work_buf), &jh); if (jr == JDR_OK) { sp = spr_alloc(id, jdec.width * jdec.height * (fb_mode + 1) >> (descale * 2)); if (sp == NULL) { close(jh.fh); return (ENOMEM); } sp->spr_size_x = jdec.width >> descale; sp->spr_size_y = jdec.height >> descale; jh.sp = sp; r = jd_decomp(&jdec, jpeg_dump_decoded, descale); close(jh.fh); if (r != JDR_OK) { spr_free(id); printf("Failed to decompress: rc=%d\n", r); return (-1); } else return (0); }
void load_jpg (void) { JDEC jd; /* Decompression object (70 bytes) */ JRESULT rc; uint8_t tjpegd_format; switch(current_color_mode) { case COLOR_12BIT:tjpegd_format=3;break; case COLOR_16BIT:tjpegd_format=2;break; case COLOR_18BIT:tjpegd_format=1;break; default: return; } /* Prepare to decompress the file */ rc = jd_prepare(&jd, tjd_input, Work, sizeof(Work), 0); if (rc == JDR_OK) { /* Start to decompress the JPEG file */ rc = jd_decomp(&jd, tjd_output, 0, tjpegd_format); } }
int load_textures() { static int unpack_jpg=TRUE; if(unpack_jpg){ void *work; int work_size=3100; printf("unpacking JPG images\n"); unpack_jpg=FALSE; work=malloc(work_size); if(work){ int i; for(i=0;i<sizeof(jpg_list)/sizeof(struct DECOMPRESS_LIST);i++){ unsigned char *src,*dst; JDEC jdec; JRESULT res; struct IOBUF io; src=jpg_list[i].src; io.in=src; io.position=0; res=jd_prepare(&jdec,in_func,work,work_size,&io); if(res==JDR_OK){ int decomp_size=3 * jdec.width * jdec.height; printf("Image dimensions: %u by %u. %u bytes used.\n", jdec.width, jdec.height, work_size - jdec.sz_pool); dst=malloc(decomp_size); if(dst){ *jpg_list[i].dst=dst; io.out=dst; io.outsize=decomp_size; io.width=jdec.width; res=jd_decomp(&jdec,out_func,0); if(res==JDR_OK){ struct TEXTURE_FILE *tf; tex_files[i].data=dst; tex_files[i].w=jdec.width; tex_files[i].h=jdec.height; tf=jpg_list[i].tf; if(tf){ tf->data=dst; } { /* FILE *f; char str[80]; sprintf(str,"c:\\temp\\test%ix%i.bin",jdec.width,jdec.height); f=fopen(str,"wb"); if(f){ fwrite(tf->data,1,tf->w*tf->h*3,f); fclose(f); } */ } } else printf("Failed to decompress: rc=%d\n", res); } }else{ struct TEXTURE_FILE *tf; tf=jpg_list[i].tf; printf("Failed to prepare: rc=%d %s\n", res,tf->name); } } free(work); } } init_texture_buttons(); return bind_textures(&gl_textures); }
//Decode the embedded image into pixel lines that can be used with the rest of the logic. esp_err_t decode_image(uint16_t ***pixels) { char *work=NULL; int r; JDEC decoder; JpegDev jd; *pixels=NULL; esp_err_t ret=ESP_OK; //Alocate pixel memory. Each line is an array of IMAGE_W 16-bit pixels; the `*pixels` array itself contains pointers to these lines. *pixels=calloc(IMAGE_H, sizeof(uint16_t*)); if (*pixels==NULL) { ESP_LOGE(TAG, "Error allocating memory for lines"); ret=ESP_ERR_NO_MEM; goto err; } for (int i=0; i<IMAGE_H; i++) { (*pixels)[i]=malloc(IMAGE_W*sizeof(uint16_t)); if ((*pixels)[i]==NULL) { ESP_LOGE(TAG, "Error allocating memory for line %d", i); ret=ESP_ERR_NO_MEM; goto err; } } //Allocate the work space for the jpeg decoder. work=calloc(WORKSZ, 1); if (work==NULL) { ESP_LOGE(TAG, "Cannot allocate workspace"); ret=ESP_ERR_NO_MEM; goto err; } //Populate fields of the JpegDev struct. jd.inData=image_jpg_start; jd.inPos=0; jd.outData=*pixels; jd.outW=IMAGE_W; jd.outH=IMAGE_H; //Prepare and decode the jpeg. r=jd_prepare(&decoder, infunc, work, WORKSZ, (void*)&jd); if (r!=JDR_OK) { ESP_LOGE(TAG, "Image decoder: jd_prepare failed (%d)", r); ret=ESP_ERR_NOT_SUPPORTED; goto err; } r=jd_decomp(&decoder, outfunc, 0); if (r!=JDR_OK) { ESP_LOGE(TAG, "Image decoder: jd_decode failed (%d)", r); ret=ESP_ERR_NOT_SUPPORTED; goto err; } //All done! Free the work area (as we don't need it anymore) and return victoriously. free(work); return ret; err: //Something went wrong! Exit cleanly, de-allocating everything we allocated. if (*pixels!=NULL) { for (int i=0; i<IMAGE_H; i++) { free((*pixels)[i]); } free(*pixels); } free(work); return ret; }