DarkPlaces
Game engine based on the Quake 1 engine by id Software, developed by LadyHavoc
 
jpeg.h File Reference
#include <stddef.h>
#include "qtypes.h"
+ Include dependency graph for jpeg.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

qbool Image_Compress (const char *imagename, size_t maxsize, void **buf, size_t *size)
 
void JPEG_CloseLibrary (void)
 
unsigned char * JPEG_LoadImage_BGRA (const unsigned char *f, int filesize, int *miplevel)
 
qbool JPEG_OpenLibrary (void)
 
qbool JPEG_SaveImage_preflipped (const char *filename, int width, int height, unsigned char *data)
 
size_t JPEG_SaveImage_to_Buffer (char *jpegbuf, size_t jpegsize, int width, int height, unsigned char *data)
 

Function Documentation

◆ Image_Compress()

qbool Image_Compress ( const char * imagename,
size_t maxsize,
void ** buf,
size_t * size )

Definition at line 1047 of file jpeg.c.

1048{
1049 unsigned char *imagedata, *newimagedata;
1050 int maxPixelCount;
1051 int components[3] = {2, 1, 0};
1053
1054 JPEG_OpenLibrary (); // for now; LH had the idea of replacing this by a better format
1055 PNG_OpenLibrary (); // for loading
1056
1057 // No DLL = no JPEGs
1058 if (!jpeg_dll)
1059 {
1060 Con_Print("You need the libjpeg library to save JPEG images\n");
1061 return false;
1062 }
1063
1064 i = CompressedImageCache_Find(imagename, maxsize);
1065 if(i)
1066 {
1067 *size = i->compressed_size;
1068 *buf = i->compressed;
1069 return (*buf != NULL);
1070 }
1071
1072 // load the image
1073 imagedata = loadimagepixelsbgra(imagename, true, false, false, NULL);
1074 if(!imagedata)
1075 return false;
1076
1077 // find an appropriate size for somewhat okay compression
1078 if(maxsize <= 768)
1079 maxPixelCount = 32 * 32;
1080 else if(maxsize <= 1024)
1081 maxPixelCount = 64 * 64;
1082 else if(maxsize <= 4096)
1083 maxPixelCount = 128 * 128;
1084 else
1085 maxPixelCount = 256 * 256;
1086
1087 while(image_width * image_height > maxPixelCount)
1088 {
1089 int one = 1;
1090 Image_MipReduce32(imagedata, imagedata, &image_width, &image_height, &one, image_width/2, image_height/2, 1);
1091 }
1092
1093 newimagedata = (unsigned char *) Mem_Alloc(tempmempool, image_width * image_height * 3);
1094
1095 // convert the image from BGRA to RGB
1096 Image_CopyMux(newimagedata, imagedata, image_width, image_height, false, false, false, 3, 4, components);
1097 Mem_Free(imagedata);
1098
1099 // try to compress it to JPEG
1100 *buf = Z_Malloc(maxsize);
1101 *size = JPEG_SaveImage_to_Buffer((char *) *buf, maxsize, image_width, image_height, newimagedata);
1102 Mem_Free(newimagedata);
1103
1104 if(!*size)
1105 {
1106 Z_Free(*buf);
1107 *buf = NULL;
1108 Con_Printf("could not compress image %s to %d bytes\n", imagename, (int)maxsize);
1109 // return false;
1110 // also cache failures!
1111 }
1112
1113 // store it in the cache
1114 CompressedImageCache_Add(imagename, maxsize, *buf, *size);
1115 return (*buf != NULL);
1116}
void Con_Print(const char *msg)
Prints to all appropriate console targets, and adds timestamps.
Definition console.c:1504
void Con_Printf(const char *fmt,...)
Prints to all appropriate console targets.
Definition console.c:1514
vector size
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition glquake.h:657
int image_height
Definition image.c:10
void Image_MipReduce32(const unsigned char *in, unsigned char *out, int *width, int *height, int *depth, int destwidth, int destheight, int destdepth)
Definition image.c:1689
void Image_CopyMux(unsigned char *outpixels, const unsigned char *inpixels, int inputwidth, int inputheight, qbool inputflipx, qbool inputflipy, qbool inputflipdiagonal, int numoutputcomponents, int numinputcomponents, int *outputinputcomponentindices)
Definition image.c:24
unsigned char * loadimagepixelsbgra(const char *filename, qbool complain, qbool allowFixtrans, qbool convertsRGB, int *miplevel)
Definition image.c:1043
int image_width
Definition image.c:9
qbool PNG_OpenLibrary(void)
Definition image_png.c:143
static void CompressedImageCache_Add(const char *imagename, size_t maxsize, void *compressed, size_t compressed_size)
Definition jpeg.c:1011
dllhandle_t jpeg_dll
Definition jpeg.c:457
qbool JPEG_OpenLibrary(void)
Definition jpeg.c:492
size_t JPEG_SaveImage_to_Buffer(char *jpegbuf, size_t jpegsize, int width, int height, unsigned char *data)
Definition jpeg.c:931
static CompressedImageCacheItem * CompressedImageCache_Find(const char *imagename, size_t maxsize)
Definition jpeg.c:1030
int i
#define NULL
Definition qtypes.h:12
mempool_t * tempmempool
Definition zone.c:794
#define Mem_Free(mem)
Definition zone.h:96
#define Mem_Alloc(pool, size)
Definition zone.h:92
#define Z_Malloc(size)
Definition zone.h:161
#define Z_Free(data)
Definition zone.h:164

