UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
uejpeg.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2/*
3
4UEJPEG - A JPEG Super-compressor using an oodle backend as the entropy coder.
5
6Can encode from a JPEG file to an UEJPEG file, and can decode directly to a JPEG file or a raw image.
7Can also encode raw images directly to UEJPEG files.
8
9API has simple APIs and threaded APIs.
10
11The simple APIs is as follows:
12-----------------------------
13
14The following functions encode from a JPEG file to a UEJPEG file...
15
16 int uejpeg_encode_jpeg_mem(const unsigned char *jpeg_data, int jpeg_data_size, unsigned char **out_uejpeg_data, int *out_uejpeg_size, int flags);
17
18The following functions encode from a raw RGBA image to a UEJPEG file...
19
20 int uejpeg_encode_to_mem(unsigned char **out_data, int *out_size, int width, int height, int channels, const unsigned char *rgba, int quality, int flags);
21
22The following functions decode from a UEJPEG file back to a JPEG file...
23
24 int uejpeg_decode_mem_to_jpeg(const unsigned char *data, int size, unsigned char **out_data, int *out_size, int flags);
25
26The following functions decode from a UEJPEG file to a raw RGBA image...
27
28 unsigned char *uejpeg_decode_mem(const unsigned char *data, int size, int *width, int *height, int *channels, int flags);
29
30The threaded APIs is as follows:
31-------------------------------
32
33The following functions encode from a JPEG file to a UEJPEG file...
34
35 uejpeg_encode_context_t uejpeg_encode_jpeg_mem_threaded_start(const unsigned char *data, int size, int flags);
36 int uejpeg_encode_jpeg_thread_run(uejpeg_encode_context_t *context, int job_idx);
37 int uejpeg_encode_jpeg_mem_threaded_finish(uejpeg_encode_context_t *context, unsigned char **out_data, int *out_size);
38
39The following functions encode from a raw RGBA image to a UEJPEG file...
40
41 uejpeg_encode_image_context_t uejpeg_encode_image_mem_threaded_start(int width, int height, int comp, const unsigned char *in_data, int quality, int flags);
42 uejpeg_encode_image_context_t uejpeg_encode_image_threaded_start(const uejpeg_io_callbacks_t *io, void *io_user, int width, int height, int comp, const unsigned char *in_data, int quality, int flags);
43 int uejpeg_encode_image_thread_run(uejpeg_encode_image_context_t *ctx, int job_idx);
44 int uejpeg_encode_image_threaded_finish(uejpeg_encode_image_context_t *ctx);
45 int uejpeg_encode_image_mem_threaded_finish(uejpeg_encode_image_context_t *ctx, unsigned char **out_data, int *out_size);
46
47The following functions decode from a UEJPEG file to a raw RGBA image...
48
49 uejpeg_decode_context_t uejpeg_decode_mem_threaded_start(const unsigned char *data, int size, int flags);
50 uejpeg_decode_context_t uejpeg_decode_threaded_start(const uejpeg_io_callbacks_t *io, void *io_user, int flags);
51 int uejpeg_decode_thread_run(uejpeg_decode_context_t *context, int job_idx);
52 unsigned char *uejpeg_decode_threaded_finish(uejpeg_decode_context_t *context, int *out_width, int *out_height, int *out_comp);
53
54You use the threaded APIs by first calling the _start function, which gives you a context. You then pass that context into the thread_run function N times,
55where N is the value of the context.jobs_to_run variable. You then wait for all the threads to finish, and then call the _finish function to get the output data.
56
57Example:
58
59 uejpeg_encode_context_t ctx = uejpeg_encode_jpeg_mem_threaded_start(input, output, flags);
60 #pragma omp parallel for
61 for(int i = 0; i < ctx.jobs_to_run; ++i) {
62 uejpeg_encode_jpeg_thread_run(&ctx, i);
63 }
64 uejpeg_encode_jpeg_file_threaded_finish(&ctx);
65
66Other Notes:
67
68* Custom Allocators can be set via uejpeg_set_alloc() function.
69* Custom compression/decompression (must be set the same in both the encoder and decoder) can be set via uejpeg_set_compression() function.
70
71*/
72#pragma once
73
74#ifdef __cplusplus
75extern "C" {
76#endif
77
78// Error codes
79typedef enum {
80 UEJPEG_ERR_NONE = 0, // No Error, everything OK! :)
81 UEJPEG_ERR_NOT_A_JPEG = 1, // Not a JPEG file. Happens if you try to encode from a non-JPEG file.
82 UEJPEG_ERR_CANNOT_OPEN_FILE = 2, // Cannot open file. Happens if the file routines cannot open the files you specified!
83 UEJPEG_ERR_NOT_UEJPEG_FILE = 3, // Not a uejpeg file. Happens if you try to decode a non-uejpeg file.
84 UEJPEG_ERR_CORRUPT_UEJPEG_FILE = 4, // Corrupt uejpeg file. Happens if the uejpeg file is corrupt.
85 UEJPEG_ERR_WRITE_FAILED = 5, // Write failed. Happens if the file routines cannot write to the files you specified!
86 UEJPEG_ERR_OUT_OF_MEMORY = 6, // Out of memory. Happens if the uejpeg library cannot allocate enough memory.
87 UEJPEG_ERR_UNSUPPORTED_COLORSPACE = 7, // Unsupported colorspace. Happens when exporting if the JPEG file is not YCbCr.
88 UEJPEG_ERR_READ_FAILED = 8, // Read failed. Happens if the file routines cannot read from the files you specified!
89 UEJPEG_ERR_UNSUPPORTED = 9, // Other Unsupported. Happens usually when you try to decode to jpeg in an uncommon format.
91
92// Flags to be used in compression or decompression
93typedef enum {
94 UEJPEG_FLAG_NONE = 0x0, // no flags
95 UEJPEG_FLAG_NO_COMPRESSION = 0x1, // don't compress the coefficients, just store them
96 UEJPEG_FLAG_FASTDCT = 0x2, // use a fast dct transform which matches libjpegturbo's fastdct.
98
99enum {
101};
102
103// IO callbacks
104// For use with uejpeg_*() to specify any input or output method. All methods internally use this!
105typedef struct {
106 size_t (*write)(void *user, const void *data, size_t size); // write 'data' with 'size' bytes. return number of bytes actually written
107 size_t (*read)(void *user, void *data, size_t size); // read 'data' with 'size' bytes. return number of bytes actually read
108 int (*eof)(void *user); // return 1 if end-of-file, 0 otherwise
110
111// For using memory buffers as input or output.
112typedef struct {
113 char *base;
114 char *ptr;
115 size_t size;
117
119
120// For setting custom memory allocators.
121typedef struct {
122 void *(*alloc)(size_t size); // allocate 'size' bytes. return pointer to allocated memory
123 void (*free)(void *ptr); // free memory at 'ptr'
124 void *(*realloc)(void *ptr, size_t size); // reallocate 'ptr' to 'size' bytes. return pointer to reallocated memory
126
127typedef struct {
128 void *data;
129 size_t size;
131
132// compression callbacks, for using custom compression methods, such as zlib
133typedef struct {
134 uejpeg_buffer_t (*compress)(const void *data, size_t size);
135 int (*decompress)(const void *data, size_t size, void *out_data, size_t out_size);
137
138// Set custom allocators. If not set, malloc/free/realloc will be used.
140// Set custom compression methods. If not set, oodle compression methods will be used.
142
143// An internal structure used to store the header of a uejpeg file.
144typedef struct uejpeg_ihdr_t {
145 unsigned version;
146 unsigned width;
147 unsigned height;
148 unsigned char bit_depth;
149 unsigned char comp;
150 unsigned char method;
151 unsigned char num_splits;
153
154// An internal structure used to store the header of a uejpeg file.
155typedef struct uejpeg_lossy_hdr_t {
156 unsigned short coef_dims[4][2];
157 // Quantization tables
158 unsigned short dequant[4][64];
159 char app14_color_transform; // valid values are 0(CMYK),1(YCCK),2(YCbCrA)
160 char jfif;
161 char comp_id[4];
163
164typedef struct {
165 int y_start[4];
166 int y_end[4];
167 int width[4];
168 int height[4];
170 short *xcoefs;
172 unsigned char *oodle_buf;
176
177typedef struct {
178 int error;
180 int split_height[4];
181 int split_blocks[4];
185
186typedef struct {
187 int error; // If there was an error, it is stored here.
188 int jobs_to_run; // How many times to run uejpeg_decode_thread_run!
189
190 // arguments
192 void *io_user;
193 int flags;
194
195 // internal state
196 int width, height, comp;
198
202
203typedef struct {
204 int error; // If there was an error, it is stored here.
205 int jobs_to_run; // How many times to run uejpeg_decode_thread_run!
206
207 // arguments
208 int flags;
209
210 // internal state
214 int yuva_stride[4];
215 int yuva_height[4];
216 unsigned char *yuva[4];
218
219typedef struct {
220 int error; // If there was an error, it is stored here.
221 int jobs_to_run; // How many times to run uejpeg_decode_thread_run!
222
223 // arguments
225 void *io_user;
226 int width, height, comp;
227 const unsigned char *in_data;
228 int flags;
229
230 // internal state
234 float fdtbl_Y[64];
235 float fdtbl_UV[64];
237
238// Encode a JPEG file to uejpeg format.
239// This second function operates totally in memory.
240int uejpeg_encode_jpeg_mem(const unsigned char *jpeg_data, int jpeg_data_size, unsigned char **out_uejpeg_data, int *out_uejpeg_size, int flags);
241
242// Threaded API
243// In this API you first call start, and it does some synchronous work and gives you a state context to pass to the run function.
244// Then inside threads, you call the run function with job_idx == 0 .. context.jobs_to_run
245// Then you wait for all the threads to finish processing all the jobs.
246// Then you call finish to get the output data.
247uejpeg_encode_context_t uejpeg_encode_jpeg_mem_threaded_start(const unsigned char *data, int size, int flags);
250
251// Encode a raw image to a uejpeg file.
252int uejpeg_encode_to_mem(unsigned char **out_data, int *out_size, int width, int height, int channels, const unsigned char *rgba, int quality, int flags);
253
254uejpeg_encode_image_context_t uejpeg_encode_image_mem_threaded_start(int width, int height, int comp, const unsigned char *in_data, int quality, int flags);
255uejpeg_encode_image_context_t uejpeg_encode_image_threaded_start(const uejpeg_io_callbacks_t *io, void *io_user, int width, int height, int comp, const unsigned char *in_data, int quality, int flags);
259
260// Decode a UEJPEG file to a raw image.
261// Interface is like stbi_load_from_file, stbi_load_from_memory, stbi_load, etc.
262unsigned char *uejpeg_decode_mem(const unsigned char *data, int size, int *width, int *height, int *channels, int flags);
263unsigned char *uejpeg_decode(const uejpeg_io_callbacks_t *io, void *io_user, int *out_width, int *out_height, int *out_comp, int flags);
264
265// Threaded API
266// In this API you first call start, and it does some synchronous work and gives you a state context to pass to the run function.
267// Then inside threads, you call the run function with job_idx == 0 .. context.jobs_to_run
268// Then you wait for all the threads to finish processing all the jobs.
269// Then you call finish to get the output data.
270
271uejpeg_decode_context_t uejpeg_decode_mem_threaded_start(const unsigned char *data, int size, int flags);
275
276// Decode a UEJPEG file to a JPEG file.
278int uejpeg_decode_mem_to_jpeg(const unsigned char *data, int size, unsigned char **out_data, int *out_size, int flags);
279
280#ifdef __cplusplus
281}
282#endif
283
OODEFFUNC typedef void(OODLE_CALLBACK t_fp_OodleCore_Plugin_Free)(void *ptr)
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
int compress(int elen, const REAL *e, REAL *h)
Definition Predicates.inl:1299
free(DecoderMem)
Definition uejpeg.h:121
Definition uejpeg.h:127
size_t size
Definition uejpeg.h:129
void * data
Definition uejpeg.h:128
Definition uejpeg.h:133
Definition uejpeg.h:203
int error
Definition uejpeg.h:204
uejpeg_lossy_hdr_t lhdr
Definition uejpeg.h:212
uejpeg_ihdr_t ihdr
Definition uejpeg.h:211
int flags
Definition uejpeg.h:208
int jobs_to_run
Definition uejpeg.h:205
uejpeg_internal_splits_t spx
Definition uejpeg.h:213
Definition uejpeg.h:186
int error
Definition uejpeg.h:187
int num_splits
Definition uejpeg.h:197
int comp
Definition uejpeg.h:196
void * io_user
Definition uejpeg.h:192
const uejpeg_io_callbacks_t * io
Definition uejpeg.h:191
uejpeg_internal_splits_t s
Definition uejpeg.h:200
uejpeg_lossy_hdr_t lhdr
Definition uejpeg.h:199
int flags
Definition uejpeg.h:193
int jobs_to_run
Definition uejpeg.h:188
Definition uejpeg.h:219
int jobs_to_run
Definition uejpeg.h:221
int comp
Definition uejpeg.h:226
int flags
Definition uejpeg.h:228
uejpeg_internal_splits_t spx
Definition uejpeg.h:232
const uejpeg_io_callbacks_t * io
Definition uejpeg.h:224
void * io_user
Definition uejpeg.h:225
uejpeg_lossy_hdr_t lhdr
Definition uejpeg.h:233
int error
Definition uejpeg.h:220
const unsigned char * in_data
Definition uejpeg.h:227
int num_splits
Definition uejpeg.h:231
Definition uejpeg.h:144
unsigned version
Definition uejpeg.h:145
unsigned height
Definition uejpeg.h:147
unsigned char num_splits
Definition uejpeg.h:151
unsigned char bit_depth
Definition uejpeg.h:148
unsigned char comp
Definition uejpeg.h:149
unsigned width
Definition uejpeg.h:146
unsigned char method
Definition uejpeg.h:150
Definition uejpeg.h:164
unsigned char * oodle_buf
Definition uejpeg.h:172
int coefs_size
Definition uejpeg.h:174
int oodle_size
Definition uejpeg.h:173
char * split_xcoefs
Definition uejpeg.h:171
int num_blocks
Definition uejpeg.h:169
short * xcoefs
Definition uejpeg.h:170
Definition uejpeg.h:177
int total_split_blocks
Definition uejpeg.h:182
int split_height_min
Definition uejpeg.h:179
int error
Definition uejpeg.h:178
Definition uejpeg.h:112
char * base
Definition uejpeg.h:113
size_t size
Definition uejpeg.h:115
char * ptr
Definition uejpeg.h:114
Definition uejpeg.h:105
Definition uejpeg.h:155
unsigned short coef_dims[4][2]
Definition uejpeg.h:156
char app14_color_transform
Definition uejpeg.h:159
char jfif
Definition uejpeg.h:160
unsigned short dequant[4][64]
Definition uejpeg.h:158
char comp_id[4]
Definition uejpeg.h:161
int uejpeg_encode_to_mem(unsigned char **out_data, int *out_size, int width, int height, int channels, const unsigned char *rgba, int quality, int flags)
void uejpeg_set_alloc(const uejpeg_alloc_t *alloc)
unsigned char * uejpeg_decode_mem(const unsigned char *data, int size, int *width, int *height, int *channels, int flags)
void uejpeg_set_compression(const uejpeg_compression_t *compression)
const uejpeg_io_callbacks_t s_uejpeg_buffer_fns
uejpeg_encode_image_context_t uejpeg_encode_image_mem_threaded_start(int width, int height, int comp, const unsigned char *in_data, int quality, int flags)
@ UEJPEG_MAX_SPLITS
Definition uejpeg.h:100
int uejpeg_decode_mem_to_jpeg(const unsigned char *data, int size, unsigned char **out_data, int *out_size, int flags)
unsigned char * uejpeg_decode_threaded_finish(uejpeg_decode_context_t *context, int *out_width, int *out_height, int *out_comp)
uejpeg_flags_t
Definition uejpeg.h:93
@ UEJPEG_FLAG_FASTDCT
Definition uejpeg.h:96
@ UEJPEG_FLAG_NONE
Definition uejpeg.h:94
@ UEJPEG_FLAG_NO_COMPRESSION
Definition uejpeg.h:95
unsigned char * uejpeg_decode(const uejpeg_io_callbacks_t *io, void *io_user, int *out_width, int *out_height, int *out_comp, int flags)
int uejpeg_decode_to_jpeg(const uejpeg_io_callbacks_t *in_io, void *in_io_user, const uejpeg_io_callbacks_t *out_io, void *out_io_user, int flags)
uejpeg_error_t
Definition uejpeg.h:79
@ UEJPEG_ERR_NOT_UEJPEG_FILE
Definition uejpeg.h:83
@ UEJPEG_ERR_WRITE_FAILED
Definition uejpeg.h:85
@ UEJPEG_ERR_CANNOT_OPEN_FILE
Definition uejpeg.h:82
@ UEJPEG_ERR_NOT_A_JPEG
Definition uejpeg.h:81
@ UEJPEG_ERR_READ_FAILED
Definition uejpeg.h:88
@ UEJPEG_ERR_OUT_OF_MEMORY
Definition uejpeg.h:86
@ UEJPEG_ERR_CORRUPT_UEJPEG_FILE
Definition uejpeg.h:84
@ UEJPEG_ERR_NONE
Definition uejpeg.h:80
@ UEJPEG_ERR_UNSUPPORTED
Definition uejpeg.h:89
@ UEJPEG_ERR_UNSUPPORTED_COLORSPACE
Definition uejpeg.h:87
int uejpeg_encode_image_threaded_finish(uejpeg_encode_image_context_t *ctx)
uejpeg_encode_image_context_t uejpeg_encode_image_threaded_start(const uejpeg_io_callbacks_t *io, void *io_user, int width, int height, int comp, const unsigned char *in_data, int quality, int flags)
uejpeg_decode_context_t uejpeg_decode_mem_threaded_start(const unsigned char *data, int size, int flags)
int uejpeg_encode_jpeg_thread_run(uejpeg_encode_context_t *context, int job_idx)
int uejpeg_encode_image_mem_threaded_finish(uejpeg_encode_image_context_t *ctx, unsigned char **out_data, int *out_size)
int uejpeg_encode_image_thread_run(uejpeg_encode_image_context_t *ctx, int job_idx)
int uejpeg_encode_jpeg_mem(const unsigned char *jpeg_data, int jpeg_data_size, unsigned char **out_uejpeg_data, int *out_uejpeg_size, int flags)
uejpeg_decode_context_t uejpeg_decode_threaded_start(const uejpeg_io_callbacks_t *io, void *io_user, int flags)
int uejpeg_decode_thread_run(uejpeg_decode_context_t *context, int job_idx)
int uejpeg_encode_jpeg_mem_threaded_finish(uejpeg_encode_context_t *context, unsigned char **out_data, int *out_size)
uejpeg_encode_context_t uejpeg_encode_jpeg_mem_threaded_start(const unsigned char *data, int size, int flags)