61 struct jpeg_source_mgr pub;
64 boolean start_of_file;
67 typedef my_source_mgr * my_src_ptr;
69 #define INPUT_BUF_SIZE 4096 77 init_source (j_decompress_ptr cinfo)
79 my_src_ptr src = (my_src_ptr) cinfo->src;
85 src->start_of_file = TRUE;
122 fill_input_buffer (j_decompress_ptr cinfo)
124 my_src_ptr src = (my_src_ptr) cinfo->src;
130 if (src->start_of_file)
131 ERREXIT(cinfo, JERR_INPUT_EMPTY);
132 WARNMS(cinfo, JWRN_JPEG_EOF);
134 src->buffer[0] = (JOCTET) 0xFF;
135 src->buffer[1] = (JOCTET) JPEG_EOI;
139 src->pub.next_input_byte = src->buffer;
140 src->pub.bytes_in_buffer = nbytes;
141 src->start_of_file = FALSE;
159 skip_input_data (j_decompress_ptr cinfo,
long num_bytes)
161 my_src_ptr src = (my_src_ptr) cinfo->src;
168 while (num_bytes > (
long) src->pub.bytes_in_buffer) {
169 num_bytes -= (long) src->pub.bytes_in_buffer;
170 fill_input_buffer(cinfo);
175 src->pub.next_input_byte += (size_t) num_bytes;
176 src->pub.bytes_in_buffer -= (size_t) num_bytes;
198 term_source (j_decompress_ptr )
210 jpeg_vl_src (j_decompress_ptr cinfo,
VirtualFile* infile)
221 if (cinfo->src ==
NULL) {
222 cinfo->src = (
struct jpeg_source_mgr *)
223 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
sizeof(my_source_mgr));
224 src = (my_src_ptr) cinfo->src;
225 src->buffer = (JOCTET *)
226 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
230 src = (my_src_ptr) cinfo->src;
231 src->pub.init_source = init_source;
232 src->pub.fill_input_buffer = fill_input_buffer;
233 src->pub.skip_input_data = skip_input_data;
234 src->pub.resync_to_restart = jpeg_resync_to_restart;
235 src->pub.term_source = term_source;
236 src->infile = infile;
237 src->pub.bytes_in_buffer = 0;
238 src->pub.next_input_byte =
NULL;
241 struct my_error_mgr {
242 struct jpeg_error_mgr pub;
243 jmp_buf setjmp_buffer;
246 typedef struct my_error_mgr * my_error_ptr;
253 my_error_exit (j_common_ptr cinfo)
256 my_error_ptr myerr = (my_error_ptr) cinfo->err;
260 (*cinfo->err->output_message) (cinfo);
263 longjmp(myerr->setjmp_buffer, 1);
271 struct jpeg_destination_mgr pub;
275 } my_destination_mgr;
277 typedef my_destination_mgr * my_dest_ptr;
279 #define OUTPUT_BUF_SIZE 4096 287 init_destination (j_compress_ptr cinfo)
289 my_dest_ptr dest = (my_dest_ptr) cinfo->dest;
292 dest->buffer = (JOCTET *)
293 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
296 dest->pub.next_output_byte = dest->buffer;
324 empty_output_buffer (j_compress_ptr cinfo)
326 my_dest_ptr dest = (my_dest_ptr) cinfo->dest;
330 ERREXIT(cinfo, JERR_FILE_WRITE);
332 dest->pub.next_output_byte = dest->buffer;
348 term_destination (j_compress_ptr cinfo)
350 my_dest_ptr dest = (my_dest_ptr)cinfo->dest;
355 if (dest->outfile->write(dest->buffer, datacount) != (
long long)datacount)
356 ERREXIT(cinfo, JERR_FILE_WRITE);
372 jpeg_vl_dest (j_compress_ptr cinfo,
VirtualFile * outfile)
382 if (cinfo->dest ==
NULL) {
383 cinfo->dest = (
struct jpeg_destination_mgr *)
384 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
385 sizeof(my_destination_mgr));
388 dest = (my_dest_ptr) cinfo->dest;
389 dest->pub.init_destination = init_destination;
390 dest->pub.empty_output_buffer = empty_output_buffer;
391 dest->pub.term_destination = term_destination;
392 dest->outfile = outfile;
407 struct jpeg_decompress_struct cinfo;
409 struct my_error_mgr jerr;
417 cinfo.err = jpeg_std_error(&jerr.pub);
418 jerr.pub.error_exit = my_error_exit;
424 if (setjmp(jerr.setjmp_buffer)) {
427 jpeg_destroy_decompress(&cinfo);
434 jpeg_create_decompress(&cinfo);
438 jpeg_vl_src(&cinfo, file);
442 jpeg_read_header(&cinfo, TRUE);
444 if (cinfo.jpeg_color_space == JCS_GRAYSCALE)
446 VL_CHECK(cinfo.out_color_space == JCS_GRAYSCALE)
451 VL_CHECK(cinfo.out_color_space == JCS_RGB)
461 jpeg_start_decompress(&cinfo);
464 row_stride = cinfo.output_width * cinfo.output_components;
466 buffer = (*cinfo.mem->alloc_sarray) ((j_common_ptr)&cinfo, JPOOL_IMAGE, row_stride, 1);
473 while (cinfo.output_scanline < cinfo.output_height)
479 jpeg_read_scanlines(&cinfo, buffer, 1);
481 memcpy(img->
pixels() + img->
pitch()*(img->
height() - cinfo.output_scanline), buffer[0], row_stride);
486 jpeg_finish_decompress(&cinfo);
491 jpeg_destroy_decompress(&cinfo);
518 unsigned char sig1[] = { 0xFF, 0xD8, 0xFF, 0xE0 };
519 unsigned char sig2[] = { 0xFF, 0xD8, 0xFF, 0xE1 };
520 unsigned char signature[4];
521 file->
read(signature,4);
523 bool sig_ok = memcmp(sig1,signature,4) == 0 || memcmp(sig2,signature,4) == 0;
541 int w = src->
width();
543 int d = src->
depth();
557 Log::error(
Say(
"saveJPG('%s'): could not convert image to IT_UNSIGNED_BYTE.\n") << fout->
path() );
567 Log::error(
Say(
"saveJPG('%s'): could not convert image to IF_RGB.\n") << fout->
path() );
584 struct jpeg_compress_struct cinfo;
594 struct jpeg_error_mgr jerr;
596 JSAMPROW row_pointer[1];
606 cinfo.err = jpeg_std_error(&jerr);
609 jpeg_create_compress(&cinfo);
624 jpeg_vl_dest(&cinfo, fout);
631 cinfo.image_width = w;
632 cinfo.image_height = h;
633 cinfo.input_components = 3;
634 cinfo.in_color_space = JCS_RGB;
640 jpeg_set_defaults(&cinfo);
644 jpeg_set_quality(&cinfo, quality, TRUE );
651 jpeg_start_compress(&cinfo, TRUE);
663 while (cinfo.next_scanline < cinfo.image_height)
669 row_pointer[0] = (JSAMPROW)(src->
pixels() + (h - 1 - cinfo.next_scanline) * row_stride);
670 jpeg_write_scanlines(&cinfo, row_pointer, 1);
675 jpeg_finish_compress(&cinfo);
680 jpeg_destroy_compress(&cinfo);
String toLowerCase() const
Returns the lower-case version of a String.
long long read(void *buffer, long long byte_count)
Reads byte_count bytes from a file. Returns the number of bytes actually read.
VLCORE_EXPORT FileSystem * defFileSystem()
Returns the default FileSystem used by VisualizationLibrary.
An abstract class representing a file.
A simple String formatting class.
const unsigned char * pixels() const
Raw pointer to pixels.
void setObjectName(const char *name)
The name of the object, by default set to the object's class name in debug builds.
The String class implements an advanced UTF16 (Unicode BMP) string manipulation engine.
ref< Image > convertType(EImageType new_type) const
Converts the type() of an image.
static void error(const String &message)
Use this function to provide information about run-time errors: file not found, out of memory...
VLCORE_EXPORT ref< Image > loadJPG(VirtualFile *file)
virtual ref< VirtualFile > locateFile(const String &full_path, const String &alternate_path=String()) const
Looks for a VirtualFile on the disk and in the currently active FileSystem.
virtual void close()=0
Closes the file.
const String & path() const
Returns the path of the file.
Visualization Library main namespace.
VLCORE_EXPORT bool isJPG(VirtualFile *file)
VLCORE_EXPORT bool saveJPG(const Image *src, const String &path, int quality=95)
virtual bool open(EOpenMode mode)=0
Opens the file in the specified mode.
ref< Image > convertFormat(EImageFormat new_format) const
Converts the format() of an image.
A VirtualFile that operates on regular disk files.
Implements a generic 1d, 2d, 3d and cubemap image that can have mipmaps.
The ref<> class is used to reference-count an Object.
void allocate2D(int x, int y, int bytealign, EImageFormat format, EImageType type)
std::string toStdString() const
Returns a UTF8 encoded std::string.
bool endsWith(const String &str) const
Returns true if a String ends with the specified String str.
EImageFormat format() const