References buf, CompressedImageCache_Add(), CompressedImageCache_Find(), Con_Print(), Con_Printf(), i, Image_CopyMux(), image_height, Image_MipReduce32(), image_width, CompressedImageCacheItem::imagename, jpeg_dll, JPEG_OpenLibrary(), JPEG_SaveImage_to_Buffer(), loadimagepixelsbgra(), CompressedImageCacheItem::maxsize, Mem_Alloc, Mem_Free, NULL, PNG_OpenLibrary(), size, tempmempool, Z_Free, and Z_Malloc.

Referenced by VM_SV_WritePicture().

◆ JPEG_CloseLibrary()

void JPEG_CloseLibrary ( void )

Definition at line 538 of file jpeg.c.

539{
540#ifndef LINK_TO_LIBJPEG
542 jpeg_tried_loading = false; // allow retry
543#endif
544}
qbool jpeg_tried_loading
Definition jpeg.c:458
void Sys_FreeLibrary(dllhandle_t *handle)
Definition sys_shared.c:245

References jpeg_dll, jpeg_tried_loading, and Sys_FreeLibrary().

Referenced by r_textures_shutdown().

◆ JPEG_LoadImage_BGRA()

unsigned char * JPEG_LoadImage_BGRA ( const unsigned char * f,
int filesize,
int * miplevel )

Definition at line 606 of file jpeg.c.

