36 using namespace kdu_core;
37 using namespace kdu_supp;
52 JP2Encoder::JP2Encoder(
const QString &jp2file,
const unsigned int nsamps,
53 const unsigned int nlines,
const unsigned int nbands,
58 p_sampleDimension = nsamps;
59 p_lineDimension = nlines;
60 p_bandDimension = nbands;
62 if(p_sampleDimension == 0 || p_lineDimension == 0 || p_bandDimension == 0) {
63 string msg =
"Invalid sample/line/band dimensions specified for output file";
67 if(type == Isis::SignedWord) {
72 else if(type == Isis::UnsignedWord) {
77 else if(type == Isis::UnsignedByte) {
83 string msg =
"Invalid pixel type specified for output file";
90 p_resolutionLevels = 1;
92 (p_sampleDimension > p_lineDimension) ? p_lineDimension : p_sampleDimension;
93 while(mindim > 64 && p_resolutionLevels < 32) {
99 p_precinctSize.resize(p_resolutionLevels, 256);
105 p_progressionOrder =
"PCRL";
106 p_codeBlockSize = 64;
107 p_tileSizeWidth = p_sampleDimension;
108 p_tileSizeHeight = p_lineDimension;
112 kdu_customize_errors(Kakadu_Error);
114 std::string msg =
"JPEG2000 has not been enabled with this build of ISIS3";
124 void JP2Encoder::OpenFile() {
127 JP2_Stream =
new jp2_family_tgt();
128 JP2_Stream->open(p_jp2File.toLatin1().data());
131 JP2_Boxes =
new jp2_target();
132 JP2_Boxes->open(JP2_Stream);
142 siz_params *codestream_parameters =
new siz_params();
143 codestream_parameters->set(Sdims, 0, 0, (
int)p_lineDimension);
144 codestream_parameters->set(Sdims, 0, 1, (
int)p_sampleDimension);
145 codestream_parameters->set(Sprecision, 0, 0, (
int)p_pixelBits);
146 codestream_parameters->set(Stiles, 0, 0, (
int)p_tileSizeHeight);
147 codestream_parameters->set(Stiles, 0, 1, (
int)p_tileSizeWidth);
148 codestream_parameters->set(Ssigned, 0, 0, p_signedData);
149 codestream_parameters->set(Scomponents, 0, 0, (
int)p_bandDimension);
150 ostringstream levels;
151 levels <<
"Clevels=" << (p_resolutionLevels - 1);
152 codestream_parameters->parse_string(levels.str().c_str());
153 codestream_parameters->parse_string(
"Creversible=yes");
154 string progression =
"Corder=" + p_progressionOrder;
155 codestream_parameters->parse_string(progression.c_str());
166 int p_flushLines = 0;
167 long long line_bytes = (
long long)p_sampleDimension * p_pixelBytes;
168 if(INCREMENTAL_FLUSH_BYTES < (p_lineDimension * line_bytes)) {
169 p_flushLines = p_tileSizeHeight;
170 while((p_flushLines * line_bytes) > INCREMENTAL_FLUSH_BYTES) {
171 p_flushLines -= 1024;
172 if(p_flushLines < 0) p_flushLines = 0;
176 TLM_segments = (int)(((
double)p_tileSizeHeight / (double)p_flushLines) + 0.5);
177 if(!TLM_segments) TLM_segments = 1;
183 ostringstream segments;
184 segments <<
"ORGgen_tlm=" << TLM_segments;
185 codestream_parameters->parse_string(segments.str().c_str());
188 codestream_parameters->parse_string(
"ORGgen_plt=yes");
191 codestream_parameters->finalize_all();
194 JPEG2000_Codestream =
new kdu_codestream();
195 JPEG2000_Codestream->create(codestream_parameters, JP2_Boxes);
200 codestream_parameters = JPEG2000_Codestream->access_siz();
201 codestream_parameters->parse_string(levels.str().c_str());
202 codestream_parameters->parse_string(
"Creversible=yes");
203 codestream_parameters->parse_string(progression.c_str());
204 codestream_parameters->parse_string(segments.str().c_str());
205 codestream_parameters->parse_string(
"ORGgen_plt=yes");
210 sizes <<
"Cprecincts=";
211 for(
unsigned int i = 0; i < p_precinctSize.size(); i++) {
212 if(i != 0) sizes <<
",";
213 sizes <<
"{" << p_precinctSize[i] <<
"," << p_precinctSize[i] <<
"}";
215 codestream_parameters->parse_string(sizes.str().c_str());
220 size <<
"Cblk={" << p_codeBlockSize <<
"," << p_codeBlockSize <<
"}";
221 codestream_parameters->parse_string(size.str().c_str());
223 codestream_parameters->finalize_all();
226 jp2_dimensions dimensions = JP2_Boxes->access_dimensions();
227 dimensions.init(codestream_parameters);
228 dimensions.finalize_compatibility(codestream_parameters);
231 jp2_colour colour = JP2_Boxes->access_colour();
232 colour.init((p_bandDimension >= 3) ? JP2_sRGB_SPACE : JP2_sLUM_SPACE);
235 JP2_Boxes->write_header();
239 JP2_Boxes->open_codestream();
243 kdu_params *COD = JPEG2000_Codestream->access_siz()->access_cluster(COD_params);
244 if(!(COD->get(Clayers, 0, 0, layers) && (layers > 0)))
245 COD->set(Clayers, 0, 0, layers = 1);
246 kdu_long *layer_sizes =
new kdu_long[layers];
247 memset(layer_sizes, 0,
sizeof(kdu_long)*layers);
250 p_compressor.start(*JPEG2000_Codestream, layers, layer_sizes, NULL, 0,
false,
254 p_stripeHeights =
new int[p_bandDimension];
255 p_maxStripeHeights =
new int[p_bandDimension];
256 p_precisions =
new int[p_bandDimension];
257 p_isSigned =
new bool[p_bandDimension];
258 p_compressor.get_recommended_stripe_heights(MIN_STRIPE_HEIGHT,
259 MAX_STRIPE_HEIGHT, p_stripeHeights, p_maxStripeHeights);
260 for(
unsigned int i = 0; i < p_bandDimension; i++) {
261 p_precisions[i] = p_pixelBits;
262 p_isSigned[i] = p_signedData;
263 p_stripeHeights[i] = 1;
276 void JP2Encoder::Write(
unsigned char **inbuf) {
278 p_writeStripes = p_compressor.push_stripe(inbuf, p_stripeHeights, NULL, NULL,
279 p_precisions, p_flushLines);
291 void JP2Encoder::Write(
short int **inbuf) {
293 p_writeStripes = p_compressor.push_stripe(inbuf, p_stripeHeights, NULL, NULL,
294 p_precisions, p_isSigned, p_flushLines);
302 JP2Encoder::~JP2Encoder() {
304 p_compressor.finish();
305 JPEG2000_Codestream->destroy();
308 delete [] p_stripeHeights;
309 delete [] p_maxStripeHeights;
310 delete [] p_precisions;
311 delete [] p_isSigned;
Namespace for the standard library.
Kakadu error messaging class.
PixelType
Enumerations for Isis Pixel Types.
#define _FILEINFO_
Macro for the filename and line number.
Namespace for ISIS/Bullet specific routines.