/* IIPImage Server: OpenJPEG JPEG2000 handler Copyright (C) 2019-2026 Ruven Pillay. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ #include "OpenJPEGImage.h" #include "Logger.h" #include #include #ifdef OPENJPEG_DEBUG #include "Timer.h" #endif // Detect High Throughput JPEG2000 #define J2K_CCP_CBLKSTY_HT 0x40 #define J2K_CCP_CBLKSTY_HTMIXED 0x80 using namespace std; // Reference our logging object extern Logger logfile; // Handle info, warning and error messages from OpenJPEG static void error_callback( const char* msg, void* ){ stringstream ss; ss << "OpenJPEG error :: " << msg; throw file_error( ss.str() ); } #ifdef OPENJPEG_DEBUG static void warning_callback( const char* msg, void* ){ if( IIPImage::logging ) logfile << "OpenJPEG warning :: " << msg << endl; } static void info_callback( const char* msg, void* ){ if( IIPImage::logging ) logfile << "OpenJPEG info :: " << msg; } #endif void OpenJPEGImage::openImage() { string filename = getFileName( currentX, currentY ); // Update our timestamp updateTimestamp( filename ); // Create decompression codec _codec = opj_create_decompress( OPJ_CODEC_JP2 ); // Set info, warning and error handlers for codec - these need to be done after codec initialization #ifdef OPENJPEG_DEBUG opj_set_info_handler( _codec, info_callback, NULL ); opj_set_warning_handler( _codec, warning_callback, NULL ); #endif opj_set_error_handler( _codec, error_callback, NULL ); // Setup decoder opj_dparameters_t parameters; // Set default decoder parameters opj_set_default_decoder_parameters( ¶meters ); if( !opj_setup_decoder( _codec, ¶meters ) ){ throw file_error( "OpenJPEG :: openImage() :: error setting up decoder" ); } #ifdef OPENJPEG_DEBUG Timer timer; timer.start(); #endif // Open the JPEG2000 file in read mode if( !(_stream = opj_stream_create_default_file_stream( filename.c_str(), true) ) ){ throw file_error( "OpenJPEG :: Unable to open '" + filename + "'" ); } #ifdef OPENJPEG_DEBUG logfile << "OpenJPEG :: openImage() :: " << "Stream created" << endl; #endif // Read header if( !opj_read_header( _stream, _codec, &_image ) ){ throw file_error( "OpenJPEG :: process() :: opj_read_header() failed" ); } #ifdef OPENJPEG_DEBUG logfile << "OpenJPEG :: openImage() :: " << "Header read" << endl; #endif // Load our metadata if not already loaded if( bpc == 0 ) loadImageInfo( currentX, currentY ); #ifdef OPENJPEG_DEBUG logfile << "OpenJPEG :: openImage() :: " << timer.getTime() << " microseconds" << endl; #endif } void OpenJPEGImage::closeImage() { #ifdef OPENJPEG_DEBUG Timer timer; timer.start(); #endif if( _codec && _stream ) opj_end_decompress( _codec, _stream ); if( _codec ){ opj_destroy_codec( _codec ); _codec = NULL; } if( _stream ){ opj_stream_destroy( _stream ); _stream = NULL; } if( _image ){ opj_image_destroy( _image ); _image = NULL; } #ifdef OPENJPEG_DEBUG logfile << "OpenJPEG :: closeImage() :: " << timer.getTime() << " microseconds" << endl; #endif } void OpenJPEGImage::loadImageInfo( int seq, int ang ) { #ifdef OPENJPEG_DEBUG Timer timer; timer.start(); #endif // Get info structure opj_codestream_info_v2_t* cst_info = opj_get_cstr_info( _codec ); numResolutions = cst_info->m_default_tile_info.tccp_info[0].numresolutions; quality_layers = cst_info->m_default_tile_info.numlayers; // High Throughput JPEG2000 #ifdef J2K_CCP_CBLKSTY_HT if( (cst_info->m_default_tile_info.tccp_info[0].cblksty & J2K_CCP_CBLKSTY_HT) != 0 || (cst_info->m_default_tile_info.tccp_info[0].cblksty & J2K_CCP_CBLKSTY_HTMIXED) != 0 ){ logfile << "OpenJPEG :: HTJ2K codestream" << endl; } #endif // Close our info structure opj_destroy_cstr_info( &cst_info ); channels = _image->numcomps; bpc = _image->comps[0].prec; // Empty any existing list of available resolution sizes image_widths.clear(); image_heights.clear(); // Save first resolution level unsigned int w = _image->x1 - _image->x0; unsigned int h = _image->y1 - _image->y0; image_widths.push_back(w); image_heights.push_back(h); #ifdef OPENJPEG_DEBUG logfile << "OpenJPEG :: DWT Levels: " << numResolutions << endl; logfile << "OpenJPEG :: Resolution : " << w << "x" << h << endl; #endif // Loop through each resolution and calculate the image dimensions - // for JPEG2000, these are defined as ceil(x/2) for( unsigned int c=1; ctile_widths[0]) || (h>tile_heights[0]) ){ n++; w = ceil( w / 2.0 ); h = ceil( h / 2.0 ); if( n > numResolutions ){ image_widths.push_back(w); image_heights.push_back(h); } } if( n > numResolutions ){ #ifdef OPENJPEG_DEBUG logfile << "OpenJPEG :: Warning! Insufficient resolution levels in JPEG2000 stream. Will generate " << n-numResolutions << " extra levels dynamically -" << endl << "OpenJPEG :: However, you are advised to regenerate the file with at least " << n << " levels" << endl; #endif virtual_levels = n-numResolutions; } numResolutions = n; // Need to assign basic colorspace information if( channels == 1 ){ colorspace = (bpc==1)? ColorSpace::BINARY : ColorSpace::GREYSCALE; } else if( channels == 3 ) colorspace = ColorSpace::sRGB; // Color space details string cs; switch( _image->color_space ){ case OPJ_CLRSPC_SRGB: cs = "sRGB"; break; case OPJ_CLRSPC_SYCC: cs = "YUV"; break; case OPJ_CLRSPC_CMYK: cs = "CMYK"; break; case OPJ_CLRSPC_EYCC: cs = "e-YCC"; break; case OPJ_CLRSPC_UNSPECIFIED: cs = "Unspecified"; break; default: cs = "Unknown"; break; } #ifdef OPENJPEG_DEBUG logfile << "OpenJPEG :: " << bpc << " bit data" << endl << "OpenJPEG :: " << channels << " channels" << endl << "OpenJPEG :: color space: " << cs << endl << "OpenJPEG :: " << quality_layers << " quality layers detected" << endl; #endif // For bilevel images, force channels to 1 as we sometimes come across such images which claim 3 channels if( bpc == 1 ) channels = 1; // Get the max and min values for our data type for( unsigned int i=0; i 8 && bpc < 16 ) max.push_back( 1< 8 ) obpc = 16; else if( bpc <= 8 ) obpc = 8; #ifdef OPENJPEG_DEBUG Timer timer; timer.start(); #endif if( res >= numResolutions ){ ostringstream tile_no; tile_no << "OpenJPEG :: Asked for non-existent resolution: " << res; throw file_error( tile_no.str() ); } int vipsres = getNativeResolution( res ); unsigned int tw = tile_widths[0]; unsigned int th = tile_heights[0]; // Get the width and height for last row and column tiles unsigned int rem_x = image_widths[vipsres] % tile_widths[0]; unsigned int rem_y = image_heights[vipsres] % tile_heights[0]; // Calculate the number of tiles in each direction unsigned int ntlx = (image_widths[vipsres] / tile_widths[0]) + (rem_x == 0 ? 0 : 1); unsigned int ntly = (image_heights[vipsres] / tile_heights[0]) + (rem_y == 0 ? 0 : 1); // Check whether requested tile exists if( tile >= ntlx*ntly ){ ostringstream tile_no; tile_no << "OpenJPEG :: Asked for non-existent tile: " << tile; throw file_error( tile_no.str() ); } // Alter the tile size if it's in the last column if( ( tile % ntlx == ntlx - 1 ) && ( rem_x != 0 ) ) { tw = rem_x; } // Alter the tile size if it's in the bottom row if( ( tile / ntlx == ntly - 1 ) && rem_y != 0 ) { th = rem_y; } // Calculate the pixel offsets for this tile int xoffset = (tile % ntlx) * tile_widths[0]; int yoffset = (unsigned int) floor((double)(tile/ntlx)) * tile_heights[0]; #ifdef OPENJPEG_DEBUG logfile << "OpenJPEG :: Tile size: " << tw << "x" << th << " @" << channels << endl; #endif // OpenJPEG only supports 8 or 16 bit images if( !( (obpc == 8) || (obpc == 16) ) ) throw file_error( "OpenJPEG :: Unsupported number of bits" ); // Create our Rawtile object and initialize with data RawTile rawtile( tile, res, seq, ang, tw, th, channels, obpc ); rawtile.filename = getImagePath(); rawtile.timestamp = timestamp; rawtile.allocate(); // Process the tile size_t bytes = process( res, layers, xoffset, yoffset, tw, th, rawtile.data ); rawtile.dataLength = bytes; #ifdef OPENJPEG_DEBUG logfile << "OpenJPEG :: getTile() :: " << timer.getTime() << " microseconds" << endl; #endif return rawtile; } // Get an entire region and not just a tile RawTile OpenJPEGImage::getRegion( int ha, int va, unsigned int res, int layers, int x, int y, unsigned int w, unsigned int h ){ // Scale up our output bit depth to the nearest factor of 8 unsigned int obpc = bpc; if( bpc <= 16 && bpc > 8 ) obpc = 16; else if( bpc <= 8 ) obpc = 8; #ifdef OPENJPEG_DEBUG Timer timer; timer.start(); #endif // OpenJPEG only supports 8 or 16 bit images if( !( (obpc == 8) || (obpc == 16) ) ) throw file_error( "OpenJPEG :: Unsupported number of bits" ); RawTile rawtile( 0, res, ha, va, w, h, channels, obpc ); rawtile.filename = getImagePath(); rawtile.timestamp = timestamp; rawtile.allocate(); size_t bytes = process( res, layers, x, y, w, h, rawtile.data ); rawtile.dataLength = bytes; #ifdef OPENJPEG_DEBUG logfile << "OpenJPEG :: getRegion() :: " << timer.getTime() << " microseconds" << endl; #endif return rawtile; } // Main processing function size_t OpenJPEGImage::process( unsigned int res, int layers, int xoffset, int yoffset, unsigned int tw, unsigned int th, void *d ) { // Unfortunately, it's not currently possible to re-use OpenJPEG's stream or image structures, // so re-open if necessary if( !_image ) openImage(); // Scale up our output bit depth to the nearest factor of 8 unsigned int obpc = bpc; if( bpc <= 16 && bpc > 8 ) obpc = 16; else if( bpc <= 8 ) obpc = 8; unsigned int factor = 1; // Downsampling factor - set it to default value int vipsres = getNativeResolution( res ); // Reverse resolution number // Calculate number of extra resolutions needed that have not been encoded in the image if( res < virtual_levels ){ factor = 2 * (virtual_levels - res); xoffset *= factor; yoffset *= factor; tw *= factor; th *= factor; // Set our resolution level back to the smallest original resolution vipsres = numResolutions - 1 - virtual_levels; #ifdef OPENJPEG_DEBUG logfile << "OpenJPEG :: using smallest existing resolution " << virtual_levels << endl; #endif } // Set the number of layers to half of the number of detected layers if we have not set the // layers parameter manually. If layers is set to less than 0, use all layers. if( layers < 0 ) layers = quality_layers; else if( layers == 0 ) layers = ceil( quality_layers/2.0 ); // Also make sure we have at least 1 layer if( layers < 1 ) layers = 1; // Set number of quality layer and resolution opj_dparameters_t params; params.cp_layer = layers; params.cp_reduce = vipsres; if( !opj_setup_decoder( _codec, ¶ms ) ){ throw file_error( "OpenJPEG :: process() :: opj_setup_decoder() failed" ); } // Set resolution - hack for openjpeg up to 2.2.0 for( OPJ_UINT32 i = 0; i < _image->numcomps; i++ ){ _image->comps[i].factor = vipsres; } // Requested image location and size projected onto full resolution image unsigned int x0 = xoffset << vipsres; unsigned int y0 = yoffset << vipsres; unsigned int w0 = (xoffset + tw) << vipsres; unsigned int h0 = (yoffset + th) << vipsres; // Make sure we don't overrun the dimensions of the full-size image. // Note that w0 and h0 are the end coordinates of the requested region, // not its width and height. if( x0 > image_widths[0] ) x0 = image_widths[0] - 1; if( y0 > image_heights[0] ) y0 = image_heights[0] - 1; if( w0 > image_widths[0] ) w0 = image_widths[0]; if( h0 > image_heights[0] ) h0 = image_heights[0]; #ifdef OPENJPEG_DEBUG logfile << "OpenJPEG :: decoding " << layers << " quality layers" << endl; logfile << "OpenJPEG :: requested region at requested resolution: position: " << xoffset << "x" << yoffset << ". size: " << tw << "x" << th << endl; logfile << "OpenJPEG :: region size mapped to full resolution: " << (tw<icc_profile_len; const char* icc = (const char*) _image->icc_profile_buf; if( icc_length > 0 ) metadata.insert( {"icc",string(icc,icc_length)} ); #ifdef OPENJPEG_DEBUG if( icc_length > 0 ){ logfile << "OpenJPEG :: ICC profile detected with size " << icc_length << endl; } #endif // Copy our decoded data by looping over all pixels size_t n = 0; for( unsigned int j=0; jcomps[k].data[index]) & 0x0000ffff); if( bpc == 10 ) v = (v << 6) | (v >> 4); else if( bpc == 12 ) v = (v << 4) | (v >> 8); else if( bpc == 14 ) v = (v << 2) | (v >> 12); ((unsigned short*)d)[n++] = v; } else{ // 1 bit binary (bi-level), 2 bit, 4 bit and 6 bit images need to be scaled up to 8 bit range unsigned char v = ((_image->comps[k].data[index]) & 0x00000ff); if( bpc == 1 ) v *= 255; else if( bpc == 2 ) v *= 85; else if( bpc == 4 ) v *= 17; else if( bpc == 6 ) v = (v << 2) | (v >> 4); ((unsigned char*)d)[n++] = v; } } } } // We need to close the image here in case we try to use the OpenJPEG // stream or image structures multiple times in the same request pipeline closeImage(); // Return number of bytes return n; }