|
| 1 | +/* |
| 2 | + * Copyright (C) 1997-2001 Id Software, Inc. |
| 3 | + * Copyright (C) 2015 Daniel Gibson |
| 4 | + * |
| 5 | + * This program is free software; you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU General Public License as published by |
| 7 | + * the Free Software Foundation; either version 2 of the License, or (at |
| 8 | + * your option) any later version. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, but |
| 11 | + * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 13 | + * |
| 14 | + * See the GNU General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU General Public License |
| 17 | + * along with this program; if not, write to the Free Software |
| 18 | + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
| 19 | + * 02111-1307, USA. |
| 20 | + * |
| 21 | + * ======================================================================= |
| 22 | + * |
| 23 | + * File formats supported by stb_image, for now only tga, png, jpg |
| 24 | + * See also https://github.com/nothings/stb |
| 25 | + * |
| 26 | + * ======================================================================= |
| 27 | + */ |
| 28 | + |
| 29 | +#include <stdlib.h> |
| 30 | + |
| 31 | +#include "../header/local.h" |
| 32 | + |
| 33 | +// don't need HDR stuff |
| 34 | +#define STBI_NO_LINEAR |
| 35 | +#define STBI_NO_HDR |
| 36 | +// make sure STB_image uses standard malloc(), as we'll use standard free() to deallocate |
| 37 | +#define STBI_MALLOC(sz) malloc(sz) |
| 38 | +#define STBI_REALLOC(p,sz) realloc(p,sz) |
| 39 | +#define STBI_FREE(p) free(p) |
| 40 | +// include implementation part of stb_image into this file |
| 41 | +#define STB_IMAGE_IMPLEMENTATION |
| 42 | +#include "stb_image.h" |
| 43 | + |
| 44 | +/* |
| 45 | + * origname: the filename to be opened, might be without extension |
| 46 | + * type: extension of the type we wanna open ("jpg", "png" or "tga") |
| 47 | + * pic: pointer RGBA pixel data will be assigned to |
| 48 | + */ |
| 49 | +qboolean |
| 50 | +LoadSTB(const char *origname, const char* type, byte **pic, int *width, int *height) |
| 51 | +{ |
| 52 | + char filename[256]; |
| 53 | + |
| 54 | + Q_strlcpy(filename, origname, sizeof(filename)); |
| 55 | + |
| 56 | + /* Add the extension */ |
| 57 | + if (strcmp(COM_FileExtension(filename), type) != 0) |
| 58 | + { |
| 59 | + Q_strlcat(filename, ".", sizeof(filename)); |
| 60 | + Q_strlcat(filename, type, sizeof(filename)); |
| 61 | + } |
| 62 | + |
| 63 | + *pic = NULL; |
| 64 | + |
| 65 | + byte* rawdata = NULL; |
| 66 | + int rawsize = FS_LoadFile(filename, (void **)&rawdata); |
| 67 | + if (rawdata == NULL) |
| 68 | + { |
| 69 | + return false; |
| 70 | + } |
| 71 | + |
| 72 | + int w, h, bytesPerPixel; |
| 73 | + byte* data = NULL; |
| 74 | + data = stbi_load_from_memory(rawdata, rawsize, &w, &h, &bytesPerPixel, STBI_rgb_alpha); |
| 75 | + if (data == NULL) |
| 76 | + { |
| 77 | + VID_Printf(PRINT_ALL, "stb_image couldn't load data from %s: %s!\n", filename, stbi_failure_reason()); |
| 78 | + FS_FreeFile(rawdata); |
| 79 | + return false; |
| 80 | + } |
| 81 | + |
| 82 | + FS_FreeFile(rawdata); |
| 83 | + |
| 84 | + VID_Printf(PRINT_DEVELOPER, "LoadSTB() loaded: %s\n", filename); |
| 85 | + |
| 86 | + *pic = data; |
| 87 | + *width = w; |
| 88 | + *height = h; |
| 89 | + return true; |
| 90 | +} |
| 91 | + |
| 92 | + |
| 93 | + |
0 commit comments