Debian Codegraph @

src/gui/image.cpp

raw
1 /**
2 * Furnace Tracker - multi-system chiptune tracker
3 * Copyright (C) 2021-2025 tildearrow and contributors
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
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "gui.h"
21 #include "image.h"
22 #include "../ta-log.h"
23
24 #define STB_IMAGE_IMPLEMENTATION
25 #define STBI_ONLY_PNG
26 #define STBI_NO_STDIO
27 #include "stb_image.h"
28
29 const unsigned char* imageData[GUI_IMAGE_MAX]={
30 image_icon_data,
31 #ifdef NO_INTRO
32 NULL, NULL, NULL,
33 NULL, NULL, NULL
34 #else
35 image_talogo_data,
36 image_tachip_data,
37 image_logo_data,
38 image_wordmark_data,
39 image_introbg_data,
40 image_pat_data
41 #endif
42 };
43
44 const unsigned int imageLen[GUI_IMAGE_MAX]={
45 image_icon_size,
46 #ifdef NO_INTRO
47 0, 0, 0,
48 0, 0, 0
49 #else
50 image_talogo_size,
51 image_tachip_size,
52 image_logo_size,
53 image_wordmark_size,
54 image_introbg_size,
55 image_pat_size
56 #endif
57 };
58
59 FurnaceGUITexture* FurnaceGUI::getTexture(FurnaceGUIImages image, FurnaceGUIBlendMode blendMode) {
60 FurnaceGUIImage* img=getImage(image);
61
62 if (img==NULL) return NULL;
63 if (img->data==NULL) return NULL;
64 if (img->width<=0 || img->height<=0) return NULL;
65
66 bool createTex=false;
67
68 if (img->tex==NULL) {
69 createTex=true;
70 } else {
71 if (!rend->isTextureValid(img->tex)) {
72 rend->destroyTexture(img->tex);
73 img->tex=NULL;
74 createTex=true;
75 }
76 }
77
78 if (createTex) {
79 img->tex=rend->createTexture(false,img->width,img->height,true,bestTexFormat);
80 if (img->tex==NULL) {
81 logE("error while creating image %d texture! %s",(int)image,SDL_GetError());
82 return NULL;
83 }
84 rend->setTextureBlendMode(img->tex,blendMode);
85
86 if (!rend->updateTexture(img->tex,img->data,img->width*4)) {
87 logE("error while updating texture of image %d! %s",(int)image,SDL_GetError());
88 }
89 }
90
91 return img->tex;
92 }
93
94 FurnaceGUIImage* FurnaceGUI::getImage(FurnaceGUIImages image) {
95 FurnaceGUIImage* ret=NULL;
96 auto retPos=images.find(image);
97 if (retPos!=images.cend()) {
98 ret=retPos->second;
99 } else {
100 ret=new FurnaceGUIImage;
101 logV("loading image %d to pool.",(int)image);
102 ret->data=stbi_load_from_memory(imageData[image],imageLen[image],&ret->width,&ret->height,&ret->ch,STBI_rgb_alpha);
103
104 if (ret->data==NULL) {
105 logE("could not load image %d!",(int)image);
106 delete ret;
107 return NULL;
108 }
109
110 logV("%dx%d",ret->width,ret->height);
111
112 #ifdef TA_BIG_ENDIAN
113 if (ret->ch==4) {
114 size_t total=ret->width*ret->height*ret->ch;
115 for (size_t i=0; i<total; i+=4) {
116 ret->data[i]^=ret->data[i|3];
117 ret->data[i|3]^=ret->data[i];
118 ret->data[i]^=ret->data[i|3];
119 ret->data[i|1]^=ret->data[i|2];
120 ret->data[i|2]^=ret->data[i|1];
121 ret->data[i|1]^=ret->data[i|2];
122 }
123 }
124 #endif
125
126 if (ret->ch==4) {
127 size_t total=ret->width*ret->height*ret->ch;
128 switch (bestTexFormat) {
129 case GUI_TEXFORMAT_ARGB32:
130 for (size_t i=0; i<total; i+=4) {
131 ret->data[i]^=ret->data[i|2];
132 ret->data[i|2]^=ret->data[i];
133 ret->data[i]^=ret->data[i|2];
134 }
135 break;
136 case GUI_TEXFORMAT_BGRA32:
137 for (size_t i=0; i<total; i+=4) {
138 ret->data[i]^=ret->data[i|3];
139 ret->data[i|3]^=ret->data[i];
140 ret->data[i]^=ret->data[i|3];
141 ret->data[i|1]^=ret->data[i|2];
142 ret->data[i|2]^=ret->data[i|1];
143 ret->data[i|1]^=ret->data[i|2];
144 ret->data[i|1]^=ret->data[i|3];
145 ret->data[i|3]^=ret->data[i|1];
146 ret->data[i|1]^=ret->data[i|3];
147 }
148 break;
149 case GUI_TEXFORMAT_RGBA32:
150 for (size_t i=0; i<total; i+=4) {
151 ret->data[i]^=ret->data[i|3];
152 ret->data[i|3]^=ret->data[i];
153 ret->data[i]^=ret->data[i|3];
154 ret->data[i|1]^=ret->data[i|2];
155 ret->data[i|2]^=ret->data[i|1];
156 ret->data[i|1]^=ret->data[i|2];
157 }
158 break;
159 default:
160 break;
161 }
162 }
163
164 images[image]=ret;
165 }
166
167 // warning silencers
168 stbi__addints_valid(2,2);
169 stbi__mul2shorts_valid(2,2);
170
171 return ret;
172 }