607{
608 struct jpeg_decompress_struct cinfo;
609 struct jpeg_error_mgr jerr;
610 unsigned char *image_buffer = NULL, *scanline = NULL;
611 unsigned int line;
612 int submip = 0;
613
614 // No DLL = no JPEGs
615 if (!jpeg_dll)
616 return NULL;
617
618 if(miplevel && r_texture_jpeg_fastpicmip.integer)
619 submip = bound(0, *miplevel, 3);
620
621 cinfo.err = qjpeg_std_error (&jerr);
623 if(setjmp(error_in_jpeg))
624 goto error_caught;
625 cinfo.err = qjpeg_std_error (&jerr);
626 cinfo.err->error_exit = JPEG_ErrorExit;
627 JPEG_MemSrc (&cinfo, f, filesize);
628 qjpeg_read_header (&cinfo, true);
629 cinfo.scale_num = 1;
630 cinfo.scale_denom = (1 << submip);
631 qjpeg_start_decompress (&cinfo);
632
633 image_width = cinfo.output_width;
634 image_height = cinfo.output_height;
635
636 if (image_width > 32768 || image_height > 32768 || image_width <= 0 || image_height <= 0)
637 {
638 Con_Printf("JPEG_LoadImage: invalid image size %ix%i\n", image_width, image_height);
639 return NULL;
640 }
641
642 image_buffer = (unsigned char *)Mem_Alloc(tempmempool, image_width * image_height * 4);
643 scanline = (unsigned char *)Mem_Alloc(tempmempool, image_width * cinfo.output_components);
644 if (!image_buffer || !scanline)
645 {
646 if (image_buffer)
647 Mem_Free (image_buffer);
648 if (scanline)
649 Mem_Free (scanline);
650
651 Con_Printf("JPEG_LoadImage: not enough memory for %i by %i image\n", image_width, image_height);
654 return NULL;
655 }
656
657 // Decompress the image, line by line
658 line = 0;
659 while (cinfo.output_scanline < cinfo.output_height)
660 {
661 unsigned char *buffer_ptr;
662 int ind;
663
664 qjpeg_read_scanlines (&cinfo, &scanline, 1);
665
666 // Convert the image to BGRA
667 switch (cinfo.output_components)
668 {
669 // RGB images
670 case 3:
671 buffer_ptr = &image_buffer[image_width * line * 4];
672 for (ind = 0; ind < image_width * 3; ind += 3, buffer_ptr += 4)
673 {
674 buffer_ptr[2] = scanline[ind];
675 buffer_ptr[1] = scanline[ind + 1];
676 buffer_ptr[0] = scanline[ind + 2];
677 buffer_ptr[3] = 255;
678 }
679 break;
680
681 // Greyscale images (default to it, just in case)
682 case 1:
683 default:
684 buffer_ptr = &image_buffer[image_width * line * 4];
685 for (ind = 0; ind < image_width; ind++, buffer_ptr += 4)
686 {
687 buffer_ptr[0] = scanline[ind];
688 buffer_ptr[1] = scanline[ind];
689 buffer_ptr[2] = scanline[ind];
690 buffer_ptr[3] = 255;
691 }
692 }
693
694 line++;
695 }
696 Mem_Free (scanline); scanline = NULL;
697
700
701 if(miplevel)
702 *miplevel -= submip;
703
704 return image_buffer;
705
706error_caught:
707 if(scanline)
708 Mem_Free (scanline);
709 if(image_buffer)
710 Mem_Free (image_buffer);
712 return NULL;
713}
static struct jpeg_error_mgr *(* qjpeg_std_error)(struct jpeg_error_mgr *err)
Definition jpeg.c:431
static JDIMENSION(* qjpeg_read_scanlines)(j_decompress_ptr cinfo, unsigned char **scanlines, JDIMENSION max_lines)
Definition jpeg.c:426
static int(* qjpeg_read_header)(j_decompress_ptr cinfo, jboolean require_image)
Definition jpeg.c:425
static void(* qjpeg_destroy_decompress)(j_decompress_ptr cinfo)
Definition jpeg.c:421
static jmp_buf error_in_jpeg
Definition jpeg.c:462
cvar_t r_texture_jpeg_fastpicmip
Definition jpeg.c:32
static jboolean(* qjpeg_start_decompress)(j_decompress_ptr cinfo)
Definition jpeg.c:430
static void JPEG_MemSrc(j_decompress_ptr cinfo, const unsigned char *buffer, size_t filesize)
Definition jpeg.c:578
static jboolean(* qjpeg_finish_decompress)(j_decompress_ptr cinfo)
Definition jpeg.c:423
static void JPEG_ErrorExit(j_common_ptr cinfo)
Definition jpeg.c:592
#define qjpeg_create_decompress(cinfo)
Definition jpeg.c:415
#define bound(min, num, max)
Definition mathlib.h:34
float f
int integer
Definition cvar.h:73

