1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
| int tjpeg2yuv(unsigned char* jpeg_buffer, int jpeg_size, unsigned char** yuv_buffer, int* yuv_size, int* yuv_type) { tjhandle handle = NULL; int width, height, subsample, colorspace; int flags = 0; int padding = 1; // 1或4均可,但不能是0 int ret = 0; handle = tjInitDecompress(); tjDecompressHeader3(handle, jpeg_buffer, jpeg_size, &width, &height, &subsample, &colorspace); printf("w: %d h: %d subsample: %d color: %d\n", width, height, subsample, colorspace); flags |= 0; *yuv_type = subsample; // 注:经测试,指定的yuv采样格式只对YUV缓冲区大小有影响,实际上还是按JPEG本身的YUV格式来转换的 *yuv_size = tjBufSizeYUV2(width, padding, height, subsample); *yuv_buffer =(unsigned char *)malloc(*yuv_size); if (*yuv_buffer == NULL) { printf("malloc buffer for rgb failed.\n"); return -1; } ret = tjDecompressToYUV2(handle, jpeg_buffer, jpeg_size, *yuv_buffer, width, padding, height, flags); if (ret < 0) { printf("compress to jpeg failed: %s\n", tjGetErrorStr()); } tjDestroy(handle); return ret; } int tyuv2jpeg(unsigned char* yuv_buffer, int yuv_size, int width, int height, int subsample, unsigned char** jpeg_buffer, unsigned long* jpeg_size, int quality) { tjhandle handle = NULL; int flags = 0; int padding = 1; // 1或4均可,但不能是0 int need_size = 0; int ret = 0; handle = tjInitCompress(); flags |= 0; need_size = tjBufSizeYUV2(width, padding, height, subsample); if (need_size != yuv_size) { printf("we detect yuv size: %d, but you give: %d, check again.\n", need_size, yuv_size); return 0; } ret = tjCompressFromYUV(handle, yuv_buffer, width, padding, height, subsample, jpeg_buffer, jpeg_size, quality, flags); if (ret < 0) { printf("compress to jpeg failed: %s\n", tjGetErrorStr()); } tjDestroy(handle); return ret; }
|