Debian Codegraph @

src/pt2_bmp.c

raw symbols SCIP
1 // for finding memory leaks in debug mode with Visual Studio
2 #if defined _DEBUG && defined _MSC_VER
3 #include <crtdbg.h>
4 #endif
5
6 #include <stdint.h>
7 #include <stdbool.h>
8 #include "pt2_palette.h"
9 #include "pt2_structs.h"
10 #include "pt2_helpers.h"
11 #include "pt2_bmp.h"
12 #include "pt2_tables.h"
13
14 uint32_t *aboutScreenBMP = NULL, *diskOpScreenBMP = NULL, *editOpModeCharsBMP = NULL;
15 uint32_t *editOpScreen1BMP = NULL, *editOpScreen2BMP = NULL, *samplerVolumeBMP = NULL;
16 uint32_t *editOpScreen3BMP = NULL, *editOpScreen4BMP = NULL, *spectrumVisualsBMP = NULL;
17 uint32_t *muteButtonsBMP = NULL, *posEdBMP = NULL, *samplerFiltersBMP = NULL;
18 uint32_t *samplerScreenBMP = NULL, *trackerFrameBMP = NULL, *sampleMonitorBMP = NULL;
19 uint32_t *samplingBoxBMP = NULL;
20
21 // fix-bitmaps for 128K sample mode
22 uint32_t *fix128KTrackerBMP = NULL;
23 uint32_t *fix128KPosBMP = NULL;
24 uint32_t *fix128KChordBMP = NULL;
25
26 void createBitmaps(void)
27 {
28 uint8_t r8, g8, b8, r8_2, g8_2, b8_2;
29
30 uint32_t pixel24 = video.palette[PAL_PATCURSOR];
31 for (int32_t y = 0; y < 14; y++)
32 {
33 // top two rows have a lighter color
34 if (y < 2)
35 {
36 r8 = R24(pixel24);
37 g8 = G24(pixel24);
38 b8 = B24(pixel24);
39
40 if (r8 <= 0xFF-0x33)
41 r8 += 0x33;
42 else
43 r8 = 0xFF;
44
45 if (g8 <= 0xFF-0x33)
46 g8 += 0x33;
47 else
48 g8 = 0xFF;
49
50 if (b8 <= 0xFF-0x33)
51 b8 += 0x33;
52 else
53 b8 = 0xFF;
54
55 for (int32_t x = 0; x < 11; x++)
56 patternCursorBMP[(y * 11) + x] = RGB24(r8, g8, b8);
57 }
58
59 // sides (same color)
60 if (y >= 2 && y <= 12)
61 {
62 patternCursorBMP[(y * 11) + 0] = pixel24;
63
64 for (int32_t x = 1; x < 10; x++)
65 patternCursorBMP[(y * 11) + x] = video.palette[PAL_COLORKEY];
66
67 patternCursorBMP[(y * 11) + 10] = pixel24;
68 }
69
70 // bottom two rows have a darker color
71 if (y > 11)
72 {
73 r8 = R24(pixel24);
74 g8 = G24(pixel24);
75 b8 = B24(pixel24);
76
77 if (r8 >= 0x33)
78 r8 -= 0x33;
79 else
80 r8 = 0x00;
81
82 if (g8 >= 0x33)
83 g8 -= 0x33;
84 else
85 g8 = 0x00;
86
87 if (b8 >= 0x33)
88 b8 -= 0x33;
89 else
90 b8 = 0x00;
91
92 for (int32_t x = 0; x < 11; x++)
93 patternCursorBMP[(y * 11) + x] = RGB24(r8, g8, b8);
94 }
95 }
96
97 // create spectrum analyzer bar graphics
98 for (int32_t i = 0; i < 36; i++)
99 analyzerColorsRGB24[i] = RGB12_to_RGB24(analyzerColors[35-i]);
100
101 // create VU-Meter bar graphics
102 for (int32_t i = 0; i < 48; i++)
103 {
104 uint16_t pixel12 = vuMeterColors[47-i];
105
106 r8_2 = r8 = R12_to_R24(pixel12);
107 g8_2 = g8 = G12_to_G24(pixel12);
108 b8_2 = b8 = B12_to_B24(pixel12);
109
110 // brighter pixels on the left side
111
112 if (r8_2 <= 0xFF-0x33)
113 r8_2 += 0x33;
114 else
115 r8_2 = 0xFF;
116
117 if (g8_2 <= 0xFF-0x33)
118 g8_2 += 0x33;
119 else
120 g8_2 = 0xFF;
121
122 if (b8_2 <= 0xFF-0x33)
123 b8_2 += 0x33;
124 else
125 b8_2 = 0xFF;
126
127 pixel24 = RGB24(r8_2, g8_2, b8_2);
128
129 vuMeterBMP[(i * 10) + 0] = pixel24;
130 vuMeterBMP[(i * 10) + 1] = pixel24;
131
132 // main pixels
133 for (int32_t j = 2; j < 8; j++)
134 vuMeterBMP[(i * 10) + j] = RGB24(r8, g8, b8);
135
136 // darker pixels on the right side
137 r8_2 = r8;
138 g8_2 = g8;
139 b8_2 = b8;
140
141 if (r8_2 >= 0x33)
142 r8_2 -= 0x33;
143 else
144 r8_2 = 0x00;
145
146 if (g8_2 >= 0x33)
147 g8_2 -= 0x33;
148 else
149 g8_2 = 0x00;
150
151 if (b8_2 >= 0x33)
152 b8_2 -= 0x33;
153 else
154 b8_2 = 0x00;
155
156 pixel24 = RGB24(r8_2, g8_2, b8_2);
157
158 vuMeterBMP[(i * 10) + 8] = pixel24;
159 vuMeterBMP[(i * 10) + 9] = pixel24;
160 }
161 }
162
163 void freeBMPs(void)
164 {
165 if (fix128KChordBMP != NULL) free(fix128KChordBMP);
166 if (fix128KPosBMP != NULL) free(fix128KPosBMP);
167 if (fix128KTrackerBMP != NULL) free(fix128KTrackerBMP);
168 if (trackerFrameBMP != NULL) free(trackerFrameBMP);
169 if (samplerScreenBMP != NULL) free(samplerScreenBMP);
170 if (samplerVolumeBMP != NULL) free(samplerVolumeBMP);
171 if (samplerFiltersBMP != NULL) free(samplerFiltersBMP);
172 if (diskOpScreenBMP != NULL) free(diskOpScreenBMP);
173 if (posEdBMP != NULL) free(posEdBMP);
174 if (spectrumVisualsBMP != NULL) free(spectrumVisualsBMP);
175 if (editOpScreen1BMP != NULL) free(editOpScreen1BMP);
176 if (editOpScreen2BMP != NULL) free(editOpScreen2BMP);
177 if (editOpScreen3BMP != NULL) free(editOpScreen3BMP);
178 if (editOpScreen4BMP != NULL) free(editOpScreen4BMP);
179 if (aboutScreenBMP != NULL) free(aboutScreenBMP);
180 if (muteButtonsBMP != NULL) free(muteButtonsBMP);
181 if (editOpModeCharsBMP != NULL) free(editOpModeCharsBMP);
182 if (sampleMonitorBMP != NULL) free(sampleMonitorBMP);
183 if (samplingBoxBMP != NULL) free(samplingBoxBMP);
184 }
185
186 uint32_t *unpackBMP(const uint8_t *src, uint32_t packedLen)
187 {
188 // RLE decode
189
190 int32_t decodedLength = (src[0] << 24) | (src[1] << 16) | (src[2] << 8) | src[3];
191
192 // 2-bit to 8-bit conversion
193 uint32_t *dst = (uint32_t *)malloc(((decodedLength * 4) * sizeof (int32_t)) + 8);
194 if (dst == NULL)
195 return NULL;
196
197 uint8_t *tmpBuffer = (uint8_t *)malloc(decodedLength + 128); // some margin is needed, the packer is buggy
198 if (tmpBuffer == NULL)
199 {
200 free(dst);
201 return NULL;
202 }
203
204 const uint8_t *packSrc = src + 4; // skip "length" field
205 uint8_t *packDst = tmpBuffer;
206
207 int32_t i = packedLen - 4; // subtract "length" field
208 while (i > 0)
209 {
210 uint8_t byteIn = *packSrc++;
211 if (byteIn == 0xCC) // compactor code
212 {
213 int16_t count = *packSrc++;
214 byteIn = *packSrc++;
215
216 while (count-- >= 0)
217 *packDst++ = byteIn;
218
219 i -= 2;
220 }
221 else
222 {
223 *packDst++ = byteIn;
224 }
225
226 i--;
227 }
228
229 for (i = 0; i < decodedLength; i++)
230 {
231 const uint8_t byte1 = (tmpBuffer[i] & 0xC0) >> 6;
232 ASSERT(byte1 < PALETTE_NUM);
233 dst[(i << 2) + 0] = video.palette[byte1];
234
235 const uint8_t byte2 = (tmpBuffer[i] & 0x30) >> 4;
236 ASSERT(byte2 < PALETTE_NUM);
237 dst[(i << 2) + 1] = video.palette[byte2];
238
239 const uint8_t byte3 = (tmpBuffer[i] & 0x0C) >> 2;
240 ASSERT(byte3 < PALETTE_NUM);
241 dst[(i << 2) + 2] = video.palette[byte3];
242
243 const uint8_t byte4 = (tmpBuffer[i] & 0x03) >> 0;
244 ASSERT(byte4 < PALETTE_NUM);
245 dst[(i << 2) + 3] = video.palette[byte4];
246 }
247
248 free(tmpBuffer);
249 return dst;
250 }
251
252 bool unpackBMPs(void)
253 {
254 fix128KChordBMP = unpackBMP(fix128KChordPackedBMP, sizeof (fix128KChordPackedBMP));
255 fix128KPosBMP = unpackBMP(fix128KPosPackedBMP, sizeof (fix128KPosPackedBMP));
256 fix128KTrackerBMP = unpackBMP(tracker128KFixPackedBMP, sizeof (tracker128KFixPackedBMP));
257 trackerFrameBMP = unpackBMP(trackerFramePackedBMP, sizeof (trackerFramePackedBMP));
258 samplerScreenBMP = unpackBMP(samplerScreenPackedBMP, sizeof (samplerScreenPackedBMP));
259 samplerVolumeBMP = unpackBMP(samplerVolumePackedBMP, sizeof (samplerVolumePackedBMP));
260 samplerFiltersBMP = unpackBMP(samplerFiltersPackedBMP, sizeof (samplerFiltersPackedBMP));
261 diskOpScreenBMP = unpackBMP(diskOpScreenPackedBMP, sizeof (diskOpScreenPackedBMP));
262 posEdBMP = unpackBMP(posEdPackedBMP, sizeof (posEdPackedBMP));
263 spectrumVisualsBMP = unpackBMP(spectrumVisualsPackedBMP, sizeof (spectrumVisualsPackedBMP));
264 editOpScreen1BMP = unpackBMP(editOpScreen1PackedBMP, sizeof (editOpScreen1PackedBMP));
265 editOpScreen2BMP = unpackBMP(editOpScreen2PackedBMP, sizeof (editOpScreen2PackedBMP));
266 editOpScreen3BMP = unpackBMP(editOpScreen3PackedBMP, sizeof (editOpScreen3PackedBMP));
267 editOpScreen4BMP = unpackBMP(editOpScreen4PackedBMP, sizeof (editOpScreen4PackedBMP));
268 aboutScreenBMP = unpackBMP(aboutScreenPackedBMP, sizeof (aboutScreenPackedBMP));
269 muteButtonsBMP = unpackBMP(muteButtonsPackedBMP, sizeof (muteButtonsPackedBMP));
270 editOpModeCharsBMP = unpackBMP(editOpModeCharsPackedBMP, sizeof (editOpModeCharsPackedBMP));
271 sampleMonitorBMP = unpackBMP(sampleMonitorPackedBMP, sizeof (sampleMonitorPackedBMP));
272 samplingBoxBMP = unpackBMP(samplingBoxPackedBMP, sizeof (samplingBoxPackedBMP));
273
274 if (fix128KTrackerBMP == NULL || fix128KPosBMP == NULL || fix128KChordBMP == NULL ||
275 trackerFrameBMP == NULL || samplerScreenBMP == NULL || samplerVolumeBMP == NULL ||
276 diskOpScreenBMP == NULL || posEdBMP == NULL || spectrumVisualsBMP == NULL ||
277 editOpScreen1BMP == NULL || editOpScreen2BMP == NULL || editOpScreen3BMP == NULL ||
278 editOpScreen4BMP == NULL || aboutScreenBMP == NULL || muteButtonsBMP == NULL ||
279 editOpModeCharsBMP == NULL || samplerFiltersBMP == NULL || sampleMonitorBMP == NULL ||
280 samplingBoxBMP == NULL)
281 {
282 showErrorMsgBox("Out of memory!");
283 return false; // BMPs are free'd in cleanUp()
284 }
285
286 createBitmaps();
287 return true;
288 }