35#ifdef HAVE_LIBWEBP_DECODE_H
36# include <webp/decode.h>
40#if JPEG_LIB_VERSION < 80
46#ifdef HAVE_LIBWEBP_DECODE_H
54#define TGA_UNMAP_COMP 10
70 png_bytep* row_pointers;
72 png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,
nullptr,
nullptr,
nullptr);
77 info_ptr = png_create_info_struct(png_ptr);
79 png_destroy_write_struct(&png_ptr, (png_infopp)
nullptr);
83 png_init_io(png_ptr,
f->f);
85 png_set_IHDR(png_ptr, info_ptr, width, height, 8, PNG_COLOR_TYPE_RGB,
86 PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
88 png_set_compression_level(png_ptr, Z_DEFAULT_COMPRESSION);
89 png_set_compression_mem_level(png_ptr, 9);
91 png_write_info(png_ptr, info_ptr);
93 row_pointers = (png_bytep*)malloc(height *
sizeof(png_bytep));
94 for (
int i = 0;
i < height;
i++)
95 row_pointers[
i] = buffer + (height - 1 -
i) * 3 * width;
97 png_write_image(png_ptr, row_pointers);
98 png_write_end(png_ptr, info_ptr);
100 png_destroy_write_struct(&png_ptr, &info_ptr);
105#define TGA_CHANNELS 3
117 size_t block_length = 0;
130 header[12] = width & 255;
131 header[13] = (width >> 8) & 255;
133 header[14] = height & 255;
134 header[15] = (height >> 8) & 255;
141 for (
int y = height - 1; y >= 0; y--) {
142 for (
size_t x = 0; x < width; x++) {
144 pixel_data[0] = buffer[
index + 2];
145 pixel_data[1] = buffer[
index + 1];
146 pixel_data[2] = buffer[
index];
148 if (block_length == 0) {
162 if (block_length > 1) {
164 rle_packet = block_length - 2;
175 if (memcmp(block_data, pixel_data,
TGA_CHANNELS) == 0) {
179 if (block_length > 1) {
181 rle_packet = block_length + 127;
193 if (block_length == 128) {
194 rle_packet = block_length - 1;
212 rle_packet = block_length - 1;
224 strncpy(&footer[8] ,
"TRUEVISION-XFILE", 16);
237 struct jpeg_compress_struct cinfo;
238 struct jpeg_error_mgr jerr;
242 cinfo.err = jpeg_std_error(&jerr);
243 jpeg_create_compress(&cinfo);
244 jpeg_stdio_dest(&cinfo,
f->f);
247 cinfo.image_width = width;
248 cinfo.image_height = height;
249 cinfo.in_color_space = JCS_RGB;
250 cinfo.input_components = 3;
251 cinfo.progressive_mode = TRUE;
253 jpeg_set_defaults(&cinfo);
254 jpeg_set_quality(&cinfo, quality, TRUE);
255 jpeg_start_compress(&cinfo, TRUE);
256 jpeg_write_marker(&cinfo, JPEG_COM, (
const byte*)
"UFOAI", (uint32_t) 5);
259 w3 = cinfo.image_width * 3;
260 offset = w3 * cinfo.image_height - w3;
261 while (cinfo.next_scanline < cinfo.image_height) {
262 s = &buffer[
offset - (cinfo.next_scanline * w3)];
263 jpeg_write_scanlines(&cinfo, &s, 1);
267 jpeg_finish_compress(&cinfo);
268 jpeg_destroy_compress(&cinfo);
274 snprintf(path,
sizeof(path),
"%s.%s",
name, suffix);
282 return SDL_CreateRGBSurface(0, width, height, 32,
283#
if SDL_BYTEORDER == SDL_BIG_ENDIAN
284 0xFF000000U, 0x00FF0000U, 0x0000FF00U, 0x000000FFU
286 0x000000FFU, 0x0000FF00U, 0x00FF0000U, 0xFF000000U
297static void readMem(png_struct*
const png, png_byte*
const dst, png_size_t
const size)
301 png_error(png,
"premature end of input");
315 SDL_Surface* res = 0;
318 if (png_struct* png = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0)) {
319 png_info* info = png_create_info_struct(png);
322 png_set_read_fn(png, &state, &
readMem);
324 png_read_info(png, info);
330 png_get_IHDR(png, info, &width, &height, &bit_depth, &color_type, 0, 0, 0);
333 png_set_gray_to_rgb(png);
334 png_set_strip_16(png);
335 png_set_packing(png);
337 png_set_add_alpha(png, 255, PNG_FILLER_AFTER);
340 png_start_read_image(png);
342 png_byte* dst =
static_cast<png_byte*
>(s->pixels);
343 size_t const pitch = s->pitch;
344 for (
size_t n = height; n != 0; dst += pitch, --n) {
345 png_read_row(png, dst, 0);
348 png_read_end(png, info);
353 png_destroy_read_struct(&png, &info, 0);
362#if JPEG_LIB_VERSION < 80
367 ERREXIT(cinfo, JERR_INPUT_EOF);
376 jpeg_source_mgr& src = *cinfo->src;
377 if (src.bytes_in_buffer < (
size_t)num_bytes)
378 ERREXIT(cinfo, JERR_INPUT_EOF);
380 src.next_input_byte += num_bytes;
381 src.bytes_in_buffer -= num_bytes;
392 SDL_Surface* res = 0;
395 jpeg_decompress_struct cinfo;
398 cinfo.err = jpeg_std_error(&jerr);
399 jpeg_create_decompress(&cinfo);
401#if JPEG_LIB_VERSION < 80
403 src.next_input_byte =
buf;
404 src.bytes_in_buffer =
len;
408 src.resync_to_restart = &jpeg_resync_to_restart;
412 jpeg_mem_src(&cinfo,
buf,
len);
415 jpeg_read_header(&cinfo, TRUE);
417 cinfo.out_color_space = JCS_RGB;
419 if (SDL_Surface*
const s =
createSurface(cinfo.image_height, cinfo.image_width)) {
420 jpeg_start_decompress(&cinfo);
422 byte* dst =
static_cast<byte*
>(s->pixels);
423 size_t const pitch = s->pitch;
424 for (
size_t n = cinfo.image_height; n != 0; dst += pitch, --n) {
425 JSAMPLE* lines[1] = { dst };
426 jpeg_read_scanlines(&cinfo, lines, 1);
429 for (
size_t x = cinfo.image_width; x-- != 0;) {
430 dst[4 * x + 0] = dst[3 * x + 0];
431 dst[4 * x + 1] = dst[3 * x + 1];
432 dst[4 * x + 2] = dst[3 * x + 2];
433 dst[4 * x + 3] = 255;
437 jpeg_finish_decompress(&cinfo);
441 jpeg_destroy_decompress(&cinfo);
449#ifdef HAVE_LIBWEBP_DECODE_H
455static SDL_Surface* Img_LoadWebP(
char const*
const name)
457 SDL_Surface* res = 0;
463 if (!WebPGetInfo(
buf,
len, &width, &height)) {
469 size_t imageSize = width * height * s->format->BytesPerPixel;
470 void* pixels = WebPDecodeRGBAInto(
buf,
len, (uint8_t*)s->pixels, imageSize,
471 width * s->format->BytesPerPixel);
472 res = pixels ? s :
nullptr;
490#ifdef HAVE_LIBWEBP_DECODE_H
491 if (SDL_Surface*
const s = Img_LoadWebP(
name))
int FS_Write(const void *buffer, int len, qFILE *f)
Properly handles partial writes.
int FS_LoadFile(const char *path, byte **buffer)
Filenames are relative to the quake search path.
void FS_FreeFile(void *buffer)
char const *const * Img_GetImageTypes(void)
static SDL_Surface * createSurface(int const height, int const width)
void R_WriteJPG(qFILE *f, byte *buffer, int width, int height, int quality)
static void djpeg_nop(jpeg_decompress_struct *)
static boolean djpeg_fill_input_buffer(jpeg_decompress_struct *const cinfo)
static SDL_Surface * Img_LoadJPG(char const *const name)
Loads a Jpeg image into an SDL_Surface.
void R_WriteCompressedTGA(qFILE *f, const byte *buffer, int width, int height)
static void djpeg_skip_input_data(jpeg_decompress_struct *const cinfo, long const num_bytes)
SDL_Surface * Img_LoadImage(char const *name)
Loads the specified image from the game filesystem into an SDL_Surface.
static byte * readFile(char const *const name, char const *const suffix, size_t &len)
static SDL_Surface * Img_LoadPNG(char const *const name)
Loads a PNG image into an SDL_Surface.
static char const *const IMAGE_TYPES[]
void R_WritePNG(qFILE *f, byte *buffer, int width, int height)
static void readMem(png_struct *const png, png_byte *const dst, png_size_t const size)
Image loading and saving functions.
QGL_EXTERN GLuint GLchar GLuint * len
QGL_EXTERN GLuint GLsizei GLsizei GLint GLenum GLchar * name