References bound, Con_Printf(), j_decompress_ptr::err, jpeg_error_mgr::error_exit, error_in_jpeg, f, image_height, image_width, cvar_t::integer, jpeg_dll, JPEG_ErrorExit(), JPEG_MemSrc(), Mem_Alloc, Mem_Free, NULL, j_decompress_ptr::output_components, j_decompress_ptr::output_height, j_decompress_ptr::output_scanline, j_decompress_ptr::output_width, qjpeg_create_decompress, qjpeg_destroy_decompress, qjpeg_finish_decompress, qjpeg_read_header, qjpeg_read_scanlines, qjpeg_start_decompress, qjpeg_std_error, r_texture_jpeg_fastpicmip, j_decompress_ptr::scale_denom, j_decompress_ptr::scale_num, and tempmempool.

Referenced by decode_image(), loadimagepixelsbgra(), and VM_CL_ReadPicture().

◆ JPEG_OpenLibrary()

qbool JPEG_OpenLibrary ( void )

Definition at line 492 of file jpeg.c.

493{
494#ifdef LINK_TO_LIBJPEG
495 return true;
496#else
497 const char* dllnames [] =
498 {
499#if defined(WIN32)
500 "libjpeg.dll",
501#elif defined(MACOSX)
502 "libjpeg.62.dylib",
503#else
504 "libjpeg.so.62",
505 "libjpeg.so",
506#endif
507 NULL
508 };
509
510 // Already loaded?
511 if (jpeg_dll)
512 return true;
513
514 if (jpeg_tried_loading) // only try once
515 return false;
516
517 jpeg_tried_loading = true;
518
519#ifdef __ANDROID__
520 // loading the native Android libjpeg.so causes crashes
521 Con_Printf("Not opening libjpeg.so dynamically on Android - use LINK_TO_LIBJPEG instead if it is needed.\n");
522 return false;
523#endif
524
525 // Load the DLL
526 return Sys_LoadDependency (dllnames, &jpeg_dll, jpegfuncs);
527#endif
528}
static dllfunction_t jpegfuncs[]
Definition jpeg.c:435
qbool Sys_LoadDependency(const char **dllnames, dllhandle_t *handle, const dllfunction_t *fcts)
Definition sys_shared.c:131

References Con_Printf(), jpeg_dll, jpeg_tried_loading, jpegfuncs, NULL, and Sys_LoadDependency().

Referenced by Image_Compress(), and r_textures_start().

◆ JPEG_SaveImage_preflipped()

qbool JPEG_SaveImage_preflipped ( const char * filename,
int width,
int height,
unsigned char * data )

Definition at line 818 of file jpeg.c.

