Replace libjpeg with stb_image.h

libjpeg is a pain in the ass, especially due to Ubuntu shipping
libjpeg-turbo in jpeg8 mode as their default libjpeg, while every other
distro I checked (including debian!) ships libjpeg-turbo in jpeg6.2 mode

Thankfully stb_image.h exists - just a single header and it even
has a (much!) friendlier API.

It's not like Doom3 (or any of the mods I checked) actually use JPEGs,
but I'm sure if I'd drop support completely, someone would complain
(perhaps rightfully so).
This commit is contained in:
Daniel Gibson 2021-04-06 01:35:04 +02:00
parent bb7346579e
commit b0e0883f6e
5 changed files with 7810 additions and 161 deletions

View file

@ -153,12 +153,6 @@ endif()
find_package(ZLIB REQUIRED)
include_directories(${ZLIB_INCLUDE_DIRS})
find_package(JPEG REQUIRED)
include_directories(${JPEG_INCLUDE_DIR})
set(CMAKE_REQUIRED_INCLUDES ${JPEG_INCLUDE_DIR})
set(CMAKE_REQUIRED_LIBRARIES ${JPEG_LIBRARY})
find_package(OGG REQUIRED)
include_directories(${OGG_INCLUDE_DIR})
@ -1024,7 +1018,6 @@ if(CORE)
${VORBIS_LIBRARIES}
${OGG_LIBRARIES}
${CURL_LIBRARY}
${JPEG_LIBRARY}
${ZLIB_LIBRARY}
${SDLx_LIBRARY}
${sys_libs}
@ -1057,7 +1050,6 @@ if(DEDICATED)
${VORBIS_LIBRARIES}
${OGG_LIBRARIES}
${CURL_LIBRARY}
${JPEG_LIBRARY}
${ZLIB_LIBRARY}
${SDLx_LIBRARY}
${sys_libs}

View file

@ -34,8 +34,12 @@ If you have questions concerning this license or the applicable additional terms
#include "renderer/Cinematic.h"
// DG: get rid of libjpeg; as far as I can tell no roqs that actually use it exist
//#define ID_USE_LIBJPEG 1
#ifdef ID_USE_LIBJPEG
#include <jpeglib.h>
#include <jerror.h>
#endif
#define CIN_system 1
#define CIN_loop 2
@ -1284,11 +1288,19 @@ void idCinematicLocal::RoQReset() {
status = FMV_LOOPED;
}
#ifdef ID_USE_LIBJPEG
/* jpeg error handling */
struct jpeg_error_mgr jerr;
#endif
int JPEGBlit( byte *wStatus, byte *data, int datasize )
{
#ifndef ID_USE_LIBJPEG
// I don't think this code is actually used, because
// * the jpeg encoder parts in the roq encoder are disabled with #if 0
// * ffmpeg doesn't support ROQ_QUAD_JPEG and can decode all doom3 roqs anyway
common->Warning("Contrary to Daniel's assumption, JPEGBlit() is actually called! Please report that as a dhewm3 bug!\n");
#else
/* This struct contains the JPEG decompression parameters and pointers to
* working space (which is allocated as needed by the JPEG library).
*/
@ -1402,7 +1414,7 @@ int JPEGBlit( byte *wStatus, byte *data, int datasize )
/* At this point you may want to check to see whether any corrupt-data
* warnings occurred (test whether jerr.pub.num_warnings is nonzero).
*/
#endif
/* And we're done! */
return 1;
}

View file

