return MM_UTIL_ERROR_NONE;
}
-static void __jpeg_convert_rgb_colorspace(mm_util_color_format_e mm_color_format, J_COLOR_SPACE *j_color_space, int *j_color_comp)
+static int __jpeg_convert_colorspace(mm_util_color_format_e mm_color_format, J_COLOR_SPACE *j_color_space, int *j_color_comp)
{
int pixel_depth = 0;
- if (mm_color_format == MM_UTIL_COLOR_RGB24) {
+ mm_util_retvm_if(!j_color_space, MM_UTIL_ERROR_INVALID_PARAMETER, "invalid j_color_space");
+
+ switch(mm_color_format) {
+ case MM_UTIL_COLOR_RGB24:
pixel_depth = 3;
*j_color_space = JCS_RGB;
mm_util_debug("JCS_RGB");
- } else if (mm_color_format == MM_UTIL_COLOR_GRAYSCALE) {
+ break;
+ case MM_UTIL_COLOR_GRAYSCALE:
pixel_depth = 1; /* one colour component */
*j_color_space = JCS_GRAYSCALE;
mm_util_debug("JCS_GRAYSCALE");
- } else if (mm_color_format == MM_UTIL_COLOR_RGBA) {
+ break;
+ case MM_UTIL_COLOR_RGBA:
pixel_depth = 4;
*j_color_space = JCS_EXT_RGBA;
mm_util_debug("JCS_EXT_RGBA");
- } else if (mm_color_format == MM_UTIL_COLOR_BGRA) {
+ break;
+ case MM_UTIL_COLOR_BGRA:
pixel_depth = 4;
*j_color_space = JCS_EXT_BGRA;
mm_util_debug("JCS_EXT_BGRA");
- } else if (mm_color_format == MM_UTIL_COLOR_ARGB) {
+ break;
+ case MM_UTIL_COLOR_ARGB:
pixel_depth = 4;
*j_color_space = JCS_EXT_ARGB;
mm_util_debug("JCS_EXT_ARGB");
+ break;
+ case MM_UTIL_COLOR_YUV420:
+ case MM_UTIL_COLOR_YUV422:
+ case MM_UTIL_COLOR_UYVY:
+ pixel_depth = 3;
+ *j_color_space = JCS_YCbCr;
+ mm_util_debug("JCS_YCbCr");
+ break;
+ default:
+ mm_util_error("not supported format [%d]", mm_color_format);
+ return MM_UTIL_ERROR_NOT_SUPPORTED_FORMAT;
}
if (j_color_comp)
*j_color_comp = pixel_depth;
+
+ return MM_UTIL_ERROR_NONE;
}
static void __jpeg_encode_open(j_compress_ptr cinfo, mm_util_jpeg_ctrl_format_e control_format, FILE *fp, void **mem, unsigned long *size)
_width = cinfo.image_width = _decoded->width;
_height = cinfo.image_height = _decoded->height;
+ ret = __jpeg_convert_colorspace(_decoded->color, &(cinfo.in_color_space), &(cinfo.input_components));
+ if (ret != MM_UTIL_ERROR_NONE) {
+ mm_util_error("__jpeg_convert_colorspace failed %d", ret);
+ jpeg_destroy_compress(&cinfo);
+ mm_util_debug("jpeg_destroy_compress");
+ return ret;
+ }
+
if (_decoded->color == MM_UTIL_COLOR_YUV420 || _decoded->color == MM_UTIL_COLOR_YUV422 || _decoded->color == MM_UTIL_COLOR_UYVY) {
- cinfo.input_components = 3;
- cinfo.in_color_space = JCS_YCbCr;
jpeg_set_defaults(&cinfo);
mm_util_debug("jpeg_set_defaults");
ret = __jpeg_encode_yuv(&cinfo, _width, _height, decoded);
if (ret != MM_UTIL_ERROR_NONE)
mm_util_error("__jpeg_encode_yuv failed");
- }
-
- else if (_decoded->color == MM_UTIL_COLOR_RGB24 || _decoded->color == MM_UTIL_COLOR_GRAYSCALE || _decoded->color == MM_UTIL_COLOR_RGBA || _decoded->color == MM_UTIL_COLOR_BGRA || _decoded->color == MM_UTIL_COLOR_ARGB) {
+ } else if (_decoded->color == MM_UTIL_COLOR_RGB24 || _decoded->color == MM_UTIL_COLOR_GRAYSCALE || _decoded->color == MM_UTIL_COLOR_RGBA || _decoded->color == MM_UTIL_COLOR_BGRA || _decoded->color == MM_UTIL_COLOR_ARGB) {
unsigned int row_stride = 0;
JSAMPROW row_pointer[1];
JSAMPLE *image_buffer = (JSAMPLE *)_decoded->data;
- __jpeg_convert_rgb_colorspace(_decoded->color, &(cinfo.in_color_space), &(cinfo.input_components));
row_stride = _width * cinfo.input_components;
jpeg_set_defaults(&cinfo);
row_pointer[0] = &image_buffer[cinfo.next_scanline * row_stride];
jpeg_write_scanlines(&cinfo, row_pointer, 1);
}
- } else {
- mm_util_error("We can't encode the IMAGE format");
- jpeg_destroy_compress(&cinfo);
- mm_util_debug("jpeg_destroy_compress");
- return MM_UTIL_ERROR_NOT_SUPPORTED_FORMAT;
}
jpeg_finish_compress(&cinfo);
/* set parameters for decompression */
__jpeg_decode_set_common_params(&dinfo, color_format, downscale);
- __jpeg_convert_rgb_colorspace(color_format, &dinfo.out_color_space, NULL);
+ ret = __jpeg_convert_colorspace(color_format, &dinfo.out_color_space, NULL);
+ if (ret != MM_UTIL_ERROR_NONE) {
+ mm_util_error("__jpeg_convert_colorspace failed %d", ret);
+ goto END;
+ }
/* Start decompressor */
jpeg_start_decompress(&dinfo);