819{
820 struct jpeg_compress_struct cinfo;
821 struct jpeg_error_mgr jerr;
822 unsigned char *scanline;
823 unsigned int offset, linesize;
824 qfile_t* file;
825
826 // No DLL = no JPEGs
827 if (!jpeg_dll)
828 {
829 Con_Print("You need the libjpeg library to save JPEG images\n");
830 return false;
831 }
832
833 // Open the file
834 file = FS_OpenRealFile(filename, "wb", true);
835 if (!file)
836 return false;
837
838 if(setjmp(error_in_jpeg))
839 goto error_caught;
840 cinfo.err = qjpeg_std_error (&jerr);
841 cinfo.err->error_exit = JPEG_ErrorExit;
842
843 qjpeg_create_compress (&cinfo);
844 JPEG_FileDest (&cinfo, file);
845
846 // Set the parameters for compression
847 cinfo.image_width = width;
848 cinfo.image_height = height;
849 cinfo.in_color_space = JCS_RGB;
850 cinfo.input_components = 3;
851 qjpeg_set_defaults (&cinfo);
852 qjpeg_set_quality (&cinfo, (int)(scr_screenshot_jpeg_quality.value * 100), true);
854
855 // turn off subsampling (to make text look better)
856 cinfo.optimize_coding = 1;
857 cinfo.comp_info[0].h_samp_factor = 1;
858 cinfo.comp_info[0].v_samp_factor = 1;
859 cinfo.comp_info[1].h_samp_factor = 1;
860 cinfo.comp_info[1].v_samp_factor = 1;
861 cinfo.comp_info[2].h_samp_factor = 1;
862 cinfo.comp_info[2].v_samp_factor = 1;
863
864 qjpeg_start_compress (&cinfo, true);
865
866 // Compress each scanline
867 linesize = cinfo.image_width * 3;
868 offset = linesize * (cinfo.image_height - 1);
869 while (cinfo.next_scanline < cinfo.image_height)
870 {
871 scanline = &data[offset - cinfo.next_scanline * linesize];
872
873 qjpeg_write_scanlines (&cinfo, &scanline, 1);
874 }
875
876 qjpeg_finish_compress (&cinfo);
877 qjpeg_destroy_compress (&cinfo);
878
879 FS_Close (file);
880 return true;
881
882error_caught:
883 qjpeg_destroy_compress (&cinfo);
884 FS_Close (file);
885 return false;
886}
cvar_t scr_screenshot_jpeg_quality
Definition cl_screen.c:60
qfile_t * FS_OpenRealFile(const char *filepath, const char *mode, qbool quiet)
Definition fs.c:2901
int FS_Close(qfile_t *file)
Definition fs.c:2970
GLenum GLsizei width
Definition glquake.h:622
GLenum GLsizei GLsizei height
Definition glquake.h:622
GLsizeiptr const GLvoid * data
Definition glquake.h:639
GLuint GLuint GLintptr offset
Definition glquake.h:632
static void(* qjpeg_finish_compress)(j_compress_ptr cinfo)
Definition jpeg.c:422
static jboolean(* qjpeg_start_compress)(j_compress_ptr cinfo, jboolean write_all_tables)
Definition jpeg.c:429
static JDIMENSION(* qjpeg_write_scanlines)(j_compress_ptr cinfo, unsigned char **scanlines, JDIMENSION num_lines)
Definition jpeg.c:432
static void(* qjpeg_destroy_compress)(j_compress_ptr cinfo)
Definition jpeg.c:420
@ JCS_RGB
Definition jpeg.c:82
static void(* qjpeg_simple_progression)(j_compress_ptr cinfo)
Definition jpeg.c:433
static void(* qjpeg_set_quality)(j_compress_ptr cinfo, int quality, jboolean force_baseline)
Definition jpeg.c:428
static void(* qjpeg_set_defaults)(j_compress_ptr cinfo)
Definition jpeg.c:427
static void JPEG_FileDest(j_compress_ptr cinfo, qfile_t *outfile)
Definition jpeg.c:756
#define qjpeg_create_compress(cinfo)
Definition jpeg.c:413
float value
Definition cvar.h:74

References j_compress_ptr::comp_info, Con_Print(), data, j_compress_ptr::err, jpeg_error_mgr::error_exit, error_in_jpeg, FS_Close(), FS_OpenRealFile(), jpeg_component_info::h_samp_factor, height, j_compress_ptr::image_height, j_compress_ptr::image_width, j_compress_ptr::in_color_space, j_compress_ptr::input_components, JCS_RGB, jpeg_dll, JPEG_ErrorExit(), JPEG_FileDest(), j_compress_ptr::next_scanline, offset, j_compress_ptr::optimize_coding, qjpeg_create_compress, qjpeg_destroy_compress, qjpeg_finish_compress, qjpeg_set_defaults, qjpeg_set_quality, qjpeg_simple_progression, qjpeg_start_compress, qjpeg_std_error, qjpeg_write_scanlines, scr_screenshot_jpeg_quality, jpeg_component_info::v_samp_factor, cvar_t::value, and width.

