From 539a9ca50eda438df66e92327990b25e8c9c59fc Mon Sep 17 00:00:00 2001 From: Ray Date: Wed, 25 Oct 2017 01:24:17 +0200 Subject: Corrected ImageTextEx() - Added new function: GenImageColor() ImageDraw() should be reviewed... specially alpha blending... --- src/textures.c | 42 +++++++++++++++++++++++------------------- 1 file changed, 23 insertions(+), 19 deletions(-) (limited to 'src/textures.c') diff --git a/src/textures.c b/src/textures.c index b9fc5c19..814c3028 100644 --- a/src/textures.c +++ b/src/textures.c @@ -1032,6 +1032,7 @@ void ImageDraw(Image *dst, Image src, Rectangle srcRec, Rectangle dstRec) dstCol.r = ((srcCol.a*(srcCol.r - dstCol.r)) >> 8) + dstCol.r; dstCol.g = ((srcCol.a*(srcCol.g - dstCol.g)) >> 8) + dstCol.g; dstCol.b = ((srcCol.a*(srcCol.b - dstCol.b)) >> 8) + dstCol.b; + dstCol.a = ((srcCol.a*(srcCol.a - dstCol.a)) >> 8) + dstCol.a; dstPixels[j*dst->width + i] = dstCol; @@ -1067,6 +1068,8 @@ Image ImageTextEx(SpriteFont font, const char *text, float fontSize, int spacing int posX = 0; Vector2 imSize = MeasureTextEx(font, text, font.baseSize, spacing); + + TraceLog(LOG_WARNING, "Text Image size: %f, %f", imSize.x, imSize.y); // NOTE: glGetTexImage() not available in OpenGL ES Image imFont = GetTextureData(font.texture); @@ -1074,31 +1077,21 @@ Image ImageTextEx(SpriteFont font, const char *text, float fontSize, int spacing ImageFormat(&imFont, UNCOMPRESSED_R8G8B8A8); // Convert to 32 bit for color tint ImageColorTint(&imFont, tint); // Apply color tint to font - Color *fontPixels = GetImageData(imFont); - // Create image to store text - // NOTE: Pixels are initialized to BLANK color (0, 0, 0, 0) - Color *pixels = (Color *)calloc((int)imSize.x*(int)imSize.y, sizeof(Color)); + Image imText = GenImageColor((int)imSize.x, (int)imSize.y, BLANK); for (int i = 0; i < length; i++) { - Rectangle letterRec = font.chars[(int)text[i] - 32].rec; - - for (int y = letterRec.y; y < (letterRec.y + letterRec.height); y++) - { - for (int x = posX; x < (posX + letterRec.width); x++) - { - pixels[(y - letterRec.y)*(int)imSize.x + x] = fontPixels[y*font.texture.width + (x - posX + letterRec.x)]; - } - } + CharInfo letter = font.chars[(int)text[i] - 32]; + + ImageDraw(&imText, imFont, letter.rec, (Rectangle){ posX + letter.offsetX, + letter.offsetY, letter.rec.width, letter.rec.height }); - posX += letterRec.width + spacing; + posX += letter.advanceX + spacing; } UnloadImage(imFont); - Image imText = LoadImageEx(pixels, (int)imSize.x, (int)imSize.y); - // Scale image depending on text size if (fontSize > imSize.y) { @@ -1110,9 +1103,6 @@ Image ImageTextEx(SpriteFont font, const char *text, float fontSize, int spacing else ImageResize(&imText, (int)(imSize.x*scaleFactor), (int)(imSize.y*scaleFactor)); } - free(pixels); - free(fontPixels); - return imText; } @@ -1452,6 +1442,20 @@ void ImageColorBrightness(Image *image, int brightness) #endif // SUPPORT_IMAGE_MANIPULATION #if defined(SUPPORT_IMAGE_GENERATION) +// Generate image: plain color +Image GenImageColor(int width, int height, Color color) +{ + Color *pixels = (Color *)calloc(width*height, sizeof(Color)); + + for (int i = 0; i < width*height; i++) pixels[i] = color; + + Image image = LoadImageEx(pixels, width, height); + + free(pixels); + + return image; +} + // Generate image: vertical gradient Image GenImageGradientV(int width, int height, Color top, Color bottom) { -- cgit v1.2.3 From 415e7e972c920cbf3194c4dfbbfe402d7c4406c5 Mon Sep 17 00:00:00 2001 From: Ray San Date: Mon, 30 Oct 2017 13:51:46 +0100 Subject: Review some issues, view description - Review RPI compilation (core_drop_files not supported) - Review ImageFormat(), some issues - GetTextureData() reviewed for RPI --- examples/Makefile | 5 ++++- src/rlgl.c | 4 ++-- src/textures.c | 37 ++++++++++++++++++------------------- 3 files changed, 24 insertions(+), 22 deletions(-) (limited to 'src/textures.c') diff --git a/examples/Makefile b/examples/Makefile index 2942f110..485368c4 100644 --- a/examples/Makefile +++ b/examples/Makefile @@ -267,7 +267,6 @@ EXAMPLES = \ core/core_input_gamepad \ core/core_random_values \ core/core_color_select \ - core/core_drop_files \ core/core_storage_values \ core/core_gestures_detection \ core/core_3d_mode \ @@ -326,6 +325,10 @@ EXAMPLES = \ physac/physics_restitution \ physac/physics_shatter \ fix_dylib \ + +ifneq ($(PLATFORM),PLATFORM_RPI) + EXAMPLES += core/core_drop_files +endif CURRENT_MAKEFILE = $(lastword $(MAKEFILE_LIST)) diff --git a/src/rlgl.c b/src/rlgl.c index 35c812a6..5854a494 100644 --- a/src/rlgl.c +++ b/src/rlgl.c @@ -2226,8 +2226,8 @@ void *rlReadTexturePixels(Texture2D texture) glEnable(GL_DEPTH_TEST); //glDisable(GL_BLEND); - glViewport(0, 0, width, height); - rlOrtho(0.0, width, height, 0.0, 0.0, 1.0); + glViewport(0, 0, texture.width, texture.height); + rlOrtho(0.0, texture.width, texture.height, 0.0, 0.0, 1.0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glUseProgram(GetShaderDefault().id); diff --git a/src/textures.c b/src/textures.c index 814c3028..5e71d029 100644 --- a/src/textures.c +++ b/src/textures.c @@ -536,14 +536,13 @@ Image GetTextureData(Texture2D texture) { image.width = texture.width; image.height = texture.height; + image.format = texture.format; image.mipmaps = 1; - if (rlGetVersion() == OPENGL_ES_20) - { - // NOTE: Data retrieved on OpenGL ES 2.0 comes as RGBA (from framebuffer) - image.format = UNCOMPRESSED_R8G8B8A8; - } - else image.format = texture.format; + // NOTE: Data retrieved on OpenGL ES 2.0 should be RGBA + // coming from FBO color buffer, but it seems original + // texture format is retrieved on RPI... weird... + //image.format = UNCOMPRESSED_R8G8B8A8; TraceLog(LOG_INFO, "Texture pixel data obtained successfully"); } @@ -622,9 +621,9 @@ void ImageFormat(Image *image, int newFormat) for (int i = 0; i < image->width*image->height; i++) { - r = (unsigned char)(round((float)pixels[k].r*31/255)); - g = (unsigned char)(round((float)pixels[k].g*63/255)); - b = (unsigned char)(round((float)pixels[k].b*31/255)); + r = (unsigned char)(round((float)pixels[i].r*31.0f/255)); + g = (unsigned char)(round((float)pixels[i].g*63.0f/255)); + b = (unsigned char)(round((float)pixels[i].b*31.0f/255)); ((unsigned short *)image->data)[i] = (unsigned short)r << 11 | (unsigned short)g << 5 | (unsigned short)b; } @@ -655,9 +654,9 @@ void ImageFormat(Image *image, int newFormat) for (int i = 0; i < image->width*image->height; i++) { - r = (unsigned char)(round((float)pixels[i].r*31/255)); - g = (unsigned char)(round((float)pixels[i].g*31/255)); - b = (unsigned char)(round((float)pixels[i].b*31/255)); + r = (unsigned char)(round((float)pixels[i].r*31.0f/255)); + g = (unsigned char)(round((float)pixels[i].g*31.0f/255)); + b = (unsigned char)(round((float)pixels[i].b*31.0f/255)); a = (pixels[i].a > ALPHA_THRESHOLD) ? 1 : 0; ((unsigned short *)image->data)[i] = (unsigned short)r << 11 | (unsigned short)g << 6 | (unsigned short)b << 1 | (unsigned short)a; @@ -675,12 +674,12 @@ void ImageFormat(Image *image, int newFormat) for (int i = 0; i < image->width*image->height; i++) { - r = (unsigned char)(round((float)pixels[i].r*15/255)); - g = (unsigned char)(round((float)pixels[i].g*15/255)); - b = (unsigned char)(round((float)pixels[i].b*15/255)); - a = (unsigned char)(round((float)pixels[i].a*15/255)); - - ((unsigned short *)image->data)[i] = (unsigned short)r << 12 | (unsigned short)g << 8| (unsigned short)b << 4| (unsigned short)a; + r = (unsigned char)(round((float)pixels[i].r*15.0f/255)); + g = (unsigned char)(round((float)pixels[i].g*15.0f/255)); + b = (unsigned char)(round((float)pixels[i].b*15.0f/255)); + a = (unsigned char)(round((float)pixels[i].a*15.0f/255)); + + ((unsigned short *)image->data)[i] = (unsigned short)r << 12 | (unsigned short)g << 8 | (unsigned short)b << 4 | (unsigned short)a; } } break; @@ -801,7 +800,7 @@ void ImageToPOT(Image *image, Color fillColor) // Copy an image to a new image Image ImageCopy(Image image) { - Image newImage; + Image newImage = { 0 }; int byteSize = image.width*image.height; -- cgit v1.2.3 From 3d755d617ab6f2a86c272ffb8ecddc2621269c19 Mon Sep 17 00:00:00 2001 From: Ray San Date: Thu, 2 Nov 2017 20:08:52 +0100 Subject: Some code tweaks... --- examples/text/text_ttf_loading.c | 2 +- src/core.c | 16 +++++++++++++--- src/text.c | 3 +-- src/textures.c | 2 ++ 4 files changed, 17 insertions(+), 6 deletions(-) (limited to 'src/textures.c') diff --git a/examples/text/text_ttf_loading.c b/examples/text/text_ttf_loading.c index c9c2fb27..fedfbfb8 100644 --- a/examples/text/text_ttf_loading.c +++ b/examples/text/text_ttf_loading.c @@ -32,7 +32,7 @@ int main() GenTextureMipmaps(&font.texture); float fontSize = font.baseSize; - Vector2 fontPosition = { 40, screenHeight/2 + 50 }; + Vector2 fontPosition = { 40, screenHeight/2 - 50 }; Vector2 textSize; SetTextureFilter(font.texture, FILTER_POINT); diff --git a/src/core.c b/src/core.c index 9f7f95d2..436a4084 100644 --- a/src/core.c +++ b/src/core.c @@ -81,7 +81,7 @@ #define SUPPORT_DEFAULT_FONT #define SUPPORT_MOUSE_GESTURES #define SUPPORT_CAMERA_SYSTEM -#define SUPPORT_GESTURES_SYSTEM +//#define SUPPORT_GESTURES_SYSTEM #define SUPPORT_BUSY_WAIT_LOOP #define SUPPORT_GIF_RECORDING //------------------------------------------------- @@ -2526,6 +2526,8 @@ static void WindowSizeCallback(GLFWwindow *window, int width, int height) rlClearScreenBuffers(); // Clear screen buffers (color and depth) // Window size must be updated to be used on 3D mode to get new aspect ratio (Begin3dMode()) + // NOTE: Be careful! GLFW3 will choose the closest fullscreen resolution supported by current monitor, + // for example, if reescaling back to 800x450 (desired), it could set 720x480 (closest fullscreen supported) screenWidth = width; screenHeight = height; renderWidth = width; @@ -2777,8 +2779,16 @@ static int32_t AndroidInputCallback(struct android_app *app, AInputEvent *event) ProcessGestureEvent(gestureEvent); #else - // TODO: Support only simple touch position - + // Support only simple touch position + if (flags == AMOTION_EVENT_ACTION_DOWN) + { + // Get first touch position + touchPosition[0].x = AMotionEvent_getX(event, 0); + touchPosition[0].y = AMotionEvent_getY(event, 0); + + touchPosition[0].x /= (float)GetScreenWidth(); + touchPosition[0].y /= (float)GetScreenHeight(); + } #endif return 0; diff --git a/src/text.c b/src/text.c index ff55ae6f..8db2fc9f 100644 --- a/src/text.c +++ b/src/text.c @@ -883,11 +883,10 @@ static SpriteFont LoadTTF(const char *fileName, int fontSize, int charsCount, in scale = stbtt_ScaleForPixelHeight(&fontInfo, fontSize); stbtt_GetFontVMetrics(&fontInfo, &ascent, 0, 0); baseline = (int)(ascent*scale); - if (fontChars[0] != 32) TraceLog(LOG_WARNING, "TTF spritefont loading: first character is not SPACE(32) character"); - // NOTE: Using stb_truetype crappy packing method, no guarante the font fits the image... + // NOTE: Using stb_truetype crappy packing method, no guarantee the font fits the image... // TODO: Replace this function by a proper packing method and support random chars order, // we already receive a list (fontChars) with the ordered expected characters int result = stbtt_BakeFontBitmap(ttfBuffer, 0, fontSize, dataBitmap, textureSize, textureSize, fontChars[0], charsCount, charData); diff --git a/src/textures.c b/src/textures.c index 5e71d029..50d22c93 100644 --- a/src/textures.c +++ b/src/textures.c @@ -58,6 +58,8 @@ #define SUPPORT_FILEFORMAT_PNG #define SUPPORT_FILEFORMAT_DDS #define SUPPORT_FILEFORMAT_HDR +#define SUPPORT_FILEFORMAT_KTX +#define SUPPORT_FILEFORMAT_ASTC #define SUPPORT_IMAGE_MANIPULATION #define SUPPORT_IMAGE_GENERATION //------------------------------------------------- -- cgit v1.2.3 From 37986b26cb709e4e85463c409c191f6fbd3e2c48 Mon Sep 17 00:00:00 2001 From: Ray San Date: Mon, 6 Nov 2017 14:13:50 +0100 Subject: Corrected issue with text drawing on image --- src/textures.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'src/textures.c') diff --git a/src/textures.c b/src/textures.c index 50d22c93..090de24b 100644 --- a/src/textures.c +++ b/src/textures.c @@ -1088,7 +1088,8 @@ Image ImageTextEx(SpriteFont font, const char *text, float fontSize, int spacing ImageDraw(&imText, imFont, letter.rec, (Rectangle){ posX + letter.offsetX, letter.offsetY, letter.rec.width, letter.rec.height }); - posX += letter.advanceX + spacing; + if (letter.advanceX == 0) posX += letter.rec.width + spacing; + else posX += letter.advanceX + spacing; } UnloadImage(imFont); -- cgit v1.2.3 From a6f9cc5629841b7adf3d9ff21eaa2aaabb3e4bc1 Mon Sep 17 00:00:00 2001 From: Ray Date: Mon, 11 Dec 2017 11:55:02 +0100 Subject: Remove rres support Let the user choose if using rres external library --- src/audio.c | 13 -- src/external/tinfl.c | 592 --------------------------------------------------- src/raylib.h | 16 -- src/rres.h | 483 ----------------------------------------- src/text.c | 29 +-- src/textures.c | 13 +- src/utils.c | 3 - src/utils.h | 2 - 8 files changed, 2 insertions(+), 1149 deletions(-) delete mode 100644 src/external/tinfl.c delete mode 100644 src/rres.h (limited to 'src/textures.c') diff --git a/src/audio.c b/src/audio.c index d397b064..15f50a24 100644 --- a/src/audio.c +++ b/src/audio.c @@ -812,19 +812,6 @@ Wave LoadWave(const char *fileName) #endif #if defined(SUPPORT_FILEFORMAT_FLAC) else if (IsFileExtension(fileName, ".flac")) wave = LoadFLAC(fileName); -#endif -#if !defined(AUDIO_STANDALONE) - else if (IsFileExtension(fileName, ".rres")) - { - RRES rres = LoadResource(fileName, 0); - - // NOTE: Parameters for RRES_TYPE_WAVE are: sampleCount, sampleRate, sampleSize, channels - - if (rres[0].type == RRES_TYPE_WAVE) wave = LoadWaveEx(rres[0].data, rres[0].param1, rres[0].param2, rres[0].param3, rres[0].param4); - else TraceLog(LOG_WARNING, "[%s] Resource file does not contain wave data", fileName); - - UnloadResource(rres); - } #endif else TraceLog(LOG_WARNING, "[%s] Audio fileformat not supported, it can't be loaded", fileName); diff --git a/src/external/tinfl.c b/src/external/tinfl.c deleted file mode 100644 index a17a156b..00000000 --- a/src/external/tinfl.c +++ /dev/null @@ -1,592 +0,0 @@ -/* tinfl.c v1.11 - public domain inflate with zlib header parsing/adler32 checking (inflate-only subset of miniz.c) - See "unlicense" statement at the end of this file. - Rich Geldreich , last updated May 20, 2011 - Implements RFC 1950: http://www.ietf.org/rfc/rfc1950.txt and RFC 1951: http://www.ietf.org/rfc/rfc1951.txt - - The entire decompressor coroutine is implemented in tinfl_decompress(). The other functions are optional high-level helpers. -*/ -#ifndef TINFL_HEADER_INCLUDED -#define TINFL_HEADER_INCLUDED - -#include - -typedef unsigned char mz_uint8; -typedef signed short mz_int16; -typedef unsigned short mz_uint16; -typedef unsigned int mz_uint32; -typedef unsigned int mz_uint; -typedef unsigned long long mz_uint64; - -#if defined(_M_IX86) || defined(_M_X64) -// Set MINIZ_USE_UNALIGNED_LOADS_AND_STORES to 1 if integer loads and stores to unaligned addresses are acceptable on the target platform (slightly faster). -#define MINIZ_USE_UNALIGNED_LOADS_AND_STORES 1 -// Set MINIZ_LITTLE_ENDIAN to 1 if the processor is little endian. -#define MINIZ_LITTLE_ENDIAN 1 -#endif - -#if defined(_WIN64) || defined(__MINGW64__) || defined(_LP64) || defined(__LP64__) -// Set MINIZ_HAS_64BIT_REGISTERS to 1 if the processor has 64-bit general purpose registers (enables 64-bit bitbuffer in inflator) -#define MINIZ_HAS_64BIT_REGISTERS 1 -#endif - -// Works around MSVC's spammy "warning C4127: conditional expression is constant" message. -#ifdef _MSC_VER - #define MZ_MACRO_END while (0, 0) -#else - #define MZ_MACRO_END while (0) -#endif - -// Decompression flags used by tinfl_decompress(). -// TINFL_FLAG_PARSE_ZLIB_HEADER: If set, the input has a valid zlib header and ends with an adler32 checksum (it's a valid zlib stream). Otherwise, the input is a raw deflate stream. -// TINFL_FLAG_HAS_MORE_INPUT: If set, there are more input bytes available beyond the end of the supplied input buffer. If clear, the input buffer contains all remaining input. -// TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF: If set, the output buffer is large enough to hold the entire decompressed stream. If clear, the output buffer is at least the size of the dictionary (typically 32KB). -// TINFL_FLAG_COMPUTE_ADLER32: Force adler-32 checksum computation of the decompressed bytes. -enum -{ - TINFL_FLAG_PARSE_ZLIB_HEADER = 1, - TINFL_FLAG_HAS_MORE_INPUT = 2, - TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF = 4, - TINFL_FLAG_COMPUTE_ADLER32 = 8 -}; - -// High level decompression functions: -// tinfl_decompress_mem_to_heap() decompresses a block in memory to a heap block allocated via malloc(). -// On entry: -// pSrc_buf, src_buf_len: Pointer and size of the Deflate or zlib source data to decompress. -// On return: -// Function returns a pointer to the decompressed data, or NULL on failure. -// *pOut_len will be set to the decompressed data's size, which could be larger than src_buf_len on uncompressible data. -// The caller must free() the returned block when it's no longer needed. -void *tinfl_decompress_mem_to_heap(const void *pSrc_buf, size_t src_buf_len, size_t *pOut_len, int flags); - -// tinfl_decompress_mem_to_mem() decompresses a block in memory to another block in memory. -// Returns TINFL_DECOMPRESS_MEM_TO_MEM_FAILED on failure, or the number of bytes written on success. -#define TINFL_DECOMPRESS_MEM_TO_MEM_FAILED ((size_t)(-1)) -size_t tinfl_decompress_mem_to_mem(void *pOut_buf, size_t out_buf_len, const void *pSrc_buf, size_t src_buf_len, int flags); - -// tinfl_decompress_mem_to_callback() decompresses a block in memory to an internal 32KB buffer, and a user provided callback function will be called to flush the buffer. -// Returns 1 on success or 0 on failure. -typedef int (*tinfl_put_buf_func_ptr)(const void* pBuf, int len, void *pUser); -int tinfl_decompress_mem_to_callback(const void *pIn_buf, size_t *pIn_buf_size, tinfl_put_buf_func_ptr pPut_buf_func, void *pPut_buf_user, int flags); - -struct tinfl_decompressor_tag; typedef struct tinfl_decompressor_tag tinfl_decompressor; - -// Max size of LZ dictionary. -#define TINFL_LZ_DICT_SIZE 32768 - -// Return status. -typedef enum -{ - TINFL_STATUS_BAD_PARAM = -3, - TINFL_STATUS_ADLER32_MISMATCH = -2, - TINFL_STATUS_FAILED = -1, - TINFL_STATUS_DONE = 0, - TINFL_STATUS_NEEDS_MORE_INPUT = 1, - TINFL_STATUS_HAS_MORE_OUTPUT = 2 -} tinfl_status; - -// Initializes the decompressor to its initial state. -#define tinfl_init(r) do { (r)->m_state = 0; } MZ_MACRO_END -#define tinfl_get_adler32(r) (r)->m_check_adler32 - -// Main low-level decompressor coroutine function. This is the only function actually needed for decompression. All the other functions are just high-level helpers for improved usability. -// This is a universal API, i.e. it can be used as a building block to build any desired higher level decompression API. In the limit case, it can be called once per every byte input or output. -tinfl_status tinfl_decompress(tinfl_decompressor *r, const mz_uint8 *pIn_buf_next, size_t *pIn_buf_size, mz_uint8 *pOut_buf_start, mz_uint8 *pOut_buf_next, size_t *pOut_buf_size, const mz_uint32 decomp_flags); - -// Internal/private bits follow. -enum -{ - TINFL_MAX_HUFF_TABLES = 3, TINFL_MAX_HUFF_SYMBOLS_0 = 288, TINFL_MAX_HUFF_SYMBOLS_1 = 32, TINFL_MAX_HUFF_SYMBOLS_2 = 19, - TINFL_FAST_LOOKUP_BITS = 10, TINFL_FAST_LOOKUP_SIZE = 1 << TINFL_FAST_LOOKUP_BITS -}; - -typedef struct -{ - mz_uint8 m_code_size[TINFL_MAX_HUFF_SYMBOLS_0]; - mz_int16 m_look_up[TINFL_FAST_LOOKUP_SIZE], m_tree[TINFL_MAX_HUFF_SYMBOLS_0 * 2]; -} tinfl_huff_table; - -#if MINIZ_HAS_64BIT_REGISTERS - #define TINFL_USE_64BIT_BITBUF 1 -#endif - -#if TINFL_USE_64BIT_BITBUF - typedef mz_uint64 tinfl_bit_buf_t; - #define TINFL_BITBUF_SIZE (64) -#else - typedef mz_uint32 tinfl_bit_buf_t; - #define TINFL_BITBUF_SIZE (32) -#endif - -struct tinfl_decompressor_tag -{ - mz_uint32 m_state, m_num_bits, m_zhdr0, m_zhdr1, m_z_adler32, m_final, m_type, m_check_adler32, m_dist, m_counter, m_num_extra, m_table_sizes[TINFL_MAX_HUFF_TABLES]; - tinfl_bit_buf_t m_bit_buf; - size_t m_dist_from_out_buf_start; - tinfl_huff_table m_tables[TINFL_MAX_HUFF_TABLES]; - mz_uint8 m_raw_header[4], m_len_codes[TINFL_MAX_HUFF_SYMBOLS_0 + TINFL_MAX_HUFF_SYMBOLS_1 + 137]; -}; - -#endif // #ifdef TINFL_HEADER_INCLUDED - -// ------------------- End of Header: Implementation follows. (If you only want the header, define MINIZ_HEADER_FILE_ONLY.) - -#ifndef TINFL_HEADER_FILE_ONLY - -#include - -// MZ_MALLOC, etc. are only used by the optional high-level helper functions. -#ifdef MINIZ_NO_MALLOC - #define MZ_MALLOC(x) NULL - #define MZ_FREE(x) x, ((void)0) - #define MZ_REALLOC(p, x) NULL -#else - #define MZ_MALLOC(x) malloc(x) - #define MZ_FREE(x) free(x) - #define MZ_REALLOC(p, x) realloc(p, x) -#endif - -#define MZ_MAX(a,b) (((a)>(b))?(a):(b)) -#define MZ_MIN(a,b) (((a)<(b))?(a):(b)) -#define MZ_CLEAR_OBJ(obj) memset(&(obj), 0, sizeof(obj)) - -#if MINIZ_USE_UNALIGNED_LOADS_AND_STORES && MINIZ_LITTLE_ENDIAN - #define MZ_READ_LE16(p) *((const mz_uint16 *)(p)) - #define MZ_READ_LE32(p) *((const mz_uint32 *)(p)) -#else - #define MZ_READ_LE16(p) ((mz_uint32)(((const mz_uint8 *)(p))[0]) | ((mz_uint32)(((const mz_uint8 *)(p))[1]) << 8U)) - #define MZ_READ_LE32(p) ((mz_uint32)(((const mz_uint8 *)(p))[0]) | ((mz_uint32)(((const mz_uint8 *)(p))[1]) << 8U) | ((mz_uint32)(((const mz_uint8 *)(p))[2]) << 16U) | ((mz_uint32)(((const mz_uint8 *)(p))[3]) << 24U)) -#endif - -#define TINFL_MEMCPY(d, s, l) memcpy(d, s, l) -#define TINFL_MEMSET(p, c, l) memset(p, c, l) - -#define TINFL_CR_BEGIN switch(r->m_state) { case 0: -#define TINFL_CR_RETURN(state_index, result) do { status = result; r->m_state = state_index; goto common_exit; case state_index:; } MZ_MACRO_END -#define TINFL_CR_RETURN_FOREVER(state_index, result) do { for ( ; ; ) { TINFL_CR_RETURN(state_index, result); } } MZ_MACRO_END -#define TINFL_CR_FINISH } - -// TODO: If the caller has indicated that there's no more input, and we attempt to read beyond the input buf, then something is wrong with the input because the inflator never -// reads ahead more than it needs to. Currently TINFL_GET_BYTE() pads the end of the stream with 0's in this scenario. -#define TINFL_GET_BYTE(state_index, c) do { \ - if (pIn_buf_cur >= pIn_buf_end) { \ - for ( ; ; ) { \ - if (decomp_flags & TINFL_FLAG_HAS_MORE_INPUT) { \ - TINFL_CR_RETURN(state_index, TINFL_STATUS_NEEDS_MORE_INPUT); \ - if (pIn_buf_cur < pIn_buf_end) { \ - c = *pIn_buf_cur++; \ - break; \ - } \ - } else { \ - c = 0; \ - break; \ - } \ - } \ - } else c = *pIn_buf_cur++; } MZ_MACRO_END - -#define TINFL_NEED_BITS(state_index, n) do { mz_uint c; TINFL_GET_BYTE(state_index, c); bit_buf |= (((tinfl_bit_buf_t)c) << num_bits); num_bits += 8; } while (num_bits < (mz_uint)(n)) -#define TINFL_SKIP_BITS(state_index, n) do { if (num_bits < (mz_uint)(n)) { TINFL_NEED_BITS(state_index, n); } bit_buf >>= (n); num_bits -= (n); } MZ_MACRO_END -#define TINFL_GET_BITS(state_index, b, n) do { if (num_bits < (mz_uint)(n)) { TINFL_NEED_BITS(state_index, n); } b = bit_buf & ((1 << (n)) - 1); bit_buf >>= (n); num_bits -= (n); } MZ_MACRO_END - -// TINFL_HUFF_BITBUF_FILL() is only used rarely, when the number of bytes remaining in the input buffer falls below 2. -// It reads just enough bytes from the input stream that are needed to decode the next Huffman code (and absolutely no more). It works by trying to fully decode a -// Huffman code by using whatever bits are currently present in the bit buffer. If this fails, it reads another byte, and tries again until it succeeds or until the -// bit buffer contains >=15 bits (deflate's max. Huffman code size). -#define TINFL_HUFF_BITBUF_FILL(state_index, pHuff) \ - do { \ - temp = (pHuff)->m_look_up[bit_buf & (TINFL_FAST_LOOKUP_SIZE - 1)]; \ - if (temp >= 0) { \ - code_len = temp >> 9; \ - if ((code_len) && (num_bits >= code_len)) \ - break; \ - } else if (num_bits > TINFL_FAST_LOOKUP_BITS) { \ - code_len = TINFL_FAST_LOOKUP_BITS; \ - do { \ - temp = (pHuff)->m_tree[~temp + ((bit_buf >> code_len++) & 1)]; \ - } while ((temp < 0) && (num_bits >= (code_len + 1))); if (temp >= 0) break; \ - } TINFL_GET_BYTE(state_index, c); bit_buf |= (((tinfl_bit_buf_t)c) << num_bits); num_bits += 8; \ - } while (num_bits < 15); - -// TINFL_HUFF_DECODE() decodes the next Huffman coded symbol. It's more complex than you would initially expect because the zlib API expects the decompressor to never read -// beyond the final byte of the deflate stream. (In other words, when this macro wants to read another byte from the input, it REALLY needs another byte in order to fully -// decode the next Huffman code.) Handling this properly is particularly important on raw deflate (non-zlib) streams, which aren't followed by a byte aligned adler-32. -// The slow path is only executed at the very end of the input buffer. -#define TINFL_HUFF_DECODE(state_index, sym, pHuff) do { \ - int temp; mz_uint code_len, c; \ - if (num_bits < 15) { \ - if ((pIn_buf_end - pIn_buf_cur) < 2) { \ - TINFL_HUFF_BITBUF_FILL(state_index, pHuff); \ - } else { \ - bit_buf |= (((tinfl_bit_buf_t)pIn_buf_cur[0]) << num_bits) | (((tinfl_bit_buf_t)pIn_buf_cur[1]) << (num_bits + 8)); pIn_buf_cur += 2; num_bits += 16; \ - } \ - } \ - if ((temp = (pHuff)->m_look_up[bit_buf & (TINFL_FAST_LOOKUP_SIZE - 1)]) >= 0) \ - code_len = temp >> 9, temp &= 511; \ - else { \ - code_len = TINFL_FAST_LOOKUP_BITS; do { temp = (pHuff)->m_tree[~temp + ((bit_buf >> code_len++) & 1)]; } while (temp < 0); \ - } sym = temp; bit_buf >>= code_len; num_bits -= code_len; } MZ_MACRO_END - -tinfl_status tinfl_decompress(tinfl_decompressor *r, const mz_uint8 *pIn_buf_next, size_t *pIn_buf_size, mz_uint8 *pOut_buf_start, mz_uint8 *pOut_buf_next, size_t *pOut_buf_size, const mz_uint32 decomp_flags) -{ - static const int s_length_base[31] = { 3,4,5,6,7,8,9,10,11,13, 15,17,19,23,27,31,35,43,51,59, 67,83,99,115,131,163,195,227,258,0,0 }; - static const int s_length_extra[31]= { 0,0,0,0,0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,0,0,0 }; - static const int s_dist_base[32] = { 1,2,3,4,5,7,9,13,17,25,33,49,65,97,129,193, 257,385,513,769,1025,1537,2049,3073,4097,6145,8193,12289,16385,24577,0,0}; - static const int s_dist_extra[32] = { 0,0,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13}; - static const mz_uint8 s_length_dezigzag[19] = { 16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15 }; - static const int s_min_table_sizes[3] = { 257, 1, 4 }; - - tinfl_status status = TINFL_STATUS_FAILED; mz_uint32 num_bits, dist, counter, num_extra; tinfl_bit_buf_t bit_buf; - const mz_uint8 *pIn_buf_cur = pIn_buf_next, *const pIn_buf_end = pIn_buf_next + *pIn_buf_size; - mz_uint8 *pOut_buf_cur = pOut_buf_next, *const pOut_buf_end = pOut_buf_next + *pOut_buf_size; - size_t out_buf_size_mask = (decomp_flags & TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF) ? (size_t)-1 : ((pOut_buf_next - pOut_buf_start) + *pOut_buf_size) - 1, dist_from_out_buf_start; - - // Ensure the output buffer's size is a power of 2, unless the output buffer is large enough to hold the entire output file (in which case it doesn't matter). - if (((out_buf_size_mask + 1) & out_buf_size_mask) || (pOut_buf_next < pOut_buf_start)) { *pIn_buf_size = *pOut_buf_size = 0; return TINFL_STATUS_BAD_PARAM; } - - num_bits = r->m_num_bits; bit_buf = r->m_bit_buf; dist = r->m_dist; counter = r->m_counter; num_extra = r->m_num_extra; dist_from_out_buf_start = r->m_dist_from_out_buf_start; - TINFL_CR_BEGIN - - bit_buf = num_bits = dist = counter = num_extra = r->m_zhdr0 = r->m_zhdr1 = 0; r->m_z_adler32 = r->m_check_adler32 = 1; - if (decomp_flags & TINFL_FLAG_PARSE_ZLIB_HEADER) - { - TINFL_GET_BYTE(1, r->m_zhdr0); TINFL_GET_BYTE(2, r->m_zhdr1); - counter = (((r->m_zhdr0 * 256 + r->m_zhdr1) % 31 != 0) || (r->m_zhdr1 & 32) || ((r->m_zhdr0 & 15) != 8)); - if (!(decomp_flags & TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF)) counter |= (((1U << (8U + (r->m_zhdr0 >> 4))) > 32768U) || ((out_buf_size_mask + 1) < (size_t)(1U << (8U + (r->m_zhdr0 >> 4))))); - if (counter) { TINFL_CR_RETURN_FOREVER(36, TINFL_STATUS_FAILED); } - } - - do - { - TINFL_GET_BITS(3, r->m_final, 3); r->m_type = r->m_final >> 1; - if (r->m_type == 0) - { - TINFL_SKIP_BITS(5, num_bits & 7); - for (counter = 0; counter < 4; ++counter) { if (num_bits) TINFL_GET_BITS(6, r->m_raw_header[counter], 8); else TINFL_GET_BYTE(7, r->m_raw_header[counter]); } - if ((counter = (r->m_raw_header[0] | (r->m_raw_header[1] << 8))) != (mz_uint)(0xFFFF ^ (r->m_raw_header[2] | (r->m_raw_header[3] << 8)))) { TINFL_CR_RETURN_FOREVER(39, TINFL_STATUS_FAILED); } - while ((counter) && (num_bits)) - { - TINFL_GET_BITS(51, dist, 8); - while (pOut_buf_cur >= pOut_buf_end) { TINFL_CR_RETURN(52, TINFL_STATUS_HAS_MORE_OUTPUT); } - *pOut_buf_cur++ = (mz_uint8)dist; - counter--; - } - while (counter) - { - size_t n; while (pOut_buf_cur >= pOut_buf_end) { TINFL_CR_RETURN(9, TINFL_STATUS_HAS_MORE_OUTPUT); } - while (pIn_buf_cur >= pIn_buf_end) - { - if (decomp_flags & TINFL_FLAG_HAS_MORE_INPUT) - { - TINFL_CR_RETURN(38, TINFL_STATUS_NEEDS_MORE_INPUT); - } - else - { - TINFL_CR_RETURN_FOREVER(40, TINFL_STATUS_FAILED); - } - } - n = MZ_MIN(MZ_MIN((size_t)(pOut_buf_end - pOut_buf_cur), (size_t)(pIn_buf_end - pIn_buf_cur)), counter); - TINFL_MEMCPY(pOut_buf_cur, pIn_buf_cur, n); pIn_buf_cur += n; pOut_buf_cur += n; counter -= (mz_uint)n; - } - } - else if (r->m_type == 3) - { - TINFL_CR_RETURN_FOREVER(10, TINFL_STATUS_FAILED); - } - else - { - if (r->m_type == 1) - { - mz_uint8 *p = r->m_tables[0].m_code_size; mz_uint i; - r->m_table_sizes[0] = 288; r->m_table_sizes[1] = 32; TINFL_MEMSET(r->m_tables[1].m_code_size, 5, 32); - for ( i = 0; i <= 143; ++i) *p++ = 8; for ( ; i <= 255; ++i) *p++ = 9; for ( ; i <= 279; ++i) *p++ = 7; for ( ; i <= 287; ++i) *p++ = 8; - } - else - { - for (counter = 0; counter < 3; counter++) { TINFL_GET_BITS(11, r->m_table_sizes[counter], "\05\05\04"[counter]); r->m_table_sizes[counter] += s_min_table_sizes[counter]; } - MZ_CLEAR_OBJ(r->m_tables[2].m_code_size); for (counter = 0; counter < r->m_table_sizes[2]; counter++) { mz_uint s; TINFL_GET_BITS(14, s, 3); r->m_tables[2].m_code_size[s_length_dezigzag[counter]] = (mz_uint8)s; } - r->m_table_sizes[2] = 19; - } - for ( ; (int)r->m_type >= 0; r->m_type--) - { - int tree_next, tree_cur; tinfl_huff_table *pTable; - mz_uint i, j, used_syms, total, sym_index, next_code[17], total_syms[16]; pTable = &r->m_tables[r->m_type]; MZ_CLEAR_OBJ(total_syms); MZ_CLEAR_OBJ(pTable->m_look_up); MZ_CLEAR_OBJ(pTable->m_tree); - for (i = 0; i < r->m_table_sizes[r->m_type]; ++i) total_syms[pTable->m_code_size[i]]++; - used_syms = 0, total = 0; next_code[0] = next_code[1] = 0; - for (i = 1; i <= 15; ++i) { used_syms += total_syms[i]; next_code[i + 1] = (total = ((total + total_syms[i]) << 1)); } - if ((65536 != total) && (used_syms > 1)) - { - TINFL_CR_RETURN_FOREVER(35, TINFL_STATUS_FAILED); - } - for (tree_next = -1, sym_index = 0; sym_index < r->m_table_sizes[r->m_type]; ++sym_index) - { - mz_uint rev_code = 0, l, cur_code, code_size = pTable->m_code_size[sym_index]; if (!code_size) continue; - cur_code = next_code[code_size]++; for (l = code_size; l > 0; l--, cur_code >>= 1) rev_code = (rev_code << 1) | (cur_code & 1); - if (code_size <= TINFL_FAST_LOOKUP_BITS) { mz_int16 k = (mz_int16)((code_size << 9) | sym_index); while (rev_code < TINFL_FAST_LOOKUP_SIZE) { pTable->m_look_up[rev_code] = k; rev_code += (1 << code_size); } continue; } - if (0 == (tree_cur = pTable->m_look_up[rev_code & (TINFL_FAST_LOOKUP_SIZE - 1)])) { pTable->m_look_up[rev_code & (TINFL_FAST_LOOKUP_SIZE - 1)] = (mz_int16)tree_next; tree_cur = tree_next; tree_next -= 2; } - rev_code >>= (TINFL_FAST_LOOKUP_BITS - 1); - for (j = code_size; j > (TINFL_FAST_LOOKUP_BITS + 1); j--) - { - tree_cur -= ((rev_code >>= 1) & 1); - if (!pTable->m_tree[-tree_cur - 1]) { pTable->m_tree[-tree_cur - 1] = (mz_int16)tree_next; tree_cur = tree_next; tree_next -= 2; } else tree_cur = pTable->m_tree[-tree_cur - 1]; - } - tree_cur -= ((rev_code >>= 1) & 1); pTable->m_tree[-tree_cur - 1] = (mz_int16)sym_index; - } - if (r->m_type == 2) - { - for (counter = 0; counter < (r->m_table_sizes[0] + r->m_table_sizes[1]); ) - { - mz_uint s; TINFL_HUFF_DECODE(16, dist, &r->m_tables[2]); if (dist < 16) { r->m_len_codes[counter++] = (mz_uint8)dist; continue; } - if ((dist == 16) && (!counter)) - { - TINFL_CR_RETURN_FOREVER(17, TINFL_STATUS_FAILED); - } - num_extra = "\02\03\07"[dist - 16]; TINFL_GET_BITS(18, s, num_extra); s += "\03\03\013"[dist - 16]; - TINFL_MEMSET(r->m_len_codes + counter, (dist == 16) ? r->m_len_codes[counter - 1] : 0, s); counter += s; - } - if ((r->m_table_sizes[0] + r->m_table_sizes[1]) != counter) - { - TINFL_CR_RETURN_FOREVER(21, TINFL_STATUS_FAILED); - } - TINFL_MEMCPY(r->m_tables[0].m_code_size, r->m_len_codes, r->m_table_sizes[0]); TINFL_MEMCPY(r->m_tables[1].m_code_size, r->m_len_codes + r->m_table_sizes[0], r->m_table_sizes[1]); - } - } - for ( ; ; ) - { - mz_uint8 *pSrc; - for ( ; ; ) - { - if (((pIn_buf_end - pIn_buf_cur) < 4) || ((pOut_buf_end - pOut_buf_cur) < 2)) - { - TINFL_HUFF_DECODE(23, counter, &r->m_tables[0]); - if (counter >= 256) - break; - while (pOut_buf_cur >= pOut_buf_end) { TINFL_CR_RETURN(24, TINFL_STATUS_HAS_MORE_OUTPUT); } - *pOut_buf_cur++ = (mz_uint8)counter; - } - else - { - int sym2; mz_uint code_len; -#if TINFL_USE_64BIT_BITBUF - if (num_bits < 30) { bit_buf |= (((tinfl_bit_buf_t)MZ_READ_LE32(pIn_buf_cur)) << num_bits); pIn_buf_cur += 4; num_bits += 32; } -#else - if (num_bits < 15) { bit_buf |= (((tinfl_bit_buf_t)MZ_READ_LE16(pIn_buf_cur)) << num_bits); pIn_buf_cur += 2; num_bits += 16; } -#endif - if ((sym2 = r->m_tables[0].m_look_up[bit_buf & (TINFL_FAST_LOOKUP_SIZE - 1)]) >= 0) - code_len = sym2 >> 9; - else - { - code_len = TINFL_FAST_LOOKUP_BITS; do { sym2 = r->m_tables[0].m_tree[~sym2 + ((bit_buf >> code_len++) & 1)]; } while (sym2 < 0); - } - counter = sym2; bit_buf >>= code_len; num_bits -= code_len; - if (counter & 256) - break; - -#if !TINFL_USE_64BIT_BITBUF - if (num_bits < 15) { bit_buf |= (((tinfl_bit_buf_t)MZ_READ_LE16(pIn_buf_cur)) << num_bits); pIn_buf_cur += 2; num_bits += 16; } -#endif - if ((sym2 = r->m_tables[0].m_look_up[bit_buf & (TINFL_FAST_LOOKUP_SIZE - 1)]) >= 0) - code_len = sym2 >> 9; - else - { - code_len = TINFL_FAST_LOOKUP_BITS; do { sym2 = r->m_tables[0].m_tree[~sym2 + ((bit_buf >> code_len++) & 1)]; } while (sym2 < 0); - } - bit_buf >>= code_len; num_bits -= code_len; - - pOut_buf_cur[0] = (mz_uint8)counter; - if (sym2 & 256) - { - pOut_buf_cur++; - counter = sym2; - break; - } - pOut_buf_cur[1] = (mz_uint8)sym2; - pOut_buf_cur += 2; - } - } - if ((counter &= 511) == 256) break; - - num_extra = s_length_extra[counter - 257]; counter = s_length_base[counter - 257]; - if (num_extra) { mz_uint extra_bits; TINFL_GET_BITS(25, extra_bits, num_extra); counter += extra_bits; } - - TINFL_HUFF_DECODE(26, dist, &r->m_tables[1]); - num_extra = s_dist_extra[dist]; dist = s_dist_base[dist]; - if (num_extra) { mz_uint extra_bits; TINFL_GET_BITS(27, extra_bits, num_extra); dist += extra_bits; } - - dist_from_out_buf_start = pOut_buf_cur - pOut_buf_start; - if ((dist > dist_from_out_buf_start) && (decomp_flags & TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF)) - { - TINFL_CR_RETURN_FOREVER(37, TINFL_STATUS_FAILED); - } - - pSrc = pOut_buf_start + ((dist_from_out_buf_start - dist) & out_buf_size_mask); - - if ((MZ_MAX(pOut_buf_cur, pSrc) + counter) > pOut_buf_end) - { - while (counter--) - { - while (pOut_buf_cur >= pOut_buf_end) { TINFL_CR_RETURN(53, TINFL_STATUS_HAS_MORE_OUTPUT); } - *pOut_buf_cur++ = pOut_buf_start[(dist_from_out_buf_start++ - dist) & out_buf_size_mask]; - } - continue; - } -#if MINIZ_USE_UNALIGNED_LOADS_AND_STORES - else if ((counter >= 9) && (counter <= dist)) - { - const mz_uint8 *pSrc_end = pSrc + (counter & ~7); - do - { - ((mz_uint32 *)pOut_buf_cur)[0] = ((const mz_uint32 *)pSrc)[0]; - ((mz_uint32 *)pOut_buf_cur)[1] = ((const mz_uint32 *)pSrc)[1]; - pOut_buf_cur += 8; - } while ((pSrc += 8) < pSrc_end); - if ((counter &= 7) < 3) - { - if (counter) - { - pOut_buf_cur[0] = pSrc[0]; - if (counter > 1) - pOut_buf_cur[1] = pSrc[1]; - pOut_buf_cur += counter; - } - continue; - } - } -#endif - do - { - pOut_buf_cur[0] = pSrc[0]; - pOut_buf_cur[1] = pSrc[1]; - pOut_buf_cur[2] = pSrc[2]; - pOut_buf_cur += 3; pSrc += 3; - } while ((int)(counter -= 3) > 2); - if ((int)counter > 0) - { - pOut_buf_cur[0] = pSrc[0]; - if ((int)counter > 1) - pOut_buf_cur[1] = pSrc[1]; - pOut_buf_cur += counter; - } - } - } - } while (!(r->m_final & 1)); - if (decomp_flags & TINFL_FLAG_PARSE_ZLIB_HEADER) - { - TINFL_SKIP_BITS(32, num_bits & 7); for (counter = 0; counter < 4; ++counter) { mz_uint s; if (num_bits) TINFL_GET_BITS(41, s, 8); else TINFL_GET_BYTE(42, s); r->m_z_adler32 = (r->m_z_adler32 << 8) | s; } - } - TINFL_CR_RETURN_FOREVER(34, TINFL_STATUS_DONE); - TINFL_CR_FINISH - -common_exit: - r->m_num_bits = num_bits; r->m_bit_buf = bit_buf; r->m_dist = dist; r->m_counter = counter; r->m_num_extra = num_extra; r->m_dist_from_out_buf_start = dist_from_out_buf_start; - *pIn_buf_size = pIn_buf_cur - pIn_buf_next; *pOut_buf_size = pOut_buf_cur - pOut_buf_next; - if ((decomp_flags & (TINFL_FLAG_PARSE_ZLIB_HEADER | TINFL_FLAG_COMPUTE_ADLER32)) && (status >= 0)) - { - const mz_uint8 *ptr = pOut_buf_next; size_t buf_len = *pOut_buf_size; - mz_uint32 i, s1 = r->m_check_adler32 & 0xffff, s2 = r->m_check_adler32 >> 16; size_t block_len = buf_len % 5552; - while (buf_len) - { - for (i = 0; i + 7 < block_len; i += 8, ptr += 8) - { - s1 += ptr[0], s2 += s1; s1 += ptr[1], s2 += s1; s1 += ptr[2], s2 += s1; s1 += ptr[3], s2 += s1; - s1 += ptr[4], s2 += s1; s1 += ptr[5], s2 += s1; s1 += ptr[6], s2 += s1; s1 += ptr[7], s2 += s1; - } - for ( ; i < block_len; ++i) s1 += *ptr++, s2 += s1; - s1 %= 65521U, s2 %= 65521U; buf_len -= block_len; block_len = 5552; - } - r->m_check_adler32 = (s2 << 16) + s1; if ((status == TINFL_STATUS_DONE) && (decomp_flags & TINFL_FLAG_PARSE_ZLIB_HEADER) && (r->m_check_adler32 != r->m_z_adler32)) status = TINFL_STATUS_ADLER32_MISMATCH; - } - return status; -} - -// Higher level helper functions. -void *tinfl_decompress_mem_to_heap(const void *pSrc_buf, size_t src_buf_len, size_t *pOut_len, int flags) -{ - tinfl_decompressor decomp; void *pBuf = NULL, *pNew_buf; size_t src_buf_ofs = 0, out_buf_capacity = 0; - *pOut_len = 0; - tinfl_init(&decomp); - for ( ; ; ) - { - size_t src_buf_size = src_buf_len - src_buf_ofs, dst_buf_size = out_buf_capacity - *pOut_len, new_out_buf_capacity; - tinfl_status status = tinfl_decompress(&decomp, (const mz_uint8*)pSrc_buf + src_buf_ofs, &src_buf_size, (mz_uint8*)pBuf, pBuf ? (mz_uint8*)pBuf + *pOut_len : NULL, &dst_buf_size, - (flags & ~TINFL_FLAG_HAS_MORE_INPUT) | TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF); - if ((status < 0) || (status == TINFL_STATUS_NEEDS_MORE_INPUT)) - { - MZ_FREE(pBuf); *pOut_len = 0; return NULL; - } - src_buf_ofs += src_buf_size; - *pOut_len += dst_buf_size; - if (status == TINFL_STATUS_DONE) break; - new_out_buf_capacity = out_buf_capacity * 2; if (new_out_buf_capacity < 128) new_out_buf_capacity = 128; - pNew_buf = MZ_REALLOC(pBuf, new_out_buf_capacity); - if (!pNew_buf) - { - MZ_FREE(pBuf); *pOut_len = 0; return NULL; - } - pBuf = pNew_buf; out_buf_capacity = new_out_buf_capacity; - } - return pBuf; -} - -size_t tinfl_decompress_mem_to_mem(void *pOut_buf, size_t out_buf_len, const void *pSrc_buf, size_t src_buf_len, int flags) -{ - tinfl_decompressor decomp; tinfl_status status; tinfl_init(&decomp); - status = tinfl_decompress(&decomp, (const mz_uint8*)pSrc_buf, &src_buf_len, (mz_uint8*)pOut_buf, (mz_uint8*)pOut_buf, &out_buf_len, (flags & ~TINFL_FLAG_HAS_MORE_INPUT) | TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF); - return (status != TINFL_STATUS_DONE) ? TINFL_DECOMPRESS_MEM_TO_MEM_FAILED : out_buf_len; -} - -int tinfl_decompress_mem_to_callback(const void *pIn_buf, size_t *pIn_buf_size, tinfl_put_buf_func_ptr pPut_buf_func, void *pPut_buf_user, int flags) -{ - int result = 0; - tinfl_decompressor decomp; - mz_uint8 *pDict = (mz_uint8*)MZ_MALLOC(TINFL_LZ_DICT_SIZE); size_t in_buf_ofs = 0, dict_ofs = 0; - if (!pDict) - return TINFL_STATUS_FAILED; - tinfl_init(&decomp); - for ( ; ; ) - { - size_t in_buf_size = *pIn_buf_size - in_buf_ofs, dst_buf_size = TINFL_LZ_DICT_SIZE - dict_ofs; - tinfl_status status = tinfl_decompress(&decomp, (const mz_uint8*)pIn_buf + in_buf_ofs, &in_buf_size, pDict, pDict + dict_ofs, &dst_buf_size, - (flags & ~(TINFL_FLAG_HAS_MORE_INPUT | TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF))); - in_buf_ofs += in_buf_size; - if ((dst_buf_size) && (!(*pPut_buf_func)(pDict + dict_ofs, (int)dst_buf_size, pPut_buf_user))) - break; - if (status != TINFL_STATUS_HAS_MORE_OUTPUT) - { - result = (status == TINFL_STATUS_DONE); - break; - } - dict_ofs = (dict_ofs + dst_buf_size) & (TINFL_LZ_DICT_SIZE - 1); - } - MZ_FREE(pDict); - *pIn_buf_size = in_buf_ofs; - return result; -} - -#endif // #ifndef TINFL_HEADER_FILE_ONLY - -/* - This is free and unencumbered software released into the public domain. - - Anyone is free to copy, modify, publish, use, compile, sell, or - distribute this software, either in source code form or as a compiled - binary, for any purpose, commercial or non-commercial, and by any - means. - - In jurisdictions that recognize copyright laws, the author or authors - of this software dedicate any and all copyright interest in the - software to the public domain. We make this dedication for the benefit - of the public at large and to the detriment of our heirs and - successors. We intend this dedication to be an overt act of - relinquishment in perpetuity of all present and future rights to this - software under copyright law. - - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, - EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. - IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR - OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, - ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR - OTHER DEALINGS IN THE SOFTWARE. - - For more information, please refer to -*/ diff --git a/src/raylib.h b/src/raylib.h index a3914c4d..ac739830 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -495,22 +495,6 @@ typedef struct AudioStream { unsigned int buffers[2]; // OpenAL audio buffers (double buffering) } AudioStream; -// rRES data returned when reading a resource, -// it contains all required data for user (24 byte) -typedef struct RRESData { - unsigned int type; // Resource type (4 byte) - - unsigned int param1; // Resouce parameter 1 (4 byte) - unsigned int param2; // Resouce parameter 2 (4 byte) - unsigned int param3; // Resouce parameter 3 (4 byte) - unsigned int param4; // Resouce parameter 4 (4 byte) - - void *data; // Resource data pointer (4 byte) -} RRESData; - -// RRES type (pointer to RRESData array) -typedef struct RRESData *RRES; - // Head-Mounted-Display device parameters typedef struct VrDeviceInfo { int hResolution; // HMD horizontal resolution in pixels diff --git a/src/rres.h b/src/rres.h deleted file mode 100644 index 75faf640..00000000 --- a/src/rres.h +++ /dev/null @@ -1,483 +0,0 @@ -/********************************************************************************************** -* -* rres v1.0 - raylib resource (rRES) custom fileformat management functions -* -* CONFIGURATION: -* -* #define RREM_IMPLEMENTATION -* Generates the implementation of the library into the included file. -* If not defined, the library is in header only mode and can be included in other headers -* or source files without problems. But only ONE file should hold the implementation. -* -* DEPENDENCIES: -* tinfl - DEFLATE decompression functions -* -* -* LICENSE: zlib/libpng -* -* Copyright (c) 2016-2017 Ramon Santamaria (@raysan5) -* -* This software is provided "as-is", without any express or implied warranty. In no event -* will the authors be held liable for any damages arising from the use of this software. -* -* Permission is granted to anyone to use this software for any purpose, including commercial -* applications, and to alter it and redistribute it freely, subject to the following restrictions: -* -* 1. The origin of this software must not be misrepresented; you must not claim that you -* wrote the original software. If you use this software in a product, an acknowledgment -* in the product documentation would be appreciated but is not required. -* -* 2. Altered source versions must be plainly marked as such, and must not be misrepresented -* as being the original software. -* -* 3. This notice may not be removed or altered from any source distribution. -* -**********************************************************************************************/ - -/* -References: - RIFF file-format: http://www.johnloomis.org/cpe102/asgn/asgn1/riff.html - ZIP file-format: https://en.wikipedia.org/wiki/Zip_(file_format) - http://www.onicos.com/staff/iz/formats/zip.html - XNB file-format: http://xbox.create.msdn.com/en-US/sample/xnb_format -*/ - -#ifndef RRES_H -#define RRES_H - -#if !defined(RRES_STANDALONE) - #include "raylib.h" -#endif - -//#define RRES_STATIC -#ifdef RRES_STATIC - #define RRESDEF static // Functions just visible to module including this file -#else - #ifdef __cplusplus - #define RRESDEF extern "C" // Functions visible from other files (no name mangling of functions in C++) - #else - #define RRESDEF extern // Functions visible from other files - #endif -#endif - -//---------------------------------------------------------------------------------- -// Defines and Macros -//---------------------------------------------------------------------------------- -#define MAX_RESOURCES_SUPPORTED 256 - -//---------------------------------------------------------------------------------- -// Types and Structures Definition -// NOTE: Some types are required for RAYGUI_STANDALONE usage -//---------------------------------------------------------------------------------- -#if defined(RRES_STANDALONE) - // rRES data returned when reading a resource, it contains all required data for user (24 byte) - // NOTE: Using void *data pointer, so we can load to image.data, wave.data, mesh.*, (unsigned char *) - typedef struct RRESData { - unsigned int type; // Resource type (4 byte) - - unsigned int param1; // Resouce parameter 1 (4 byte) - unsigned int param2; // Resouce parameter 2 (4 byte) - unsigned int param3; // Resouce parameter 3 (4 byte) - unsigned int param4; // Resouce parameter 4 (4 byte) - - void *data; // Resource data pointer (4 byte) - } RRESData; - - // RRES type (pointer to RRESData array) - typedef struct RRESData *RRES; // Resource pointer - - // RRESData type - typedef enum { - RRES_TYPE_RAW = 0, - RRES_TYPE_IMAGE, - RRES_TYPE_WAVE, - RRES_TYPE_VERTEX, - RRES_TYPE_TEXT, - RRES_TYPE_FONT_IMAGE, - RRES_TYPE_FONT_CHARDATA, // CharInfo { int value, recX, recY, recWidth, recHeight, offsetX, offsetY, xAdvance } - RRES_TYPE_DIRECTORY - } RRESDataType; - -// Parameters information depending on resource type - -// RRES_TYPE_RAW params: -// RRES_TYPE_IMAGE params: width, height, mipmaps, format -// RRES_TYPE_WAVE params: sampleCount, sampleRate, sampleSize, channels -// RRES_TYPE_VERTEX params: vertexCount, vertexType, vertexFormat // Use masks instead? -// RRES_TYPE_TEXT params: charsCount, cultureCode -// RRES_TYPE_FONT_IMAGE params: width, height, format, mipmaps; -// RRES_TYPE_FONT_CHARDATA params: charsCount, baseSize -// RRES_TYPE_DIRECTORY params: fileCount, directoryCount - -// SpriteFont = RRES_TYPE_FONT_IMAGE chunk + RRES_TYPE_FONT_DATA chunk -// Mesh = multiple RRES_TYPE_VERTEX chunks - - -#endif - -//---------------------------------------------------------------------------------- -// Global variables -//---------------------------------------------------------------------------------- -//... - -//---------------------------------------------------------------------------------- -// Module Functions Declaration -//---------------------------------------------------------------------------------- -//RRESDEF RRESData LoadResourceData(const char *rresFileName, int rresId, int part); -RRESDEF RRES LoadResource(const char *fileName, int rresId); -RRESDEF void UnloadResource(RRES rres); - -/* -QUESTION: How to load each type of data from RRES ? - -rres->type == RRES_TYPE_RAW -unsigned char data = (unsigned char *)rres[0]->data; - -rres->type == RRES_TYPE_IMAGE -Image image; -image.data = rres[0]->data; // Be careful, duplicate pointer -image.width = rres[0]->param1; -image.height = rres[0]->param2; -image.mipmaps = rres[0]->param3; -image.format = rres[0]->format; - -rres->type == RRES_TYPE_WAVE -Wave wave; -wave.data = rres[0]->data; -wave.sampleCount = rres[0]->param1; -wave.sampleRate = rres[0]->param2; -wave.sampleSize = rres[0]->param3; -wave.channels = rres[0]->param4; - -rres->type == RRES_TYPE_VERTEX (multiple parts) -Mesh mesh; -mesh.vertexCount = rres[0]->param1; -mesh.vertices = (float *)rres[0]->data; -mesh.texcoords = (float *)rres[1]->data; -mesh.normals = (float *)rres[2]->data; -mesh.tangents = (float *)rres[3]->data; -mesh.tangents = (unsigned char *)rres[4]->data; - -rres->type == RRES_TYPE_TEXT -unsigned char *text = (unsigned char *)rres->data; -Shader shader = LoadShaderText(text, rres->param1); Shader LoadShaderText(const char *shdrText, int length); - -rres->type == RRES_TYPE_FONT_IMAGE (multiple parts) -rres->type == RRES_TYPE_FONT_CHARDATA -SpriteFont font; -font.texture = LoadTextureFromImage(image); // rres[0] -font.chars = (CharInfo *)rres[1]->data; -font.charsCount = rres[1]->param1; -font.baseSize = rres[1]->param2; - -rres->type == RRES_TYPE_DIRECTORY -unsigned char *fileNames = (unsigned char *)rres[0]->data; // fileNames separed by \n -int filesCount = rres[0]->param1; -*/ - -#endif // RRES_H - - -/*********************************************************************************** -* -* RRES IMPLEMENTATION -* -************************************************************************************/ - -#if defined(RRES_IMPLEMENTATION) - -#include // Required for: FILE, fopen(), fclose() - -// Check if custom malloc/free functions defined, if not, using standard ones -#if !defined(RRES_MALLOC) - #include // Required for: malloc(), free() - - #define RRES_MALLOC(size) malloc(size) - #define RRES_FREE(ptr) free(ptr) -#endif - -#include "external/tinfl.c" // Required for: tinfl_decompress_mem_to_mem() - // NOTE: DEFLATE algorythm data decompression - -//---------------------------------------------------------------------------------- -// Defines and Macros -//---------------------------------------------------------------------------------- -//... - -//---------------------------------------------------------------------------------- -// Types and Structures Definition -//---------------------------------------------------------------------------------- - -// rRES file header (8 byte) -typedef struct { - char id[4]; // File identifier: rRES (4 byte) - unsigned short version; // File version and subversion (2 byte) - unsigned short count; // Number of resources in this file (2 byte) -} RRESFileHeader; - -// rRES info header, every resource includes this header (16 byte + 16 byte) -typedef struct { - unsigned int id; // Resource unique identifier (4 byte) - unsigned char dataType; // Resource data type (1 byte) - unsigned char compType; // Resource data compression type (1 byte) - unsigned char cryptoType; // Resource data encryption type (1 byte) - unsigned char partsCount; // Resource data parts count, used for splitted data (1 byte) - unsigned int dataSize; // Resource data size (compressed or not, only DATA) (4 byte) - unsigned int uncompSize; // Resource data size (uncompressed, only DATA) (4 byte) - - unsigned int param1; // Resouce parameter 1 (4 byte) - unsigned int param2; // Resouce parameter 2 (4 byte) - unsigned int param3; // Resouce parameter 3 (4 byte) - unsigned int param4; // Resouce parameter 4 (4 byte) -} RRESInfoHeader; - -// Compression types -typedef enum { - RRES_COMP_NONE = 0, // No data compression - RRES_COMP_DEFLATE, // DEFLATE compression - RRES_COMP_LZ4, // LZ4 compression - RRES_COMP_LZMA, // LZMA compression - RRES_COMP_BROTLI, // BROTLI compression - // gzip, zopfli, lzo, zstd // Other compression algorythms... -} RRESCompressionType; - -// Encryption types -typedef enum { - RRES_CRYPTO_NONE = 0, // No data encryption - RRES_CRYPTO_XOR, // XOR (128 bit) encryption - RRES_CRYPTO_AES, // RIJNDAEL (128 bit) encryption (AES) - RRES_CRYPTO_TDES, // Triple DES encryption - RRES_CRYPTO_BLOWFISH, // BLOWFISH encryption - RRES_CRYPTO_XTEA, // XTEA encryption - // twofish, RC5, RC6 // Other encryption algorythm... -} RRESEncryptionType; - -// Image/Texture data type -typedef enum { - RRES_IM_UNCOMP_GRAYSCALE = 1, // 8 bit per pixel (no alpha) - RRES_IM_UNCOMP_GRAY_ALPHA, // 16 bpp (2 channels) - RRES_IM_UNCOMP_R5G6B5, // 16 bpp - RRES_IM_UNCOMP_R8G8B8, // 24 bpp - RRES_IM_UNCOMP_R5G5B5A1, // 16 bpp (1 bit alpha) - RRES_IM_UNCOMP_R4G4B4A4, // 16 bpp (4 bit alpha) - RRES_IM_UNCOMP_R8G8B8A8, // 32 bpp - RRES_IM_COMP_DXT1_RGB, // 4 bpp (no alpha) - RRES_IM_COMP_DXT1_RGBA, // 4 bpp (1 bit alpha) - RRES_IM_COMP_DXT3_RGBA, // 8 bpp - RRES_IM_COMP_DXT5_RGBA, // 8 bpp - RRES_IM_COMP_ETC1_RGB, // 4 bpp - RRES_IM_COMP_ETC2_RGB, // 4 bpp - RRES_IM_COMP_ETC2_EAC_RGBA, // 8 bpp - RRES_IM_COMP_PVRT_RGB, // 4 bpp - RRES_IM_COMP_PVRT_RGBA, // 4 bpp - RRES_IM_COMP_ASTC_4x4_RGBA, // 8 bpp - RRES_IM_COMP_ASTC_8x8_RGBA // 2 bpp - //... -} RRESImageFormat; - -// Vertex data type -typedef enum { - RRES_VERT_POSITION, - RRES_VERT_TEXCOORD1, - RRES_VERT_TEXCOORD2, - RRES_VERT_TEXCOORD3, - RRES_VERT_TEXCOORD4, - RRES_VERT_NORMAL, - RRES_VERT_TANGENT, - RRES_VERT_COLOR, - RRES_VERT_INDEX, - //... -} RRESVertexType; - -// Vertex data format type -typedef enum { - RRES_VERT_BYTE, - RRES_VERT_SHORT, - RRES_VERT_INT, - RRES_VERT_HFLOAT, - RRES_VERT_FLOAT, - //... -} RRESVertexFormat; - -#if defined(RRES_STANDALONE) -typedef enum { LOG_INFO = 0, LOG_ERROR, LOG_WARNING, LOG_DEBUG, LOG_OTHER } TraceLogType; -#endif - -//---------------------------------------------------------------------------------- -// Global Variables Definition -//---------------------------------------------------------------------------------- -//... - -//---------------------------------------------------------------------------------- -// Module specific Functions Declaration -//---------------------------------------------------------------------------------- -static void *DecompressData(const unsigned char *data, unsigned long compSize, int uncompSize); - -//---------------------------------------------------------------------------------- -// Module Functions Definition -//---------------------------------------------------------------------------------- - -// Load resource from file by id (could be multiple parts) -// NOTE: Returns uncompressed data with parameters, search resource by id -RRESDEF RRES LoadResource(const char *fileName, int rresId) -{ - RRES rres = { 0 }; - - RRESFileHeader fileHeader; - RRESInfoHeader infoHeader; - - FILE *rresFile = fopen(fileName, "rb"); - - if (rresFile == NULL) TraceLog(LOG_WARNING, "[%s] rRES raylib resource file could not be opened", fileName); - else - { - // Read rres file info header - fread(&fileHeader.id[0], sizeof(char), 1, rresFile); - fread(&fileHeader.id[1], sizeof(char), 1, rresFile); - fread(&fileHeader.id[2], sizeof(char), 1, rresFile); - fread(&fileHeader.id[3], sizeof(char), 1, rresFile); - fread(&fileHeader.version, sizeof(short), 1, rresFile); - fread(&fileHeader.count, sizeof(short), 1, rresFile); - - // Verify "rRES" identifier - if ((fileHeader.id[0] != 'r') && (fileHeader.id[1] != 'R') && (fileHeader.id[2] != 'E') && (fileHeader.id[3] != 'S')) - { - TraceLog(LOG_WARNING, "[%s] This is not a valid raylib resource file", fileName); - } - else - { - for (int i = 0; i < fileHeader.count; i++) - { - // Read resource info and parameters - fread(&infoHeader, sizeof(RRESInfoHeader), 1, rresFile); - - if (infoHeader.id == rresId) - { - rres = (RRES)malloc(sizeof(RRESData)*infoHeader.partsCount); - - // Load all required resources parts - for (int k = 0; k < infoHeader.partsCount; k++) - { - // TODO: Verify again that rresId is the same in every part - - // Register data type and parameters - rres[k].type = infoHeader.dataType; - rres[k].param1 = infoHeader.param1; - rres[k].param2 = infoHeader.param2; - rres[k].param3 = infoHeader.param3; - rres[k].param4 = infoHeader.param4; - - // Read resource data block - void *data = RRES_MALLOC(infoHeader.dataSize); - fread(data, infoHeader.dataSize, 1, rresFile); - - if (infoHeader.compType == RRES_COMP_DEFLATE) - { - void *uncompData = DecompressData(data, infoHeader.dataSize, infoHeader.uncompSize); - - rres[k].data = uncompData; - - RRES_FREE(data); - } - else rres[k].data = data; - - if (rres[k].data != NULL) TraceLog(LOG_INFO, "[%s][ID %i] Resource data loaded successfully", fileName, (int)infoHeader.id); - - // Read next part - fread(&infoHeader, sizeof(RRESInfoHeader), 1, rresFile); - } - } - else - { - // Skip required data to read next resource infoHeader - fseek(rresFile, infoHeader.dataSize, SEEK_CUR); - } - } - - if (rres[0].data == NULL) TraceLog(LOG_WARNING, "[%s][ID %i] Requested resource could not be found", fileName, (int)rresId); - } - - fclose(rresFile); - } - - return rres; -} - -// Unload resource data -RRESDEF void UnloadResource(RRES rres) -{ - // TODO: When you load resource... how many parts conform it? depends on type? --> Not clear... - - if (rres[0].data != NULL) free(rres[0].data); -} - -//---------------------------------------------------------------------------------- -// Module specific Functions Definition -//---------------------------------------------------------------------------------- - -// Data decompression function -// NOTE: Allocated data MUST be freed by user -static void *DecompressData(const unsigned char *data, unsigned long compSize, int uncompSize) -{ - int tempUncompSize; - void *uncompData; - - // Allocate buffer to hold decompressed data - uncompData = (mz_uint8 *)RRES_MALLOC((size_t)uncompSize); - - // Check correct memory allocation - if (uncompData == NULL) - { - TraceLog(LOG_WARNING, "Out of memory while decompressing data"); - } - else - { - // Decompress data - tempUncompSize = tinfl_decompress_mem_to_mem(uncompData, (size_t)uncompSize, data, compSize, 1); - - if (tempUncompSize == -1) - { - TraceLog(LOG_WARNING, "Data decompression failed"); - RRES_FREE(uncompData); - } - - if (uncompSize != (int)tempUncompSize) - { - TraceLog(LOG_WARNING, "Expected uncompressed size do not match, data may be corrupted"); - TraceLog(LOG_WARNING, " -- Expected uncompressed size: %i", uncompSize); - TraceLog(LOG_WARNING, " -- Returned uncompressed size: %i", tempUncompSize); - } - - TraceLog(LOG_INFO, "Data decompressed successfully from %u bytes to %u bytes", (mz_uint32)compSize, (mz_uint32)tempUncompSize); - } - - return uncompData; -} - -// Some required functions for rres standalone module version -#if defined(RRES_STANDALONE) -// Show trace log messages (LOG_INFO, LOG_WARNING, LOG_ERROR, LOG_DEBUG) -void TraceLog(int logType, const char *text, ...) -{ - va_list args; - va_start(args, text); - - switch (msgType) - { - case LOG_INFO: fprintf(stdout, "INFO: "); break; - case LOG_ERROR: fprintf(stdout, "ERROR: "); break; - case LOG_WARNING: fprintf(stdout, "WARNING: "); break; - case LOG_DEBUG: fprintf(stdout, "DEBUG: "); break; - default: break; - } - - vfprintf(stdout, text, args); - fprintf(stdout, "\n"); - - va_end(args); - - if (msgType == LOG_ERROR) exit(1); -} -#endif - -#endif // RRES_IMPLEMENTATION diff --git a/src/text.c b/src/text.c index 8db2fc9f..0215bb5b 100644 --- a/src/text.c +++ b/src/text.c @@ -288,35 +288,8 @@ SpriteFont LoadSpriteFont(const char *fileName) SpriteFont spriteFont = { 0 }; - // Check file extension - if (IsFileExtension(fileName, ".rres")) - { - // TODO: Read multiple resource blocks from file (RRES_FONT_IMAGE, RRES_FONT_CHARDATA) - RRES rres = LoadResource(fileName, 0); - - // Load sprite font texture - if (rres[0].type == RRES_TYPE_FONT_IMAGE) - { - // NOTE: Parameters for RRES_FONT_IMAGE type are: width, height, format, mipmaps - Image image = LoadImagePro(rres[0].data, rres[0].param1, rres[0].param2, rres[0].param3); - spriteFont.texture = LoadTextureFromImage(image); - UnloadImage(image); - } - - // Load sprite characters data - if (rres[1].type == RRES_TYPE_FONT_CHARDATA) - { - // NOTE: Parameters for RRES_FONT_CHARDATA type are: fontSize, charsCount - spriteFont.baseSize = rres[1].param1; - spriteFont.charsCount = rres[1].param2; - spriteFont.chars = rres[1].data; - } - - // TODO: Do not free rres.data memory (chars info data!) - //UnloadResource(rres[0]); - } #if defined(SUPPORT_FILEFORMAT_TTF) - else if (IsFileExtension(fileName, ".ttf")) spriteFont = LoadSpriteFontEx(fileName, DEFAULT_TTF_FONTSIZE, 0, NULL); + if (IsFileExtension(fileName, ".ttf")) spriteFont = LoadSpriteFontEx(fileName, DEFAULT_TTF_FONTSIZE, 0, NULL); #endif #if defined(SUPPORT_FILEFORMAT_FNT) else if (IsFileExtension(fileName, ".fnt")) spriteFont = LoadBMFont(fileName); diff --git a/src/textures.c b/src/textures.c index 090de24b..6bd306f4 100644 --- a/src/textures.c +++ b/src/textures.c @@ -167,18 +167,7 @@ Image LoadImage(const char *fileName) { Image image = { 0 }; - if (IsFileExtension(fileName, ".rres")) - { - RRES rres = LoadResource(fileName, 0); - - // NOTE: Parameters for RRES_TYPE_IMAGE are: width, height, format, mipmaps - - if (rres[0].type == RRES_TYPE_IMAGE) image = LoadImagePro(rres[0].data, rres[0].param1, rres[0].param2, rres[0].param3); - else TraceLog(LOG_WARNING, "[%s] Resource file does not contain image data", fileName); - - UnloadResource(rres); - } - else if ((IsFileExtension(fileName, ".png")) + if ((IsFileExtension(fileName, ".png")) #if defined(SUPPORT_FILEFORMAT_BMP) || (IsFileExtension(fileName, ".bmp")) #endif diff --git a/src/utils.c b/src/utils.c index 967ed916..aaa5bdb4 100644 --- a/src/utils.c +++ b/src/utils.c @@ -66,9 +66,6 @@ #include "external/stb_image_write.h" // Required for: stbi_write_bmp(), stbi_write_png() #endif -#define RRES_IMPLEMENTATION -#include "rres.h" - //---------------------------------------------------------------------------------- // Global Variables Definition //---------------------------------------------------------------------------------- diff --git a/src/utils.h b/src/utils.h index 64592c73..a1801245 100644 --- a/src/utils.h +++ b/src/utils.h @@ -32,8 +32,6 @@ #include // Required for: AAssetManager #endif -#include "rres.h" - #define SUPPORT_SAVE_PNG //---------------------------------------------------------------------------------- -- cgit v1.2.3 From 08fc886afd88907516dc7519ac7542fa30e5f369 Mon Sep 17 00:00:00 2001 From: user Date: Thu, 14 Dec 2017 11:40:08 +0100 Subject: added proper define checks for png-save if it's disabled --- src/core.c | 2 +- src/textures.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'src/textures.c') diff --git a/src/core.c b/src/core.c index ec7db978..d4646268 100644 --- a/src/core.c +++ b/src/core.c @@ -1134,7 +1134,7 @@ void SetConfigFlags(char flags) // Takes a screenshot of current screen (saved a .png) void TakeScreenshot(const char *fileName) { -#if defined(PLATFORM_DESKTOP) || defined(PLATFORM_RPI) +#if (defined(PLATFORM_DESKTOP) || defined(PLATFORM_RPI)) && defined(SUPPORT_SAVE_PNG) unsigned char *imgData = rlReadScreenPixels(renderWidth, renderHeight); SavePNG(fileName, imgData, renderWidth, renderHeight, 4); // Save image as PNG free(imgData); diff --git a/src/textures.c b/src/textures.c index 6bd306f4..55749118 100644 --- a/src/textures.c +++ b/src/textures.c @@ -554,7 +554,7 @@ void UpdateTexture(Texture2D texture, const void *pixels) // Save image to a PNG file void SaveImageAs(const char* fileName, Image image) { -#if defined(PLATFORM_DESKTOP) || defined(PLATFORM_RPI) +#if (defined(PLATFORM_DESKTOP) || defined(PLATFORM_RPI)) && defined(SUPPORT_SAVE_PNG) unsigned char* imgData = (unsigned char*)GetImageData(image); // this works since Color is just a container for the RGBA values SavePNG(fileName, imgData, image.width, image.height, 4); free(imgData); -- cgit v1.2.3 From 53ad53d05127ac083f7439c5c2e1c2ab5e73e1c0 Mon Sep 17 00:00:00 2001 From: Ray San Date: Fri, 15 Dec 2017 13:44:31 +0100 Subject: Manually review previous PR --- src/core.c | 21 ++++++++++----------- src/raylib.h | 14 ++++++-------- src/raymath.h | 12 ++++++++---- src/rlgl.c | 6 ++++++ src/rlgl.h | 10 ++++------ src/textures.c | 5 ++--- 6 files changed, 36 insertions(+), 32 deletions(-) (limited to 'src/textures.c') diff --git a/src/core.c b/src/core.c index 8e26d2b1..ec7db978 100644 --- a/src/core.c +++ b/src/core.c @@ -153,9 +153,8 @@ //#define GLFW_DLL // Using GLFW DLL on Windows -> No, we use static version! #if !defined(SUPPORT_BUSY_WAIT_LOOP) && defined(_WIN32) - // NOTE: Those functions require linking with winmm library - unsigned int __stdcall timeBeginPeriod(unsigned int uPeriod); - unsigned int __stdcall timeEndPeriod(unsigned int uPeriod); + __stdcall unsigned int timeBeginPeriod(unsigned int uPeriod); + __stdcall unsigned int timeEndPeriod(unsigned int uPeriod); #endif #endif @@ -352,7 +351,7 @@ extern void UnloadDefaultFont(void); // [Module: text] Unloads default fo static void InitGraphicsDevice(int width, int height); // Initialize graphics device static void SetupFramebufferSize(int displayWidth, int displayHeight); static void InitTimer(void); // Initialize timer - double GetTime(void); // Returns time since InitTimer() was run +static double GetTime(void); // Returns time since InitTimer() was run static void Wait(float ms); // Wait for some milliseconds (stop program execution) static bool GetKeyStatus(int key); // Returns if a key has been pressed static bool GetMouseButtonStatus(int button); // Returns if a mouse button has been pressed @@ -413,7 +412,7 @@ static void *GamepadThread(void *arg); // Mouse reading thread #if defined(PLATFORM_DESKTOP) || defined(PLATFORM_RPI) || defined(PLATFORM_WEB) // Initialize window and OpenGL context // NOTE: data parameter could be used to pass any kind of required data to the initialization -void InitRLWindow(int width, int height, void *data) +void InitWindow(int width, int height, void *data) { TraceLog(LOG_INFO, "Initializing raylib (v1.9-dev)"); @@ -477,7 +476,7 @@ void InitRLWindow(int width, int height, void *data) #if defined(PLATFORM_ANDROID) // Initialize window and OpenGL context (and Android activity) // NOTE: data parameter could be used to pass any kind of required data to the initialization -void InitRLWindow(int width, int height, void *data) +void InitWindow(int width, int height, void *data) { TraceLog(LOG_INFO, "Initializing raylib (v1.9-dev)"); @@ -538,7 +537,7 @@ void InitRLWindow(int width, int height, void *data) #endif // Close window and unload OpenGL context -void CloseRLWindow(void) +void CloseWindow(void) { #if defined(SUPPORT_GIF_RECORDING) if (gifRecording) @@ -715,7 +714,7 @@ int GetScreenHeight(void) } // Show mouse cursor -void ShowRLCursor() +void ShowCursor() { #if defined(PLATFORM_DESKTOP) #if defined(__linux__) @@ -728,7 +727,7 @@ void ShowRLCursor() } // Hides mouse cursor -void HideRLCursor() +void HideCursor() { #if defined(PLATFORM_DESKTOP) #if defined(__linux__) @@ -1135,7 +1134,7 @@ void SetConfigFlags(char flags) // Takes a screenshot of current screen (saved a .png) void TakeScreenshot(const char *fileName) { -#if (defined(PLATFORM_DESKTOP) || defined(PLATFORM_RPI)) && defined(SUPPORT_SAVE_PNG) +#if defined(PLATFORM_DESKTOP) || defined(PLATFORM_RPI) unsigned char *imgData = rlReadScreenPixels(renderWidth, renderHeight); SavePNG(fileName, imgData, renderWidth, renderHeight, 4); // Save image as PNG free(imgData); @@ -2120,7 +2119,7 @@ static void InitTimer(void) } // Get current time measure (in seconds) since InitTimer() -double GetTime(void) +static double GetTime(void) { #if defined(PLATFORM_DESKTOP) || defined(PLATFORM_WEB) return glfwGetTime(); diff --git a/src/raylib.h b/src/raylib.h index 16595b5b..a581929e 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -682,8 +682,8 @@ extern "C" { // Prevents name mangling of functions //------------------------------------------------------------------------------------ // Window-related functions -RLAPI void InitRLWindow(int width, int height, void *data); // Initialize window and OpenGL context -RLAPI void CloseRLWindow(void); // Close window and unload OpenGL context +RLAPI void InitWindow(int width, int height, void *data); // Initialize window and OpenGL context +RLAPI void CloseWindow(void); // Close window and unload OpenGL context RLAPI bool WindowShouldClose(void); // Check if KEY_ESCAPE pressed or Close icon pressed RLAPI bool IsWindowMinimized(void); // Check if window has been minimized (or lost focus) RLAPI void ToggleFullscreen(void); // Toggle fullscreen mode (only PLATFORM_DESKTOP) @@ -696,8 +696,8 @@ RLAPI int GetScreenWidth(void); // Get current RLAPI int GetScreenHeight(void); // Get current screen height // Cursor-related functions -RLAPI void ShowRLCursor(void); // Shows cursor -RLAPI void HideRLCursor(void); // Hides cursor +RLAPI void ShowCursor(void); // Shows cursor +RLAPI void HideCursor(void); // Hides cursor RLAPI bool IsCursorHidden(void); // Check if cursor is not visible RLAPI void EnableCursor(void); // Enables cursor (unlock cursor) RLAPI void DisableCursor(void); // Disables cursor (lock cursor) @@ -722,9 +722,7 @@ RLAPI Matrix GetCameraMatrix(Camera camera); // Returns cam RLAPI void SetTargetFPS(int fps); // Set target FPS (maximum) RLAPI int GetFPS(void); // Returns current FPS RLAPI float GetFrameTime(void); // Returns time in seconds for last frame drawn - -RLAPI double GetTime(void); // Return time in seconds - +//RLAPI double GetCurrentTime(void); // Return current time in seconds // Color-related functions RLAPI int GetHexValue(Color color); // Returns hexadecimal value for a Color @@ -1049,7 +1047,7 @@ RLAPI void SetShaderValuei(Shader shader, int uniformLoc, const int *value, int RLAPI void SetShaderValueMatrix(Shader shader, int uniformLoc, Matrix mat); // Set shader uniform value (matrix 4x4) RLAPI void SetMatrixProjection(Matrix proj); // Set a custom projection matrix (replaces internal projection matrix) RLAPI void SetMatrixModelview(Matrix view); // Set a custom modelview matrix (replaces internal modelview matrix) -RLAPI Matrix GetMatrixModelview(); +RLAPI Matrix GetMatrixModelview(); // Get internal modelview matrix // Texture maps generation (PBR) // NOTE: Required shaders should be provided diff --git a/src/raymath.h b/src/raymath.h index 3d95a089..c29c6b95 100644 --- a/src/raymath.h +++ b/src/raymath.h @@ -227,13 +227,15 @@ RMDEF float Clamp(float value, float min, float max) //---------------------------------------------------------------------------------- // Vector with components value 0.0f -RMDEF Vector2 Vector2Zero(void) { +RMDEF Vector2 Vector2Zero(void) +{ Vector2 tmp = {0.0f, 0.0f}; return tmp; } // Vector with components value 1.0f -RMDEF Vector2 Vector2One(void) { +RMDEF Vector2 Vector2One(void) +{ Vector2 tmp = {1.0f, 1.0f}; return tmp; } @@ -312,13 +314,15 @@ RMDEF void Vector2Normalize(Vector2 *v) //---------------------------------------------------------------------------------- // Vector with components value 0.0f -RMDEF Vector3 Vector3Zero(void) { +RMDEF Vector3 Vector3Zero(void) +{ Vector3 tmp = { 0.0f, 0.0f, 0.0f }; return tmp; } // Vector with components value 1.0f -RMDEF Vector3 Vector3One(void) { +RMDEF Vector3 Vector3One(void) +{ Vector3 tmp = { 1.0f, 1.0f, 1.0f }; return tmp; } diff --git a/src/rlgl.c b/src/rlgl.c index 713646ac..96932686 100644 --- a/src/rlgl.c +++ b/src/rlgl.c @@ -1293,6 +1293,12 @@ int rlGetVersion(void) #endif } +// Set debug marker +void rlSetDebugMarker(const char *text) +{ + if(debugMarkerSupported) glInsertEventMarkerEXT(0, text); // 0 terminated string +} + // Load OpenGL extensions // NOTE: External loader function could be passed as a pointer void rlLoadExtensions(void *loader) diff --git a/src/rlgl.h b/src/rlgl.h index 93afb44e..2437487d 100644 --- a/src/rlgl.h +++ b/src/rlgl.h @@ -422,6 +422,7 @@ void rlglClose(void); // De-inititialize rlgl (buffers void rlglDraw(void); // Update and Draw default buffers (lines, triangles, quads) int rlGetVersion(void); // Returns current OpenGL version +void rlDebugSetMarker(const char *text); // Set debug marker for analysis void rlLoadExtensions(void *loader); // Load OpenGL extensions Vector3 rlUnproject(Vector3 source, Matrix proj, Matrix view); // Get world coordinates from screen coordinates @@ -440,9 +441,6 @@ void rlUpdateMesh(Mesh mesh, int buffer, int numVertex); // Update ve void rlDrawMesh(Mesh mesh, Material material, Matrix transform); // Draw a 3d mesh with material and transform void rlUnloadMesh(Mesh *mesh); // Unload mesh data from CPU and GPU -// Debug Marker for Analysis -void rlSetMarker(const char *text); - // NOTE: There is a set of shader related functions that are available to end user, // to avoid creating function wrappers through core module, they have been directly declared in raylib.h @@ -462,9 +460,9 @@ int GetShaderLocation(Shader shader, const char *uniformName); // G void SetShaderValue(Shader shader, int uniformLoc, const float *value, int size); // Set shader uniform value (float) void SetShaderValuei(Shader shader, int uniformLoc, const int *value, int size); // Set shader uniform value (int) void SetShaderValueMatrix(Shader shader, int uniformLoc, Matrix mat); // Set shader uniform value (matrix 4x4) -void SetMatrixProjection(Matrix proj); // Set a custom projection matrix (replaces internal projection matrix) -void SetMatrixModelview(Matrix view); // Set a custom modelview matrix (replaces internal modelview matrix) -Matrix GetMatrixModelview(); +void SetMatrixProjection(Matrix proj); // Set a custom projection matrix (replaces internal projection matrix) +void SetMatrixModelview(Matrix view); // Set a custom modelview matrix (replaces internal modelview matrix) +Matrix GetMatrixModelview(); // Get internal modelview matrix // Texture maps generation (PBR) diff --git a/src/textures.c b/src/textures.c index 55749118..01b8e4fc 100644 --- a/src/textures.c +++ b/src/textures.c @@ -554,13 +554,12 @@ void UpdateTexture(Texture2D texture, const void *pixels) // Save image to a PNG file void SaveImageAs(const char* fileName, Image image) { -#if (defined(PLATFORM_DESKTOP) || defined(PLATFORM_RPI)) && defined(SUPPORT_SAVE_PNG) - unsigned char* imgData = (unsigned char*)GetImageData(image); // this works since Color is just a container for the RGBA values + // NOTE: Getting Color array as RGBA unsigned char values + unsigned char* imgData = (unsigned char*)GetImageData(image); SavePNG(fileName, imgData, image.width, image.height, 4); free(imgData); TraceLog(LOG_INFO, "Image saved: %s", fileName); -#endif } // Convert image data to desired format -- cgit v1.2.3 From 27c274bc3c5ca343de66a9f76e55c79696aed833 Mon Sep 17 00:00:00 2001 From: Ray San Date: Wed, 20 Dec 2017 12:36:47 +0100 Subject: Corrected issue with SaveImageAs() That function depends on SavePNG() and forces platform check on textures module, that behaviour should be changed, platform definition should be only required on core module... --- src/textures.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'src/textures.c') diff --git a/src/textures.c b/src/textures.c index 01b8e4fc..9c41ae7d 100644 --- a/src/textures.c +++ b/src/textures.c @@ -34,7 +34,7 @@ * * LICENSE: zlib/libpng * -* Copyright (c) 2014-2017 Ramon Santamaria (@raysan5) +* Copyright (c) 2014-2018 Ramon Santamaria (@raysan5) * * This software is provided "as-is", without any express or implied warranty. In no event * will the authors be held liable for any damages arising from the use of this software. @@ -552,14 +552,16 @@ void UpdateTexture(Texture2D texture, const void *pixels) } // Save image to a PNG file -void SaveImageAs(const char* fileName, Image image) +void SaveImageAs(const char *fileName, Image image) { +#if defined(PLATFORM_DESKTOP) || defined(PLATFORM_RPI) // NOTE: Getting Color array as RGBA unsigned char values - unsigned char* imgData = (unsigned char*)GetImageData(image); + unsigned char *imgData = (unsigned char *)GetImageData(image); SavePNG(fileName, imgData, image.width, image.height, 4); free(imgData); TraceLog(LOG_INFO, "Image saved: %s", fileName); +#endif } // Convert image data to desired format -- cgit v1.2.3 From b19e155b3419b50bd546632f9b77793bc95824d1 Mon Sep 17 00:00:00 2001 From: raysan5 Date: Thu, 28 Dec 2017 17:58:37 +0100 Subject: Support UNCOMPRESSED_R32G32B32A32 texture format --- src/raylib.h | 1 + src/rlgl.c | 4 +++- src/rlgl.h | 1 + src/textures.c | 32 ++++++++++++++++++++++++++++++-- 4 files changed, 35 insertions(+), 3 deletions(-) (limited to 'src/textures.c') diff --git a/src/raylib.h b/src/raylib.h index 75dafa42..e71ff47b 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -582,6 +582,7 @@ typedef enum { UNCOMPRESSED_R4G4B4A4, // 16 bpp (4 bit alpha) UNCOMPRESSED_R8G8B8A8, // 32 bpp UNCOMPRESSED_R32G32B32, // 32 bit per channel (float) - HDR + UNCOMPRESSED_R32G32B32A32, // 32 bit per channel (float) - HDR COMPRESSED_DXT1_RGB, // 4 bpp (no alpha) COMPRESSED_DXT1_RGBA, // 4 bpp (1 bit alpha) COMPRESSED_DXT3_RGBA, // 8 bpp diff --git a/src/rlgl.c b/src/rlgl.c index 8f8a97c1..58e9187d 100644 --- a/src/rlgl.c +++ b/src/rlgl.c @@ -1421,6 +1421,7 @@ unsigned int rlLoadTexture(void *data, int width, int height, int format, int mi #endif #if defined(GRAPHICS_API_OPENGL_ES2) case UNCOMPRESSED_R32G32B32: if (texFloatSupported) glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_FLOAT, (float *)data); break; // NOTE: Requires extension OES_texture_float + case UNCOMPRESSED_R32G32B32A32: if (texFloatSupported) glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGB, GL_FLOAT, (float *)data); break; // NOTE: Requires extension OES_texture_float case COMPRESSED_DXT1_RGB: if (texCompDXTSupported) LoadTextureCompressed((unsigned char *)data, width, height, GL_COMPRESSED_RGB_S3TC_DXT1_EXT, mipmapCount); break; case COMPRESSED_DXT1_RGBA: if (texCompDXTSupported) LoadTextureCompressed((unsigned char *)data, width, height, GL_COMPRESSED_RGBA_S3TC_DXT1_EXT, mipmapCount); break; case COMPRESSED_DXT3_RGBA: if (texCompDXTSupported) LoadTextureCompressed((unsigned char *)data, width, height, GL_COMPRESSED_RGBA_S3TC_DXT3_EXT, mipmapCount); break; // NOTE: Not supported by WebGL @@ -1468,13 +1469,14 @@ unsigned int rlLoadTexture(void *data, int width, int height, int format, int mi GLint swizzleMask[] = { GL_RED, GL_RED, GL_RED, GL_GREEN }; glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_RGBA, swizzleMask); } break; - + // Supported texture formats: https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/glTexImage2D.xhtml case UNCOMPRESSED_R5G6B5: glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB565, width, height, 0, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, (unsigned short *)data); break; case UNCOMPRESSED_R8G8B8: glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, (unsigned char *)data); break; case UNCOMPRESSED_R5G5B5A1: glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB5_A1, width, height, 0, GL_RGBA, GL_UNSIGNED_SHORT_5_5_5_1, (unsigned short *)data); break; case UNCOMPRESSED_R4G4B4A4: glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA4, width, height, 0, GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4, (unsigned short *)data); break; case UNCOMPRESSED_R8G8B8A8: glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, (unsigned char *)data); break; case UNCOMPRESSED_R32G32B32: if (texFloatSupported) glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB32F, width, height, 0, GL_RGB, GL_FLOAT, (float *)data); break; + case UNCOMPRESSED_R32G32B32A32: if (texFloatSupported) glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, width, height, 0, GL_RGB, GL_FLOAT, (float *)data); break; case COMPRESSED_DXT1_RGB: if (texCompDXTSupported) LoadTextureCompressed((unsigned char *)data, width, height, GL_COMPRESSED_RGB_S3TC_DXT1_EXT, mipmapCount); break; case COMPRESSED_DXT1_RGBA: if (texCompDXTSupported) LoadTextureCompressed((unsigned char *)data, width, height, GL_COMPRESSED_RGBA_S3TC_DXT1_EXT, mipmapCount); break; case COMPRESSED_DXT3_RGBA: if (texCompDXTSupported) LoadTextureCompressed((unsigned char *)data, width, height, GL_COMPRESSED_RGBA_S3TC_DXT3_EXT, mipmapCount); break; diff --git a/src/rlgl.h b/src/rlgl.h index d3982eb7..0e4a5d74 100644 --- a/src/rlgl.h +++ b/src/rlgl.h @@ -260,6 +260,7 @@ typedef unsigned char byte; UNCOMPRESSED_R4G4B4A4, // 16 bpp (4 bit alpha) UNCOMPRESSED_R8G8B8A8, // 32 bpp UNCOMPRESSED_R32G32B32, // 32 bit per channel (float) - HDR + UNCOMPRESSED_R32G32B32A32, // 32 bit per channel (float) - HDR COMPRESSED_DXT1_RGB, // 4 bpp (no alpha) COMPRESSED_DXT1_RGBA, // 4 bpp (1 bit alpha) COMPRESSED_DXT3_RGBA, // 8 bpp diff --git a/src/textures.c b/src/textures.c index 9c41ae7d..34f2c323 100644 --- a/src/textures.c +++ b/src/textures.c @@ -227,10 +227,11 @@ Image LoadImage(const char *fileName) image.mipmaps = 1; if (imgBpp == 3) image.format = UNCOMPRESSED_R32G32B32; + else if (imgBpp == 4) image.format = UNCOMPRESSED_R32G32B32A32; else { // TODO: Support different number of channels at 32 bit float - TraceLog(LOG_WARNING, "[%s] Image fileformat not supported (only 3 channel 32 bit floats)", fileName); + TraceLog(LOG_WARNING, "[%s] Image fileformat not supported", fileName); UnloadImage(image); } } @@ -329,6 +330,7 @@ Image LoadImageRaw(const char *fileName, int width, int height, int format, int case UNCOMPRESSED_R4G4B4A4: image.data = (unsigned short *)malloc(size); break; // 16 bpp (4 bit alpha) case UNCOMPRESSED_R8G8B8A8: image.data = (unsigned char *)malloc(size*4); size *= 4; break; // 32 bpp case UNCOMPRESSED_R32G32B32: image.data = (float *)malloc(size*12); size *= 12; break; // 4 byte per channel (12 byte) + case UNCOMPRESSED_R32G32B32A32: image.data = (float *)malloc(size*16); size *= 16; break; // 4 byte per channel (16 byte) default: TraceLog(LOG_WARNING, "Image format not suported"); break; } @@ -679,7 +681,7 @@ void ImageFormat(Image *image, int newFormat) { image->data = (unsigned char *)malloc(image->width*image->height*4*sizeof(unsigned char)); - for (int i = 0; i < image->width*image->height*4; i += 4) + for (int i = 0; i < image->width*image->height*3; i += 3) { ((unsigned char *)image->data)[i] = pixels[k].r; ((unsigned char *)image->data)[i + 1] = pixels[k].g; @@ -688,6 +690,31 @@ void ImageFormat(Image *image, int newFormat) k++; } } break; + case UNCOMPRESSED_R32G32B32: + { + image->data = (float *)malloc(image->width*image->height*3*sizeof(float)); + + for (int i = 0; i < image->width*image->height*3; i += 3) + { + ((float *)image->data)[i] = (float)pixels[k].r/255.0f; + ((float *)image->data)[i + 1] = (float)pixels[k].g/255.0f; + ((float *)image->data)[i + 2] = (float)pixels[k].b/255.0f; + k++; + } + } break; + case UNCOMPRESSED_R32G32B32A32: + { + image->data = (float *)malloc(image->width*image->height*4*sizeof(float)); + + for (int i = 0; i < image->width*image->height*4; i += 4) + { + ((float *)image->data)[i] = (float)pixels[k].r/255.0f; + ((float *)image->data)[i + 1] = (float)pixels[k].g/255.0f; + ((float *)image->data)[i + 2] = (float)pixels[k].b/255.0f; + ((float *)image->data)[i + 3] = (float)pixels[k].a/255.0f; + k++; + } + } break; default: break; } @@ -806,6 +833,7 @@ Image ImageCopy(Image image) case UNCOMPRESSED_R8G8B8: byteSize *= 3; break; // 24 bpp (3 bytes) case UNCOMPRESSED_R8G8B8A8: byteSize *= 4; break; // 32 bpp (4 bytes) case UNCOMPRESSED_R32G32B32: byteSize *= 12; break; // 4 byte per channel (12 bytes) + case UNCOMPRESSED_R32G32B32A32: byteSize *= 16; break; // 4 byte per channel (16 bytes) case COMPRESSED_DXT3_RGBA: case COMPRESSED_DXT5_RGBA: case COMPRESSED_ETC2_EAC_RGBA: -- cgit v1.2.3 From e7cf03b1e4c1aa7872bda7b35a7d064815332759 Mon Sep 17 00:00:00 2001 From: raysan5 Date: Thu, 28 Dec 2017 19:27:02 +0100 Subject: Minor tweaks --- src/models.c | 6 ++---- src/rlgl.c | 8 ++++---- src/textures.c | 2 +- 3 files changed, 7 insertions(+), 9 deletions(-) (limited to 'src/textures.c') diff --git a/src/models.c b/src/models.c index 002ffac3..1f37e345 100644 --- a/src/models.c +++ b/src/models.c @@ -45,9 +45,7 @@ #include "raylib.h" -#if defined(PLATFORM_ANDROID) - #include "utils.h" // Android fopen function map -#endif +#include "utils.h" // Required for: fopen() Android mapping #include // Required for: FILE, fopen(), fclose(), fscanf(), feof(), rewind(), fgets() #include // Required for: malloc(), free() @@ -57,7 +55,7 @@ #include "rlgl.h" // raylib OpenGL abstraction layer to OpenGL 1.1, 2.1, 3.3+ or ES2 #define PAR_SHAPES_IMPLEMENTATION -#include "external/par_shapes.h" +#include "external/par_shapes.h" // Shapes 3d parametric generation //---------------------------------------------------------------------------------- // Defines and Macros diff --git a/src/rlgl.c b/src/rlgl.c index 58e9187d..4bbebf03 100644 --- a/src/rlgl.c +++ b/src/rlgl.c @@ -1109,7 +1109,7 @@ void rlglInit(int width, int height) if (strcmp(extList[i], (const char *)"GL_OES_texture_npot") == 0) texNPOTSupported = true; // Check texture float support - if (strcmp(extList[i], (const char *)"OES_texture_float") == 0) texFloatSupported = true; + if (strcmp(extList[i], (const char *)"GL_OES_texture_float") == 0) texFloatSupported = true; #endif // DDS texture compression support @@ -1137,11 +1137,11 @@ void rlglInit(int width, int height) glGetFloatv(0x84FF, &maxAnisotropicLevel); // GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT } - // Debug marker support - if(strcmp(extList[i], (const char *)"GL_EXT_debug_marker") == 0) debugMarkerSupported = true; - // Clamp mirror wrap mode supported if (strcmp(extList[i], (const char *)"GL_EXT_texture_mirror_clamp") == 0) texClampMirrorSupported = true; + + // Debug marker support + if(strcmp(extList[i], (const char *)"GL_EXT_debug_marker") == 0) debugMarkerSupported = true; } #ifdef _MSC_VER diff --git a/src/textures.c b/src/textures.c index 34f2c323..fd2fae0d 100644 --- a/src/textures.c +++ b/src/textures.c @@ -1681,7 +1681,7 @@ Image GenImageCellular(int width, int height, int tileSize) // Generate GPU mipmaps for a texture void GenTextureMipmaps(Texture2D *texture) { -#if PLATFORM_WEB +#if defined(PLATFORM_WEB) // Calculate next power-of-two values int potWidth = (int)powf(2, ceilf(logf((float)texture->width)/logf(2))); int potHeight = (int)powf(2, ceilf(logf((float)texture->height)/logf(2))); -- cgit v1.2.3 From 7fa28611607c210789054056095588b17c5bdaac Mon Sep 17 00:00:00 2001 From: raysan5 Date: Sat, 6 Jan 2018 02:50:20 +0100 Subject: Added function: GetPixelDataSize() Just found I need that function... --- src/raylib.h | 1 + src/textures.c | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) (limited to 'src/textures.c') diff --git a/src/raylib.h b/src/raylib.h index 75bd883d..01699ae7 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -869,6 +869,7 @@ RLAPI void UnloadImage(Image image); RLAPI void UnloadTexture(Texture2D texture); // Unload texture from GPU memory (VRAM) RLAPI void UnloadRenderTexture(RenderTexture2D target); // Unload render texture from GPU memory (VRAM) RLAPI Color *GetImageData(Image image); // Get pixel data from image as a Color struct array +RLAPI int GetImageDataSize(Image image); // Get image data size in bytes RLAPI Image GetTextureData(Texture2D texture); // Get pixel data from GPU texture and return an Image RLAPI void UpdateTexture(Texture2D texture, const void *pixels); // Update GPU texture with new data RLAPI void SaveImageAs(const char *fileName, Image image); // Save image to a PNG file diff --git a/src/textures.c b/src/textures.c index fd2fae0d..9abee62b 100644 --- a/src/textures.c +++ b/src/textures.c @@ -515,6 +515,43 @@ Color *GetImageData(Image image) return pixels; } +// Get pixel data size in bytes (image or texture) +// NOTE: Size depends on pixel format +int GetPixelDataSize(int width, int height, int format) +{ + int dataSize = 0; // Size in bytes + int bpp = 0; // Bits per pixel + + switch (format) + { + case UNCOMPRESSED_GRAYSCALE: bpp = 8; break; + case UNCOMPRESSED_GRAY_ALPHA: + case UNCOMPRESSED_R5G6B5: + case UNCOMPRESSED_R5G5B5A1: + case UNCOMPRESSED_R4G4B4A4: bpp = 16; break; + case UNCOMPRESSED_R8G8B8A8: bpp = 32; break; + case UNCOMPRESSED_R8G8B8: bpp = 24; break; + case UNCOMPRESSED_R32G32B32: bpp = 32*3; break; + case UNCOMPRESSED_R32G32B32A32: bpp = 32*4; break; + case COMPRESSED_DXT1_RGB: + case COMPRESSED_DXT1_RGBA: + case COMPRESSED_ETC1_RGB: + case COMPRESSED_ETC2_RGB: + case COMPRESSED_PVRT_RGB: + case COMPRESSED_PVRT_RGBA: bpp = 4; break; + case COMPRESSED_DXT3_RGBA: + case COMPRESSED_DXT5_RGBA: + case COMPRESSED_ETC2_EAC_RGBA: + case COMPRESSED_ASTC_4x4_RGBA: bpp = 8; break; + case COMPRESSED_ASTC_8x8_RGBA: bpp = 2; break; + default: break; + } + + dataSize = width*height*bpp/8; // Total data size in bytes + + return dataSize; +} + // Get pixel data from GPU texture and return an Image // NOTE: Compressed texture formats not supported Image GetTextureData(Texture2D texture) -- cgit v1.2.3 From 7caa3201d51a04b739122ffadac476493bd05395 Mon Sep 17 00:00:00 2001 From: raysan5 Date: Sat, 6 Jan 2018 13:43:48 +0100 Subject: Improved pixel formats support - Renamed enum TextureFormat to PixelFormat for consistency - Added support for pixel format UNCOMPRESSED_R32 - Using GetPixelDataSize() where required --- src/raylib.h | 15 +++++++------- src/rlgl.c | 2 ++ src/rlgl.h | 9 +++++---- src/textures.c | 63 +++++++++++++++++----------------------------------------- 4 files changed, 33 insertions(+), 56 deletions(-) (limited to 'src/textures.c') diff --git a/src/raylib.h b/src/raylib.h index a2f28661..c44653e8 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -341,7 +341,7 @@ typedef struct Image { int width; // Image base width int height; // Image base height int mipmaps; // Mipmap levels, 1 by default - int format; // Data format (TextureFormat type) + int format; // Data format (PixelFormat type) } Image; // Texture2D type @@ -351,7 +351,7 @@ typedef struct Texture2D { int width; // Texture base width int height; // Texture base height int mipmaps; // Mipmap levels, 1 by default - int format; // Data format (TextureFormat type) + int format; // Data format (PixelFormat type) } Texture2D; // RenderTexture2D type, for texture rendering @@ -571,18 +571,19 @@ typedef enum { #define MAP_DIFFUSE MAP_ALBEDO #define MAP_SPECULAR MAP_METALNESS -// Texture formats +// Pixel formats // NOTE: Support depends on OpenGL version and platform typedef enum { UNCOMPRESSED_GRAYSCALE = 1, // 8 bit per pixel (no alpha) - UNCOMPRESSED_GRAY_ALPHA, // 16 bpp (2 channels) + UNCOMPRESSED_GRAY_ALPHA, // 8*2 bpp (2 channels) UNCOMPRESSED_R5G6B5, // 16 bpp UNCOMPRESSED_R8G8B8, // 24 bpp UNCOMPRESSED_R5G5B5A1, // 16 bpp (1 bit alpha) UNCOMPRESSED_R4G4B4A4, // 16 bpp (4 bit alpha) UNCOMPRESSED_R8G8B8A8, // 32 bpp - UNCOMPRESSED_R32G32B32, // 32 bit per channel (float) - HDR - UNCOMPRESSED_R32G32B32A32, // 32 bit per channel (float) - HDR + UNCOMPRESSED_R32, // 32 bpp (1 channel - float) + UNCOMPRESSED_R32G32B32, // 32*3 bpp (3 channels - float) + UNCOMPRESSED_R32G32B32A32, // 32*4 bpp (4 channels - float) COMPRESSED_DXT1_RGB, // 4 bpp (no alpha) COMPRESSED_DXT1_RGBA, // 4 bpp (1 bit alpha) COMPRESSED_DXT3_RGBA, // 8 bpp @@ -594,7 +595,7 @@ typedef enum { COMPRESSED_PVRT_RGBA, // 4 bpp COMPRESSED_ASTC_4x4_RGBA, // 8 bpp COMPRESSED_ASTC_8x8_RGBA // 2 bpp -} TextureFormat; +} PixelFormat; // Texture parameters: filter mode // NOTE 1: Filtering considers mipmaps if available in the texture diff --git a/src/rlgl.c b/src/rlgl.c index 4bbebf03..e7ab7cd7 100644 --- a/src/rlgl.c +++ b/src/rlgl.c @@ -1420,6 +1420,7 @@ unsigned int rlLoadTexture(void *data, int width, int height, int format, int mi case COMPRESSED_DXT5_RGBA: if (texCompDXTSupported) LoadTextureCompressed((unsigned char *)data, width, height, GL_COMPRESSED_RGBA_S3TC_DXT5_EXT, mipmapCount); break; #endif #if defined(GRAPHICS_API_OPENGL_ES2) + case UNCOMPRESSED_R32: if (texFloatSupported) glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, width, height, 0, GL_RGB, GL_FLOAT, (float *)data); break; // NOTE: Requires extension OES_texture_float case UNCOMPRESSED_R32G32B32: if (texFloatSupported) glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_FLOAT, (float *)data); break; // NOTE: Requires extension OES_texture_float case UNCOMPRESSED_R32G32B32A32: if (texFloatSupported) glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGB, GL_FLOAT, (float *)data); break; // NOTE: Requires extension OES_texture_float case COMPRESSED_DXT1_RGB: if (texCompDXTSupported) LoadTextureCompressed((unsigned char *)data, width, height, GL_COMPRESSED_RGB_S3TC_DXT1_EXT, mipmapCount); break; @@ -1475,6 +1476,7 @@ unsigned int rlLoadTexture(void *data, int width, int height, int format, int mi case UNCOMPRESSED_R5G5B5A1: glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB5_A1, width, height, 0, GL_RGBA, GL_UNSIGNED_SHORT_5_5_5_1, (unsigned short *)data); break; case UNCOMPRESSED_R4G4B4A4: glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA4, width, height, 0, GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4, (unsigned short *)data); break; case UNCOMPRESSED_R8G8B8A8: glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, (unsigned char *)data); break; + case UNCOMPRESSED_R32: if (texFloatSupported) glTexImage2D(GL_TEXTURE_2D, 0, GL_R32F, width, height, 0, GL_RGB, GL_FLOAT, (float *)data); break; case UNCOMPRESSED_R32G32B32: if (texFloatSupported) glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB32F, width, height, 0, GL_RGB, GL_FLOAT, (float *)data); break; case UNCOMPRESSED_R32G32B32A32: if (texFloatSupported) glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, width, height, 0, GL_RGB, GL_FLOAT, (float *)data); break; case COMPRESSED_DXT1_RGB: if (texCompDXTSupported) LoadTextureCompressed((unsigned char *)data, width, height, GL_COMPRESSED_RGB_S3TC_DXT1_EXT, mipmapCount); break; diff --git a/src/rlgl.h b/src/rlgl.h index 0e4a5d74..68d8d4eb 100644 --- a/src/rlgl.h +++ b/src/rlgl.h @@ -169,7 +169,7 @@ typedef unsigned char byte; int width; // Texture base width int height; // Texture base height int mipmaps; // Mipmap levels, 1 by default - int format; // Data format (TextureFormat) + int format; // Data format (PixelFormat) } Texture2D; // RenderTexture2D type, for texture rendering @@ -259,8 +259,9 @@ typedef unsigned char byte; UNCOMPRESSED_R5G5B5A1, // 16 bpp (1 bit alpha) UNCOMPRESSED_R4G4B4A4, // 16 bpp (4 bit alpha) UNCOMPRESSED_R8G8B8A8, // 32 bpp - UNCOMPRESSED_R32G32B32, // 32 bit per channel (float) - HDR - UNCOMPRESSED_R32G32B32A32, // 32 bit per channel (float) - HDR + UNCOMPRESSED_R32, // 32 bpp (1 channel - float) + UNCOMPRESSED_R32G32B32, // 32*3 bpp (3 channels - float) + UNCOMPRESSED_R32G32B32A32, // 32*4 bpp (4 channels - float) COMPRESSED_DXT1_RGB, // 4 bpp (no alpha) COMPRESSED_DXT1_RGBA, // 4 bpp (1 bit alpha) COMPRESSED_DXT3_RGBA, // 8 bpp @@ -272,7 +273,7 @@ typedef unsigned char byte; COMPRESSED_PVRT_RGBA, // 4 bpp COMPRESSED_ASTC_4x4_RGBA, // 8 bpp COMPRESSED_ASTC_8x8_RGBA // 2 bpp - } TextureFormat; + } PixelFormat; // Texture parameters: filter mode // NOTE 1: Filtering considers mipmaps if available in the texture diff --git a/src/textures.c b/src/textures.c index 9abee62b..7ea54c2c 100644 --- a/src/textures.c +++ b/src/textures.c @@ -226,11 +226,11 @@ Image LoadImage(const char *fileName) image.mipmaps = 1; - if (imgBpp == 3) image.format = UNCOMPRESSED_R32G32B32; + if (imgBpp == 1) image.format = UNCOMPRESSED_R32; + else if (imgBpp == 3) image.format = UNCOMPRESSED_R32G32B32; else if (imgBpp == 4) image.format = UNCOMPRESSED_R32G32B32A32; else { - // TODO: Support different number of channels at 32 bit float TraceLog(LOG_WARNING, "[%s] Image fileformat not supported", fileName); UnloadImage(image); } @@ -318,21 +318,9 @@ Image LoadImageRaw(const char *fileName, int width, int height, int format, int { if (headerSize > 0) fseek(rawFile, headerSize, SEEK_SET); - unsigned int size = width*height; - - switch (format) - { - case UNCOMPRESSED_GRAYSCALE: image.data = (unsigned char *)malloc(size); break; // 8 bit per pixel (no alpha) - case UNCOMPRESSED_GRAY_ALPHA: image.data = (unsigned char *)malloc(size*2); size *= 2; break; // 16 bpp (2 channels) - case UNCOMPRESSED_R5G6B5: image.data = (unsigned short *)malloc(size); break; // 16 bpp - case UNCOMPRESSED_R8G8B8: image.data = (unsigned char *)malloc(size*3); size *= 3; break; // 24 bpp - case UNCOMPRESSED_R5G5B5A1: image.data = (unsigned short *)malloc(size); break; // 16 bpp (1 bit alpha) - case UNCOMPRESSED_R4G4B4A4: image.data = (unsigned short *)malloc(size); break; // 16 bpp (4 bit alpha) - case UNCOMPRESSED_R8G8B8A8: image.data = (unsigned char *)malloc(size*4); size *= 4; break; // 32 bpp - case UNCOMPRESSED_R32G32B32: image.data = (float *)malloc(size*12); size *= 12; break; // 4 byte per channel (12 byte) - case UNCOMPRESSED_R32G32B32A32: image.data = (float *)malloc(size*16); size *= 16; break; // 4 byte per channel (16 byte) - default: TraceLog(LOG_WARNING, "Image format not suported"); break; - } + unsigned int size = GetPixelDataSize(width, height, format); + + image.data = malloc(size); // Allocate required memory in bytes // NOTE: fread() returns num read elements instead of bytes, // to get bytes we need to read (1 byte size, elements) instead of (x byte size, 1 element) @@ -531,6 +519,7 @@ int GetPixelDataSize(int width, int height, int format) case UNCOMPRESSED_R4G4B4A4: bpp = 16; break; case UNCOMPRESSED_R8G8B8A8: bpp = 32; break; case UNCOMPRESSED_R8G8B8: bpp = 24; break; + case UNCOMPRESSED_R32: bpp = 32; break; case UNCOMPRESSED_R32G32B32: bpp = 32*3; break; case UNCOMPRESSED_R32G32B32A32: bpp = 32*4; break; case COMPRESSED_DXT1_RGB: @@ -727,6 +716,15 @@ void ImageFormat(Image *image, int newFormat) k++; } } break; + case UNCOMPRESSED_R32: + { + image->data = (float *)malloc(image->width*image->height*sizeof(float)); + + for (int i = 0; i < image->width*image->height; i++) + { + ((float *)image->data)[i] = (float)((float)pixels[i].r*0.299f/255.0f + (float)pixels[i].g*0.587f/255.0f + (float)pixels[i].b*0.114f/255.0f); + } + } break; case UNCOMPRESSED_R32G32B32: { image->data = (float *)malloc(image->width*image->height*3*sizeof(float)); @@ -858,39 +856,14 @@ Image ImageCopy(Image image) { Image newImage = { 0 }; - int byteSize = image.width*image.height; - - switch (image.format) - { - case UNCOMPRESSED_GRAYSCALE: break; // 8 bpp (1 byte) - case UNCOMPRESSED_GRAY_ALPHA: // 16 bpp - case UNCOMPRESSED_R5G6B5: // 16 bpp - case UNCOMPRESSED_R5G5B5A1: // 16 bpp - case UNCOMPRESSED_R4G4B4A4: byteSize *= 2; break; // 16 bpp (2 bytes) - case UNCOMPRESSED_R8G8B8: byteSize *= 3; break; // 24 bpp (3 bytes) - case UNCOMPRESSED_R8G8B8A8: byteSize *= 4; break; // 32 bpp (4 bytes) - case UNCOMPRESSED_R32G32B32: byteSize *= 12; break; // 4 byte per channel (12 bytes) - case UNCOMPRESSED_R32G32B32A32: byteSize *= 16; break; // 4 byte per channel (16 bytes) - case COMPRESSED_DXT3_RGBA: - case COMPRESSED_DXT5_RGBA: - case COMPRESSED_ETC2_EAC_RGBA: - case COMPRESSED_ASTC_4x4_RGBA: break; // 8 bpp (1 byte) - case COMPRESSED_DXT1_RGB: - case COMPRESSED_DXT1_RGBA: - case COMPRESSED_ETC1_RGB: - case COMPRESSED_ETC2_RGB: - case COMPRESSED_PVRT_RGB: - case COMPRESSED_PVRT_RGBA: byteSize /= 2; break; // 4 bpp - case COMPRESSED_ASTC_8x8_RGBA: byteSize /= 4; break;// 2 bpp - default: TraceLog(LOG_WARNING, "Image format not recognized"); break; - } + int size = GetPixelDataSize(image.width, image.height, image.format); - newImage.data = malloc(byteSize); + newImage.data = malloc(size); if (newImage.data != NULL) { // NOTE: Size must be provided in bytes - memcpy(newImage.data, image.data, byteSize); + memcpy(newImage.data, image.data, size); newImage.width = image.width; newImage.height = image.height; -- cgit v1.2.3 From b97134c3e157f241cef485b34b8fb0e09575e7bc Mon Sep 17 00:00:00 2001 From: raysan5 Date: Sat, 6 Jan 2018 18:17:38 +0100 Subject: Review float pixel format textures support --- src/rlgl.c | 8 ++++---- src/textures.c | 3 ++- 2 files changed, 6 insertions(+), 5 deletions(-) (limited to 'src/textures.c') diff --git a/src/rlgl.c b/src/rlgl.c index e7ab7cd7..b893d5e5 100644 --- a/src/rlgl.c +++ b/src/rlgl.c @@ -1420,9 +1420,9 @@ unsigned int rlLoadTexture(void *data, int width, int height, int format, int mi case COMPRESSED_DXT5_RGBA: if (texCompDXTSupported) LoadTextureCompressed((unsigned char *)data, width, height, GL_COMPRESSED_RGBA_S3TC_DXT5_EXT, mipmapCount); break; #endif #if defined(GRAPHICS_API_OPENGL_ES2) - case UNCOMPRESSED_R32: if (texFloatSupported) glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, width, height, 0, GL_RGB, GL_FLOAT, (float *)data); break; // NOTE: Requires extension OES_texture_float + case UNCOMPRESSED_R32: if (texFloatSupported) glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, width, height, 0, GL_LUMINANCE, GL_FLOAT, (float *)data); break; // NOTE: Requires extension OES_texture_float case UNCOMPRESSED_R32G32B32: if (texFloatSupported) glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_FLOAT, (float *)data); break; // NOTE: Requires extension OES_texture_float - case UNCOMPRESSED_R32G32B32A32: if (texFloatSupported) glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGB, GL_FLOAT, (float *)data); break; // NOTE: Requires extension OES_texture_float + case UNCOMPRESSED_R32G32B32A32: if (texFloatSupported) glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_FLOAT, (float *)data); break; // NOTE: Requires extension OES_texture_float case COMPRESSED_DXT1_RGB: if (texCompDXTSupported) LoadTextureCompressed((unsigned char *)data, width, height, GL_COMPRESSED_RGB_S3TC_DXT1_EXT, mipmapCount); break; case COMPRESSED_DXT1_RGBA: if (texCompDXTSupported) LoadTextureCompressed((unsigned char *)data, width, height, GL_COMPRESSED_RGBA_S3TC_DXT1_EXT, mipmapCount); break; case COMPRESSED_DXT3_RGBA: if (texCompDXTSupported) LoadTextureCompressed((unsigned char *)data, width, height, GL_COMPRESSED_RGBA_S3TC_DXT3_EXT, mipmapCount); break; // NOTE: Not supported by WebGL @@ -1476,9 +1476,9 @@ unsigned int rlLoadTexture(void *data, int width, int height, int format, int mi case UNCOMPRESSED_R5G5B5A1: glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB5_A1, width, height, 0, GL_RGBA, GL_UNSIGNED_SHORT_5_5_5_1, (unsigned short *)data); break; case UNCOMPRESSED_R4G4B4A4: glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA4, width, height, 0, GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4, (unsigned short *)data); break; case UNCOMPRESSED_R8G8B8A8: glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, (unsigned char *)data); break; - case UNCOMPRESSED_R32: if (texFloatSupported) glTexImage2D(GL_TEXTURE_2D, 0, GL_R32F, width, height, 0, GL_RGB, GL_FLOAT, (float *)data); break; + case UNCOMPRESSED_R32: if (texFloatSupported) glTexImage2D(GL_TEXTURE_2D, 0, GL_R32F, width, height, 0, GL_RED, GL_FLOAT, (float *)data); break; case UNCOMPRESSED_R32G32B32: if (texFloatSupported) glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB32F, width, height, 0, GL_RGB, GL_FLOAT, (float *)data); break; - case UNCOMPRESSED_R32G32B32A32: if (texFloatSupported) glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, width, height, 0, GL_RGB, GL_FLOAT, (float *)data); break; + case UNCOMPRESSED_R32G32B32A32: if (texFloatSupported) glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, width, height, 0, GL_RGBA, GL_FLOAT, (float *)data); break; case COMPRESSED_DXT1_RGB: if (texCompDXTSupported) LoadTextureCompressed((unsigned char *)data, width, height, GL_COMPRESSED_RGB_S3TC_DXT1_EXT, mipmapCount); break; case COMPRESSED_DXT1_RGBA: if (texCompDXTSupported) LoadTextureCompressed((unsigned char *)data, width, height, GL_COMPRESSED_RGBA_S3TC_DXT1_EXT, mipmapCount); break; case COMPRESSED_DXT3_RGBA: if (texCompDXTSupported) LoadTextureCompressed((unsigned char *)data, width, height, GL_COMPRESSED_RGBA_S3TC_DXT3_EXT, mipmapCount); break; diff --git a/src/textures.c b/src/textures.c index 7ea54c2c..fe75bd74 100644 --- a/src/textures.c +++ b/src/textures.c @@ -417,6 +417,7 @@ void UnloadRenderTexture(RenderTexture2D target) } // Get pixel data from image in the form of Color struct array +// TODO: Support float pixel data retrieval Color *GetImageData(Image image) { Color *pixels = (Color *)malloc(image.width*image.height*sizeof(Color)); @@ -719,7 +720,7 @@ void ImageFormat(Image *image, int newFormat) case UNCOMPRESSED_R32: { image->data = (float *)malloc(image->width*image->height*sizeof(float)); - + for (int i = 0; i < image->width*image->height; i++) { ((float *)image->data)[i] = (float)((float)pixels[i].r*0.299f/255.0f + (float)pixels[i].g*0.587f/255.0f + (float)pixels[i].b*0.114f/255.0f); -- cgit v1.2.3 From e4be917d1b36a65286ca49b85328187ea8c190ad Mon Sep 17 00:00:00 2001 From: raysan5 Date: Sun, 7 Jan 2018 00:51:26 +0100 Subject: Added new image functions - Added: ImageAlphaClear() - Added: ImageAlphaPremultiply() - Reorganized some functions --- src/raylib.h | 6 +- src/textures.c | 374 +++++++++++++++++++++++++++++++-------------------------- 2 files changed, 210 insertions(+), 170 deletions(-) (limited to 'src/textures.c') diff --git a/src/raylib.h b/src/raylib.h index c44653e8..b482ab3b 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -876,14 +876,16 @@ RLAPI void UpdateTexture(Texture2D texture, const void *pixels); RLAPI void SaveImageAs(const char *fileName, Image image); // Save image to a PNG file // Image manipulation functions +RLAPI Image ImageCopy(Image image); // Create an image duplicate (useful for transformations) RLAPI void ImageToPOT(Image *image, Color fillColor); // Convert image to POT (power-of-two) RLAPI void ImageFormat(Image *image, int newFormat); // Convert image data to desired format RLAPI void ImageAlphaMask(Image *image, Image alphaMask); // Apply alpha mask to image -RLAPI void ImageDither(Image *image, int rBpp, int gBpp, int bBpp, int aBpp); // Dither image data to 16bpp or lower (Floyd-Steinberg dithering) -RLAPI Image ImageCopy(Image image); // Create an image duplicate (useful for transformations) +RLAPI void ImageAlphaClear(Image *image, Color color, float threshold); // Clear alpha channel to desired color +RLAPI void ImageAlphaPremultiply(Image *image); // Premultiply alpha channel RLAPI void ImageCrop(Image *image, Rectangle crop); // Crop an image to a defined rectangle RLAPI void ImageResize(Image *image, int newWidth, int newHeight); // Resize and image (bilinear filtering) RLAPI void ImageResizeNN(Image *image,int newWidth,int newHeight); // Resize and image (Nearest-Neighbor scaling algorithm) +RLAPI void ImageDither(Image *image, int rBpp, int gBpp, int bBpp, int aBpp); // Dither image data to 16bpp or lower (Floyd-Steinberg dithering) RLAPI Image ImageText(const char *text, int fontSize, Color color); // Create an image from text (default font) RLAPI Image ImageTextEx(SpriteFont font, const char *text, float fontSize, int spacing, Color tint); // Create an image from text (custom sprite font) RLAPI void ImageDraw(Image *dst, Image src, Rectangle srcRec, Rectangle dstRec); // Draw a source image within a destination image diff --git a/src/textures.c b/src/textures.c index fe75bd74..44a9fef2 100644 --- a/src/textures.c +++ b/src/textures.c @@ -593,6 +593,74 @@ void SaveImageAs(const char *fileName, Image image) #endif } +// Copy an image to a new image +Image ImageCopy(Image image) +{ + Image newImage = { 0 }; + + int size = GetPixelDataSize(image.width, image.height, image.format); + + newImage.data = malloc(size); + + if (newImage.data != NULL) + { + // NOTE: Size must be provided in bytes + memcpy(newImage.data, image.data, size); + + newImage.width = image.width; + newImage.height = image.height; + newImage.mipmaps = image.mipmaps; + newImage.format = image.format; + } + + return newImage; +} + +// Convert image to POT (power-of-two) +// NOTE: It could be useful on OpenGL ES 2.0 (RPI, HTML5) +void ImageToPOT(Image *image, Color fillColor) +{ + Color *pixels = GetImageData(*image); // Get pixels data + + // Calculate next power-of-two values + // NOTE: Just add the required amount of pixels at the right and bottom sides of image... + int potWidth = (int)powf(2, ceilf(logf((float)image->width)/logf(2))); + int potHeight = (int)powf(2, ceilf(logf((float)image->height)/logf(2))); + + // Check if POT texture generation is required (if texture is not already POT) + if ((potWidth != image->width) || (potHeight != image->height)) + { + Color *pixelsPOT = NULL; + + // Generate POT array from NPOT data + pixelsPOT = (Color *)malloc(potWidth*potHeight*sizeof(Color)); + + for (int j = 0; j < potHeight; j++) + { + for (int i = 0; i < potWidth; i++) + { + if ((j < image->height) && (i < image->width)) pixelsPOT[j*potWidth + i] = pixels[j*image->width + i]; + else pixelsPOT[j*potWidth + i] = fillColor; + } + } + + TraceLog(LOG_WARNING, "Image converted to POT: (%ix%i) -> (%ix%i)", image->width, image->height, potWidth, potHeight); + + free(pixels); // Free pixels data + free(image->data); // Free old image data + + int format = image->format; // Store image data format to reconvert later + + // TODO: Image width and height changes... do we want to store new values or keep the old ones? + // NOTE: Issues when using image.width and image.height for sprite animations... + *image = LoadImageEx(pixelsPOT, potWidth, potHeight); + + free(pixelsPOT); // Free POT pixels data + + ImageFormat(image, format); // Reconvert image to previous format + } +} + // Convert image data to desired format void ImageFormat(Image *image, int newFormat) { @@ -806,75 +874,45 @@ void ImageAlphaMask(Image *image, Image alphaMask) } } -// Convert image to POT (power-of-two) -// NOTE: It could be useful on OpenGL ES 2.0 (RPI, HTML5) -void ImageToPOT(Image *image, Color fillColor) +// Clear alpha channel to desired color +// NOTE: Threshold defines the alpha limit, 0.0f to 1.0f +void ImageAlphaClear(Image *image, Color color, float threshold) { - Color *pixels = GetImageData(*image); // Get pixels data - - // Calculate next power-of-two values - // NOTE: Just add the required amount of pixels at the right and bottom sides of image... - int potWidth = (int)powf(2, ceilf(logf((float)image->width)/logf(2))); - int potHeight = (int)powf(2, ceilf(logf((float)image->height)/logf(2))); - - // Check if POT texture generation is required (if texture is not already POT) - if ((potWidth != image->width) || (potHeight != image->height)) - { - Color *pixelsPOT = NULL; - - // Generate POT array from NPOT data - pixelsPOT = (Color *)malloc(potWidth*potHeight*sizeof(Color)); - - for (int j = 0; j < potHeight; j++) - { - for (int i = 0; i < potWidth; i++) - { - if ((j < image->height) && (i < image->width)) pixelsPOT[j*potWidth + i] = pixels[j*image->width + i]; - else pixelsPOT[j*potWidth + i] = fillColor; - } - } - - TraceLog(LOG_WARNING, "Image converted to POT: (%ix%i) -> (%ix%i)", image->width, image->height, potWidth, potHeight); - - free(pixels); // Free pixels data - free(image->data); // Free old image data - - int format = image->format; // Store image data format to reconvert later - - // TODO: Image width and height changes... do we want to store new values or keep the old ones? - // NOTE: Issues when using image.width and image.height for sprite animations... - *image = LoadImageEx(pixelsPOT, potWidth, potHeight); - - free(pixelsPOT); // Free POT pixels data + Color *pixels = GetImageData(*image); + + for (int i = 0; i < image->width*image->height; i++) if (pixels[i].a <= (unsigned char)(threshold*255.0f)) pixels[i] = color; - ImageFormat(image, format); // Reconvert image to previous format - } + UnloadImage(*image); + + int prevFormat = image->format; + *image = LoadImageEx(pixels, image->width, image->height); + + ImageFormat(image, prevFormat); } -#if defined(SUPPORT_IMAGE_MANIPULATION) -// Copy an image to a new image -Image ImageCopy(Image image) +// Premultiply alpha channel +void ImageAlphaPremultiply(Image *image) { - Image newImage = { 0 }; - - int size = GetPixelDataSize(image.width, image.height, image.format); - - newImage.data = malloc(size); - - if (newImage.data != NULL) + float alpha = 0.0f; + Color *pixels = GetImageData(*image); + + for (int i = 0; i < image->width*image->height; i++) { - // NOTE: Size must be provided in bytes - memcpy(newImage.data, image.data, size); - - newImage.width = image.width; - newImage.height = image.height; - newImage.mipmaps = image.mipmaps; - newImage.format = image.format; + alpha = (float)pixels[i].a/255.0f; + pixels[i].r = (unsigned char)((float)pixels[i].r*alpha); + pixels[i].g = (unsigned char)((float)pixels[i].g*alpha); + pixels[i].b = (unsigned char)((float)pixels[i].b*alpha); } - return newImage; + UnloadImage(*image); + + int prevFormat = image->format; + *image = LoadImageEx(pixels, image->width, image->height); + + ImageFormat(image, prevFormat); } +#if defined(SUPPORT_IMAGE_MANIPULATION) // Crop an image to area defined by a rectangle // NOTE: Security checks are performed in case rectangle goes out of bounds void ImageCrop(Image *image, Rectangle crop) @@ -982,6 +1020,115 @@ void ImageResizeNN(Image *image,int newWidth,int newHeight) free(pixels); } +// Dither image data to 16bpp or lower (Floyd-Steinberg dithering) +// NOTE: In case selected bpp do not represent an known 16bit format, +// dithered data is stored in the LSB part of the unsigned short +void ImageDither(Image *image, int rBpp, int gBpp, int bBpp, int aBpp) +{ + if (image->format >= COMPRESSED_DXT1_RGB) + { + TraceLog(LOG_WARNING, "Compressed data formats can not be dithered"); + return; + } + + if ((rBpp+gBpp+bBpp+aBpp) > 16) + { + TraceLog(LOG_WARNING, "Unsupported dithering bpps (%ibpp), only 16bpp or lower modes supported", (rBpp+gBpp+bBpp+aBpp)); + } + else + { + Color *pixels = GetImageData(*image); + + free(image->data); // free old image data + + if ((image->format != UNCOMPRESSED_R8G8B8) && (image->format != UNCOMPRESSED_R8G8B8A8)) + { + TraceLog(LOG_WARNING, "Image format is already 16bpp or lower, dithering could have no effect"); + } + + // Define new image format, check if desired bpp match internal known format + if ((rBpp == 5) && (gBpp == 6) && (bBpp == 5) && (aBpp == 0)) image->format = UNCOMPRESSED_R5G6B5; + else if ((rBpp == 5) && (gBpp == 5) && (bBpp == 5) && (aBpp == 1)) image->format = UNCOMPRESSED_R5G5B5A1; + else if ((rBpp == 4) && (gBpp == 4) && (bBpp == 4) && (aBpp == 4)) image->format = UNCOMPRESSED_R4G4B4A4; + else + { + image->format = 0; + TraceLog(LOG_WARNING, "Unsupported dithered OpenGL internal format: %ibpp (R%iG%iB%iA%i)", (rBpp+gBpp+bBpp+aBpp), rBpp, gBpp, bBpp, aBpp); + } + + // NOTE: We will store the dithered data as unsigned short (16bpp) + image->data = (unsigned short *)malloc(image->width*image->height*sizeof(unsigned short)); + + Color oldPixel = WHITE; + Color newPixel = WHITE; + + int rError, gError, bError; + unsigned short rPixel, gPixel, bPixel, aPixel; // Used for 16bit pixel composition + + #define MIN(a,b) (((a)<(b))?(a):(b)) + + for (int y = 0; y < image->height; y++) + { + for (int x = 0; x < image->width; x++) + { + oldPixel = pixels[y*image->width + x]; + + // NOTE: New pixel obtained by bits truncate, it would be better to round values (check ImageFormat()) + newPixel.r = oldPixel.r >> (8 - rBpp); // R bits + newPixel.g = oldPixel.g >> (8 - gBpp); // G bits + newPixel.b = oldPixel.b >> (8 - bBpp); // B bits + newPixel.a = oldPixel.a >> (8 - aBpp); // A bits (not used on dithering) + + // NOTE: Error must be computed between new and old pixel but using same number of bits! + // We want to know how much color precision we have lost... + rError = (int)oldPixel.r - (int)(newPixel.r << (8 - rBpp)); + gError = (int)oldPixel.g - (int)(newPixel.g << (8 - gBpp)); + bError = (int)oldPixel.b - (int)(newPixel.b << (8 - bBpp)); + + pixels[y*image->width + x] = newPixel; + + // NOTE: Some cases are out of the array and should be ignored + if (x < (image->width - 1)) + { + pixels[y*image->width + x+1].r = MIN((int)pixels[y*image->width + x+1].r + (int)((float)rError*7.0f/16), 0xff); + pixels[y*image->width + x+1].g = MIN((int)pixels[y*image->width + x+1].g + (int)((float)gError*7.0f/16), 0xff); + pixels[y*image->width + x+1].b = MIN((int)pixels[y*image->width + x+1].b + (int)((float)bError*7.0f/16), 0xff); + } + + if ((x > 0) && (y < (image->height - 1))) + { + pixels[(y+1)*image->width + x-1].r = MIN((int)pixels[(y+1)*image->width + x-1].r + (int)((float)rError*3.0f/16), 0xff); + pixels[(y+1)*image->width + x-1].g = MIN((int)pixels[(y+1)*image->width + x-1].g + (int)((float)gError*3.0f/16), 0xff); + pixels[(y+1)*image->width + x-1].b = MIN((int)pixels[(y+1)*image->width + x-1].b + (int)((float)bError*3.0f/16), 0xff); + } + + if (y < (image->height - 1)) + { + pixels[(y+1)*image->width + x].r = MIN((int)pixels[(y+1)*image->width + x].r + (int)((float)rError*5.0f/16), 0xff); + pixels[(y+1)*image->width + x].g = MIN((int)pixels[(y+1)*image->width + x].g + (int)((float)gError*5.0f/16), 0xff); + pixels[(y+1)*image->width + x].b = MIN((int)pixels[(y+1)*image->width + x].b + (int)((float)bError*5.0f/16), 0xff); + } + + if ((x < (image->width - 1)) && (y < (image->height - 1))) + { + pixels[(y+1)*image->width + x+1].r = MIN((int)pixels[(y+1)*image->width + x+1].r + (int)((float)rError*1.0f/16), 0xff); + pixels[(y+1)*image->width + x+1].g = MIN((int)pixels[(y+1)*image->width + x+1].g + (int)((float)gError*1.0f/16), 0xff); + pixels[(y+1)*image->width + x+1].b = MIN((int)pixels[(y+1)*image->width + x+1].b + (int)((float)bError*1.0f/16), 0xff); + } + + rPixel = (unsigned short)newPixel.r; + gPixel = (unsigned short)newPixel.g; + bPixel = (unsigned short)newPixel.b; + aPixel = (unsigned short)newPixel.a; + + ((unsigned short *)image->data)[y*image->width + x] = (rPixel << (gBpp + bBpp + aBpp)) | (gPixel << (bBpp + aBpp)) | (bPixel << aBpp) | aPixel; + } + } + + free(pixels); + } +} + // Draw an image (source) within an image (destination) // TODO: Feel this function could be simplified... void ImageDraw(Image *dst, Image src, Rectangle srcRec, Rectangle dstRec) @@ -1205,115 +1352,6 @@ void ImageFlipHorizontal(Image *image) image->data = processed.data; } -// Dither image data to 16bpp or lower (Floyd-Steinberg dithering) -// NOTE: In case selected bpp do not represent an known 16bit format, -// dithered data is stored in the LSB part of the unsigned short -void ImageDither(Image *image, int rBpp, int gBpp, int bBpp, int aBpp) -{ - if (image->format >= COMPRESSED_DXT1_RGB) - { - TraceLog(LOG_WARNING, "Compressed data formats can not be dithered"); - return; - } - - if ((rBpp+gBpp+bBpp+aBpp) > 16) - { - TraceLog(LOG_WARNING, "Unsupported dithering bpps (%ibpp), only 16bpp or lower modes supported", (rBpp+gBpp+bBpp+aBpp)); - } - else - { - Color *pixels = GetImageData(*image); - - free(image->data); // free old image data - - if ((image->format != UNCOMPRESSED_R8G8B8) && (image->format != UNCOMPRESSED_R8G8B8A8)) - { - TraceLog(LOG_WARNING, "Image format is already 16bpp or lower, dithering could have no effect"); - } - - // Define new image format, check if desired bpp match internal known format - if ((rBpp == 5) && (gBpp == 6) && (bBpp == 5) && (aBpp == 0)) image->format = UNCOMPRESSED_R5G6B5; - else if ((rBpp == 5) && (gBpp == 5) && (bBpp == 5) && (aBpp == 1)) image->format = UNCOMPRESSED_R5G5B5A1; - else if ((rBpp == 4) && (gBpp == 4) && (bBpp == 4) && (aBpp == 4)) image->format = UNCOMPRESSED_R4G4B4A4; - else - { - image->format = 0; - TraceLog(LOG_WARNING, "Unsupported dithered OpenGL internal format: %ibpp (R%iG%iB%iA%i)", (rBpp+gBpp+bBpp+aBpp), rBpp, gBpp, bBpp, aBpp); - } - - // NOTE: We will store the dithered data as unsigned short (16bpp) - image->data = (unsigned short *)malloc(image->width*image->height*sizeof(unsigned short)); - - Color oldPixel = WHITE; - Color newPixel = WHITE; - - int rError, gError, bError; - unsigned short rPixel, gPixel, bPixel, aPixel; // Used for 16bit pixel composition - - #define MIN(a,b) (((a)<(b))?(a):(b)) - - for (int y = 0; y < image->height; y++) - { - for (int x = 0; x < image->width; x++) - { - oldPixel = pixels[y*image->width + x]; - - // NOTE: New pixel obtained by bits truncate, it would be better to round values (check ImageFormat()) - newPixel.r = oldPixel.r >> (8 - rBpp); // R bits - newPixel.g = oldPixel.g >> (8 - gBpp); // G bits - newPixel.b = oldPixel.b >> (8 - bBpp); // B bits - newPixel.a = oldPixel.a >> (8 - aBpp); // A bits (not used on dithering) - - // NOTE: Error must be computed between new and old pixel but using same number of bits! - // We want to know how much color precision we have lost... - rError = (int)oldPixel.r - (int)(newPixel.r << (8 - rBpp)); - gError = (int)oldPixel.g - (int)(newPixel.g << (8 - gBpp)); - bError = (int)oldPixel.b - (int)(newPixel.b << (8 - bBpp)); - - pixels[y*image->width + x] = newPixel; - - // NOTE: Some cases are out of the array and should be ignored - if (x < (image->width - 1)) - { - pixels[y*image->width + x+1].r = MIN((int)pixels[y*image->width + x+1].r + (int)((float)rError*7.0f/16), 0xff); - pixels[y*image->width + x+1].g = MIN((int)pixels[y*image->width + x+1].g + (int)((float)gError*7.0f/16), 0xff); - pixels[y*image->width + x+1].b = MIN((int)pixels[y*image->width + x+1].b + (int)((float)bError*7.0f/16), 0xff); - } - - if ((x > 0) && (y < (image->height - 1))) - { - pixels[(y+1)*image->width + x-1].r = MIN((int)pixels[(y+1)*image->width + x-1].r + (int)((float)rError*3.0f/16), 0xff); - pixels[(y+1)*image->width + x-1].g = MIN((int)pixels[(y+1)*image->width + x-1].g + (int)((float)gError*3.0f/16), 0xff); - pixels[(y+1)*image->width + x-1].b = MIN((int)pixels[(y+1)*image->width + x-1].b + (int)((float)bError*3.0f/16), 0xff); - } - - if (y < (image->height - 1)) - { - pixels[(y+1)*image->width + x].r = MIN((int)pixels[(y+1)*image->width + x].r + (int)((float)rError*5.0f/16), 0xff); - pixels[(y+1)*image->width + x].g = MIN((int)pixels[(y+1)*image->width + x].g + (int)((float)gError*5.0f/16), 0xff); - pixels[(y+1)*image->width + x].b = MIN((int)pixels[(y+1)*image->width + x].b + (int)((float)bError*5.0f/16), 0xff); - } - - if ((x < (image->width - 1)) && (y < (image->height - 1))) - { - pixels[(y+1)*image->width + x+1].r = MIN((int)pixels[(y+1)*image->width + x+1].r + (int)((float)rError*1.0f/16), 0xff); - pixels[(y+1)*image->width + x+1].g = MIN((int)pixels[(y+1)*image->width + x+1].g + (int)((float)gError*1.0f/16), 0xff); - pixels[(y+1)*image->width + x+1].b = MIN((int)pixels[(y+1)*image->width + x+1].b + (int)((float)bError*1.0f/16), 0xff); - } - - rPixel = (unsigned short)newPixel.r; - gPixel = (unsigned short)newPixel.g; - bPixel = (unsigned short)newPixel.b; - aPixel = (unsigned short)newPixel.a; - - ((unsigned short *)image->data)[y*image->width + x] = (rPixel << (gBpp + bBpp + aBpp)) | (gPixel << (bBpp + aBpp)) | (bPixel << aBpp) | aPixel; - } - } - - free(pixels); - } -} - // Modify image color: tint void ImageColorTint(Image *image, Color color) { -- cgit v1.2.3 From 278d8575bdd09ba6536291e1b66dc9d32224fb2d Mon Sep 17 00:00:00 2001 From: Ray Date: Thu, 11 Jan 2018 10:22:32 +0100 Subject: Added new function: ImageAlphaCrop() --- src/raylib.h | 1 + src/textures.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) (limited to 'src/textures.c') diff --git a/src/raylib.h b/src/raylib.h index b482ab3b..dc02370d 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -881,6 +881,7 @@ RLAPI void ImageToPOT(Image *image, Color fillColor); RLAPI void ImageFormat(Image *image, int newFormat); // Convert image data to desired format RLAPI void ImageAlphaMask(Image *image, Image alphaMask); // Apply alpha mask to image RLAPI void ImageAlphaClear(Image *image, Color color, float threshold); // Clear alpha channel to desired color +RLAPI void ImageAlphaCrop(Image *image, float threshold); // Crop image depending on alpha value RLAPI void ImageAlphaPremultiply(Image *image); // Premultiply alpha channel RLAPI void ImageCrop(Image *image, Rectangle crop); // Crop an image to a defined rectangle RLAPI void ImageResize(Image *image, int newWidth, int newHeight); // Resize and image (bilinear filtering) diff --git a/src/textures.c b/src/textures.c index 44a9fef2..665e5447 100644 --- a/src/textures.c +++ b/src/textures.c @@ -890,6 +890,51 @@ void ImageAlphaClear(Image *image, Color color, float threshold) ImageFormat(image, prevFormat); } +// Crop image depending on alpha value +void ImageAlphaCrop(Image *image, float threshold) +{ + Rectangle crop = { 0 }; + + Color *pixels = GetImageData(*image); + + int minx = 0; + int miny = 0; + + for (int i = 0; i < image->width*image->height; i++) + { + if (pixels[i].a > (unsigned char)(threshold*255.0f)) + { + minx = i%image->width; + miny = -(-((i/image->width) + 1) + 1); + + if (crop.y == 0) crop.y = miny; + + if (crop.x == 0) crop.x = minx; + else if (minx < crop.x) crop.x = minx; + + if (crop.width == 0) crop.width = minx; + else if (crop.width < minx) crop.width = minx; + + if (crop.height == 0) crop.height = miny; + else if (crop.height < miny) crop.height = miny; + } + } + + crop.width -= (crop.x - 1); + crop.height -= (crop.y - 1); + + TraceLog(LOG_INFO, "Crop rectangle: (%i, %i, %i, %i)", crop.x, crop.y, crop.width, crop.height); + + free(pixels); + + // NOTE: Added this weird check to avoid additional 1px crop to + // image data that has already been cropped... + if ((crop.x != 1) && + (crop.y != 1) && + (crop.width != image->width - 1) && + (crop.height != image->height - 1)) ImageCrop(image, crop); +} + // Premultiply alpha channel void ImageAlphaPremultiply(Image *image) { @@ -912,6 +957,8 @@ void ImageAlphaPremultiply(Image *image) ImageFormat(image, prevFormat); } + + #if defined(SUPPORT_IMAGE_MANIPULATION) // Crop an image to area defined by a rectangle // NOTE: Security checks are performed in case rectangle goes out of bounds -- cgit v1.2.3 From c8e97df233f50b0bc1cba07e014aca93c5c0f15c Mon Sep 17 00:00:00 2001 From: Ray Date: Wed, 17 Jan 2018 00:43:30 +0100 Subject: Reviewed function GenImagePerlinNoise() Added support for noise image offset --- examples/textures/textures_image_generation.c | 4 ++-- src/raylib.h | 2 +- src/textures.c | 21 +++++++++++++++------ 3 files changed, 18 insertions(+), 9 deletions(-) (limited to 'src/textures.c') diff --git a/examples/textures/textures_image_generation.c b/examples/textures/textures_image_generation.c index 7d8e017e..790c34f1 100644 --- a/examples/textures/textures_image_generation.c +++ b/examples/textures/textures_image_generation.c @@ -24,10 +24,10 @@ int main() Image verticalGradient = GenImageGradientV(screenWidth, screenHeight, RED, BLUE); Image horizontalGradient = GenImageGradientH(screenWidth, screenHeight, RED, BLUE); - Image radialGradient = GenImageGradientRadial(screenWidth, screenHeight, 0.f, WHITE, BLACK); + Image radialGradient = GenImageGradientRadial(screenWidth, screenHeight, 0.0f, WHITE, BLACK); Image checked = GenImageChecked(screenWidth, screenHeight, 32, 32, RED, BLUE); Image whiteNoise = GenImageWhiteNoise(screenWidth, screenHeight, 0.5f); - Image perlinNoise = GenImagePerlinNoise(screenWidth, screenHeight, 8.f); + Image perlinNoise = GenImagePerlinNoise(screenWidth, screenHeight, 50, 50, 4.0f); Image cellular = GenImageCellular(screenWidth, screenHeight, 32); Texture2D textures[NUM_TEXTURES]; diff --git a/src/raylib.h b/src/raylib.h index dc02370d..2bb04f77 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -908,7 +908,7 @@ RLAPI Image GenImageGradientH(int width, int height, Color left, Color right); RLAPI Image GenImageGradientRadial(int width, int height, float density, Color inner, Color outer); // Generate image: radial gradient RLAPI Image GenImageChecked(int width, int height, int checksX, int checksY, Color col1, Color col2); // Generate image: checked RLAPI Image GenImageWhiteNoise(int width, int height, float factor); // Generate image: white noise -RLAPI Image GenImagePerlinNoise(int width, int height, float scale); // Generate image: perlin noise +RLAPI Image GenImagePerlinNoise(int width, int height, int offsetX, int offsetY, float scale); // Generate image: perlin noise RLAPI Image GenImageCellular(int width, int height, int tileSize); // Generate image: cellular algorithm. Bigger tileSize means bigger cells // Texture2D configuration functions diff --git a/src/textures.c b/src/textures.c index 665e5447..e228b356 100644 --- a/src/textures.c +++ b/src/textures.c @@ -611,6 +611,8 @@ Image ImageCopy(Image image) newImage.height = image.height; newImage.mipmaps = image.mipmaps; newImage.format = image.format; + + //if (image.mipmaps > 1) ImageMipmaps(&newImage); } return newImage; @@ -823,6 +825,8 @@ void ImageFormat(Image *image, int newFormat) } free(pixels); + + //if (image->mipmaps > 1) ImageMipmaps(image); } else TraceLog(LOG_WARNING, "Image data format is compressed, can not be converted"); } @@ -1688,7 +1692,7 @@ Image GenImageWhiteNoise(int width, int height, float factor) } // Generate image: perlin noise -Image GenImagePerlinNoise(int width, int height, float scale) +Image GenImagePerlinNoise(int width, int height, int offsetX, int offsetY, float scale) { Color *pixels = (Color *)malloc(width*height*sizeof(Color)); @@ -1696,13 +1700,18 @@ Image GenImagePerlinNoise(int width, int height, float scale) { for (int x = 0; x < width; x++) { - float nx = (float)x*scale/(float)width; - float ny = (float)y*scale/(float)height; + float nx = (float)(x + offsetX)*scale/(float)width; + float ny = (float)(y + offsetY)*scale/(float)height; - // we need to translate the data from [-1; 1] to [0; 1] - float p = (stb_perlin_fbm_noise3(nx, ny, 1.0f, 2.0f, 0.5f, 6, 0, 0, 0) + 1.0f) / 2.0f; + // Typical values to start playing with: + // lacunarity = ~2.0 -- spacing between successive octaves (use exactly 2.0 for wrapping output) + // gain = 0.5 -- relative weighting applied to each successive octave + // octaves = 6 -- number of "octaves" of noise3() to sum + + // NOTE: We need to translate the data from [-1..1] to [0..1] + float p = (stb_perlin_fbm_noise3(nx, ny, 1.0f, 2.0f, 0.5f, 6, 0, 0, 0) + 1.0f)/2.0f; - int intensity = (int)(p * 255.0f); + int intensity = (int)(p*255.0f); pixels[y*width + x] = (Color){intensity, intensity, intensity, 255}; } } -- cgit v1.2.3 From ce9f191f1b0dee3bc9607e58232a166c10cef9c7 Mon Sep 17 00:00:00 2001 From: Ray Date: Thu, 18 Jan 2018 00:23:28 +0100 Subject: Added function: ImageMipmaps() --- src/raylib.h | 1 + src/textures.c | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+) (limited to 'src/textures.c') diff --git a/src/raylib.h b/src/raylib.h index 2bb04f77..85c035c3 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -886,6 +886,7 @@ RLAPI void ImageAlphaPremultiply(Image *image); RLAPI void ImageCrop(Image *image, Rectangle crop); // Crop an image to a defined rectangle RLAPI void ImageResize(Image *image, int newWidth, int newHeight); // Resize and image (bilinear filtering) RLAPI void ImageResizeNN(Image *image,int newWidth,int newHeight); // Resize and image (Nearest-Neighbor scaling algorithm) +RLAPI void ImageMipmaps(Image *image); // Generate all mipmap levels for a provided image RLAPI void ImageDither(Image *image, int rBpp, int gBpp, int bBpp, int aBpp); // Dither image data to 16bpp or lower (Floyd-Steinberg dithering) RLAPI Image ImageText(const char *text, int fontSize, Color color); // Create an image from text (default font) RLAPI Image ImageTextEx(SpriteFont font, const char *text, float fontSize, int spacing, Color tint); // Create an image from text (custom sprite font) diff --git a/src/textures.c b/src/textures.c index e228b356..f53adb73 100644 --- a/src/textures.c +++ b/src/textures.c @@ -1071,6 +1071,81 @@ void ImageResizeNN(Image *image,int newWidth,int newHeight) free(pixels); } +// Generate all mipmap levels for a provided image +// NOTE 1: Supports POT and NPOT images +// NOTE 2: image.data is scaled to include mipmap levels +// NOTE 3: Mipmaps format is the same as base image +void ImageMipmaps(Image *image) +{ + int mipCount = 1; // Required mipmap levels count (including base level) + int mipWidth = image->width; // Base image width + int mipHeight = image->height; // Base image height + int mipSize = GetPixelDataSize(mipWidth, mipHeight, image->format); // Image data size (in bytes) + + // Count mipmap levels required + while ((mipWidth != 1) || (mipHeight != 1)) + { + if (mipWidth != 1) mipWidth /= 2; + if (mipHeight != 1) mipHeight /= 2; + + // Security check for NPOT textures + if (mipWidth < 1) mipWidth = 1; + if (mipHeight < 1) mipHeight = 1; + + TraceLog(LOG_DEBUG, "Next mipmap level: %i x %i - current size %i", mipWidth, mipHeight, mipSize); + + mipCount++; + mipSize += GetPixelDataSize(mipWidth, mipHeight, image->format); // Add mipmap size (in bytes) + } + + TraceLog(LOG_DEBUG, "Total mipmaps required: %i", mipCount); + TraceLog(LOG_DEBUG, "Total size of data required: %i", mipSize); + TraceLog(LOG_DEBUG, "Image data original memory point: %i", image->data); + + if (image->mipmaps < mipCount) + { + void *temp = realloc(image->data, mipSize); + + if (temp != NULL) + { + image->data = temp; // Assign new pointer (new size) to store mipmaps data + TraceLog(LOG_DEBUG, "Image data memory point reallocated: %i", temp); + } + else TraceLog(LOG_WARNING, "Mipmaps required memory could not be allocated"); + + // Pointer to allocated memory point where store next mipmap level data + unsigned char *nextmip = image->data + GetPixelDataSize(image->width, image->height, image->format); + + mipWidth = image->width/2; + mipHeight = image->height/2; + mipSize = GetPixelDataSize(mipWidth, mipHeight, image->format); + Image imCopy = ImageCopy(*image); + + for (int i = 1; i < mipCount; i++) + { + TraceLog(LOG_DEBUG, "Next mipmap level %i (%i x %i) - size %i - mem pos: %i", i, mipWidth, mipHeight, mipSize, nextmip); + + ImageResize(&imCopy, mipWidth, mipHeight); // Uses internally Mitchell cubic downscale filter + + memcpy(nextmip, imCopy.data, mipSize); + nextmip += mipSize; + image->mipmaps++; + + mipWidth /= 2; + mipHeight /= 2; + + // Security check for NPOT textures + if (mipWidth < 1) mipWidth = 1; + if (mipHeight < 1) mipHeight = 1; + + mipSize = GetPixelDataSize(mipWidth, mipHeight, image->format); + } + + UnloadImage(imCopy); + } + else TraceLog(LOG_WARNING, "Image mipmaps already available"); +} + // Dither image data to 16bpp or lower (Floyd-Steinberg dithering) // NOTE: In case selected bpp do not represent an known 16bit format, // dithered data is stored in the LSB part of the unsigned short -- cgit v1.2.3 From a2edc9d64191d9b9253ee7faa5f80cd742f769b2 Mon Sep 17 00:00:00 2001 From: Ahmad Fatoum Date: Thu, 18 Jan 2018 13:19:19 +0100 Subject: MSVC: Fix void pointer arithmetic error GNU C allows it as a compiler extension, but MSVC doesn't. --- src/textures.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/textures.c') diff --git a/src/textures.c b/src/textures.c index f53adb73..50f7e63a 100644 --- a/src/textures.c +++ b/src/textures.c @@ -1114,7 +1114,7 @@ void ImageMipmaps(Image *image) else TraceLog(LOG_WARNING, "Mipmaps required memory could not be allocated"); // Pointer to allocated memory point where store next mipmap level data - unsigned char *nextmip = image->data + GetPixelDataSize(image->width, image->height, image->format); + unsigned char *nextmip = (unsigned char *)image->data + GetPixelDataSize(image->width, image->height, image->format); mipWidth = image->width/2; mipHeight = image->height/2; -- cgit v1.2.3 From 04af83ff997dc4836fe059685de316d02cad85a6 Mon Sep 17 00:00:00 2001 From: Ray Date: Mon, 22 Jan 2018 00:20:42 +0100 Subject: Improved pixel formats support Review rlLoadTexture() function to make it simpler, now OpenGL texture glInternalFormat, glFormat and glType are retrieved with new function GetGlFormats() --- src/rlgl.c | 200 +++++++++++++++++++++++++-------------------------------- src/textures.c | 48 ++++++++------ 2 files changed, 114 insertions(+), 134 deletions(-) (limited to 'src/textures.c') diff --git a/src/rlgl.c b/src/rlgl.c index d94edd7f..afb366e7 100644 --- a/src/rlgl.c +++ b/src/rlgl.c @@ -358,6 +358,9 @@ static void UnloadBuffersDefault(void); // Unload default internal buffers v static void GenDrawCube(void); // Generate and draw cube static void GenDrawQuad(void); // Generate and draw quad +// Get OpenGL internal formats and data type from raylib PixelFormat +static void GetGlFormats(int format, int *glInternalFormat, int *glFormat, int *glType); + #if defined(SUPPORT_VR_SIMULATOR) static void SetStereoConfig(VrDeviceInfo info); // Configure stereo rendering (including distortion shader) with HMD device parameters static void SetStereoView(int eye, Matrix matProjection, Matrix matModelView); // Set internal projection and modelview matrix depending on eye @@ -1418,7 +1421,6 @@ unsigned int rlLoadTexture(void *data, int width, int height, int format, int mi glPixelStorei(GL_UNPACK_ALIGNMENT, 1); - glGenTextures(1, &id); // Generate Pointer to the texture #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) @@ -1436,76 +1438,28 @@ unsigned int rlLoadTexture(void *data, int width, int height, int format, int mi { unsigned int mipSize = GetPixelDataSize(mipWidth, mipHeight, format); - switch (format) + int glInternalFormat, glFormat, glType; + GetGlFormats(format, &glInternalFormat, &glFormat, &glType); + + TraceLog(LOG_INFO, "Mip level %i (%i x %i), size: %i, formats: %i - %i", i, mipWidth, mipHeight, mipSize, glInternalFormat, glFormat); + + if (glInternalFormat != -1) { - #if defined(GRAPHICS_API_OPENGL_11) || defined(GRAPHICS_API_OPENGL_21) || defined(GRAPHICS_API_OPENGL_ES2) - // NOTE: on OpenGL ES 2.0 (WebGL), internalFormat must match format and options allowed are: GL_LUMINANCE, GL_RGB, GL_RGBA - case UNCOMPRESSED_GRAYSCALE: glTexImage2D(GL_TEXTURE_2D, i, GL_LUMINANCE, mipWidth, mipHeight, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, (unsigned char *)data + mipOffset); break; - case UNCOMPRESSED_GRAY_ALPHA: glTexImage2D(GL_TEXTURE_2D, i, GL_LUMINANCE_ALPHA, mipWidth, mipHeight, 0, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, (unsigned char *)data + mipOffset); break; - case UNCOMPRESSED_R5G6B5: glTexImage2D(GL_TEXTURE_2D, i, GL_RGB, mipWidth, mipHeight, 0, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, (unsigned char *)data + mipOffset); break; - case UNCOMPRESSED_R8G8B8: glTexImage2D(GL_TEXTURE_2D, i, GL_RGB, mipWidth, mipHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, (unsigned char *)data + mipOffset); break; - case UNCOMPRESSED_R5G5B5A1: glTexImage2D(GL_TEXTURE_2D, i, GL_RGBA, mipWidth, mipHeight, 0, GL_RGBA, GL_UNSIGNED_SHORT_5_5_5_1, (unsigned char *)data + mipOffset); break; - case UNCOMPRESSED_R4G4B4A4: glTexImage2D(GL_TEXTURE_2D, i, GL_RGBA, mipWidth, mipHeight, 0, GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4, (unsigned char *)data + mipOffset); break; - case UNCOMPRESSED_R8G8B8A8: glTexImage2D(GL_TEXTURE_2D, i, GL_RGBA, mipWidth, mipHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, (unsigned char *)data + mipOffset); break; - #if !defined(GRAPHICS_API_OPENGL_11) - case UNCOMPRESSED_R32: if (texFloatSupported) glTexImage2D(GL_TEXTURE_2D, i, GL_LUMINANCE, mipWidth, mipHeight, 0, GL_LUMINANCE, GL_FLOAT, (unsigned char *)data + mipOffset); break; // NOTE: Requires extension OES_texture_float - case UNCOMPRESSED_R32G32B32: if (texFloatSupported) glTexImage2D(GL_TEXTURE_2D, i, GL_RGB, mipWidth, mipHeight, 0, GL_RGB, GL_FLOAT, (unsigned char *)data + mipOffset); break; // NOTE: Requires extension OES_texture_float - case UNCOMPRESSED_R32G32B32A32: if (texFloatSupported) glTexImage2D(GL_TEXTURE_2D, i, GL_RGBA, mipWidth, mipHeight, 0, GL_RGBA, GL_FLOAT, (unsigned char *)data + mipOffset); break; // NOTE: Requires extension OES_texture_float - #endif - #elif defined(GRAPHICS_API_OPENGL_33) - // NOTE: We define internal (GPU) format as GL_RGBA8 (probably BGRA8 in practice, driver takes care) - // NOTE: On embedded systems, we let the driver choose the best internal format - - // Support for multiple color modes (16bit color modes and grayscale) - // (sized)internalFormat format type - // GL_R GL_RED GL_UNSIGNED_BYTE - // GL_RGB565 GL_RGB GL_UNSIGNED_BYTE, GL_UNSIGNED_SHORT_5_6_5 - // GL_RGB5_A1 GL_RGBA GL_UNSIGNED_BYTE, GL_UNSIGNED_SHORT_5_5_5_1 - // GL_RGBA4 GL_RGBA GL_UNSIGNED_BYTE, GL_UNSIGNED_SHORT_4_4_4_4 - // GL_RGBA8 GL_RGBA GL_UNSIGNED_BYTE - // GL_RGB8 GL_RGB GL_UNSIGNED_BYTE - - case UNCOMPRESSED_GRAYSCALE: + if (format < COMPRESSED_DXT1_RGB) glTexImage2D(GL_TEXTURE_2D, i, glInternalFormat, mipWidth, mipHeight, 0, glFormat, glType, (unsigned char *)data + mipOffset); + else glCompressedTexImage2D(GL_TEXTURE_2D, i, glInternalFormat, mipWidth, mipHeight, 0, mipSize, (unsigned char *)data + mipOffset); + + #if defined(GRAPHICS_API_OPENGL_33) + if (format == UNCOMPRESSED_GRAYSCALE) { - glTexImage2D(GL_TEXTURE_2D, i, GL_R8, mipWidth, mipHeight, 0, GL_RED, GL_UNSIGNED_BYTE, (unsigned char *)data + mipOffset); - - // With swizzleMask we define how a one channel texture will be mapped to RGBA - // Required GL >= 3.3 or EXT_texture_swizzle/ARB_texture_swizzle GLint swizzleMask[] = { GL_RED, GL_RED, GL_RED, GL_ONE }; glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_RGBA, swizzleMask); - - TraceLog(LOG_DEBUG, "[TEX ID %i] Grayscale texture loaded and swizzled", id); - } break; - case UNCOMPRESSED_GRAY_ALPHA: + } + else if (format == UNCOMPRESSED_GRAY_ALPHA) { - glTexImage2D(GL_TEXTURE_2D, i, GL_RG8, mipWidth, mipHeight, 0, GL_RG, GL_UNSIGNED_BYTE, (unsigned char *)data + mipOffset); - GLint swizzleMask[] = { GL_RED, GL_RED, GL_RED, GL_GREEN }; glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_RGBA, swizzleMask); - } break; - case UNCOMPRESSED_R5G6B5: glTexImage2D(GL_TEXTURE_2D, i, GL_RGB565, mipWidth, mipHeight, 0, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, (unsigned char *)data + mipOffset); break; - case UNCOMPRESSED_R8G8B8: glTexImage2D(GL_TEXTURE_2D, i, GL_RGB8, mipWidth, mipHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, (unsigned char *)data + mipOffset); break; - case UNCOMPRESSED_R5G5B5A1: glTexImage2D(GL_TEXTURE_2D, i, GL_RGB5_A1, mipWidth, mipHeight, 0, GL_RGBA, GL_UNSIGNED_SHORT_5_5_5_1, (unsigned char *)data + mipOffset); break; - case UNCOMPRESSED_R4G4B4A4: glTexImage2D(GL_TEXTURE_2D, i, GL_RGBA4, mipWidth, mipHeight, 0, GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4, (unsigned char *)data + mipOffset); break; - case UNCOMPRESSED_R8G8B8A8: glTexImage2D(GL_TEXTURE_2D, i, GL_RGBA8, mipWidth, mipHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, (unsigned char *)data + mipOffset); break; - case UNCOMPRESSED_R32: if (texFloatSupported) glTexImage2D(GL_TEXTURE_2D, i, GL_R32F, mipWidth, mipHeight, 0, GL_RED, GL_FLOAT, (unsigned char *)data + mipOffset); break; - case UNCOMPRESSED_R32G32B32: if (texFloatSupported) glTexImage2D(GL_TEXTURE_2D, i, GL_RGB32F, mipWidth, mipHeight, 0, GL_RGB, GL_FLOAT, (unsigned char *)data + mipOffset); break; - case UNCOMPRESSED_R32G32B32A32: if (texFloatSupported) glTexImage2D(GL_TEXTURE_2D, i, GL_RGBA32F, mipWidth, mipHeight, 0, GL_RGBA, GL_FLOAT, (unsigned char *)data + mipOffset); break; - #endif - #if !defined(GRAPHICS_API_OPENGL_11) - case COMPRESSED_DXT1_RGB: if (texCompDXTSupported) glCompressedTexImage2D(GL_TEXTURE_2D, i, GL_COMPRESSED_RGB_S3TC_DXT1_EXT, mipWidth, mipHeight, 0, mipSize, (unsigned char *)data + mipOffset); break; - case COMPRESSED_DXT1_RGBA: if (texCompDXTSupported) glCompressedTexImage2D(GL_TEXTURE_2D, i, GL_COMPRESSED_RGBA_S3TC_DXT1_EXT, mipWidth, mipHeight, 0, mipSize, (unsigned char *)data + mipOffset); break; - case COMPRESSED_DXT3_RGBA: if (texCompDXTSupported) glCompressedTexImage2D(GL_TEXTURE_2D, i, GL_COMPRESSED_RGBA_S3TC_DXT3_EXT, mipWidth, mipHeight, 0, mipSize, (unsigned char *)data + mipOffset); break; // NOTE: Not supported by WebGL - case COMPRESSED_DXT5_RGBA: if (texCompDXTSupported) glCompressedTexImage2D(GL_TEXTURE_2D, i, GL_COMPRESSED_RGBA_S3TC_DXT5_EXT, mipWidth, mipHeight, 0, mipSize, (unsigned char *)data + mipOffset); break; // NOTE: Not supported by WebGL - case COMPRESSED_ETC1_RGB: if (texCompETC1Supported) glCompressedTexImage2D(GL_TEXTURE_2D, i, GL_ETC1_RGB8_OES, mipWidth, mipHeight, 0, mipSize, (unsigned char *)data + mipOffset); break; // NOTE: Requires OpenGL ES 2.0 or OpenGL 4.3 - case COMPRESSED_ETC2_RGB: if (texCompETC2Supported) glCompressedTexImage2D(GL_TEXTURE_2D, i, GL_COMPRESSED_RGB8_ETC2, mipWidth, mipHeight, 0, mipSize, (unsigned char *)data + mipOffset); break; // NOTE: Requires OpenGL ES 3.0 or OpenGL 4.3 - case COMPRESSED_ETC2_EAC_RGBA: if (texCompETC2Supported) glCompressedTexImage2D(GL_TEXTURE_2D, i, GL_COMPRESSED_RGBA8_ETC2_EAC, mipWidth, mipHeight, 0, mipSize, (unsigned char *)data + mipOffset); break; // NOTE: Requires OpenGL ES 3.0 or OpenGL 4.3 - case COMPRESSED_PVRT_RGB: if (texCompPVRTSupported) glCompressedTexImage2D(GL_TEXTURE_2D, i, GL_COMPRESSED_RGB_PVRTC_4BPPV1_IMG, mipWidth, mipHeight, 0, mipSize, (unsigned char *)data + mipOffset); break; // NOTE: Requires PowerVR GPU - case COMPRESSED_PVRT_RGBA: if (texCompPVRTSupported) glCompressedTexImage2D(GL_TEXTURE_2D, i, GL_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG, mipWidth, mipHeight, 0, mipSize, (unsigned char *)data + mipOffset); break; // NOTE: Requires PowerVR GPU - case COMPRESSED_ASTC_4x4_RGBA: if (texCompASTCSupported) glCompressedTexImage2D(GL_TEXTURE_2D, i, GL_COMPRESSED_RGBA_ASTC_4x4_KHR, mipWidth, mipHeight, 0, mipSize, (unsigned char *)data + mipOffset); break; // NOTE: Requires OpenGL ES 3.1 or OpenGL 4.3 - case COMPRESSED_ASTC_8x8_RGBA: if (texCompASTCSupported) glCompressedTexImage2D(GL_TEXTURE_2D, i, GL_COMPRESSED_RGBA_ASTC_8x8_KHR, mipWidth, mipHeight, 0, mipSize, (unsigned char *)data + mipOffset); break; // NOTE: Requires OpenGL ES 3.1 or OpenGL 4.3 + } #endif - default: TraceLog(LOG_WARNING, "Texture format not supported"); break; } mipWidth /= 2; @@ -1567,33 +1521,15 @@ unsigned int rlLoadTexture(void *data, int width, int height, int format, int mi void rlUpdateTexture(unsigned int id, int width, int height, int format, const void *data) { glBindTexture(GL_TEXTURE_2D, id); + + int glInternalFormat, glFormat, glType; + GetGlFormats(format, &glInternalFormat, &glFormat, &glType); -#if defined(GRAPHICS_API_OPENGL_33) - switch (format) - { - case UNCOMPRESSED_GRAYSCALE: glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, GL_RED, GL_UNSIGNED_BYTE, (unsigned char *)data); break; - case UNCOMPRESSED_GRAY_ALPHA: glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, GL_RG, GL_UNSIGNED_BYTE, (unsigned char *)data); break; - case UNCOMPRESSED_R5G6B5: glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, (unsigned short *)data); break; - case UNCOMPRESSED_R8G8B8: glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE, (unsigned char *)data); break; - case UNCOMPRESSED_R5G5B5A1: glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, GL_RGBA, GL_UNSIGNED_SHORT_5_5_5_1, (unsigned short *)data); break; - case UNCOMPRESSED_R4G4B4A4: glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4, (unsigned short *)data); break; - case UNCOMPRESSED_R8G8B8A8: glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, (unsigned char *)data); break; - default: TraceLog(LOG_WARNING, "Texture format updating not supported"); break; - } -#elif defined(GRAPHICS_API_OPENGL_11) || defined(GRAPHICS_API_OPENGL_ES2) - // NOTE: on OpenGL ES 2.0 (WebGL), internalFormat must match format and options allowed are: GL_LUMINANCE, GL_RGB, GL_RGBA - switch (format) + if ((glInternalFormat != -1) && (format < COMPRESSED_DXT1_RGB)) { - case UNCOMPRESSED_GRAYSCALE: glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, GL_LUMINANCE, GL_UNSIGNED_BYTE, (unsigned char *)data); break; - case UNCOMPRESSED_GRAY_ALPHA: glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, (unsigned char *)data); break; - case UNCOMPRESSED_R5G6B5: glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, (unsigned short *)data); break; - case UNCOMPRESSED_R8G8B8: glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE, (unsigned char *)data); break; - case UNCOMPRESSED_R5G5B5A1: glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, GL_RGBA, GL_UNSIGNED_SHORT_5_5_5_1, (unsigned short *)data); break; - case UNCOMPRESSED_R4G4B4A4: glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4, (unsigned short *)data); break; - case UNCOMPRESSED_R8G8B8A8: glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, (unsigned char *)data); break; - default: TraceLog(LOG_WARNING, "Texture format updating not supported"); break; + glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, glFormat, glType, (unsigned char *)data); } -#endif + else TraceLog(LOG_WARNING, "Texture format updating not supported"); } // Unload texture from GPU memory @@ -2211,44 +2147,28 @@ void *rlReadTexturePixels(Texture2D texture) glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_INTERNAL_FORMAT, &format); // Other texture info: GL_TEXTURE_RED_SIZE, GL_TEXTURE_GREEN_SIZE, GL_TEXTURE_BLUE_SIZE, GL_TEXTURE_ALPHA_SIZE */ - - int glFormat = 0, glType = 0; - - unsigned int size = texture.width*texture.height; - - // NOTE: GL_LUMINANCE and GL_LUMINANCE_ALPHA are removed since OpenGL 3.1 - // Must be replaced by GL_RED and GL_RG on Core OpenGL 3.3 - - switch (texture.format) - { -#if defined(GRAPHICS_API_OPENGL_11) - case UNCOMPRESSED_GRAYSCALE: pixels = (unsigned char *)malloc(size); glFormat = GL_LUMINANCE; glType = GL_UNSIGNED_BYTE; break; // 8 bit per pixel (no alpha) - case UNCOMPRESSED_GRAY_ALPHA: pixels = (unsigned char *)malloc(size*2); glFormat = GL_LUMINANCE_ALPHA; glType = GL_UNSIGNED_BYTE; break; // 16 bpp (2 channels) -#elif defined(GRAPHICS_API_OPENGL_33) - case UNCOMPRESSED_GRAYSCALE: pixels = (unsigned char *)malloc(size); glFormat = GL_RED; glType = GL_UNSIGNED_BYTE; break; - case UNCOMPRESSED_GRAY_ALPHA: pixels = (unsigned char *)malloc(size*2); glFormat = GL_RG; glType = GL_UNSIGNED_BYTE; break; -#endif - case UNCOMPRESSED_R5G6B5: pixels = (unsigned short *)malloc(size); glFormat = GL_RGB; glType = GL_UNSIGNED_SHORT_5_6_5; break; // 16 bpp - case UNCOMPRESSED_R8G8B8: pixels = (unsigned char *)malloc(size*3); glFormat = GL_RGB; glType = GL_UNSIGNED_BYTE; break; // 24 bpp - case UNCOMPRESSED_R5G5B5A1: pixels = (unsigned short *)malloc(size); glFormat = GL_RGBA; glType = GL_UNSIGNED_SHORT_5_5_5_1; break; // 16 bpp (1 bit alpha) - case UNCOMPRESSED_R4G4B4A4: pixels = (unsigned short *)malloc(size); glFormat = GL_RGBA; glType = GL_UNSIGNED_SHORT_4_4_4_4; break; // 16 bpp (4 bit alpha) - case UNCOMPRESSED_R8G8B8A8: pixels = (unsigned char *)malloc(size*4); glFormat = GL_RGBA; glType = GL_UNSIGNED_BYTE; break; // 32 bpp - default: TraceLog(LOG_WARNING, "Texture data retrieval, format not suported"); break; - } - + // NOTE: Each row written to or read from by OpenGL pixel operations like glGetTexImage are aligned to a 4 byte boundary by default, which may add some padding. // Use glPixelStorei to modify padding with the GL_[UN]PACK_ALIGNMENT setting. // GL_PACK_ALIGNMENT affects operations that read from OpenGL memory (glReadPixels, glGetTexImage, etc.) // GL_UNPACK_ALIGNMENT affects operations that write to OpenGL memory (glTexImage, etc.) glPixelStorei(GL_PACK_ALIGNMENT, 1); - glGetTexImage(GL_TEXTURE_2D, 0, glFormat, glType, pixels); + int glInternalFormat, glFormat, glType; + GetGlFormats(texture.format, &glInternalFormat, &glFormat, &glType); + unsigned int size = GetPixelDataSize(texture.width, texture.height, texture.format); + + if ((glInternalFormat != -1) && (texture.format < COMPRESSED_DXT1_RGB)) + { + pixels = (unsigned char *)malloc(size); + glGetTexImage(GL_TEXTURE_2D, 0, glFormat, glType, pixels); + } + else TraceLog(LOG_WARNING, "Texture data retrieval not suported for pixel format"); glBindTexture(GL_TEXTURE_2D, 0); #endif #if defined(GRAPHICS_API_OPENGL_ES2) - RenderTexture2D fbo = rlLoadRenderTexture(texture.width, texture.height); // NOTE: Two possible Options: @@ -3989,6 +3909,58 @@ static void GenDrawCube(void) glDeleteVertexArrays(1, &cubeVAO); } +// Get OpenGL internal formats and data type from raylib PixelFormat +static void GetGlFormats(int format, int *glInternalFormat, int *glFormat, int *glType) +{ + *glInternalFormat = -1; + *glFormat = -1; + *glType = -1; + + switch (format) + { + #if defined(GRAPHICS_API_OPENGL_11) || defined(GRAPHICS_API_OPENGL_21) || defined(GRAPHICS_API_OPENGL_ES2) + // NOTE: on OpenGL ES 2.0 (WebGL), internalFormat must match format and options allowed are: GL_LUMINANCE, GL_RGB, GL_RGBA + case UNCOMPRESSED_GRAYSCALE: *glInternalFormat = GL_LUMINANCE; *glFormat = GL_LUMINANCE; *glType = GL_UNSIGNED_BYTE; break; + case UNCOMPRESSED_GRAY_ALPHA: *glInternalFormat = GL_LUMINANCE_ALPHA; *glFormat = GL_LUMINANCE_ALPHA; *glType = GL_UNSIGNED_BYTE; break; + case UNCOMPRESSED_R5G6B5: *glInternalFormat = GL_RGB; *glFormat = GL_RGB; *glType = GL_UNSIGNED_SHORT_5_6_5; break; + case UNCOMPRESSED_R8G8B8: *glInternalFormat = GL_RGB; *glFormat = GL_RGB; *glType = GL_UNSIGNED_BYTE; break; + case UNCOMPRESSED_R5G5B5A1: *glInternalFormat = GL_RGBA; *glFormat = GL_RGBA; *glType = GL_UNSIGNED_SHORT_5_5_5_1; break; + case UNCOMPRESSED_R4G4B4A4: *glInternalFormat = GL_RGBA; *glFormat = GL_RGBA; *glType = GL_UNSIGNED_SHORT_4_4_4_4; break; + case UNCOMPRESSED_R8G8B8A8: *glInternalFormat = GL_RGBA; *glFormat = GL_RGBA; *glType = GL_UNSIGNED_BYTE; break; + #if !defined(GRAPHICS_API_OPENGL_11) + case UNCOMPRESSED_R32: if (texFloatSupported) *glInternalFormat = GL_LUMINANCE; *glFormat = GL_LUMINANCE; *glType = GL_FLOAT; break; // NOTE: Requires extension OES_texture_float + case UNCOMPRESSED_R32G32B32: if (texFloatSupported) *glInternalFormat = GL_RGB; *glFormat = GL_RGB; *glType = GL_FLOAT; break; // NOTE: Requires extension OES_texture_float + case UNCOMPRESSED_R32G32B32A32: if (texFloatSupported) *glInternalFormat = GL_RGBA; *glFormat = GL_RGBA; *glType = GL_FLOAT; break; // NOTE: Requires extension OES_texture_float + #endif + #elif defined(GRAPHICS_API_OPENGL_33) + case UNCOMPRESSED_GRAYSCALE: *glInternalFormat = GL_R8; *glFormat = GL_RED; *glType = GL_UNSIGNED_BYTE; break; + case UNCOMPRESSED_GRAY_ALPHA: *glInternalFormat = GL_RG8; *glFormat = GL_RG; *glType = GL_UNSIGNED_BYTE; break; + case UNCOMPRESSED_R5G6B5: *glInternalFormat = GL_RGB565; *glFormat = GL_RGB; *glType = GL_UNSIGNED_SHORT_5_6_5; break; + case UNCOMPRESSED_R8G8B8: *glInternalFormat = GL_RGB8; *glFormat = GL_RGB; *glType = GL_UNSIGNED_BYTE; break; + case UNCOMPRESSED_R5G5B5A1: *glInternalFormat = GL_RGB5_A1; *glFormat = GL_RGBA; *glType = GL_UNSIGNED_SHORT_5_5_5_1; break; + case UNCOMPRESSED_R4G4B4A4: *glInternalFormat = GL_RGBA4; *glFormat = GL_RGBA; *glType = GL_UNSIGNED_SHORT_4_4_4_4; break; + case UNCOMPRESSED_R8G8B8A8: *glInternalFormat = GL_RGBA8; *glFormat = GL_RGBA; *glType = GL_UNSIGNED_BYTE; break; + case UNCOMPRESSED_R32: if (texFloatSupported) *glInternalFormat = GL_R32F; *glFormat = GL_RED; *glType = GL_FLOAT; break; + case UNCOMPRESSED_R32G32B32: if (texFloatSupported) *glInternalFormat = GL_RGB32F; *glFormat = GL_RGB; *glType = GL_FLOAT; break; + case UNCOMPRESSED_R32G32B32A32: if (texFloatSupported) *glInternalFormat = GL_RGBA32F; *glFormat = GL_RGBA; *glType = GL_FLOAT; break; + #endif + #if !defined(GRAPHICS_API_OPENGL_11) + case COMPRESSED_DXT1_RGB: if (texCompDXTSupported) *glInternalFormat = GL_COMPRESSED_RGB_S3TC_DXT1_EXT; break; + case COMPRESSED_DXT1_RGBA: if (texCompDXTSupported) *glInternalFormat = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT; break; + case COMPRESSED_DXT3_RGBA: if (texCompDXTSupported) *glInternalFormat = GL_COMPRESSED_RGBA_S3TC_DXT3_EXT; break; + case COMPRESSED_DXT5_RGBA: if (texCompDXTSupported) *glInternalFormat = GL_COMPRESSED_RGBA_S3TC_DXT5_EXT; break; + case COMPRESSED_ETC1_RGB: if (texCompETC1Supported) *glInternalFormat = GL_ETC1_RGB8_OES; break; // NOTE: Requires OpenGL ES 2.0 or OpenGL 4.3 + case COMPRESSED_ETC2_RGB: if (texCompETC2Supported) *glInternalFormat = GL_COMPRESSED_RGB8_ETC2; break; // NOTE: Requires OpenGL ES 3.0 or OpenGL 4.3 + case COMPRESSED_ETC2_EAC_RGBA: if (texCompETC2Supported) *glInternalFormat = GL_COMPRESSED_RGBA8_ETC2_EAC; break; // NOTE: Requires OpenGL ES 3.0 or OpenGL 4.3 + case COMPRESSED_PVRT_RGB: if (texCompPVRTSupported) *glInternalFormat = GL_COMPRESSED_RGB_PVRTC_4BPPV1_IMG; break; // NOTE: Requires PowerVR GPU + case COMPRESSED_PVRT_RGBA: if (texCompPVRTSupported) *glInternalFormat = GL_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG; break; // NOTE: Requires PowerVR GPU + case COMPRESSED_ASTC_4x4_RGBA: if (texCompASTCSupported) *glInternalFormat = GL_COMPRESSED_RGBA_ASTC_4x4_KHR; break; // NOTE: Requires OpenGL ES 3.1 or OpenGL 4.3 + case COMPRESSED_ASTC_8x8_RGBA: if (texCompASTCSupported) *glInternalFormat = GL_COMPRESSED_RGBA_ASTC_8x8_KHR; break; // NOTE: Requires OpenGL ES 3.1 or OpenGL 4.3 + #endif + default: TraceLog(LOG_WARNING, "Texture format not supported"); break; + } +} + #if defined(SUPPORT_VR_SIMULATOR) // Configure stereo rendering (including distortion shader) with HMD device parameters // NOTE: It modifies the global variable: VrStereoConfig vrConfig diff --git a/src/textures.c b/src/textures.c index 50f7e63a..2c7c1c2d 100644 --- a/src/textures.c +++ b/src/textures.c @@ -377,7 +377,7 @@ Texture2D LoadTextureFromImage(Image image) texture.mipmaps = image.mipmaps; texture.format = image.format; - TraceLog(LOG_DEBUG, "[TEX ID %i] Parameters: %ix%i, %i mips, format %i", texture.id, texture.width, texture.height, texture.mipmaps, texture.format); + TraceLog(LOG_INFO, "[TEX ID %i] Parameters: %ix%i, %i mips, format %i", texture.id, texture.width, texture.height, texture.mipmaps, texture.format); return texture; } @@ -422,20 +422,17 @@ Color *GetImageData(Image image) { Color *pixels = (Color *)malloc(image.width*image.height*sizeof(Color)); - int k = 0; - - for (int i = 0; i < image.width*image.height; i++) + for (int i = 0, k = 0; i < image.width*image.height; i++) { switch (image.format) { case UNCOMPRESSED_GRAYSCALE: { - pixels[i].r = ((unsigned char *)image.data)[k]; - pixels[i].g = ((unsigned char *)image.data)[k]; - pixels[i].b = ((unsigned char *)image.data)[k]; + pixels[i].r = ((unsigned char *)image.data)[i]; + pixels[i].g = ((unsigned char *)image.data)[i]; + pixels[i].b = ((unsigned char *)image.data)[i]; pixels[i].a = 255; - - k++; + } break; case UNCOMPRESSED_GRAY_ALPHA: { @@ -448,36 +445,33 @@ Color *GetImageData(Image image) } break; case UNCOMPRESSED_R5G5B5A1: { - unsigned short pixel = ((unsigned short *)image.data)[k]; + unsigned short pixel = ((unsigned short *)image.data)[i]; pixels[i].r = (unsigned char)((float)((pixel & 0b1111100000000000) >> 11)*(255/31)); pixels[i].g = (unsigned char)((float)((pixel & 0b0000011111000000) >> 6)*(255/31)); pixels[i].b = (unsigned char)((float)((pixel & 0b0000000000111110) >> 1)*(255/31)); pixels[i].a = (unsigned char)((pixel & 0b0000000000000001)*255); - k++; } break; case UNCOMPRESSED_R5G6B5: { - unsigned short pixel = ((unsigned short *)image.data)[k]; + unsigned short pixel = ((unsigned short *)image.data)[i]; pixels[i].r = (unsigned char)((float)((pixel & 0b1111100000000000) >> 11)*(255/31)); pixels[i].g = (unsigned char)((float)((pixel & 0b0000011111100000) >> 5)*(255/63)); pixels[i].b = (unsigned char)((float)(pixel & 0b0000000000011111)*(255/31)); pixels[i].a = 255; - k++; } break; case UNCOMPRESSED_R4G4B4A4: { - unsigned short pixel = ((unsigned short *)image.data)[k]; + unsigned short pixel = ((unsigned short *)image.data)[i]; pixels[i].r = (unsigned char)((float)((pixel & 0b1111000000000000) >> 12)*(255/15)); pixels[i].g = (unsigned char)((float)((pixel & 0b0000111100000000) >> 8)*(255/15)); pixels[i].b = (unsigned char)((float)((pixel & 0b0000000011110000) >> 4)*(255/15)); pixels[i].a = (unsigned char)((float)(pixel & 0b0000000000001111)*(255/15)); - - k++; + } break; case UNCOMPRESSED_R8G8B8A8: { @@ -598,8 +592,22 @@ Image ImageCopy(Image image) { Image newImage = { 0 }; - int size = GetPixelDataSize(image.width, image.height, image.format); + int width = image.width; + int height = image.height; + int size = 0; + for (int i = 0; i < image.mipmaps; i++) + { + size += GetPixelDataSize(width, height, image.format); + + width /= 2; + height /= 2; + + // Security check for NPOT textures + if (width < 1) width = 1; + if (height < 1) height = 1; + } + newImage.data = malloc(size); if (newImage.data != NULL) @@ -611,8 +619,6 @@ Image ImageCopy(Image image) newImage.height = image.height; newImage.mipmaps = image.mipmaps; newImage.format = image.format; - - //if (image.mipmaps > 1) ImageMipmaps(&newImage); } return newImage; @@ -826,7 +832,9 @@ void ImageFormat(Image *image, int newFormat) free(pixels); - //if (image->mipmaps > 1) ImageMipmaps(image); + // In case original image had mipmaps, generate mipmaps for formated image + // NOTE: Original mipmaps are replaced by new ones, if custom mipmaps were used, they are lost + if (image->mipmaps > 1) ImageMipmaps(image); } else TraceLog(LOG_WARNING, "Image data format is compressed, can not be converted"); } -- cgit v1.2.3 From 2dcaddc81ca2918dc6b474ed703ede2b04d1e61e Mon Sep 17 00:00:00 2001 From: Ray Date: Tue, 23 Jan 2018 13:23:34 +0100 Subject: Review mipmaps generation issue When changing image format, mipmaps are lost and regenerated from scratch --- src/rlgl.c | 6 ++++-- src/textures.c | 35 ++++++++++++++++------------------- 2 files changed, 20 insertions(+), 21 deletions(-) (limited to 'src/textures.c') diff --git a/src/rlgl.c b/src/rlgl.c index afb366e7..d325a1bd 100644 --- a/src/rlgl.c +++ b/src/rlgl.c @@ -1433,6 +1433,8 @@ unsigned int rlLoadTexture(void *data, int width, int height, int format, int mi int mipHeight = height; int mipOffset = 0; // Mipmap data offset + TraceLog(LOG_DEBUG, "Load texture from data memory address: 0x%x", data); + // Load the different mipmap levels for (int i = 0; i < mipmapCount; i++) { @@ -1441,7 +1443,7 @@ unsigned int rlLoadTexture(void *data, int width, int height, int format, int mi int glInternalFormat, glFormat, glType; GetGlFormats(format, &glInternalFormat, &glFormat, &glType); - TraceLog(LOG_INFO, "Mip level %i (%i x %i), size: %i, formats: %i - %i", i, mipWidth, mipHeight, mipSize, glInternalFormat, glFormat); + TraceLog(LOG_DEBUG, "Load mipmap level %i (%i x %i), size: %i, offset: %i", i, mipWidth, mipHeight, mipSize, mipOffset); if (glInternalFormat != -1) { @@ -1511,7 +1513,7 @@ unsigned int rlLoadTexture(void *data, int width, int height, int format, int mi // Unbind current texture glBindTexture(GL_TEXTURE_2D, 0); - if (id > 0) TraceLog(LOG_INFO, "[TEX ID %i] Texture created successfully (%ix%i)", id, width, height); + if (id > 0) TraceLog(LOG_INFO, "[TEX ID %i] Texture created successfully (%ix%i - %i mipmaps)", id, width, height, mipmapCount); else TraceLog(LOG_WARNING, "Texture could not be created"); return id; diff --git a/src/textures.c b/src/textures.c index 2c7c1c2d..34d6182c 100644 --- a/src/textures.c +++ b/src/textures.c @@ -376,8 +376,6 @@ Texture2D LoadTextureFromImage(Image image) texture.height = image.height; texture.mipmaps = image.mipmaps; texture.format = image.format; - - TraceLog(LOG_INFO, "[TEX ID %i] Parameters: %ix%i, %i mips, format %i", texture.id, texture.width, texture.height, texture.mipmaps, texture.format); return texture; } @@ -678,7 +676,7 @@ void ImageFormat(Image *image, int newFormat) { Color *pixels = GetImageData(*image); - free(image->data); + free(image->data); // WARNING! We loose mipmaps data --> Regenerated at the end... image->format = newFormat; @@ -700,11 +698,10 @@ void ImageFormat(Image *image, int newFormat) { image->data = (unsigned char *)malloc(image->width*image->height*2*sizeof(unsigned char)); - for (int i = 0; i < image->width*image->height*2; i += 2) + for (int i = 0; i < image->width*image->height*2; i += 2, k++) { ((unsigned char *)image->data)[i] = (unsigned char)((float)pixels[k].r*0.299f + (float)pixels[k].g*0.587f + (float)pixels[k].b*0.114f); ((unsigned char *)image->data)[i + 1] = pixels[k].a; - k++; } } break; @@ -730,12 +727,11 @@ void ImageFormat(Image *image, int newFormat) { image->data = (unsigned char *)malloc(image->width*image->height*3*sizeof(unsigned char)); - for (int i = 0; i < image->width*image->height*3; i += 3) + for (int i = 0; i < image->width*image->height*3; i += 3, k++) { ((unsigned char *)image->data)[i] = pixels[k].r; ((unsigned char *)image->data)[i + 1] = pixels[k].g; ((unsigned char *)image->data)[i + 2] = pixels[k].b; - k++; } } break; case UNCOMPRESSED_R5G5B5A1: @@ -784,13 +780,12 @@ void ImageFormat(Image *image, int newFormat) { image->data = (unsigned char *)malloc(image->width*image->height*4*sizeof(unsigned char)); - for (int i = 0; i < image->width*image->height*3; i += 3) + for (int i = 0; i < image->width*image->height*3; i += 3, k++) { ((unsigned char *)image->data)[i] = pixels[k].r; ((unsigned char *)image->data)[i + 1] = pixels[k].g; ((unsigned char *)image->data)[i + 2] = pixels[k].b; ((unsigned char *)image->data)[i + 3] = pixels[k].a; - k++; } } break; case UNCOMPRESSED_R32: @@ -806,25 +801,23 @@ void ImageFormat(Image *image, int newFormat) { image->data = (float *)malloc(image->width*image->height*3*sizeof(float)); - for (int i = 0; i < image->width*image->height*3; i += 3) + for (int i = 0; i < image->width*image->height*3; i += 3, k++) { ((float *)image->data)[i] = (float)pixels[k].r/255.0f; ((float *)image->data)[i + 1] = (float)pixels[k].g/255.0f; ((float *)image->data)[i + 2] = (float)pixels[k].b/255.0f; - k++; } } break; case UNCOMPRESSED_R32G32B32A32: { image->data = (float *)malloc(image->width*image->height*4*sizeof(float)); - for (int i = 0; i < image->width*image->height*4; i += 4) + for (int i = 0; i < image->width*image->height*4; i += 4, k++) { ((float *)image->data)[i] = (float)pixels[k].r/255.0f; ((float *)image->data)[i + 1] = (float)pixels[k].g/255.0f; ((float *)image->data)[i + 2] = (float)pixels[k].b/255.0f; ((float *)image->data)[i + 3] = (float)pixels[k].a/255.0f; - k++; } } break; default: break; @@ -834,7 +827,11 @@ void ImageFormat(Image *image, int newFormat) // In case original image had mipmaps, generate mipmaps for formated image // NOTE: Original mipmaps are replaced by new ones, if custom mipmaps were used, they are lost - if (image->mipmaps > 1) ImageMipmaps(image); + if (image->mipmaps > 1) + { + image->mipmaps = 1; + ImageMipmaps(image); + } } else TraceLog(LOG_WARNING, "Image data format is compressed, can not be converted"); } @@ -1106,9 +1103,9 @@ void ImageMipmaps(Image *image) mipSize += GetPixelDataSize(mipWidth, mipHeight, image->format); // Add mipmap size (in bytes) } - TraceLog(LOG_DEBUG, "Total mipmaps required: %i", mipCount); - TraceLog(LOG_DEBUG, "Total size of data required: %i", mipSize); - TraceLog(LOG_DEBUG, "Image data original memory point: %i", image->data); + TraceLog(LOG_DEBUG, "Mipmaps available: %i - Mipmaps required: %i", image->mipmaps, mipCount); + TraceLog(LOG_DEBUG, "Mipmaps total size required: %i", mipSize); + TraceLog(LOG_DEBUG, "Image data memory start address: 0x%x", image->data); if (image->mipmaps < mipCount) { @@ -1117,7 +1114,7 @@ void ImageMipmaps(Image *image) if (temp != NULL) { image->data = temp; // Assign new pointer (new size) to store mipmaps data - TraceLog(LOG_DEBUG, "Image data memory point reallocated: %i", temp); + TraceLog(LOG_DEBUG, "Image data memory point reallocated: 0x%x", temp); } else TraceLog(LOG_WARNING, "Mipmaps required memory could not be allocated"); @@ -1131,7 +1128,7 @@ void ImageMipmaps(Image *image) for (int i = 1; i < mipCount; i++) { - TraceLog(LOG_DEBUG, "Next mipmap level %i (%i x %i) - size %i - mem pos: %i", i, mipWidth, mipHeight, mipSize, nextmip); + TraceLog(LOG_DEBUG, "Gen mipmap level: %i (%i x %i) - size: %i - offset: 0x%x", i, mipWidth, mipHeight, mipSize, nextmip); ImageResize(&imCopy, mipWidth, mipHeight); // Uses internally Mitchell cubic downscale filter -- cgit v1.2.3 From 1ce8c80de9f2270735ac371b655b2ca7de90d55a Mon Sep 17 00:00:00 2001 From: Ray Date: Sun, 28 Jan 2018 16:52:18 +0100 Subject: Corrected several issues... --- src/text.c | 21 ++++++++++----------- src/textures.c | 9 ++++----- 2 files changed, 14 insertions(+), 16 deletions(-) (limited to 'src/textures.c') diff --git a/src/text.c b/src/text.c index eaf450b0..e2629b09 100644 --- a/src/text.c +++ b/src/text.c @@ -66,8 +66,8 @@ //---------------------------------------------------------------------------------- // Defines and Macros //---------------------------------------------------------------------------------- -#define MAX_FORMATTEXT_LENGTH 64 -#define MAX_SUBTEXT_LENGTH 64 +#define MAX_FORMATTEXT_LENGTH 256 +#define MAX_SUBTEXT_LENGTH 256 //---------------------------------------------------------------------------------- // Types and Structures Definition @@ -318,21 +318,20 @@ SpriteFont LoadSpriteFont(const char *fileName) SpriteFont LoadSpriteFontEx(const char *fileName, int fontSize, int charsCount, int *fontChars) { SpriteFont spriteFont = { 0 }; + int totalChars = 95; // Default charset [32..126] #if defined(SUPPORT_FILEFORMAT_TTF) if (IsFileExtension(fileName, ".ttf")) { - if ((fontChars == NULL) || (charsCount == 0)) + if (charsCount != 0) totalChars = charsCount; + + if (fontChars == NULL) { - int totalChars = 95; // Default charset [32..126] - - int *defaultFontChars = (int *)malloc(totalChars*sizeof(int)); - - for (int i = 0; i < totalChars; i++) defaultFontChars[i] = i + 32; // Default first character: SPACE[32] - - spriteFont = LoadTTF(fileName, fontSize, totalChars, defaultFontChars); + fontChars = (int *)malloc(totalChars*sizeof(int)); + for (int i = 0; i < totalChars; i++) fontChars[i] = i + 32; // Default first character: SPACE[32] } - else spriteFont = LoadTTF(fileName, fontSize, charsCount, fontChars); + + spriteFont = LoadTTF(fileName, fontSize, totalChars, fontChars); } #endif diff --git a/src/textures.c b/src/textures.c index 34d6182c..2772e246 100644 --- a/src/textures.c +++ b/src/textures.c @@ -1377,13 +1377,12 @@ Image ImageTextEx(SpriteFont font, const char *text, float fontSize, int spacing Vector2 imSize = MeasureTextEx(font, text, font.baseSize, spacing); - TraceLog(LOG_WARNING, "Text Image size: %f, %f", imSize.x, imSize.y); + TraceLog(LOG_DEBUG, "Text Image size: %f, %f", imSize.x, imSize.y); // NOTE: glGetTexImage() not available in OpenGL ES Image imFont = GetTextureData(font.texture); - - ImageFormat(&imFont, UNCOMPRESSED_R8G8B8A8); // Convert to 32 bit for color tint - ImageColorTint(&imFont, tint); // Apply color tint to font + + ImageColorTint(&imFont, tint); // Apply color tint to font // Create image to store text Image imText = GenImageColor((int)imSize.x, (int)imSize.y, BLANK); @@ -1405,7 +1404,7 @@ Image ImageTextEx(SpriteFont font, const char *text, float fontSize, int spacing if (fontSize > imSize.y) { float scaleFactor = fontSize/imSize.y; - TraceLog(LOG_INFO, "Scalefactor: %f", scaleFactor); + TraceLog(LOG_INFO, "Image text scaled by factor: %f", scaleFactor); // Using nearest-neighbor scaling algorithm for default font if (font.texture.id == GetDefaultFont().texture.id) ImageResizeNN(&imText, (int)(imSize.x*scaleFactor), (int)(imSize.y*scaleFactor)); -- cgit v1.2.3 From 4492a70a4bfc32f699932eaaddc09d8225963f42 Mon Sep 17 00:00:00 2001 From: raysan5 Date: Sun, 18 Feb 2018 18:07:57 +0100 Subject: Support UTF8 basic characters on ImageTextEx() Supported UTF8 range equivalent to [128..255] (80h..FFh) Exposed and renamed text function GetGlyphIndex() Renamed spriteFont parameter name to simply font Small security check on transmission mission ending screen --- games/transmission/screens/screen_ending.c | 2 +- src/raylib.h | 7 ++- src/text.c | 95 +++++++++++++++--------------- src/textures.c | 40 +++++++++++-- 4 files changed, 86 insertions(+), 58 deletions(-) (limited to 'src/textures.c') diff --git a/games/transmission/screens/screen_ending.c b/games/transmission/screens/screen_ending.c index d9ed4696..e6ab3518 100644 --- a/games/transmission/screens/screen_ending.c +++ b/games/transmission/screens/screen_ending.c @@ -113,7 +113,7 @@ void InitEndingScreen(void) strcpy(headline, title); // Base headline updated - free(title); + if (title != NULL) free(title); } } diff --git a/src/raylib.h b/src/raylib.h index fba973d0..782dd4e5 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -948,19 +948,20 @@ RLAPI void DrawTexturePro(Texture2D texture, Rectangle sourceRec, Rectangle dest RLAPI SpriteFont GetDefaultFont(void); // Get the default SpriteFont RLAPI SpriteFont LoadSpriteFont(const char *fileName); // Load SpriteFont from file into GPU memory (VRAM) RLAPI SpriteFont LoadSpriteFontEx(const char *fileName, int fontSize, int charsCount, int *fontChars); // Load SpriteFont from file with extended parameters -RLAPI void UnloadSpriteFont(SpriteFont spriteFont); // Unload SpriteFont from GPU memory (VRAM) +RLAPI void UnloadSpriteFont(SpriteFont font); // Unload SpriteFont from GPU memory (VRAM) // Text drawing functions RLAPI void DrawFPS(int posX, int posY); // Shows current FPS RLAPI void DrawText(const char *text, int posX, int posY, int fontSize, Color color); // Draw text (using default font) -RLAPI void DrawTextEx(SpriteFont spriteFont, const char* text, Vector2 position, // Draw text using SpriteFont and additional parameters +RLAPI void DrawTextEx(SpriteFont font, const char* text, Vector2 position, // Draw text using SpriteFont and additional parameters float fontSize, int spacing, Color tint); // Text misc. functions RLAPI int MeasureText(const char *text, int fontSize); // Measure string width for default font -RLAPI Vector2 MeasureTextEx(SpriteFont spriteFont, const char *text, float fontSize, int spacing); // Measure string size for SpriteFont +RLAPI Vector2 MeasureTextEx(SpriteFont font, const char *text, float fontSize, int spacing); // Measure string size for SpriteFont RLAPI const char *FormatText(const char *text, ...); // Formatting of text with variables to 'embed' RLAPI const char *SubText(const char *text, int position, int length); // Get a piece of a text string +RLAPI int GetGlyphIndex(SpriteFont font, int character); // Returns index position for a unicode character on sprite font //------------------------------------------------------------------------------------ // Basic 3d Shapes Drawing Functions (Module: models) diff --git a/src/text.c b/src/text.c index 227992e6..2a05773c 100644 --- a/src/text.c +++ b/src/text.c @@ -90,11 +90,9 @@ static SpriteFont defaultFont; // Default font provided by raylib //---------------------------------------------------------------------------------- // Module specific Functions Declaration //---------------------------------------------------------------------------------- -static int GetCharIndex(SpriteFont font, int letter); - static SpriteFont LoadImageFont(Image image, Color key, int firstChar); // Load a Image font file (XNA style) #if defined(SUPPORT_FILEFORMAT_FNT) -static SpriteFont LoadBMFont(const char *fileName); // Load a BMFont file (AngelCode font file) +static SpriteFont LoadBMFont(const char *fileName); // Load a BMFont file (AngelCode font file) #endif #if defined(SUPPORT_FILEFORMAT_TTF) static SpriteFont LoadTTF(const char *fileName, int fontSize, int charsCount, int *fontChars); // Load spritefont from TTF data @@ -345,13 +343,13 @@ SpriteFont LoadSpriteFontEx(const char *fileName, int fontSize, int charsCount, } // Unload SpriteFont from GPU memory (VRAM) -void UnloadSpriteFont(SpriteFont spriteFont) +void UnloadSpriteFont(SpriteFont font) { // NOTE: Make sure spriteFont is not default font (fallback) - if (spriteFont.texture.id != GetDefaultFont().texture.id) + if (font.texture.id != GetDefaultFont().texture.id) { - UnloadTexture(spriteFont.texture); - free(spriteFont.chars); + UnloadTexture(font.texture); + free(font.chars); TraceLog(LOG_DEBUG, "Unloaded sprite font data"); } @@ -377,7 +375,7 @@ void DrawText(const char *text, int posX, int posY, int fontSize, Color color) // Draw text using SpriteFont // NOTE: chars spacing is NOT proportional to fontSize -void DrawTextEx(SpriteFont spriteFont, const char *text, Vector2 position, float fontSize, int spacing, Color tint) +void DrawTextEx(SpriteFont font, const char *text, Vector2 position, float fontSize, int spacing, Color tint) { int length = strlen(text); int textOffsetX = 0; // Offset between characters @@ -387,7 +385,7 @@ void DrawTextEx(SpriteFont spriteFont, const char *text, Vector2 position, float unsigned char letter; // Current character int index; // Index position in sprite font - scaleFactor = fontSize/spriteFont.baseSize; + scaleFactor = fontSize/font.baseSize; // NOTE: Some ugly hacks are made to support Latin-1 Extended characters directly // written in C code files (codified by default as UTF-8) @@ -397,7 +395,7 @@ void DrawTextEx(SpriteFont spriteFont, const char *text, Vector2 position, float if ((unsigned char)text[i] == '\n') { // NOTE: Fixed line spacing of 1.5 lines - textOffsetY += (int)((spriteFont.baseSize + spriteFont.baseSize/2)*scaleFactor); + textOffsetY += (int)((font.baseSize + font.baseSize/2)*scaleFactor); textOffsetX = 0; } else @@ -406,29 +404,29 @@ void DrawTextEx(SpriteFont spriteFont, const char *text, Vector2 position, float { // Support UTF-8 encoded values from [0xc2 0x80] -> [0xc2 0xbf](¿) letter = (unsigned char)text[i + 1]; - index = GetCharIndex(spriteFont, (int)letter); + index = GetGlyphIndex(font, (int)letter); i++; } else if ((unsigned char)text[i] == 0xc3) // UTF-8 encoding identification HACK! { // Support UTF-8 encoded values from [0xc3 0x80](À) -> [0xc3 0xbf](ÿ) letter = (unsigned char)text[i + 1]; - index = GetCharIndex(spriteFont, (int)letter + 64); + index = GetGlyphIndex(font, (int)letter + 64); i++; } - else index = GetCharIndex(spriteFont, (unsigned char)text[i]); + else index = GetGlyphIndex(font, (unsigned char)text[i]); if ((unsigned char)text[i] != ' ') { - DrawTexturePro(spriteFont.texture, spriteFont.chars[index].rec, - (Rectangle){ position.x + textOffsetX + spriteFont.chars[index].offsetX*scaleFactor, - position.y + textOffsetY + spriteFont.chars[index].offsetY*scaleFactor, - spriteFont.chars[index].rec.width*scaleFactor, - spriteFont.chars[index].rec.height*scaleFactor }, (Vector2){ 0, 0 }, 0.0f, tint); + DrawTexturePro(font.texture, font.chars[index].rec, + (Rectangle){ position.x + textOffsetX + font.chars[index].offsetX*scaleFactor, + position.y + textOffsetY + font.chars[index].offsetY*scaleFactor, + font.chars[index].rec.width*scaleFactor, + font.chars[index].rec.height*scaleFactor }, (Vector2){ 0, 0 }, 0.0f, tint); } - if (spriteFont.chars[index].advanceX == 0) textOffsetX += (int)(spriteFont.chars[index].rec.width*scaleFactor + spacing); - else textOffsetX += (int)(spriteFont.chars[index].advanceX*scaleFactor + spacing); + if (font.chars[index].advanceX == 0) textOffsetX += (int)(font.chars[index].rec.width*scaleFactor + spacing); + else textOffsetX += (int)(font.chars[index].advanceX*scaleFactor + spacing); } } } @@ -490,7 +488,7 @@ int MeasureText(const char *text, int fontSize) } // Measure string size for SpriteFont -Vector2 MeasureTextEx(SpriteFont spriteFont, const char *text, float fontSize, int spacing) +Vector2 MeasureTextEx(SpriteFont font, const char *text, float fontSize, int spacing) { int len = strlen(text); int tempLen = 0; // Used to count longer text line num chars @@ -499,8 +497,8 @@ Vector2 MeasureTextEx(SpriteFont spriteFont, const char *text, float fontSize, i float textWidth = 0; float tempTextWidth = 0; // Used to count longer text line width - float textHeight = (float)spriteFont.baseSize; - float scaleFactor = fontSize/(float)spriteFont.baseSize; + float textHeight = (float)font.baseSize; + float scaleFactor = fontSize/(float)font.baseSize; for (int i = 0; i < len; i++) { @@ -508,17 +506,17 @@ Vector2 MeasureTextEx(SpriteFont spriteFont, const char *text, float fontSize, i if (text[i] != '\n') { - int index = GetCharIndex(spriteFont, (int)text[i]); + int index = GetGlyphIndex(font, (int)text[i]); - if (spriteFont.chars[index].advanceX != 0) textWidth += spriteFont.chars[index].advanceX; - else textWidth += (spriteFont.chars[index].rec.width + spriteFont.chars[index].offsetX); + if (font.chars[index].advanceX != 0) textWidth += font.chars[index].advanceX; + else textWidth += (font.chars[index].rec.width + font.chars[index].offsetX); } else { if (tempTextWidth < textWidth) tempTextWidth = textWidth; lenCounter = 0; textWidth = 0; - textHeight += ((float)spriteFont.baseSize*1.5f); // NOTE: Fixed line spacing of 1.5 lines + textHeight += ((float)font.baseSize*1.5f); // NOTE: Fixed line spacing of 1.5 lines } if (tempLen < lenCounter) tempLen = lenCounter; @@ -533,6 +531,28 @@ Vector2 MeasureTextEx(SpriteFont spriteFont, const char *text, float fontSize, i return vec; } +// Returns index position for a unicode character on spritefont +int GetGlyphIndex(SpriteFont font, int character) +{ +#define UNORDERED_CHARSET +#if defined(UNORDERED_CHARSET) + int index = 0; + + for (int i = 0; i < font.charsCount; i++) + { + if (font.chars[i].value == character) + { + index = i; + break; + } + } + + return index; +#else + return (character - 32); +#endif +} + // Shows current FPS on top-left corner // NOTE: Uses default font void DrawFPS(int posX, int posY) @@ -559,27 +579,6 @@ void DrawFPS(int posX, int posY) // Module specific Functions Definition //---------------------------------------------------------------------------------- -static int GetCharIndex(SpriteFont font, int letter) -{ -#define UNORDERED_CHARSET -#if defined(UNORDERED_CHARSET) - int index = 0; - - for (int i = 0; i < font.charsCount; i++) - { - if (font.chars[i].value == letter) - { - index = i; - break; - } - } - - return index; -#else - return (letter - 32); -#endif -} - // Load an Image font file (XNA style) static SpriteFont LoadImageFont(Image image, Color key, int firstChar) { diff --git a/src/textures.c b/src/textures.c index 2772e246..ba0bad54 100644 --- a/src/textures.c +++ b/src/textures.c @@ -1374,6 +1374,8 @@ Image ImageTextEx(SpriteFont font, const char *text, float fontSize, int spacing { int length = strlen(text); int posX = 0; + int index; // Index position in sprite font + unsigned char character; // Current character Vector2 imSize = MeasureTextEx(font, text, font.baseSize, spacing); @@ -1389,13 +1391,39 @@ Image ImageTextEx(SpriteFont font, const char *text, float fontSize, int spacing for (int i = 0; i < length; i++) { - CharInfo letter = font.chars[(int)text[i] - 32]; - - ImageDraw(&imText, imFont, letter.rec, (Rectangle){ posX + letter.offsetX, - letter.offsetY, letter.rec.width, letter.rec.height }); + if ((unsigned char)text[i] == '\n') + { + // TODO: Support line break + } + else + { + if ((unsigned char)text[i] == 0xc2) // UTF-8 encoding identification HACK! + { + // Support UTF-8 encoded values from [0xc2 0x80] -> [0xc2 0xbf](¿) + character = (unsigned char)text[i + 1]; + index = GetGlyphIndex(font, (int)character); + i++; + } + else if ((unsigned char)text[i] == 0xc3) // UTF-8 encoding identification HACK! + { + // Support UTF-8 encoded values from [0xc3 0x80](À) -> [0xc3 0xbf](ÿ) + character = (unsigned char)text[i + 1]; + index = GetGlyphIndex(font, (int)character + 64); + i++; + } + else index = GetGlyphIndex(font, (unsigned char)text[i]); - if (letter.advanceX == 0) posX += letter.rec.width + spacing; - else posX += letter.advanceX + spacing; + CharInfo letter = font.chars[index]; + + if ((unsigned char)text[i] != ' ') + { + ImageDraw(&imText, imFont, letter.rec, (Rectangle){ posX + letter.offsetX, + letter.offsetY, letter.rec.width, letter.rec.height }); + } + + if (letter.advanceX == 0) posX += letter.rec.width + spacing; + else posX += letter.advanceX + spacing; + } } UnloadImage(imFont); -- cgit v1.2.3 From 0958904eac63a82b427575439de1d96a62d4f5cf Mon Sep 17 00:00:00 2001 From: raysan5 Date: Sun, 18 Feb 2018 19:29:13 +0100 Subject: Added comments to review function ImageTextEx() --- src/textures.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'src/textures.c') diff --git a/src/textures.c b/src/textures.c index ba0bad54..a571718b 100644 --- a/src/textures.c +++ b/src/textures.c @@ -1375,13 +1375,16 @@ Image ImageTextEx(SpriteFont font, const char *text, float fontSize, int spacing int length = strlen(text); int posX = 0; int index; // Index position in sprite font - unsigned char character; // Current character + unsigned char character; // Current character + // TODO: ISSUE: Measured text size does not seem to be correct... issue on ImageDraw() Vector2 imSize = MeasureTextEx(font, text, font.baseSize, spacing); TraceLog(LOG_DEBUG, "Text Image size: %f, %f", imSize.x, imSize.y); // NOTE: glGetTexImage() not available in OpenGL ES + // TODO: This is horrible, retrieving font texture from GPU!!! + // Define ImageFont struct? or include Image spritefont in SpriteFont struct? Image imFont = GetTextureData(font.texture); ImageColorTint(&imFont, tint); // Apply color tint to font -- cgit v1.2.3 From 28a9a181cb499b1106837930758b473675894df8 Mon Sep 17 00:00:00 2001 From: Martinfx Date: Thu, 15 Mar 2018 22:24:05 +0100 Subject: Fix use after free --- src/textures.c | 59 +++++++++++++++++++++++++++++----------------------------- 1 file changed, 30 insertions(+), 29 deletions(-) (limited to 'src/textures.c') diff --git a/src/textures.c b/src/textures.c index a571718b..54055ce0 100644 --- a/src/textures.c +++ b/src/textures.c @@ -16,7 +16,7 @@ * #define SUPPORT_FILEFORMAT_KTX * #define SUPPORT_FILEFORMAT_PVR * #define SUPPORT_FILEFORMAT_ASTC -* Selecte desired fileformats to be supported for image data loading. Some of those formats are +* Selecte desired fileformats to be supported for image data loading. Some of those formats are * supported by default, to remove support, just comment unrequired #define in this module * * #define SUPPORT_IMAGE_MANIPULATION @@ -112,7 +112,7 @@ #include "external/stb_image.h" // Required for: stbi_load_from_file() // NOTE: Used to read image data (multiple formats support) #endif - + #if defined(SUPPORT_IMAGE_MANIPULATION) #define STB_IMAGE_RESIZE_IMPLEMENTATION #include "external/stb_image_resize.h" // Required for: stbir_resize_uint8() @@ -188,14 +188,14 @@ Image LoadImage(const char *fileName) int imgWidth = 0; int imgHeight = 0; int imgBpp = 0; - + FILE *imFile = fopen(fileName, "rb"); - + if (imFile != NULL) { // NOTE: Using stb_image to load images (Supports: BMP, TGA, PNG, JPG, ...) image.data = stbi_load_from_file(imFile, &imgWidth, &imgHeight, &imgBpp, 0); - + fclose(imFile); image.width = imgWidth; @@ -212,24 +212,24 @@ Image LoadImage(const char *fileName) else if (IsFileExtension(fileName, ".hdr")) { int imgBpp = 0; - + FILE *imFile = fopen(fileName, "rb"); - + stbi_set_flip_vertically_on_load(true); - - // Load 32 bit per channel floats data + + // Load 32 bit per channel floats data image.data = stbi_loadf_from_file(imFile, &image.width, &image.height, &imgBpp, 0); - + stbi_set_flip_vertically_on_load(false); fclose(imFile); - + image.mipmaps = 1; - + if (imgBpp == 1) image.format = UNCOMPRESSED_R32; else if (imgBpp == 3) image.format = UNCOMPRESSED_R32G32B32; else if (imgBpp == 4) image.format = UNCOMPRESSED_R32G32B32A32; - else + else { TraceLog(LOG_WARNING, "[%s] Image fileformat not supported", fileName); UnloadImage(image); @@ -319,10 +319,10 @@ Image LoadImageRaw(const char *fileName, int width, int height, int format, int if (headerSize > 0) fseek(rawFile, headerSize, SEEK_SET); unsigned int size = GetPixelDataSize(width, height, format); - + image.data = malloc(size); // Allocate required memory in bytes - // NOTE: fread() returns num read elements instead of bytes, + // NOTE: fread() returns num read elements instead of bytes, // to get bytes we need to read (1 byte size, elements) instead of (x byte size, 1 element) int bytes = fread(image.data, 1, size, rawFile); @@ -430,7 +430,7 @@ Color *GetImageData(Image image) pixels[i].g = ((unsigned char *)image.data)[i]; pixels[i].b = ((unsigned char *)image.data)[i]; pixels[i].a = 255; - + } break; case UNCOMPRESSED_GRAY_ALPHA: { @@ -469,7 +469,7 @@ Color *GetImageData(Image image) pixels[i].g = (unsigned char)((float)((pixel & 0b0000111100000000) >> 8)*(255/15)); pixels[i].b = (unsigned char)((float)((pixel & 0b0000000011110000) >> 4)*(255/15)); pixels[i].a = (unsigned char)((float)(pixel & 0b0000000000001111)*(255/15)); - + } break; case UNCOMPRESSED_R8G8B8A8: { @@ -516,7 +516,7 @@ int GetPixelDataSize(int width, int height, int format) case UNCOMPRESSED_R32G32B32: bpp = 32*3; break; case UNCOMPRESSED_R32G32B32A32: bpp = 32*4; break; case COMPRESSED_DXT1_RGB: - case COMPRESSED_DXT1_RGBA: + case COMPRESSED_DXT1_RGBA: case COMPRESSED_ETC1_RGB: case COMPRESSED_ETC2_RGB: case COMPRESSED_PVRT_RGB: @@ -530,7 +530,7 @@ int GetPixelDataSize(int width, int height, int format) } dataSize = width*height*bpp/8; // Total data size in bytes - + return dataSize; } @@ -539,7 +539,7 @@ int GetPixelDataSize(int width, int height, int format) Image GetTextureData(Texture2D texture) { Image image = { 0 }; - + if (texture.format < 8) { image.data = rlReadTexturePixels(texture); @@ -550,7 +550,7 @@ Image GetTextureData(Texture2D texture) image.height = texture.height; image.format = texture.format; image.mipmaps = 1; - + // NOTE: Data retrieved on OpenGL ES 2.0 should be RGBA // coming from FBO color buffer, but it seems original // texture format is retrieved on RPI... weird... @@ -597,15 +597,15 @@ Image ImageCopy(Image image) for (int i = 0; i < image.mipmaps; i++) { size += GetPixelDataSize(width, height, image.format); - + width /= 2; height /= 2; - + // Security check for NPOT textures if (width < 1) width = 1; if (height < 1) height = 1; } - + newImage.data = malloc(size); if (newImage.data != NULL) @@ -677,7 +677,7 @@ void ImageFormat(Image *image, int newFormat) Color *pixels = GetImageData(*image); free(image->data); // WARNING! We loose mipmaps data --> Regenerated at the end... - + image->data = NULL; image->format = newFormat; int k = 0; @@ -771,7 +771,7 @@ void ImageFormat(Image *image, int newFormat) g = (unsigned char)(round((float)pixels[i].g*15.0f/255)); b = (unsigned char)(round((float)pixels[i].b*15.0f/255)); a = (unsigned char)(round((float)pixels[i].a*15.0f/255)); - + ((unsigned short *)image->data)[i] = (unsigned short)r << 12 | (unsigned short)g << 8 | (unsigned short)b << 4 | (unsigned short)a; } @@ -791,7 +791,7 @@ void ImageFormat(Image *image, int newFormat) case UNCOMPRESSED_R32: { image->data = (float *)malloc(image->width*image->height*sizeof(float)); - + for (int i = 0; i < image->width*image->height; i++) { ((float *)image->data)[i] = (float)((float)pixels[i].r*0.299f/255.0f + (float)pixels[i].g*0.587f/255.0f + (float)pixels[i].b*0.114f/255.0f); @@ -824,12 +824,13 @@ void ImageFormat(Image *image, int newFormat) } free(pixels); - + pixels = NULL; // In case original image had mipmaps, generate mipmaps for formated image // NOTE: Original mipmaps are replaced by new ones, if custom mipmaps were used, they are lost - if (image->mipmaps > 1) + if (image->mipmaps > 1) { image->mipmaps = 1; + assert(image->data != NULL); ImageMipmaps(image); } } -- cgit v1.2.3 From 533780aadf1ba1b553ef69f191171f6a528ae894 Mon Sep 17 00:00:00 2001 From: Ray Date: Tue, 3 Apr 2018 12:42:22 +0200 Subject: Review ImageDraw() alpha blending Not sure if math is ok... just left a commented piece of code that uses pre-multiplied alpha. --- src/textures.c | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) (limited to 'src/textures.c') diff --git a/src/textures.c b/src/textures.c index 54055ce0..8e68df7e 100644 --- a/src/textures.c +++ b/src/textures.c @@ -1337,11 +1337,30 @@ void ImageDraw(Image *dst, Image src, Rectangle srcRec, Rectangle dstRec) // Alpha blending implementation dstCol = dstPixels[j*dst->width + i]; srcCol = srcPixels[(j - dstRec.y)*dstRec.width + (i - dstRec.x)]; + + /* + // Pre-multiply alpha + Vector3 dstColf = { (float)dstCol.r/255.0f, (float)dstCol.g/255.0f, (float)dstCol.b/255.0f }; + dstColf = Vector3Multiply(dstColf, (float)dstCol.a/255.0f); + Vector3 srcColf = { (float)srcCol.r/255.0f, (float)srcCol.g/255.0f, (float)srcCol.b/255.0f }; + srcColf = Vector3Multiply(srcColf, (float)srcCol.a/255.0f); + + dstColf = Vector3Add(dstColf, srcColf); + + if (dstColf.x > 1.0f) dstColf.x = 1.0f; + if (dstColf.y > 1.0f) dstColf.y = 1.0f; + if (dstColf.z > 1.0f) dstColf.z = 1.0f; + + dstCol.r = (unsigned char)(dstColf.x*255.0f); + dstCol.g = (unsigned char)(dstColf.y*255.0f); + dstCol.b = (unsigned char)(dstColf.z*255.0f); + dstCol.a = srcCol.a; + */ dstCol.r = ((srcCol.a*(srcCol.r - dstCol.r)) >> 8) + dstCol.r; dstCol.g = ((srcCol.a*(srcCol.g - dstCol.g)) >> 8) + dstCol.g; dstCol.b = ((srcCol.a*(srcCol.b - dstCol.b)) >> 8) + dstCol.b; - dstCol.a = ((srcCol.a*(srcCol.a - dstCol.a)) >> 8) + dstCol.a; + dstCol.a = srcCol.a; dstPixels[j*dst->width + i] = dstCol; -- cgit v1.2.3 From 931b672c92f069a9e2122dc725a371de8fc2c201 Mon Sep 17 00:00:00 2001 From: Ray Date: Wed, 4 Apr 2018 12:02:20 +0200 Subject: Added: ImageDrawRectangle() Renamed SaveImageAs() to ExportImage() for consistency on what actually happens with data. --- src/raylib.h | 5 ++++- src/textures.c | 17 +++++++++++++++-- 2 files changed, 19 insertions(+), 3 deletions(-) (limited to 'src/textures.c') diff --git a/src/raylib.h b/src/raylib.h index acca46bd..3986ebcf 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -888,6 +888,7 @@ RLAPI Image LoadImage(const char *fileName); RLAPI Image LoadImageEx(Color *pixels, int width, int height); // Load image from Color array data (RGBA - 32bit) RLAPI Image LoadImagePro(void *data, int width, int height, int format); // Load image from raw data with parameters RLAPI Image LoadImageRaw(const char *fileName, int width, int height, int format, int headerSize); // Load image from RAW file data +RLAPI void ExportImage(const char *fileName, Image image); // Export image as a PNG file RLAPI Texture2D LoadTexture(const char *fileName); // Load texture from file into GPU memory (VRAM) RLAPI Texture2D LoadTextureFromImage(Image image); // Load texture from image data RLAPI RenderTexture2D LoadRenderTexture(int width, int height); // Load texture for rendering (framebuffer) @@ -898,7 +899,7 @@ RLAPI Color *GetImageData(Image image); RLAPI int GetPixelDataSize(int width, int height, int format); // Get pixel data size in bytes (image or texture) RLAPI Image GetTextureData(Texture2D texture); // Get pixel data from GPU texture and return an Image RLAPI void UpdateTexture(Texture2D texture, const void *pixels); // Update GPU texture with new data -RLAPI void SaveImageAs(const char *fileName, Image image); // Save image to a PNG file + // Image manipulation functions RLAPI Image ImageCopy(Image image); // Create an image duplicate (useful for transformations) @@ -916,6 +917,7 @@ RLAPI void ImageDither(Image *image, int rBpp, int gBpp, int bBpp, int aBpp); RLAPI Image ImageText(const char *text, int fontSize, Color color); // Create an image from text (default font) RLAPI Image ImageTextEx(SpriteFont font, const char *text, float fontSize, int spacing, Color tint); // Create an image from text (custom sprite font) RLAPI void ImageDraw(Image *dst, Image src, Rectangle srcRec, Rectangle dstRec); // Draw a source image within a destination image +RLAPI void ImageDrawRectangle(Image *dst, Vector2 position, Rectangle rec, Color color); // Draw rectangle within an image RLAPI void ImageDrawText(Image *dst, Vector2 position, const char *text, int fontSize, Color color); // Draw text (default font) within an image (destination) RLAPI void ImageDrawTextEx(Image *dst, Vector2 position, SpriteFont font, const char *text, float fontSize, int spacing, Color color); // Draw text (custom sprite font) within an image (destination) @@ -1007,6 +1009,7 @@ RLAPI void UnloadModel(Model model); // Mesh loading/unloading functions RLAPI Mesh LoadMesh(const char *fileName); // Load mesh from file RLAPI void UnloadMesh(Mesh *mesh); // Unload mesh from memory (RAM and/or VRAM) +RLAPI void ExportMesh(const char *fileName, Mesh mesh); // Export mesh as an OBJ file // Mesh manipulation functions RLAPI BoundingBox MeshBoundingBox(Mesh mesh); // Compute mesh bounding box limits diff --git a/src/textures.c b/src/textures.c index 8e68df7e..f5c905cb 100644 --- a/src/textures.c +++ b/src/textures.c @@ -572,8 +572,8 @@ void UpdateTexture(Texture2D texture, const void *pixels) rlUpdateTexture(texture.id, texture.width, texture.height, texture.format, pixels); } -// Save image to a PNG file -void SaveImageAs(const char *fileName, Image image) +// Export image as a PNG file +void ExportImage(const char *fileName, Image image) { #if defined(PLATFORM_DESKTOP) || defined(PLATFORM_RPI) // NOTE: Getting Color array as RGBA unsigned char values @@ -1360,6 +1360,7 @@ void ImageDraw(Image *dst, Image src, Rectangle srcRec, Rectangle dstRec) dstCol.r = ((srcCol.a*(srcCol.r - dstCol.r)) >> 8) + dstCol.r; dstCol.g = ((srcCol.a*(srcCol.g - dstCol.g)) >> 8) + dstCol.g; dstCol.b = ((srcCol.a*(srcCol.b - dstCol.b)) >> 8) + dstCol.b; + //dstCol.a = ((srcCol.a*(srcCol.a - dstCol.a)) >> 8) + dstCol.a; dstCol.a = srcCol.a; dstPixels[j*dst->width + i] = dstCol; @@ -1465,6 +1466,18 @@ Image ImageTextEx(SpriteFont font, const char *text, float fontSize, int spacing return imText; } +// Draw rectangle within an image +void ImageDrawRectangle(Image *dst, Vector2 position, Rectangle rec, Color color) +{ + Image imRec = GenImageColor(rec.width, rec.height, color); + + Rectangle dstRec = { (int)position.x, (int)position.y, imRec.width, imRec.height }; + + ImageDraw(dst, imRec, rec, dstRec); + + UnloadImage(imRec); +} + // Draw text (default font) within an image (destination) void ImageDrawText(Image *dst, Vector2 position, const char *text, int fontSize, Color color) { -- cgit v1.2.3 From 005f2ffb7502668fe5820d5fe041dea7daaac48c Mon Sep 17 00:00:00 2001 From: Ray Date: Thu, 5 Apr 2018 19:18:44 +0200 Subject: Simplified some code --- src/textures.c | 10 +--------- src/utils.c | 9 ++++++--- 2 files changed, 7 insertions(+), 12 deletions(-) (limited to 'src/textures.c') diff --git a/src/textures.c b/src/textures.c index f5c905cb..11258150 100644 --- a/src/textures.c +++ b/src/textures.c @@ -575,14 +575,10 @@ void UpdateTexture(Texture2D texture, const void *pixels) // Export image as a PNG file void ExportImage(const char *fileName, Image image) { -#if defined(PLATFORM_DESKTOP) || defined(PLATFORM_RPI) // NOTE: Getting Color array as RGBA unsigned char values unsigned char *imgData = (unsigned char *)GetImageData(image); SavePNG(fileName, imgData, image.width, image.height, 4); free(imgData); - - TraceLog(LOG_INFO, "Image saved: %s", fileName); -#endif } // Copy an image to a new image @@ -1930,12 +1926,8 @@ Image GenImageCellular(int width, int height, int tileSize) void GenTextureMipmaps(Texture2D *texture) { #if defined(PLATFORM_WEB) - // Calculate next power-of-two values - int potWidth = (int)powf(2, ceilf(logf((float)texture->width)/logf(2))); - int potHeight = (int)powf(2, ceilf(logf((float)texture->height)/logf(2))); - // Check if texture is POT - if ((potWidth != texture->width) || (potHeight != texture->height)) + if (((texture->width & (texture->width - 1)) != 0) || ((texture->height & (texture->height - 1)) != 0)) { TraceLog(LOG_WARNING, "Limited NPOT support, no mipmaps available for NPOT textures"); } diff --git a/src/utils.c b/src/utils.c index 743ef98c..e37b4ff7 100644 --- a/src/utils.c +++ b/src/utils.c @@ -141,13 +141,14 @@ void TraceLog(int msgType, const char *text, ...) #endif // SUPPORT_TRACELOG } -#if defined(PLATFORM_DESKTOP) || defined(PLATFORM_RPI) - #if defined(SUPPORT_SAVE_BMP) // Creates a BMP image file from an array of pixel data void SaveBMP(const char *fileName, unsigned char *imgData, int width, int height, int compSize) { +#if defined(PLATFORM_DESKTOP) || defined(PLATFORM_RPI) stbi_write_bmp(fileName, width, height, compSize, imgData); + TraceLog(LOG_INFO, "BMP Image saved: %s", fileName); +#endif } #endif @@ -155,9 +156,11 @@ void SaveBMP(const char *fileName, unsigned char *imgData, int width, int height // Creates a PNG image file from an array of pixel data void SavePNG(const char *fileName, unsigned char *imgData, int width, int height, int compSize) { +#if defined(PLATFORM_DESKTOP) || defined(PLATFORM_RPI) stbi_write_png(fileName, width, height, compSize, imgData, width*compSize); -} + TraceLog(LOG_INFO, "PNG Image saved: %s", fileName); #endif +} #endif // Keep track of memory allocated -- cgit v1.2.3 From b8bd1d2ea68adbf85b05a273df42eab42352a7c2 Mon Sep 17 00:00:00 2001 From: Ray Date: Thu, 5 Apr 2018 19:22:45 +0200 Subject: Remove unnecesary NPOT check Now PLATFORM checks only used on core and utils modules --- src/textures.c | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) (limited to 'src/textures.c') diff --git a/src/textures.c b/src/textures.c index 11258150..8a562887 100644 --- a/src/textures.c +++ b/src/textures.c @@ -1925,16 +1925,9 @@ Image GenImageCellular(int width, int height, int tileSize) // Generate GPU mipmaps for a texture void GenTextureMipmaps(Texture2D *texture) { -#if defined(PLATFORM_WEB) - // Check if texture is POT - if (((texture->width & (texture->width - 1)) != 0) || ((texture->height & (texture->height - 1)) != 0)) - { - TraceLog(LOG_WARNING, "Limited NPOT support, no mipmaps available for NPOT textures"); - } - else rlGenerateMipmaps(texture); -#else + // NOTE: NPOT textures support check inside function + // On WebGL (OpenGL ES 2.0) NPOT textures support is limited rlGenerateMipmaps(texture); -#endif } // Set texture scaling filter mode -- cgit v1.2.3 From 1841afad11892bab16976b976d69b7757b617c8b Mon Sep 17 00:00:00 2001 From: Ahmad Fatoum Date: Sat, 7 Apr 2018 22:29:53 +0200 Subject: Refactor all #define SUPPORT_* into a config.h That way, a user needs only to touch a single file to configure what features raylib is built with. Include guards are left out intentionally, because config.h should only be included in source files, not headers. Later on, config.h can also define the raylib version (#461). --- src/audio.c | 12 +++------- src/config.h | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/core.c | 12 ++-------- src/models.c | 7 +----- src/rlgl.c | 6 +---- src/rlgl.h | 4 ++-- src/shapes.c | 8 +------ src/text.c | 9 ++----- src/textures.c | 12 +--------- src/utils.c | 2 +- src/utils.h | 6 +++-- 11 files changed, 92 insertions(+), 60 deletions(-) create mode 100644 src/config.h (limited to 'src/textures.c') diff --git a/src/audio.c b/src/audio.c index 1fcf6f91..3ee1fe81 100644 --- a/src/audio.c +++ b/src/audio.c @@ -11,7 +11,7 @@ * - Manage raw audio context * * CONFIGURATION: -* +* * #define AUDIO_STANDALONE * Define to use the module as standalone library (independently of raylib). * Required types and functions are defined in the same module. @@ -24,7 +24,7 @@ * #define SUPPORT_FILEFORMAT_XM * #define SUPPORT_FILEFORMAT_MOD * #define SUPPORT_FILEFORMAT_FLAC -* Selected desired fileformats to be supported for loading. Some of those formats are +* Selected desired fileformats to be supported for loading. Some of those formats are * supported by default, to remove support, just comment unrequired #define in this module * * LIMITATIONS (only OpenAL Soft): @@ -72,13 +72,7 @@ * **********************************************************************************************/ -// Default configuration flags (supported features) -//------------------------------------------------- -#define SUPPORT_FILEFORMAT_WAV -#define SUPPORT_FILEFORMAT_OGG -#define SUPPORT_FILEFORMAT_XM -#define SUPPORT_FILEFORMAT_MOD -//------------------------------------------------- +#include "config.h" #if !defined(USE_OPENAL_BACKEND) #define USE_MINI_AL 1 // Set to 1 to use mini_al; 0 to use OpenAL. diff --git a/src/config.h b/src/config.h new file mode 100644 index 00000000..40b9d7c4 --- /dev/null +++ b/src/config.h @@ -0,0 +1,74 @@ +/* Edit to control what features raylib is compiled with. */ + +// text.c +/* Default font is loaded on window initialization to be available for the user to render simple text. NOTE: If enabled, uses external module functions to load default raylib font (module: text) */ +#define SUPPORT_DEFAULT_FONT 1 +/* Selected desired fileformats to be supported for loading. */ +#define SUPPORT_FILEFORMAT_FNT 1 +#define SUPPORT_FILEFORMAT_TTF 1 + +// textures.c +/* Selecte desired fileformats to be supported for image data loading. */ +#define SUPPORT_FILEFORMAT_PNG 1 +#define SUPPORT_FILEFORMAT_DDS 1 +#define SUPPORT_FILEFORMAT_HDR 1 +#define SUPPORT_FILEFORMAT_KTX 1 +#define SUPPORT_FILEFORMAT_ASTC 1 +/* #undef SUPPORT_FILEFORMAT_BMP */ +/* #undef SUPPORT_FILEFORMAT_TGA */ +/* #undef SUPPORT_FILEFORMAT_JPG */ +/* #undef SUPPORT_FILEFORMAT_GIF */ +/* #undef SUPPORT_FILEFORMAT_PSD */ +/* #undef SUPPORT_FILEFORMAT_PKM */ +/* #undef SUPPORT_FILEFORMAT_PVR */ + +/* Support multiple image editing functions to scale, adjust colors, flip, draw on images, crop... If not defined only three image editing functions supported: ImageFormat(), ImageAlphaMask(), ImageToPOT() */ +#define SUPPORT_IMAGE_MANIPULATION 1 + +/* Support proedural image generation functionality (gradient, spot, perlin-noise, cellular) */ +#define SUPPORT_IMAGE_GENERATION 1 + +// rlgl.c +/* Support VR simulation functionality (stereo rendering) */ +#define SUPPORT_VR_SIMULATOR 1 +/* Include stereo rendering distortion shader (shader_distortion.h) */ +#define SUPPORT_DISTORTION_SHADER 1 + +// core.c +/* Camera module is included (camera.h) and multiple predefined cameras are available: free, 1st/3rd person, orbital */ +#define SUPPORT_CAMERA_SYSTEM 1 +/* Gestures module is included (gestures.h) to support gestures detection: tap, hold, swipe, drag */ +#define SUPPORT_GESTURES_SYSTEM 1 +/* Mouse gestures are directly mapped like touches and processed by gestures system. */ +#define SUPPORT_MOUSE_GESTURES 1 +/* Use busy wait loop for timing sync, if not defined, a high-resolution timer is setup and used */ +#define SUPPORT_BUSY_WAIT_LOOP 1 +/* Allow automatic gif recording of current screen pressing CTRL+F12, defined in KeyCallback() */ +#define SUPPORT_GIF_RECORDING 1 + +// audio.c +/* Desired fileformats to be supported for loading. */ +#define SUPPORT_FILEFORMAT_WAV 1 +#define SUPPORT_FILEFORMAT_OGG 1 +#define SUPPORT_FILEFORMAT_XM 1 +#define SUPPORT_FILEFORMAT_MOD 1 +/* #undef SUPPORT_FILEFORMAT_FLAC */ + +// models.c +/* Selected desired fileformats to be supported for loading. */ +#define SUPPORT_FILEFORMAT_OBJ 1 +#define SUPPORT_FILEFORMAT_MTL 1 + +/* Support procedural mesh generation functions, uses external par_shapes.h library + * NOTE: Some generated meshes DO NOT include generated texture coordinates + */ +#define SUPPORT_MESH_GENERATION 1 + +// utils.c +/* Show TraceLog() output messages. NOTE: By default LOG_DEBUG traces not shown */ +#define SUPPORT_TRACELOG 1 + +/* Support saving image data as PNG fileformat. NOTE: Requires stb_image_write library */ +#define SUPPORT_SAVE_PNG 1 +/* Support saving image data as PMP fileformat. NOTE: Requires stb_image_write library */ +/* #undef SUPPORT_SAVE_BMP */ diff --git a/src/core.c b/src/core.c index e10494c1..0265afd0 100644 --- a/src/core.c +++ b/src/core.c @@ -48,7 +48,7 @@ * Mouse gestures are directly mapped like touches and processed by gestures system. * * #define SUPPORT_BUSY_WAIT_LOOP -* Use busy wait loop for timming sync, if not defined, a high-resolution timer is setup and used +* Use busy wait loop for timing sync, if not defined, a high-resolution timer is setup and used * * #define SUPPORT_GIF_RECORDING * Allow automatic gif recording of current screen pressing CTRL+F12, defined in KeyCallback() @@ -81,15 +81,7 @@ * **********************************************************************************************/ -// Default configuration flags (supported features) -//------------------------------------------------- -#define SUPPORT_DEFAULT_FONT -#define SUPPORT_MOUSE_GESTURES -#define SUPPORT_CAMERA_SYSTEM -#define SUPPORT_GESTURES_SYSTEM -#define SUPPORT_BUSY_WAIT_LOOP -#define SUPPORT_GIF_RECORDING -//------------------------------------------------- +#include "config.h" #include "raylib.h" diff --git a/src/models.c b/src/models.c index ae1fc968..0486d17c 100644 --- a/src/models.c +++ b/src/models.c @@ -36,12 +36,7 @@ * **********************************************************************************************/ -// Default configuration flags (supported features) -//------------------------------------------------- -#define SUPPORT_FILEFORMAT_OBJ -#define SUPPORT_FILEFORMAT_MTL -#define SUPPORT_MESH_GENERATION -//------------------------------------------------- +#include "config.h" #include "raylib.h" diff --git a/src/rlgl.c b/src/rlgl.c index 118823db..68bd3670 100644 --- a/src/rlgl.c +++ b/src/rlgl.c @@ -54,11 +54,7 @@ * **********************************************************************************************/ -// Default configuration flags (supported features) -//------------------------------------------------- -#define SUPPORT_VR_SIMULATOR -#define SUPPORT_DISTORTION_SHADER -//------------------------------------------------- +#include "config.h" #include "rlgl.h" diff --git a/src/rlgl.h b/src/rlgl.h index 01278699..c071acac 100644 --- a/src/rlgl.h +++ b/src/rlgl.h @@ -24,7 +24,7 @@ * #define RLGL_STANDALONE * Use rlgl as standalone library (no raylib dependency) * -* #define SUPPORT_VR_SIMULATION / SUPPORT_STEREO_RENDERING +* #define SUPPORT_VR_SIMULATOR * Support VR simulation functionality (stereo rendering) * * #define SUPPORT_DISTORTION_SHADER @@ -496,4 +496,4 @@ void TraceLog(int msgType, const char *text, ...); // Show trace log messag } #endif -#endif // RLGL_H \ No newline at end of file +#endif // RLGL_H diff --git a/src/shapes.c b/src/shapes.c index 693463ff..a1bc7098 100644 --- a/src/shapes.c +++ b/src/shapes.c @@ -4,12 +4,6 @@ * * CONFIGURATION: * -* #define SUPPORT_QUADS_ONLY -* Draw shapes using only QUADS, vertex are accumulated in QUADS arrays (like textures) -* -* #define SUPPORT_TRIANGLES_ONLY -* Draw shapes using only TRIANGLES, vertex are accumulated in TRIANGLES arrays -* * #define USE_DEFAULT_FONT_TEXTURE * Draw rectangle shapes using font texture white character instead of default white texture * Allows drawing rectangles and text with a single draw call, very useful for GUI systems! @@ -36,7 +30,7 @@ * **********************************************************************************************/ -#define USE_DEFAULT_FONT_TEXTURE +#include "config.h" #include "raylib.h" diff --git a/src/text.c b/src/text.c index d053be30..1a9d386a 100644 --- a/src/text.c +++ b/src/text.c @@ -6,7 +6,7 @@ * * #define SUPPORT_FILEFORMAT_FNT * #define SUPPORT_FILEFORMAT_TTF -* Selected desired fileformats to be supported for loading. Some of those formats are +* Selected desired fileformats to be supported for loading. Some of those formats are * supported by default, to remove support, just comment unrequired #define in this module * * #define SUPPORT_DEFAULT_FONT @@ -36,12 +36,7 @@ * **********************************************************************************************/ -// Default supported features -//------------------------------------- -#define SUPPORT_DEFAULT_FONT -#define SUPPORT_FILEFORMAT_FNT -#define SUPPORT_FILEFORMAT_TTF -//------------------------------------- +#include "config.h" #include "raylib.h" diff --git a/src/textures.c b/src/textures.c index 8a562887..43453f73 100644 --- a/src/textures.c +++ b/src/textures.c @@ -52,17 +52,7 @@ * 3. This notice may not be removed or altered from any source distribution. * **********************************************************************************************/ - -// Default configuration flags (supported features) -//------------------------------------------------- -#define SUPPORT_FILEFORMAT_PNG -#define SUPPORT_FILEFORMAT_DDS -#define SUPPORT_FILEFORMAT_HDR -#define SUPPORT_FILEFORMAT_KTX -#define SUPPORT_FILEFORMAT_ASTC -#define SUPPORT_IMAGE_MANIPULATION -#define SUPPORT_IMAGE_GENERATION -//------------------------------------------------- +#include "config.h" #include "raylib.h" diff --git a/src/utils.c b/src/utils.c index e37b4ff7..9d9d8b55 100644 --- a/src/utils.c +++ b/src/utils.c @@ -41,7 +41,7 @@ * **********************************************************************************************/ -#define SUPPORT_TRACELOG // Output tracelog messages +#include "config.h" #include "raylib.h" // WARNING: Required for: LogType enum #include "utils.h" diff --git a/src/utils.h b/src/utils.h index f4a1a01a..ed75eb68 100644 --- a/src/utils.h +++ b/src/utils.h @@ -32,7 +32,9 @@ #include // Required for: AAssetManager #endif -#define SUPPORT_SAVE_PNG +#ifndef SUPPORT_SAVE_PNG +#define SUPPORT_SAVE_PNG 1 +#endif //---------------------------------------------------------------------------------- // Some basic Defines @@ -74,4 +76,4 @@ FILE *android_fopen(const char *fileName, const char *mode); // Replacement f } #endif -#endif // UTILS_H \ No newline at end of file +#endif // UTILS_H -- cgit v1.2.3