@ -26,15 +26,21 @@ If you have questions concerning this license or the applicable additional terms
===========================================================================
*/
// DG: replace libjpeg with stb_image.h because it causes fewer headaches
// include this first, otherwise build breaks because of use_idStr_* #defines in Str.h
#define STB_IMAGE_IMPLEMENTATION
#define STBI_NO_HDR
#define STBI_NO_LINEAR
#define STBI_ONLY_JPEG // at least for now, only use it for JPEG
#define STBI_NO_STDIO // images are passed as buffers
#include "stb_image.h"
#include "sys/platform.h"
#include "renderer/tr_local.h"
#include "renderer/Image.h"
#include <jpeglib.h>
#include <jerror.h>
/*
This file only has a single entry point:
@ -757,50 +763,16 @@ LoadJPG
=============
*/
static void LoadJPG( const char *filename, unsigned char **pic, int *width, int *height, ID_TIME_T *timestamp ) {
/* This struct contains the JPEG decompression parameters and pointers to
* working space (which is allocated as needed by the JPEG library).
*/
struct jpeg_decompress_struct cinfo;
/* We use our private extension JPEG error handler.
* Note that this struct must live as long as the main JPEG parameter
* struct, to avoid dangling-pointer problems.
*/
/* This struct represents a JPEG error handler. It is declared separately
* because applications often want to supply a specialized error handler
* (see the second half of this file for an example). But here we just
* take the easy way out and use the standard error handler, which will
* print a message on stderr and call exit() if compression fails.
* Note that this struct must live as long as the main JPEG parameter
* struct, to avoid dangling-pointer problems.
*/
struct jpeg_error_mgr jerr;
/* More stuff */
JSAMPARRAY buffer; /* Output row buffer */
int row_stride; /* physical row width in output buffer */
unsigned char *out;
byte *fbuffer;
byte *bbuf;
/* In this example we want to open the input file before doing anything else,
* so that the setjmp() error recovery below can assume the file is open.
* VERY IMPORTANT: use "b" option to fopen() if you are on a machine that
* requires it in order to read binary files.
*/
if ( pic ) {
*pic = NULL; // until proven otherwise
}
// JDC: because fill_input_buffer() blindly copies INPUT_BUF_SIZE bytes,
// we need to make sure the file buffer is padded or it may crash
if ( pic ) {
*pic = NULL; // until proven otherwise
}
int len;
idFile *f;
f = fileSystem->OpenFileRead( filename );
idFile *f = fileSystem->OpenFileRead( filename );
if ( !f ) {
return;
}
len = f->Length();
int len = f->Length();
if ( timestamp ) {
*timestamp = f->Timestamp();
}
@ -808,120 +780,31 @@ static void LoadJPG( const char *filename, unsigned char **pic, int *width, int
fileSystem->CloseFile( f );
return; // just getting timestamp
}
fbuffer = (byte *)Mem_ClearedAlloc( len + 4096 );
byte *fbuffer = (byte *)Mem_ClearedAlloc( len );
f->Read( fbuffer, len );
fileSystem->CloseFile( f );
/* Step 1: allocate and initialize JPEG decompression object */
int w=0, h=0, comp=0;
byte* decodedImageData = stbi_load_from_memory( fbuffer, len, &w, &h, &comp, 4 );
/* We have to set up the error handler first, in case the initialization
* step fails. (Unlikely, but it could happen if you are out of memory.)
* This routine fills in the contents of struct jerr, and returns jerr's
* address which we place into the link field in cinfo.
*/
cinfo.err = jpeg_std_error(&jerr);
Mem_Free( fbuffer );
/* Now we can initialize the JPEG decompression object. */
jpeg_create_decompress(&cinfo);
if ( decodedImageData == NULL ) {
common->Warning( "stb_image was unable to load JPG %s : %s\n",
filename, stbi_failure_reason());
return;
}
/* Step 2: specify data source (eg, a file) */
jpeg_mem_src(&cinfo, fbuffer, len);
/* Step 3: read file parameters with jpeg_read_header() */
(void) jpeg_read_header(&cinfo, (boolean)true);
/* We can ignore the return value from jpeg_read_header since
* (a) suspension is not possible with the stdio data source, and
* (b) we passed TRUE to reject a tables-only JPEG file as an error.
* See libjpeg.doc for more info.
*/
/* Step 4: set parameters for decompression */
/* In this example, we don't need to change any of the defaults set by
* jpeg_read_header(), so we do nothing here.
*/
/* Step 5: Start decompressor */
(void) jpeg_start_decompress(&cinfo);
/* We can ignore the return value since suspension is not possible
* with the stdio data source.
*/
/* We may need to do some setup of our own at this point before reading
* the data. After jpeg_start_decompress() we have the correct scaled
* output image dimensions available, as well as the output colormap
* if we asked for color quantization.
* In this example, we need to make an output work buffer of the right size.
*/
/* JSAMPLEs per row in output buffer */
row_stride = cinfo.output_width * cinfo.output_components;
if (cinfo.output_components!=4) {
common->DWarning( "JPG %s is unsupported color depth (%d)",
filename, cinfo.output_components);
}
out = (byte *)R_StaticAlloc(cinfo.output_width*cinfo.output_height*4);
*pic = out;
*width = cinfo.output_width;
*height = cinfo.output_height;
/* Step 6: while (scan lines remain to be read) */
/* jpeg_read_scanlines(...); */
/* Here we use the library's state variable cinfo.output_scanline as the
* loop counter, so that we don't have to keep track ourselves.
*/
while (cinfo.output_scanline < cinfo.output_height) {
/* jpeg_read_scanlines expects an array of pointers to scanlines.
* Here the array is only one element long, but you could ask for
* more than one scanline at a time if that's more convenient.
*/
bbuf = ((out+(row_stride*cinfo.output_scanline)));
buffer = &bbuf;
(void) jpeg_read_scanlines(&cinfo, buffer, 1);
}
// clear all the alphas to 255
{
int i, j;
byte *buf;
buf = *pic;
j = cinfo.output_width * cinfo.output_height * 4;
for ( i = 3 ; i < j ; i+=4 ) {
buf[i] = 255;
}
}
/* Step 7: Finish decompression */
(void) jpeg_finish_decompress(&cinfo);
/* We can ignore the return value since suspension is not possible
* with the stdio data source.
*/
/* Step 8: Release JPEG decompression object */
/* This is an important step since it will release a good deal of memory. */
jpeg_destroy_decompress(&cinfo);
/* After finish_decompress, we can close the input file.
* Here we postpone it until after no more JPEG errors are possible,
* so as to simplify the setjmp error logic above. (Actually, I don't
* think that jpeg_destroy can do an error exit, but why assume anything...)
*/
Mem_Free( fbuffer );
/* At this point you may want to check to see whether any corrupt-data
* warnings occurred (test whether jerr.pub.num_warnings is nonzero).
*/
/* And we're done! */
// *pic must be allocated with R_StaticAlloc(), but stb_image allocates with malloc()
// (and as there is no R_StaticRealloc(), #define STBI_MALLOC etc won't help)
// so the decoded data must be copied once
int size = w*h*4;
*pic = (byte *)R_StaticAlloc( size );
memcpy( *pic, decodedImageData, size );
*width = w;
*height = h;
// now that decodedImageData has been copied into *pic, it's not needed anymore
stbi_image_free( decodedImageData );
}
//===================================================================

7762
neo/renderer/stb_image.h Normal file

File diff suppressed because it is too large Load diff

View file

@ -29,7 +29,7 @@ If you have questions concerning this license or the applicable additional terms
#define __roq_h__
//#define JPEG_INTERNALS
#include <jpeglib.h>
//#include <jpeglib.h> // DG: unused
#include "tools/compilers/roqvq/gdefs.h"
#include "tools/compilers/roqvq/roqParam.h"