27 #include "precompiled.h"
43 # pragma warning(disable: 4611)
48 cassert(
sizeof(JOCTET) == 1 && CHAR_BIT == 8);
58 struct jpeg_source_mgr pub;
95 SrcPtr src = (
SrcPtr)cinfo->src;
96 static const JOCTET eoi[2] = { 0xFF, JPEG_EOI };
104 WARNMS(cinfo, JWRN_JPEG_EOF);
106 src->pub.next_input_byte = eoi;
107 src->pub.bytes_in_buffer = 2;
120 size_t skip_count = (
size_t)num_bytes;
132 if(skip_count > src->
pub.bytes_in_buffer)
133 skip_count = src->
pub.bytes_in_buffer;
135 src->
pub.bytes_in_buffer -= skip_count;
136 src->
pub.next_input_byte += skip_count;
178 ERREXIT(cinfo, JERR_INPUT_EMPTY);
190 cinfo->src = (
struct jpeg_source_mgr*)
191 (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_PERMANENT,
199 src->
pub.resync_to_restart = jpeg_resync_to_restart;
206 src->
pub.bytes_in_buffer = size;
207 src->
pub.next_input_byte = (JOCTET*)data;
217 struct jpeg_destination_mgr pub;
226 #define OUTPUT_BUF_SIZE 64*KiB
235 void* start = da->
base + da->cur_size;
238 ERREXIT(cinfo, JERR_FILE_WRITE);
240 dst->
pub.next_output_byte = (JOCTET*)start;
312 if (cinfo->dest == NULL) {
313 cinfo->dest = (
struct jpeg_destination_mgr*)(*cinfo->mem->alloc_small)
314 ((j_common_ptr)cinfo, JPOOL_PERMANENT,
sizeof(
DstMgr));
343 struct jpeg_error_mgr
pub;
350 char msg[JMSG_LENGTH_MAX];
366 (*cinfo->err->output_message)(cinfo);
384 if(err_mgr->
msg[0] !=
'\0')
388 (*cinfo->err->format_message)(cinfo, err_mgr->
msg);
395 jpeg_std_error(&
pub);
450 (void)jpeg_read_header(cinfo, TRUE);
455 cinfo->out_color_space = JCS_RGB;
456 if(cinfo->num_components == 1)
459 cinfo->out_color_space = JCS_GRAYSCALE;
463 cinfo->dct_method = JDCT_IFAST;
464 cinfo->do_fancy_upsampling = FALSE;
470 (void)jpeg_start_decompress(cinfo);
473 int w = cinfo->output_width;
474 int h = cinfo->output_height;
475 int bpp = cinfo->output_components * 8;
478 const size_t pitch = w * bpp / 8;
479 const size_t imgSize = pitch * h;
487 JSAMPARRAY row = (JSAMPARRAY)&rows[0];
488 JDIMENSION lines_left = h;
489 while(lines_left != 0)
491 JDIMENSION lines_read = jpeg_read_scanlines(cinfo, row, lines_left);
493 lines_left -= lines_read;
500 (void)jpeg_finish_decompress(cinfo);
503 if(cinfo->err->num_warnings != 0)
525 cinfo->image_width = (JDIMENSION)t->
w;
526 cinfo->image_height = (JDIMENSION)t->
h;
527 cinfo->input_components = (int)t->
bpp / 8;
528 cinfo->in_color_space = (t->
bpp == 8)? JCS_GRAYSCALE : JCS_RGB;
530 jpeg_set_defaults(cinfo);
535 jpeg_start_compress(cinfo, TRUE);
540 const size_t pitch = t->
w * t->
bpp / 8;
546 JSAMPARRAY row = (JSAMPARRAY)&rows[0];
547 JDIMENSION lines_left = (JDIMENSION)t->
h;
548 while(lines_left != 0)
550 JDIMENSION lines_read = jpeg_write_scanlines(cinfo, row, lines_left);
552 lines_left -= lines_read;
557 jpeg_finish_compress(cinfo);
560 if(cinfo->err->num_warnings != 0)
572 return (file[0] == 0xff && file[1] == 0xd8);
578 return extension == L
".jpg" || extension == L
".jpeg";
592 struct jpeg_decompress_struct cinfo;
595 if(setjmp(jerr.call_site))
598 jpeg_create_decompress(&cinfo);
602 jpeg_destroy_decompress(&cinfo);
613 struct jpeg_compress_struct cinfo;
616 if(setjmp(jerr.call_site))
619 jpeg_create_compress(&cinfo);
623 jpeg_destroy_compress(&cinfo);
struct jpeg_error_mgr pub
size_t ofs
offset to image data in file.
static const size_t pageSize
#define UNUSED(param)
mark a function parameter as unused and avoid the corresponding compiler warning. ...
#define WARN_IF_ERR(expression)
const Status TEX_CODEC_CANNOT_HANDLE
static boolean dst_empty_output_buffer(j_compress_ptr cinfo)
Status tex_transform_to(Tex *t, size_t new_flags)
Change <t>'s pixel format (2nd version) (note: this is equivalent to tex_transform(t, t->flags^new_flags).
static bool jpg_is_hdr(const u8 *file)
provides a memory range that can be expanded but doesn't waste physical memory or relocate itself...
shared_ptr< u8 > data
file buffer or image data.
indicates the image is 8bpp greyscale.
static Status jpg_encode_impl(Tex *t, jpeg_compress_struct *cinfo, DynArray *da)
static bool jpg_is_ext(const OsPath &extension)
static boolean src_fill_buffer(j_decompress_ptr cinfo)
indicates B and R pixel components are exchanged.
struct jpeg_source_mgr pub
JpgErrorMgr(jpeg_compress_struct &cinfo)
static void dst_term(j_compress_ptr cinfo)
char msg[JMSG_LENGTH_MAX]
std::vector< RowPtr > tex_codec_alloc_rows(const u8 *data, size_t h, size_t pitch, size_t src_flags, size_t dst_orientation)
void dst_prepare(j_compress_ptr cinfo, DynArray *da)
static Status jpg_decode(rpU8 data, size_t size, Tex *RESTRICT t)
void src_prepare(j_decompress_ptr cinfo, rpU8 data, size_t size)
Status da_set_size(DynArray *da, size_t new_size)
expand or shrink the array: changes the amount of currently committed (i.e.
u8 * tex_get_data(const Tex *t)
rationale: since Tex is a struct, its fields are accessible to callers.
static size_t jpg_hdr_size(const u8 *file)
struct jpeg_destination_mgr pub
static void err_error_exit(j_common_ptr cinfo)
i64 Status
Error handling system.
stores all data describing an image.
static Status jpg_decode_impl(rpU8 data, size_t size, jpeg_decompress_struct *cinfo, Tex *t)
const Status TEX_INVALID_DATA
static Status AllocateAligned(shared_ptr< T > &p, size_t size, size_t alignment=cacheLineSize)
static void src_term(j_decompress_ptr cinfo)
static void make_room_in_buffer(j_compress_ptr cinfo)
#define TEX_CODEC_REGISTER(name)
build codec vtbl and register it.
#define WARN_RETURN(status)
static void src_init(j_decompress_ptr cinfo)
static Status jpg_transform(Tex *t, size_t transforms)
#define cassert(expr)
Compile-time assertion.
static void src_skip_data(j_decompress_ptr cinfo, long num_bytes)
static void dst_init(j_compress_ptr cinfo)
static void err_output_message(j_common_ptr cinfo)
size_t flags
see TexFlags and "Format Conversion" in docs.
static Status jpg_encode(Tex *RESTRICT t, DynArray *RESTRICT da)