#include #include #include #include #include #include "jpeg2k.h" #include "openjpeg.h" #define J2K_CFMT 0 #define BMP_DFMT 2 static void on_msg(const char *msg, void *client_data) { OutputDebugStringA(msg); } typedef struct memory_stream_t { void *data; size_t length; size_t capacity; }memory_stream_t; static memory_stream_t *alloc_memory_stream(int init_capacity) { memory_stream_t *mem = (memory_stream_t *)malloc(sizeof(memory_stream_t)); mem->data = malloc(init_capacity); mem->length = 0; mem->capacity = init_capacity; return mem; } static void free_memory_stream(memory_stream_t *mem) { free(mem->data); free(mem); } static OPJ_SIZE_T memory_stream_write(void * p_buffer, OPJ_SIZE_T p_nb_bytes, memory_stream_t *mem) { if (mem->length + p_nb_bytes > mem->capacity) { int new_capacity = mem->capacity; do { new_capacity = new_capacity * 2; } while (new_capacity < mem->length + p_nb_bytes); mem->capacity = new_capacity; mem->data = realloc(mem->data, new_capacity); } memcpy((char*)mem->data+mem->length, p_buffer, p_nb_bytes); mem->length += p_nb_bytes; return p_nb_bytes; } static OPJ_SIZE_T memory_stream_read (void * p_buffer, OPJ_SIZE_T p_nb_bytes, memory_stream_t *mem) { OPJ_SIZE_T cnt; if (p_nb_bytes > mem->capacity - mem->length) { cnt = mem->capacity - mem->length; memcpy(p_buffer, (char*)mem->data+mem->length, cnt); mem->length = mem->capacity; } else { memcpy(p_buffer, (char*)mem->data+mem->length, p_nb_bytes); mem->length += p_nb_bytes; cnt = p_nb_bytes; } return cnt ? cnt : -1; } static OPJ_OFF_T memory_stream_skip(OPJ_OFF_T p_nb_bytes, memory_stream_t *mem) { mem->length += p_nb_bytes; return p_nb_bytes; } static OPJ_BOOL memory_stream_seek (OPJ_OFF_T p_nb_bytes, memory_stream_t *mem) { mem->length = p_nb_bytes; return OPJ_TRUE; } BIZCHAN_API(int) jpeg2k_encode(jpeg2k_raw_image *raw, jpeg2k_coded_image * coded, int ratio) { opj_cparameters_t parameters; opj_image_t *oimage = NULL; opj_image_cmptparm_t *cmptparm; int i; opj_codec_t *codec = NULL; OPJ_BOOL bSuccess; opj_stream_t *out = NULL; memory_stream_t *mem = NULL; if (ratio < 0 || ratio > 100) return -1; opj_set_default_encoder_parameters(¶meters); parameters.subsampling_dx = 1; parameters.subsampling_dy = 1; parameters.tcp_rates[0] = ratio; parameters.tcp_numlayers = 1; parameters.cp_disto_alloc = 1; parameters.numresolution = 6; //parameters.cblockw_init = 32; //parameters.cblockh_init = 32; parameters.prcw_init[0] = 128; parameters.prch_init[0] = 128; parameters.prcw_init[1] = 128; parameters.prch_init[1] = 128; parameters.res_spec = 2; parameters.prog_order = OPJ_LRCP; cmptparm = (opj_image_cmptparm_t*) malloc(3 * sizeof(opj_image_cmptparm_t)); memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t)); for(i = 0; i < 3; i++) { cmptparm[i].prec = 8; cmptparm[i].bpp = 8; cmptparm[i].sgnd = 0; cmptparm[i].dx = parameters.subsampling_dx; cmptparm[i].dy = parameters.subsampling_dy; cmptparm[i].w = raw->width; cmptparm[i].h = raw->height; } oimage = opj_image_create(3, &cmptparm[0], OPJ_CLRSPC_SRGB); if(!oimage) { if (cmptparm) free(cmptparm); return -1; } oimage->x0 = parameters.image_offset_x0; oimage->y0 = parameters.image_offset_y0; oimage->x1 = parameters.image_offset_x0 + (raw->width - 1) * 1 + 1; oimage->y1 = parameters.image_offset_y0 + (raw->height - 1) * 1 + 1; { unsigned char *value = raw->data; int area = raw->width * raw->height; for (i = 0; i < area; i++) { oimage->comps[0].data[i] = *(value++); oimage->comps[1].data[i] = *(value++); oimage->comps[2].data[i] = *(value++); } } codec = opj_create_compress(OPJ_CODEC_J2K); opj_set_info_handler(codec, &on_msg, NULL); opj_set_warning_handler(codec, &on_msg, NULL); opj_set_error_handler(codec, &on_msg, NULL); bSuccess = opj_setup_encoder(codec, ¶meters, oimage); if (!bSuccess) goto on_error; mem = alloc_memory_stream(8192); out = opj_stream_create(OPJ_J2K_STREAM_CHUNK_SIZE, 0); opj_stream_set_user_data(out, mem); opj_stream_set_user_data_length(out, mem->capacity); opj_stream_set_write_function(out, &memory_stream_write); opj_stream_set_read_function(out, &memory_stream_read); opj_stream_set_skip_function(out, &memory_stream_skip); opj_stream_set_seek_function(out, &memory_stream_seek); bSuccess = opj_start_compress(codec, oimage, out); if (!bSuccess) goto on_error; bSuccess = opj_encode(codec, out); if (!bSuccess) goto on_error; bSuccess = opj_end_compress(codec, out); if (!bSuccess) goto on_error; coded->data = malloc(mem->length); memcpy(coded->data, mem->data, mem->length); coded->width = raw->width; coded->height = raw->height; coded->len = mem->length; if (out) { free_memory_stream(mem); opj_stream_destroy(out); } if (codec) opj_destroy_codec(codec); if (oimage) opj_image_destroy(oimage); if (cmptparm) free(cmptparm); return 0; on_error: if (out) { free_memory_stream(mem); opj_stream_destroy(out); } if (codec) opj_destroy_codec(codec); if (oimage) opj_image_destroy(oimage); if (cmptparm) free(cmptparm); return -1; } BIZCHAN_API(void) jpeg2k_encode_free(jpeg2k_coded_image * coded) { free(coded->data); memset(coded, 0, sizeof(jpeg2k_coded_image)); } BIZCHAN_API(int) jpeg2k_decode(jpeg2k_raw_image *raw, jpeg2k_coded_image * coded) { opj_dparameters_t parameters; /* decompression parameters */ opj_image_t *opjimage = NULL; unsigned char *src = NULL; unsigned char *ptr; opj_codec_t *codec = NULL; opj_stream_t *instream = NULL; memory_stream_t *mem = NULL; int i; int ret = 0; opj_set_default_decoder_parameters(¶meters);/**/ strncpy(parameters.infile, "", sizeof(parameters.infile)-1); strncpy(parameters.outfile, "", sizeof(parameters.outfile)-1); parameters.decod_format = J2K_CFMT; parameters.cod_format = BMP_DFMT; parameters.cp_reduce = 0; codec = opj_create_decompress(OPJ_CODEC_J2K); mem = alloc_memory_stream(coded->len); instream = opj_stream_create(OPJ_J2K_STREAM_CHUNK_SIZE, 1); opj_stream_set_user_data(instream, mem); opj_stream_set_user_data_length(instream, mem->capacity); opj_stream_set_write_function(instream, &memory_stream_write); opj_stream_set_read_function(instream, &memory_stream_read); opj_stream_set_skip_function(instream, &memory_stream_skip); opj_stream_set_seek_function(instream, &memory_stream_seek); //mem->length = coded->len; memcpy(mem->data, coded->data, coded->len); opj_setup_decoder(codec, ¶meters); opj_set_info_handler(codec, &on_msg, NULL); opj_set_warning_handler(codec, &on_msg, NULL); opj_set_error_handler(codec, &on_msg, NULL); if (!opj_read_header(instream, codec, &opjimage)) { ret = -1; goto on_error; } if (!opj_decode(codec, instream, opjimage)) { ret = -1; goto on_error; } if (!opj_end_decompress(codec, instream)) { ret = -1; goto on_error; } if (opjimage) { unsigned char *p; int *r, *b, *g; int area = opjimage->comps[0].w * opjimage->comps[0].h; raw->width = opjimage->comps[0].w; raw->height = opjimage->comps[0].h; raw->len = area * 3; raw->data = malloc(raw->len); b = opjimage->comps[0].data; g = opjimage->comps[1].data; r = opjimage->comps[2].data; p = raw->data; for (i = 0; i < area; ++i) { *p++ = *b++; *p++ = *g++; *p++ = *r++; } } else { ret = -1; } on_error: if (instream) { free_memory_stream(mem); opj_stream_destroy(instream); } opj_destroy_codec(codec); opj_image_destroy(opjimage); return ret; } BIZCHAN_API(void) jpeg2k_decode_free(jpeg2k_raw_image *raw) { free(raw->data); memset(raw, 0, sizeof(jpeg2k_raw_image)); }