-
Notifications
You must be signed in to change notification settings - Fork 104
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented proper quality parameter JPEG support in TIFF image pyramids
- Loading branch information
Showing
9 changed files
with
181 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
#include "JPEGCompression.hpp" | ||
#include <jpeglib.h> | ||
|
||
namespace fast { | ||
|
||
|
||
inline void jpegErrorExit(j_common_ptr cinfo) { | ||
char jpegLastErrorMsg[JMSG_LENGTH_MAX]; | ||
// Create message | ||
( *( cinfo->err->format_message ) ) ( cinfo, jpegLastErrorMsg ); | ||
throw std::runtime_error( jpegLastErrorMsg ); | ||
} | ||
|
||
void* JPEGCompression::decompress(uchar* compressedData, std::size_t bytes, int* widthOut, int* heightOut, uchar* outputBuffer) { | ||
jpeg_decompress_struct cinfo; | ||
jpeg_error_mgr jerr; //error handling | ||
//jerr.error_exit = jpegErrorExit; | ||
cinfo.err = jpeg_std_error(&jerr); | ||
try { | ||
jpeg_create_decompress(&cinfo); | ||
jpeg_mem_src(&cinfo, compressedData, bytes); | ||
int ret = jpeg_read_header(&cinfo, TRUE); | ||
if(ret != JPEG_HEADER_OK) { | ||
throw Exception("Unable to read JPEG header."); | ||
} | ||
jpeg_start_decompress(&cinfo); // output_width and output_height is available after this call | ||
|
||
*widthOut = cinfo.output_width; | ||
*heightOut = cinfo.output_height; | ||
//cinfo.jpeg_color_space = JCS_YCbCr; | ||
//cinfo.jpeg_color_space = JCS_RGB; | ||
if(outputBuffer == nullptr) | ||
outputBuffer = new uchar[(*widthOut) * (*heightOut) * 3]; | ||
unsigned char* line = (uchar*)outputBuffer; | ||
while(cinfo.output_scanline < cinfo.output_height) { | ||
jpeg_read_scanlines (&cinfo, &line, 1); | ||
line += cinfo.output_components*cinfo.output_width; | ||
} | ||
jpeg_finish_decompress(&cinfo); | ||
jpeg_destroy_decompress(&cinfo); | ||
} catch(std::exception &e) { | ||
jpeg_destroy_decompress( &cinfo ); | ||
throw Exception("JPEG error: " + std::string(e.what())); // or return an error code | ||
} | ||
return outputBuffer; | ||
} | ||
|
||
void JPEGCompression::compress(void *data, int width, int height, std::vector<uint8_t> *compressedData, int quality) { | ||
jpeg_compress_struct cinfo; | ||
jpeg_error_mgr jerr; | ||
|
||
cinfo.err = jpeg_std_error(&jerr); | ||
jpeg_create_compress(&cinfo); | ||
|
||
// Set up memory | ||
unsigned long resultSize = 0; | ||
uchar* resultBuffer = nullptr; | ||
jpeg_mem_dest(&cinfo, &resultBuffer, &resultSize); | ||
|
||
// Set parameters | ||
cinfo.image_width = width; | ||
cinfo.image_height = height; | ||
cinfo.input_components = 3; | ||
cinfo.in_color_space = JCS_RGB; | ||
jpeg_set_defaults(&cinfo); | ||
jpeg_set_quality(&cinfo, quality, TRUE); | ||
// Use 4:4:4 subsampling, default is 4:2:0 | ||
//cinfo.comp_info[0].h_samp_factor = cinfo.comp_info[0].v_samp_factor = 1; | ||
|
||
jpeg_start_compress(&cinfo, TRUE); | ||
|
||
auto line = (uchar*)data; | ||
while(cinfo.next_scanline < cinfo.image_height) { | ||
jpeg_write_scanlines(&cinfo, &line, 1); | ||
line += cinfo.image_width*cinfo.input_components; | ||
} | ||
jpeg_finish_compress(&cinfo); | ||
|
||
// Copy buffer after jpeg_finish_compress | ||
// TODO can we avoid this copy? | ||
compressedData->resize(resultSize); | ||
std::copy(resultBuffer, resultBuffer + resultSize, compressedData->begin()); | ||
free(resultBuffer); | ||
jpeg_destroy_compress(&cinfo); | ||
} | ||
JPEGCompression::JPEGCompression() { | ||
|
||
} | ||
|
||
} // End namespace |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#pragma once | ||
|
||
#include <FAST/Data/DataTypes.hpp> | ||
|
||
namespace fast { | ||
|
||
/** | ||
* @brief Class for JPEG image compression | ||
* | ||
* Only supports 8 bit RGB images for now. | ||
*/ | ||
class JPEGCompression { | ||
public: | ||
JPEGCompression(); | ||
void compress(void* data, int width, int height, std::vector<uint8_t>* compressedData, int quality = 90); | ||
/** | ||
* @brief Decompress | ||
* @param compressedData | ||
* @param bytes | ||
* @param widthOut | ||
* @param heightOut | ||
* @param outputBuffer if nullptr this buffer will be used to store data | ||
* @return decompressed data | ||
*/ | ||
void* decompress(uchar* compressedData, std::size_t bytes, int* widthOut, int* heightOut, uchar* outputBuffer = nullptr); | ||
|
||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters