mirror of
https://bitbucket.org/CPMADevs/cnq3
synced 2024-11-27 14:22:32 +00:00
8ef496d22b
the problem is that stb_image can and will allocate much more than it needs to e.g. for a 2048x2048 BGR image: it allocates an unnecessary intermediate 12 MB buffer to decode the image instead of decoding it directly into the final 16 MB RGBA buffer the old CNQ3 code didn't decode greyscale properly because of a missing macro call it also didn't range-check memory accesses at all
93 lines
2.4 KiB
C++
93 lines
2.4 KiB
C++
/*
|
|
===========================================================================
|
|
Copyright (C) 2017 Gian 'myT' Schellenbaum
|
|
|
|
This file is part of Challenge Quake 3 (CNQ3).
|
|
|
|
Challenge Quake 3 is free software; you can redistribute it
|
|
and/or modify it under the terms of the GNU General Public License as
|
|
published by the Free Software Foundation; either version 2 of the License,
|
|
or (at your option) any later version.
|
|
|
|
Challenge Quake 3 is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with Challenge Quake 3. If not, see <https://www.gnu.org/licenses/>.
|
|
===========================================================================
|
|
*/
|
|
// implements LoadSTB, which is the interface between CNQ3 and stb_image
|
|
|
|
#include "tr_local.h"
|
|
|
|
#if defined(_MSC_VER)
|
|
# pragma warning(disable: 4505) // unreferenced local function
|
|
# pragma warning(disable: 4459) // declaration of 'ri' hides global declaration
|
|
#endif
|
|
|
|
static void* q_malloc( size_t );
|
|
static void q_free( void* );
|
|
static void* q_realloc_sized( void*, size_t, size_t );
|
|
|
|
// we only support png and tga
|
|
#define STBI_MALLOC q_malloc
|
|
#define STBI_REALLOC_SIZED q_realloc_sized
|
|
#define STBI_FREE q_free
|
|
#define STBI_NO_STDIO
|
|
#define STBI_FAILURE_USERMSG
|
|
#define STBI_NO_JPEG
|
|
#define STBI_NO_TGA
|
|
#define STBI_NO_BMP
|
|
#define STBI_NO_PSD
|
|
#define STBI_NO_GIF
|
|
#define STBI_NO_HDR
|
|
#define STBI_NO_PIC
|
|
#define STBI_NO_PNM
|
|
#define STB_IMAGE_IMPLEMENTATION
|
|
#include "stb_image.h"
|
|
|
|
|
|
static void* q_malloc( size_t bytes )
|
|
{
|
|
return ri.Malloc((int)bytes);
|
|
}
|
|
|
|
|
|
static void q_free( void* buffer )
|
|
{
|
|
if (buffer != NULL)
|
|
ri.Free(buffer);
|
|
}
|
|
|
|
|
|
static void* q_realloc_sized( void* ptr, size_t oldSize, size_t newSize )
|
|
{
|
|
if (ptr == NULL)
|
|
return q_malloc(newSize);
|
|
|
|
if (newSize <= oldSize)
|
|
return ptr;
|
|
|
|
void* ptr2 = q_malloc(newSize);
|
|
memcpy(ptr2, ptr, oldSize);
|
|
q_free(ptr);
|
|
|
|
return ptr2;
|
|
}
|
|
|
|
|
|
qbool LoadSTB( const char* fileName, byte* buffer, int len, byte** pic, int* w, int* h, textureFormat_t* format )
|
|
{
|
|
int comp;
|
|
*pic = (byte*)stbi_load_from_memory(buffer, len, w, h, &comp, 4);
|
|
if (*pic == NULL) {
|
|
ri.Printf(PRINT_WARNING, "stb_image: couldn't load %s: %s\n", fileName, stbi_failure_reason());
|
|
return qfalse;
|
|
}
|
|
|
|
*format = TF_RGBA8;
|
|
|
|
return qtrue;
|
|
}
|