1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545
|
/*=========================================================================
Program: Insight Segmentation & Registration Toolkit
Module: $RCSfile: itkJPEGImageIO.cxx,v $
Language: C++
Date: $Date: 2007-07-28 12:35:25 $
Version: $Revision: 1.22 $
Copyright (c) Insight Software Consortium. All rights reserved.
See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
=========================================================================*/
#ifdef _MSC_VER
// warning C4611: interaction between '_setjmp' and C++ object
// destruction is non-portable
#pragma warning( disable : 4611 )
#endif
#include "itkJPEGImageIO.h"
#include "itkRGBPixel.h"
#include "itkRGBAPixel.h"
#include <stdio.h>
extern "C"
{
// The regular jpeg lossy lib is the 8bits one:
#include <itkjpeg/8/jpeglib.h>
#include <setjmp.h>
}
// create an error handler for jpeg that
// can longjmp out of the jpeg library
struct itk_jpeg_error_mgr
{
struct jpeg_error_mgr pub; /* "public" fields */
jmp_buf setjmp_buffer; /* for return to caller */
};
extern "C" {
METHODDEF(void) itk_jpeg_error_exit (j_common_ptr cinfo)
{
/* cinfo->err really points to a itk_jpeg_error_mgr struct, so coerce pointer */
itk_jpeg_error_mgr *myerr = (itk_jpeg_error_mgr*) cinfo->err;
/* Always display the message. */
/* We could postpone this until after returning, if we chose. */
(*cinfo->err->output_message) (cinfo);
jpeg_abort(cinfo); /* clean up libjpeg state */
/* Return control to the setjmp point */
longjmp(myerr->setjmp_buffer, 1);
}
METHODDEF(void) itk_jpeg_output_message (j_common_ptr)
{
#if 0
char buffer[JMSG_LENGTH_MAX];
/* Create the message */
(*cinfo->err->format_message) (cinfo, buffer);
// Custom display message, we could be more fancy and throw an exception:
std::cerr << "output:" << buffer << std::endl;
#endif
}
}
namespace itk
{
// simple class to call fopen on construct and
// fclose on destruct
class JPEGFileWrapper
{
public:
JPEGFileWrapper(const char * const fname, const char * const openMode):m_FilePointer(NULL)
{
m_FilePointer = fopen(fname, openMode);
}
virtual ~JPEGFileWrapper()
{
if(m_FilePointer!=NULL)
{
fclose(m_FilePointer);
}
}
FILE* m_FilePointer;
};
bool JPEGImageIO::CanReadFile(const char* file)
{
// First check the extension
std::string filename = file;
if( filename == "" )
{
itkDebugMacro(<<"No filename specified.");
return false;
}
bool extensionFound = false;
std::string::size_type JPEGPos = filename.rfind(".jpeg");
if ((JPEGPos != std::string::npos)
&& (JPEGPos == filename.length() - 5))
{
extensionFound = true;
}
JPEGPos = filename.rfind(".JPEG");
if ((JPEGPos != std::string::npos)
&& (JPEGPos == filename.length() - 5))
{
extensionFound = true;
}
JPEGPos = filename.rfind(".jpg");
if ((JPEGPos != std::string::npos)
&& (JPEGPos == filename.length() - 4))
{
extensionFound = true;
}
JPEGPos = filename.rfind(".JPG");
if ((JPEGPos != std::string::npos)
&& (JPEGPos == filename.length() - 4))
{
extensionFound = true;
}
if( !extensionFound )
{
itkDebugMacro(<<"The filename extension is not recognized");
return false;
}
// Now check the file header
JPEGFileWrapper JPEGfp(file,"rb");
if(JPEGfp.m_FilePointer==NULL)
{
return false;
}
// read the first two bytes
unsigned char magic[2];
int n = static_cast<int>(fread(magic, sizeof(magic), 1, JPEGfp.m_FilePointer));
if (n != 1)
{
return false;
}
// check for the magic stuff:
// 0xFF followed by 0xD8
if( magic[0] != 0xFF ||
magic[1] != 0xD8 )
{
return false;
}
// go back to the start of the file
fseek(JPEGfp.m_FilePointer, 0, SEEK_SET);
// magic number is ok, try and read the header
struct itk_jpeg_error_mgr jerr;
struct jpeg_decompress_struct cinfo;
cinfo.err = jpeg_std_error(&jerr.pub);
// for any jpeg error call itk_jpeg_error_exit
jerr.pub.error_exit = itk_jpeg_error_exit;
// for any output message call itk_jpeg_output_message
jerr.pub.output_message = itk_jpeg_output_message;
// set the jump point, if there is a jpeg error or warning
// this will evaluate to true
if (setjmp(jerr.setjmp_buffer))
{
// clean up
jpeg_destroy_decompress(&cinfo);
// this is not a valid jpeg file
return false;
}
/* Now we can initialize the JPEG decompression object. */
jpeg_create_decompress(&cinfo);
/* Step 2: specify data source (eg, a file) */
jpeg_stdio_src(&cinfo, JPEGfp.m_FilePointer);
/* Step 3: read file parameters with jpeg_read_header() */
jpeg_read_header(&cinfo, TRUE);
// if no errors have occurred yet, then it must be jpeg
jpeg_destroy_decompress(&cinfo);
return true;
}
void JPEGImageIO::ReadVolume(void*)
{
}
//-----------------------------------------------------------------------------
void JPEGImageIO::Read(void* buffer)
{
unsigned int ui;
// use this class so return will call close
JPEGFileWrapper JPEGfp(this->GetFileName(),"rb");
FILE* fp = JPEGfp.m_FilePointer;
if(!fp)
{
itkExceptionMacro("Error JPEGImageIO could not open file: "
<< this->GetFileName());
return;
}
// create jpeg decompression object and error handler
struct jpeg_decompress_struct cinfo;
struct itk_jpeg_error_mgr jerr;
cinfo.err = jpeg_std_error(&jerr.pub);
// for any jpeg error call itk_jpeg_error_exit
jerr.pub.error_exit = itk_jpeg_error_exit;
// for any output message call itk_jpeg_output_message
jerr.pub.output_message = itk_jpeg_output_message;
if (setjmp(jerr.setjmp_buffer))
{
// clean up
jpeg_destroy_decompress(&cinfo);
itkExceptionMacro("libjpeg could not read file: "
<< this->GetFileName());
// this is not a valid jpeg file
return;
}
jpeg_create_decompress(&cinfo);
// set the source file
jpeg_stdio_src(&cinfo, fp);
// read the header
jpeg_read_header(&cinfo, TRUE);
// prepare to read the bulk data
jpeg_start_decompress(&cinfo);
unsigned long rowbytes = cinfo.output_components * cinfo.output_width;
JSAMPLE *tempImage = static_cast<JSAMPLE*>(buffer);
JSAMPROW *row_pointers = new JSAMPROW [cinfo.output_height];
for (ui = 0; ui < cinfo.output_height; ++ui)
{
row_pointers[ ui ] = tempImage + rowbytes*ui;
}
// read the bulk data
unsigned int remainingRows;
while (cinfo.output_scanline < cinfo.output_height)
{
remainingRows = cinfo.output_height - cinfo.output_scanline;
jpeg_read_scanlines(&cinfo, &row_pointers[cinfo.output_scanline],
remainingRows);
}
// finish the decompression step
jpeg_finish_decompress(&cinfo);
// destroy the decompression object
jpeg_destroy_decompress(&cinfo);
delete [] row_pointers;
}
JPEGImageIO::JPEGImageIO()
{
this->SetNumberOfDimensions(2);
m_PixelType = SCALAR;
// 12bits is not working right now, but this should be doable
#if BITS_IN_JSAMPLE == 8
m_ComponentType = UCHAR;
#else
m_ComponentType = UINT;
#endif
m_UseCompression = false;
m_Quality = 95;
m_Progressive = true;
m_Spacing[0] = 1.0;
m_Spacing[1] = 1.0;
m_Origin[0] = 0.0;
m_Origin[1] = 0.0;
}
JPEGImageIO::~JPEGImageIO()
{
}
void JPEGImageIO::PrintSelf(std::ostream& os, Indent indent) const
{
Superclass::PrintSelf(os, indent);
os << indent << "Quality : " << m_Quality << "\n";
os << indent << "Progressive : " << m_Progressive << "\n";
}
void JPEGImageIO::ReadImageInformation()
{
m_Spacing[0] = 1.0; // We'll look for JPEG pixel size information later,
m_Spacing[1] = 1.0; // but set the defaults now
m_Origin[0] = 0.0;
m_Origin[1] = 0.0;
// use this class so return will call close
JPEGFileWrapper JPEGfp(m_FileName.c_str(),"rb");
FILE* fp = JPEGfp.m_FilePointer;
if(!fp)
{
itkExceptionMacro("Error JPEGImageIO could not open file: "
<< this->GetFileName());
return;
}
// create jpeg decompression object and error handler
struct jpeg_decompress_struct cinfo;
struct itk_jpeg_error_mgr jerr;
cinfo.err = jpeg_std_error(&jerr.pub);
jerr.pub.error_exit = itk_jpeg_error_exit;
if (setjmp(jerr.setjmp_buffer))
{
// clean up
jpeg_destroy_decompress(&cinfo);
// this is not a valid jpeg file
itkExceptionMacro("Error JPEGImageIO could not open file: "
<< this->GetFileName());
return;
}
jpeg_create_decompress(&cinfo);
// set the source file
jpeg_stdio_src(&cinfo, fp);
// read the header
jpeg_read_header(&cinfo, TRUE);
// force the output image size to be calculated (we could have used
// cinfo.image_height etc. but that would preclude using libjpeg's
// ability to scale an image on input).
jpeg_calc_output_dimensions(&cinfo);
// pull out the width/height
this->SetNumberOfDimensions(2);
m_Dimensions[0] = cinfo.output_width;
m_Dimensions[1] = cinfo.output_height;
this->SetNumberOfComponents(cinfo.output_components);
switch (this->GetNumberOfComponents())
{
case 1: m_PixelType = SCALAR;
break;
case 2: m_PixelType = VECTOR;
break;
case 3: m_PixelType = RGB;
break;
case 4: m_PixelType = RGBA;
break;
}
// close the file
jpeg_destroy_decompress(&cinfo);
return;
}
bool JPEGImageIO::CanWriteFile( const char * name )
{
std::string filename = name;
if (filename == "")
{
return false;
}
std::string::size_type JPEGPos = filename.rfind(".jpeg");
if ( (JPEGPos != std::string::npos)
&& (JPEGPos == filename.length() - 5) )
{
return true;
}
JPEGPos = filename.rfind(".JPEG");
if ( (JPEGPos != std::string::npos)
&& (JPEGPos == filename.length() - 5) )
{
return true;
}
JPEGPos = filename.rfind(".jpg");
if ( (JPEGPos != std::string::npos)
&& (JPEGPos == filename.length() - 4) )
{
return true;
}
JPEGPos = filename.rfind(".JPG");
if ( (JPEGPos != std::string::npos)
&& (JPEGPos == filename.length() - 4) )
{
return true;
}
return false;
}
void JPEGImageIO::WriteImageInformation(void)
{
}
void JPEGImageIO::Write(const void* buffer)
{
ImageIORegion ioRegion = this->GetIORegion();
// Make sure the region to be written is 2D
if ( ioRegion.GetRegionDimension() != 2 )
{
itkExceptionMacro(<<"JPEG Writer can only write 2-dimensional images");
}
if ( this->GetComponentType() != UCHAR
&& this->GetComponentType() != UINT)
{
itkExceptionMacro(<<"JPEG supports unsigned char/int only");
}
this->WriteSlice(m_FileName, buffer);
}
void JPEGImageIO::WriteSlice(std::string& fileName, const void* buffer)
{
const JSAMPLE *outPtr = ( (const JSAMPLE*) buffer);
// use this class so return will call close
JPEGFileWrapper JPEGfp(fileName.c_str(),"wb");
FILE* fp = JPEGfp.m_FilePointer;
if(!fp)
{
itkExceptionMacro("Unable to open file " << fileName);
}
// Call the correct templated function for the output
unsigned int ui;
// overriding jpeg_error_mgr so we don't exit when an error happens
// Create the jpeg compression object and error handler
//struct jpeg_compress_struct cinfo;
//struct itk_jpeg_error_mgr jerr;
struct itk_jpeg_error_mgr jerr;
struct jpeg_compress_struct cinfo;
cinfo.err = jpeg_std_error(&jerr.pub);
// set the jump point, if there is a jpeg error or warning
// this will evaluate to true
if (setjmp(jerr.setjmp_buffer))
{
jpeg_destroy_compress(&cinfo);
itkExceptionMacro(<<"JPEG : Out of disk space");
return;
}
jpeg_create_compress(&cinfo);
// set the destination file
//struct jpeg_destination_mgr compressionDestination;
jpeg_stdio_dest(&cinfo, fp);
// set the information about image
unsigned int width, height;
width = m_Dimensions[0];
height = m_Dimensions[1];
cinfo.image_width = width;
cinfo.image_height = height;
cinfo.input_components = this->GetNumberOfComponents();
unsigned int numComp = this->GetNumberOfComponents();
switch (cinfo.input_components)
{
case 1: cinfo.in_color_space = JCS_GRAYSCALE;
break;
case 3: cinfo.in_color_space = JCS_RGB;
break;
default: cinfo.in_color_space = JCS_UNKNOWN;
break;
}
// set the compression parameters
jpeg_set_defaults(&cinfo); // start with reasonable defaults
jpeg_set_quality(&cinfo, m_Quality, TRUE);
if (m_Progressive)
{
jpeg_simple_progression(&cinfo);
}
// start compression
jpeg_start_compress(&cinfo, TRUE);
// write the data. in jpeg, the first row is the top row of the image
JSAMPROW *row_pointers = new JSAMPROW [height];
int rowInc = numComp*width;
for (ui = 0; ui < height; ui++)
{
row_pointers[ui] = const_cast<JSAMPROW>(outPtr);
outPtr = const_cast<JSAMPLE *>(outPtr) + rowInc;
}
jpeg_write_scanlines(&cinfo, row_pointers, height);
if (fflush(fp) == EOF)
{
itkExceptionMacro(<<"JPEG : Out of disk space");
return;
}
// finish the compression
jpeg_finish_compress(&cinfo);
// clean up and close the file
delete [] row_pointers;
jpeg_destroy_compress(&cinfo);
}
} // end namespace itk
|