| 278 | } |
| 279 | |
| 280 | void JPEG2000Codec::encode(char* data, unsigned int& size, const unsigned int& tileSize, const unsigned int& rate, const unsigned int& nrComponents, const pathology::DataType& dataType, const pathology::ColorType& colorSpace) const |
| 281 | { |
| 282 | |
| 283 | int depth = 8; |
| 284 | if (dataType == pathology::DataType::Float || dataType == pathology::DataType::InvalidDataType) { |
| 285 | return; |
| 286 | } |
| 287 | else if (dataType == pathology::DataType::UInt16) { |
| 288 | depth = 16; |
| 289 | } |
| 290 | else if (dataType == pathology::DataType::UInt32 && colorSpace != pathology::ColorType::RGBA) { |
| 291 | depth = 32; |
| 292 | } |
| 293 | |
| 294 | opj_cparameters_t encodeParameters; // compression parameters. |
| 295 | encodeParameters.tcp_mct = nrComponents > 1 ? 1 : 0; //Decide if MCT should be used. |
| 296 | opj_set_default_encoder_parameters(&encodeParameters); |
| 297 | if (rate < 100) |
| 298 | { |
| 299 | encodeParameters.tcp_rates[0] = 100.0f / rate; |
| 300 | encodeParameters.irreversible = 1;//ICT |
| 301 | } |
| 302 | else { |
| 303 | encodeParameters.tcp_rates[0] = 0; |
| 304 | } |
| 305 | encodeParameters.tcp_numlayers = 1; |
| 306 | encodeParameters.cp_disto_alloc = 1; |
| 307 | |
| 308 | //Set the image components parameters. |
| 309 | opj_image_cmptparm_t*componentParameters = new opj_image_cmptparm_t[nrComponents]; |
| 310 | for (unsigned int cnt = 0; cnt < nrComponents; cnt++) |
| 311 | { |
| 312 | componentParameters[cnt].dx = encodeParameters.subsampling_dx; |
| 313 | componentParameters[cnt].dy = encodeParameters.subsampling_dy; |
| 314 | componentParameters[cnt].h = tileSize; |
| 315 | componentParameters[cnt].w = tileSize; |
| 316 | componentParameters[cnt].prec = depth; |
| 317 | componentParameters[cnt].bpp = depth; |
| 318 | componentParameters[cnt].sgnd = 0; |
| 319 | componentParameters[cnt].x0 = 0; |
| 320 | componentParameters[cnt].y0 = 0; |
| 321 | } |
| 322 | // Also set the colorspace |
| 323 | OPJ_COLOR_SPACE jpegColorSpace = OPJ_CLRSPC_GRAY;// Set the default. |
| 324 | if (colorSpace == pathology::ColorType::RGB || colorSpace == pathology::ColorType::RGBA) { |
| 325 | jpegColorSpace = OPJ_CLRSPC_SRGB; |
| 326 | } |
| 327 | |
| 328 | // Get a J2K compressor handle. |
| 329 | opj_codec_t* encoder = opj_create_compress(OPJ_CODEC_J2K); |
| 330 | |
| 331 | //Catch events using our callbacks and give a local context. |
| 332 | opj_set_info_handler(encoder, info_callback, NULL); |
| 333 | opj_set_warning_handler(encoder, warning_callback, NULL); |
| 334 | opj_set_error_handler(encoder, error_callback, NULL); |
| 335 | |
| 336 | //Set the "OpenJpeg like" stream data. |
| 337 | opj_memory_stream encodingBuffer; |
no test coverage detected