Referenced by SCR_ScreenShot().

◆ JPEG_SaveImage_to_Buffer()

size_t JPEG_SaveImage_to_Buffer ( char * jpegbuf,
size_t jpegsize,
int width,
int height,
unsigned char * data )
Returns
0 if failed, or the size actually used.

Definition at line 931 of file jpeg.c.

932{
933 struct jpeg_compress_struct cinfo;
934 struct jpeg_error_mgr jerr;
935
936 int quality;
937 int quality_guess;
938 size_t result;
939
940 // No DLL = no JPEGs
941 if (!jpeg_dll)
942 {
943 Con_Print("You need the libjpeg library to save JPEG images\n");
944 return false;
945 }
946
947 if(setjmp(error_in_jpeg))
948 goto error_caught;
949 cinfo.err = qjpeg_std_error (&jerr);
950 cinfo.err->error_exit = JPEG_ErrorExit;
951
952 qjpeg_create_compress (&cinfo);
953
954#if 0
955 // used to get the formula below
956 {
957 char buf[1048576];
958 unsigned char *img;
959 int i;
960
962 for(i = 0; i < width * height * 3; ++i)
963 img[i] = rand() & 0xFF;
964
965 for(i = 0; i <= 100; ++i)
966 {
967 Con_Printf("! %d %d %d %d\n", width, height, i, (int) JPEG_try_SaveImage_to_Buffer(&cinfo, buf, sizeof(buf), i, width, height, img));
968 }
969
970 Mem_Free(img);
971 }
972#endif
973
974 //quality_guess = (int)((100 * jpegsize - 41000) / (width*height) + 2); // fits random data
975 quality_guess = (int)((256 * jpegsize - 81920) / (width*height) - 8); // fits Nexuiz's/Xonotic's map pictures
976
977 quality_guess = bound(0, quality_guess, 100);
978 quality = bound(0, quality_guess + sv_writepicture_quality.integer, 100); // assume it can do 10 failed attempts
979
980 while(!(result = JPEG_try_SaveImage_to_Buffer(&cinfo, jpegbuf, jpegsize, quality, width, height, data)))
981 {
982 --quality;
983 if(quality < 0)
984 {
985 Con_Printf("couldn't write image at all, probably too big\n");
986 return 0;
987 }
988 }
989 qjpeg_destroy_compress (&cinfo);
990 Con_DPrintf("JPEG_SaveImage_to_Buffer: guessed quality/size %d/%d, actually got %d/%d\n", quality_guess, (int)jpegsize, quality, (int)result);
991
992 return result;
993
994error_caught:
995 qjpeg_destroy_compress (&cinfo);
996 return 0;
997}
void Con_DPrintf(const char *fmt,...)
A Con_Printf that only shows up if the "developer" cvar is set.
Definition console.c:1544
static int(ZEXPORT *qz_inflate)(z_stream *strm
GLint void * img
Definition glquake.h:692
static size_t JPEG_try_SaveImage_to_Buffer(struct jpeg_compress_struct *cinfo, char *jpegbuf, size_t jpegsize, int quality, int width, int height, unsigned char *data)
Definition jpeg.c:888
cvar_t sv_writepicture_quality
Definition sv_main.c:219

References bound, buf, Con_DPrintf(), Con_Print(), Con_Printf(), data, j_compress_ptr::err, jpeg_error_mgr::error_exit, error_in_jpeg, height, i, img, int(), cvar_t::integer, jpeg_dll, JPEG_ErrorExit(), JPEG_try_SaveImage_to_Buffer(), Mem_Alloc, Mem_Free, qjpeg_create_compress, qjpeg_destroy_compress, qjpeg_std_error, sv_writepicture_quality, tempmempool, and width.

Referenced by Image_Compress().