64 const char magic[2] = {0xFF, 0xD8};
65 if (size <
sizeof(magic)) {
68 return memcmp(mem, magic,
sizeof(magic)) == 0;
90 (*cinfo->err->output_message)(cinfo);
129 src->
pub.bytes_in_buffer = 2;
131 src->
terminal[1] = JOCTET(JPEG_EOI);
142 size_t skip_size = size_t(num_bytes) <= src->
pub.bytes_in_buffer ? num_bytes :
143 src->
pub.bytes_in_buffer;
145 src->
pub.next_input_byte = src->
pub.next_input_byte + skip_size;
146 src->
pub.bytes_in_buffer = src->
pub.bytes_in_buffer - skip_size;
159 if (cinfo->src ==
nullptr) {
160 cinfo->src =
static_cast<jpeg_source_mgr *
>((*cinfo->mem->alloc_small)(
161 reinterpret_cast<j_common_ptr
>(cinfo), JPOOL_PERMANENT,
sizeof(
my_source_mgr)));
164 src =
reinterpret_cast<my_src_ptr>(cinfo->src);
168 src->
pub.resync_to_restart = jpeg_resync_to_restart;
171 src->
pub.bytes_in_buffer = size;
172 src->
pub.next_input_byte = buffer;
184#define MAKESTMT(stuff) \
189#define INPUT_VARS(cinfo) \
190 jpeg_source_mgr *datasrc = (cinfo)->src; \
191 const JOCTET *next_input_byte = datasrc->next_input_byte; \
192 size_t bytes_in_buffer = datasrc->bytes_in_buffer
195#define INPUT_SYNC(cinfo) \
196 (datasrc->next_input_byte = next_input_byte, datasrc->bytes_in_buffer = bytes_in_buffer)
199#define INPUT_RELOAD(cinfo) \
200 (next_input_byte = datasrc->next_input_byte, bytes_in_buffer = datasrc->bytes_in_buffer)
206#define MAKE_BYTE_AVAIL(cinfo, action) \
207 if (bytes_in_buffer == 0) { \
208 if (!(*datasrc->fill_input_buffer)(cinfo)) { \
211 INPUT_RELOAD(cinfo); \
218#define INPUT_BYTE(cinfo, V, action) \
219 MAKESTMT(MAKE_BYTE_AVAIL(cinfo, action); bytes_in_buffer--; V = GETJOCTET(*next_input_byte++);)
224#define INPUT_2BYTES(cinfo, V, action) \
225 MAKESTMT(MAKE_BYTE_AVAIL(cinfo, action); bytes_in_buffer--; \
226 V = uint(GETJOCTET(*next_input_byte++)) << 8; \
227 MAKE_BYTE_AVAIL(cinfo, action); \
229 V += GETJOCTET(*next_input_byte++);)
251 for (i = 0; i <
length; i++) {
262 (*cinfo->src->skip_input_data)(cinfo,
length);
279 JSAMPARRAY row_pointer;
280 JSAMPLE *buffer =
nullptr;
282 int x,
y, depth, r, g,
b, k;
283 ImBuf *ibuf =
nullptr;
285 jpeg_saved_marker_ptr marker;
286 char *
str, *key, *value;
290 jpeg_set_marker_processor(cinfo, 0xe1,
handle_app1);
291 cinfo->dct_method = JDCT_FLOAT;
292 jpeg_save_markers(cinfo, JPEG_COM, 0xffff);
294 if (jpeg_read_header(cinfo,
false) == JPEG_HEADER_OK) {
295 depth = cinfo->num_components;
297 if (cinfo->jpeg_color_space == JCS_YCCK) {
298 cinfo->out_color_space = JCS_CMYK;
302 *r_width = cinfo->image_width;
305 *r_height = cinfo->image_height;
311 float scale =
float(max_size) / std::max(cinfo->image_width, cinfo->image_height);
312 cinfo->scale_denom = 8;
313 cinfo->scale_num =
max_uu(1,
min_uu(8, ceill(
scale *
float(cinfo->scale_denom))));
314 cinfo->dct_method = JDCT_FASTEST;
315 cinfo->dither_mode = JDITHER_ORDERED;
318 jpeg_start_decompress(cinfo);
320 x = cinfo->output_width;
321 y = cinfo->output_height;
327 else if (depth == 3) {
332 jpeg_abort_decompress(cinfo);
341 jpeg_abort_decompress(cinfo);
345 row_stride = cinfo->output_width * depth;
347 row_pointer = (*cinfo->mem->alloc_sarray)(
348 reinterpret_cast<j_common_ptr
>(cinfo), JPOOL_IMAGE, row_stride, 1);
351 for (
y = ibuf->
y - 1;
y >= 0;
y--) {
352 jpeg_read_scanlines(cinfo, row_pointer, 1);
353 rect = byte_data + 4 *
y * size_t(ibuf->
x);
354 buffer = row_pointer[0];
358 for (
x = ibuf->
x;
x > 0;
x--) {
360 rect[0] = rect[1] = rect[2] = *buffer++;
365 for (
x = ibuf->
x;
x > 0;
x--) {
374 for (
x = ibuf->
x;
x > 0;
x--) {
394 marker = cinfo->marker_list;
396 if (marker->marker != JPEG_COM) {
397 goto next_stamp_marker;
407 reinterpret_cast<const char *
>(marker->data),
408 BLI_strnlen(
reinterpret_cast<const char *
>(marker->data), marker->data_length));
432 goto next_stamp_marker;
435 key = strchr(
str,
':');
442 goto next_stamp_marker;
446 value = strchr(key,
':');
449 goto next_stamp_marker;
458 marker = marker->next;
461 jpeg_finish_decompress(cinfo);
466 if (cinfo->density_unit == 1) {
468 ibuf->
ppm[0] = double(cinfo->X_density) / 0.0254;
469 ibuf->
ppm[1] = double(cinfo->Y_density) / 0.0254;
471 else if (cinfo->density_unit == 2) {
472 ibuf->
ppm[0] = double(cinfo->X_density) * 100.0;
473 ibuf->
ppm[1] = double(cinfo->Y_density) * 100.0;
479 jpeg_destroy(
reinterpret_cast<j_common_ptr
>(cinfo));
490 jpeg_decompress_struct _cinfo, *cinfo = &_cinfo;
498 cinfo->err = jpeg_std_error(&jerr.
pub);
506 jpeg_destroy_decompress(cinfo);
510 jpeg_create_decompress(cinfo);
525#define JPEG_MARKER_MSB (0xFF)
526#define JPEG_MARKER_SOI (0xD8)
527#define JPEG_MARKER_APP1 (0xE1)
528#define JPEG_APP1_MAX (1 << 16)
532 const size_t max_thumb_size,
537 jpeg_decompress_struct _cinfo, *cinfo = &_cinfo;
539 FILE *infile =
nullptr;
541 cinfo->err = jpeg_std_error(&jerr.
pub);
549 jpeg_destroy_decompress(cinfo);
553 if ((infile =
BLI_fopen(filepath,
"rb")) ==
nullptr) {
567 !feof(infile) && i--)
570 if (i > 0 && !feof(infile)) {
572 ImBuf *ibuf =
nullptr;
580 MEM_SAFE_DELETE(buffer);
590 fseek(infile, 0, SEEK_SET);
591 jpeg_create_decompress(cinfo);
593 jpeg_stdio_src(cinfo, infile);
600#undef JPEG_MARKER_MSB
601#undef JPEG_MARKER_SOI
602#undef JPEG_MARKER_APP1
613#define MAX_LIBJPEG_MARKER_LENGTH 65533
617 JSAMPLE *buffer =
nullptr;
618 JSAMPROW row_pointer[1];
623 jpeg_start_compress(cinfo,
true);
626 neogeo_word =
reinterpret_cast<NeoGeo_Word *
>(neogeo + 6);
627 memset(neogeo_word, 0,
sizeof(*neogeo_word));
629 jpeg_write_marker(cinfo, 0xe1,
reinterpret_cast<JOCTET *
>(neogeo), 10);
633 char static_text[1024];
634 const size_t static_text_size =
ARRAY_SIZE(static_text);
638 if (
STREQ(prop.name,
"None")) {
640 cinfo, JPEG_COM,
reinterpret_cast<JOCTET *
> IDP_string_get(&prop), prop.len);
643 char *text = static_text;
644 size_t text_size = static_text_size;
648 const size_t text_length_required = 7 + 2 + strlen(prop.name) +
650 if (text_length_required > static_text_size) {
652 text_size = text_length_required;
665 text, text_size,
"Blender:%s:%s", prop.name,
IDP_string_get(&prop));
671 CLOG_WARN(&
LOG,
"Writing truncated data for \"%s\"", prop.name);
675 jpeg_write_marker(cinfo, JPEG_COM,
reinterpret_cast<JOCTET *
>(text),
uint(text_len));
680 if (text != static_text) {
690 if (
colorspace && cinfo->in_color_space == JCS_RGB) {
693 icc_profile.
prepend({
'I',
'C',
'C',
'_',
'P',
'R',
'O',
'F',
'I',
'L',
'E', 0, 0, 1});
694 jpeg_write_marker(cinfo,
696 reinterpret_cast<const JOCTET *
>(icc_profile.
data()),
702 size_t(cinfo->input_components) * size_t(cinfo->image_width),
"jpeg row_pointer");
705 for (
y = ibuf->
y - 1;
y >= 0;
y--) {
706 const uchar *rect = byte_data + 4 *
y * size_t(ibuf->
x);
707 buffer = row_pointer[0];
709 switch (cinfo->in_color_space) {
711 for (
x = 0;
x < ibuf->
x;
x++) {
719 for (
x = 0;
x < ibuf->
x;
x++) {
725 memcpy(buffer, rect, 4 * ibuf->
x);
733 jpeg_write_scanlines(cinfo, row_pointer, 1);
736 jpeg_finish_compress(cinfo);
737 MEM_delete(row_pointer[0]);
747 const double unit_scale,
751 for (
int i = 0; i < 2; i++) {
752 const double density = ppm[i] * unit_scale;
754 if (!(density_round >= 1.0 && density_round <=
double(std::numeric_limits<UINT16>::max()))) {
755 return std::numeric_limits<double>::max();
757 r_density[i] = UINT16(density_round);
758 error +=
math::abs(density_round - density) / density;
771 quality = std::min(quality, 100);
773 jpeg_create_compress(cinfo);
774 jpeg_stdio_dest(cinfo, outfile);
776 cinfo->image_width = ibuf->
x;
777 cinfo->image_height = ibuf->
y;
779 cinfo->in_color_space = JCS_RGB;
781 cinfo->in_color_space = JCS_GRAYSCALE;
783 switch (cinfo->in_color_space) {
785 cinfo->input_components = 3;
788 cinfo->input_components = 1;
791 cinfo->input_components = 4;
798 jpeg_set_defaults(cinfo);
802 cinfo->dct_method = JDCT_FLOAT;
803 jpeg_set_quality(cinfo, quality,
true);
807 if (ibuf->
ppm[0] > 0.0 && ibuf->
ppm[1] > 0.0) {
813 const Unit units[] = {
817 double error_best = std::numeric_limits<double>::max();
818 for (
const Unit &unit : units) {
821 if (error_test < error_best) {
822 error_best = error_test;
823 cinfo->density_unit = unit.code;
824 cinfo->X_density = density[0];
825 cinfo->Y_density = density[1];
836 jpeg_compress_struct _cinfo, *cinfo = &_cinfo;
839 if ((outfile =
BLI_fopen(filepath,
"wb")) ==
nullptr) {
843 cinfo->err = jpeg_std_error(&jerr.
pub);
851 jpeg_destroy_compress(cinfo);
862 jpeg_destroy_compress(cinfo);
#define IDP_string_get(prop)
File and directory operations.
#define STRNCPY_UTF8(dst, src)
#define CLOG_ERROR(clg_ref,...)
#define CLOG_WARN(clg_ref,...)
ID and Library types, which are fundamental for SDNA.
Read Blender memory allocation and freeing.
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
void prepend(const T &value)
float length(VecOp< float, D >) RET
float3x3 scale(float3x3 mat, float2 scale)
my_source_mgr * my_src_ptr
static boolean fill_input_buffer(j_decompress_ptr cinfo)
BLI_STATIC_ASSERT(ARRAY_SIZE(data_mode_strings)==CTX_MODE_NUM+1, "Must have a string for each context mode") const char *CTX_data_mode_string(const bContext *C)
static void jpeg_error(j_common_ptr cinfo) ATTR_NORETURN
void * MEM_new_array_uninitialized(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
Vector< char > IMB_colormanagement_space_to_icc_profile(const ColorSpace *colorspace)
static const uchar jpeg_default_quality
FILE * BLI_fopen(const char *filepath, const char *mode) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
static ImBuf * ibJpegImageFromCinfo(jpeg_decompress_struct *cinfo, ImBufFlags flags, int max_size, size_t *r_width, size_t *r_height)
static void memory_source(j_decompress_ptr cinfo, const uchar *buffer, size_t size)
static uchar ibuf_quality
MINLINE uint max_uu(uint a, uint b)
char * BLI_strdupn(const char *str, size_t len) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
my_error_mgr * my_error_ptr
bool imb_is_a_jpeg(const uchar *mem, const size_t size)
ImBuf * imb_load_jpeg(const uchar *buffer, size_t size, ImBufFlags flags, ImFileColorSpace &)
static double jpeg_density_from_ppm(const double ppm[2], const double unit_scale, UINT16 r_density[2])
static void init_source(j_decompress_ptr cinfo)
static void write_jpeg(jpeg_compress_struct *cinfo, ImBuf *ibuf)
static boolean handle_app1(j_decompress_ptr cinfo)
static int init_jpeg(FILE *outfile, jpeg_compress_struct *cinfo, ImBuf *ibuf)
const char * imb_file_extensions_jpeg[]
static void skip_input_data(j_decompress_ptr cinfo, long num_bytes)
constexpr size_t ARRAY_SIZE(const T(&arg)[N]) noexcept
int char char int int int int size_t BLI_strnlen(const char *str, size_t maxlen) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
bool imb_savejpeg(ImBuf *ibuf, const char *filepath, ImBufFlags flags)
ImBuf * IMB_allocImBuf(unsigned int x, unsigned int y, ImBufFlags flags)
static void term_source(j_decompress_ptr cinfo)
size_t BLI_snprintf_utf8_rlen(char *__restrict dst, size_t dst_maxncpy, const char *__restrict format,...) ATTR_NONNULL(1
ImBuf * imb_thumbnail_jpeg(const char *filepath, const ImBufFlags flags, const size_t max_thumb_size, ImFileColorSpace &r_colorspace, size_t *r_width, size_t *r_height)
void IMB_metadata_set_field(IDProperty *metadata, const char *key, const char *value)
ocio::ColorSpace ColorSpace
void * MEM_new_array_zeroed(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
MINLINE uint min_uu(uint a, uint b)
static bool save_stdjpeg(const char *filepath, ImBuf *ibuf)
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
ListBaseT< struct IDProperty > group
const ColorSpace * colorspace
ImbFormatOptions foptions
ImBufByteBuffer byte_buffer
const uint8_t * byte_data() const
uint8_t * byte_data_for_write()
IDProperty * metadata_for_write()
const IDProperty * metadata() const