|
@@ -1,852 +0,0 @@
|
1
|
|
-/*================================================================
|
2
|
|
- *
|
3
|
|
- * Project : Freyja
|
4
|
|
- * Author : Mongoose
|
5
|
|
- * Website : http://www.westga.edu/~stu7440/
|
6
|
|
- * Email : stu7440@westga.edu
|
7
|
|
- * Object : MtkImage
|
8
|
|
- * License : GPL See file COPYING, also (C) 2000 Mongoose
|
9
|
|
- * Comments: This is the new mtkImage class for mtk
|
10
|
|
- *
|
11
|
|
- *
|
12
|
|
- * This file was generated using Mongoose's C++
|
13
|
|
- * template generator script. <stu7440@westga.edu>
|
14
|
|
- *
|
15
|
|
- *-- History -------------------------------------------------
|
16
|
|
- *
|
17
|
|
- * 2000-10-05:
|
18
|
|
- * Mongoose - Created
|
19
|
|
- =================================================================*/
|
20
|
|
-
|
21
|
|
-#include "MtkImage.h"
|
22
|
|
-
|
23
|
|
-#ifdef DEBUG_MEMEORY
|
24
|
|
-# include "memeory_test.h"
|
25
|
|
-#endif
|
26
|
|
-
|
27
|
|
-
|
28
|
|
-#include <dlfcn.h>
|
29
|
|
-static const char *mtk_image_module_names[] = {
|
30
|
|
- "pcx",
|
31
|
|
- "ppm",
|
32
|
|
- "tga",
|
33
|
|
- "jpeg",
|
34
|
|
- "png"
|
35
|
|
-};
|
36
|
|
-
|
37
|
|
-#define MTK_IMAGE_MODULE_NAME_COUNT (sizeof(mtk_image_module_names) / sizeof(mtk_image_module_names[0]))
|
38
|
|
-
|
39
|
|
-
|
40
|
|
-#include <stdio.h>
|
41
|
|
-#include <stdlib.h>
|
42
|
|
-#include <math.h>
|
43
|
|
-
|
44
|
|
-int mtkExtCheck(char *filename, char *ext)
|
45
|
|
-{
|
46
|
|
- unsigned int len_f, len_e;
|
47
|
|
-
|
48
|
|
-
|
49
|
|
- len_f = strlen(filename) - 1;
|
50
|
|
- len_e = strlen(ext) - 1;
|
51
|
|
-
|
52
|
|
- while (len_f+1 && len_e+1)
|
53
|
|
- {
|
54
|
|
- //printf("extcheck: %c %c\n", filename[len_f], ext[len_e]);
|
55
|
|
-
|
56
|
|
- if (filename[len_f] == ext[len_e])
|
57
|
|
- {
|
58
|
|
- if (ext[len_e] == '.')
|
59
|
|
- {
|
60
|
|
- //printf("extcheck: match\n");
|
61
|
|
- return 0;
|
62
|
|
- }
|
63
|
|
- }
|
64
|
|
- else if (filename[len_f] < ext[len_e])
|
65
|
|
- return -1;
|
66
|
|
- else if (filename[len_f] > ext[len_e])
|
67
|
|
- return 1;
|
68
|
|
-
|
69
|
|
- len_f--;
|
70
|
|
- len_e--;
|
71
|
|
- }
|
72
|
|
-
|
73
|
|
- return 1;
|
74
|
|
-}
|
75
|
|
-
|
76
|
|
-//////////////////////////////////////////////
|
77
|
|
-
|
78
|
|
-MtkImage::MtkImage()
|
79
|
|
-{
|
80
|
|
- _image = NULL;
|
81
|
|
- _palette = NULL;
|
82
|
|
- _width = 0;
|
83
|
|
- _height = 0;
|
84
|
|
- _original_width = 0;
|
85
|
|
- _original_height = 0;
|
86
|
|
- _color_mode = COLORMODE_RGB;
|
87
|
|
- _image_bpp = 0;
|
88
|
|
- _palette_bpp = 0;
|
89
|
|
-}
|
90
|
|
-
|
91
|
|
-
|
92
|
|
-MtkImage::~MtkImage()
|
93
|
|
-{
|
94
|
|
- Clear();
|
95
|
|
-}
|
96
|
|
-
|
97
|
|
-
|
98
|
|
-void MtkImage::Clear()
|
99
|
|
-{
|
100
|
|
- if (_image)
|
101
|
|
- delete [] _image;
|
102
|
|
-
|
103
|
|
- if (_palette)
|
104
|
|
- delete [] _palette;
|
105
|
|
-
|
106
|
|
- _width = 0;
|
107
|
|
- _height = 0;
|
108
|
|
- _original_width = 0;
|
109
|
|
- _original_height = 0;
|
110
|
|
- _palette_bpp = 0;
|
111
|
|
-}
|
112
|
|
-
|
113
|
|
-
|
114
|
|
-int MtkImage::Width()
|
115
|
|
-{
|
116
|
|
- return _width;
|
117
|
|
-}
|
118
|
|
-
|
119
|
|
-
|
120
|
|
-int MtkImage::Height()
|
121
|
|
-{
|
122
|
|
- return _height;
|
123
|
|
-}
|
124
|
|
-
|
125
|
|
-
|
126
|
|
-int MtkImage::OriginalWidth()
|
127
|
|
-{
|
128
|
|
- return _original_width;
|
129
|
|
-}
|
130
|
|
-
|
131
|
|
-
|
132
|
|
-int MtkImage::OriginalHeight()
|
133
|
|
-{
|
134
|
|
- return _original_height;
|
135
|
|
-}
|
136
|
|
-
|
137
|
|
-
|
138
|
|
-void MtkImage::Mode(mtk_colormode mode)
|
139
|
|
-{
|
140
|
|
- fprintf(stderr, "MtkImage::Mode> ERROR not implemented, %s:%i\n",
|
141
|
|
- __FILE__, __LINE__);
|
142
|
|
-}
|
143
|
|
-
|
144
|
|
-
|
145
|
|
-//FIXME: Add support for 32bit!
|
146
|
|
-int MtkImage::PaletteLoad(unsigned char *buffer, int bpp)
|
147
|
|
-{
|
148
|
|
- if (buffer && buffer != _palette &&
|
149
|
|
- (bpp == 8 || bpp == 16 || bpp == 24))
|
150
|
|
- {
|
151
|
|
- if (_palette)
|
152
|
|
- delete [] _palette;
|
153
|
|
-
|
154
|
|
- _palette_bpp = bpp;
|
155
|
|
-
|
156
|
|
- _palette = new unsigned char[768];
|
157
|
|
- memcpy(_palette, buffer, 768);
|
158
|
|
-
|
159
|
|
- return 0;
|
160
|
|
- }
|
161
|
|
-
|
162
|
|
- printf("MtkImage::LoadPaletteBuffer()> Assertion Error bpp not {8,16,24}\n");
|
163
|
|
- return -1;
|
164
|
|
-}
|
165
|
|
-
|
166
|
|
-
|
167
|
|
-int MtkImage::LoadIndexed(unsigned char *image, int width, int height)
|
168
|
|
-{
|
169
|
|
- int i, size, index;
|
170
|
|
-
|
171
|
|
-
|
172
|
|
- if (!image || !_palette || !width || !height || image == _image)
|
173
|
|
- {
|
174
|
|
- printf("MtkImage::LoadIndexedBuffer> Assertion failed\n");
|
175
|
|
- return -1;
|
176
|
|
- }
|
177
|
|
-
|
178
|
|
- if (_image)
|
179
|
|
- delete [] _image;
|
180
|
|
-
|
181
|
|
- size = width * height;
|
182
|
|
-
|
183
|
|
- _width = _original_width = width;
|
184
|
|
- _height = _original_height = height;
|
185
|
|
- _image = new unsigned char[size * 3];
|
186
|
|
-
|
187
|
|
- _color_mode = COLORMODE_RGB;
|
188
|
|
-
|
189
|
|
- for (i = 0; i < size; i++)
|
190
|
|
- {
|
191
|
|
- index = image[i];
|
192
|
|
-
|
193
|
|
- if (index * 3 + 2 > 768)
|
194
|
|
- printf("Index outside of palette!\n");
|
195
|
|
- else
|
196
|
|
- {
|
197
|
|
- _image[i*3] = _palette[index*3];
|
198
|
|
- _image[i*3+1] = _palette[index*3+1];
|
199
|
|
- _image[i*3+2] = _palette[index*3+2];
|
200
|
|
- }
|
201
|
|
- }
|
202
|
|
-
|
203
|
|
- return 0;
|
204
|
|
-}
|
205
|
|
-
|
206
|
|
-void MtkImage::PaletteBrighten(float p)
|
207
|
|
-{
|
208
|
|
- int i;
|
209
|
|
-
|
210
|
|
-
|
211
|
|
- if (p <= 0.0 || p > 0.999)
|
212
|
|
- {
|
213
|
|
- printf("MtkImage::BrightenPalette> %f isn't a postive percentage\n", p);
|
214
|
|
- return;
|
215
|
|
- }
|
216
|
|
-
|
217
|
|
- if (_palette)
|
218
|
|
- {
|
219
|
|
- for (i = 0; i < 255; i++)
|
220
|
|
- {
|
221
|
|
- _palette[i*3] += (unsigned char)(_palette[i*3] * p);
|
222
|
|
- _palette[i*3+1] += (unsigned char)(_palette[i*3+1] * p);
|
223
|
|
- _palette[i*3+2] += (unsigned char)(_palette[i*3+2] * p);
|
224
|
|
- }
|
225
|
|
- }
|
226
|
|
-}
|
227
|
|
-
|
228
|
|
-
|
229
|
|
-void MtkImage::Image(unsigned char **buffer)
|
230
|
|
-{
|
231
|
|
- *buffer = NULL;
|
232
|
|
-
|
233
|
|
- if (!_image)
|
234
|
|
- {
|
235
|
|
- printf("Image::ImageBuffer> No image allocated\n");
|
236
|
|
- return;
|
237
|
|
- }
|
238
|
|
-
|
239
|
|
- switch (_color_mode)
|
240
|
|
- {
|
241
|
|
- case COLORMODE_RGB:
|
242
|
|
- *buffer = new unsigned char[_width*_height*3];
|
243
|
|
- memcpy(*buffer, _image, _width*_height*3);
|
244
|
|
- break;
|
245
|
|
- case COLORMODE_RGBA:
|
246
|
|
- *buffer = new unsigned char[_width*_height*4];
|
247
|
|
- memcpy(*buffer, _image, _width*_height*4);
|
248
|
|
- break;
|
249
|
|
- case COLORMODE_INDEXED:
|
250
|
|
- break;
|
251
|
|
- }
|
252
|
|
-}
|
253
|
|
-
|
254
|
|
-
|
255
|
|
-void MtkImage::Palette(unsigned char **buffer)
|
256
|
|
-{
|
257
|
|
- *buffer = NULL;
|
258
|
|
-
|
259
|
|
- if (!_palette)
|
260
|
|
- {
|
261
|
|
- printf("Image::PaletteBuffer> No palette allocated\n");
|
262
|
|
- return;
|
263
|
|
- }
|
264
|
|
-
|
265
|
|
- *buffer = new unsigned char[768];
|
266
|
|
- memcpy(*buffer, _palette, 768);
|
267
|
|
-}
|
268
|
|
-
|
269
|
|
-
|
270
|
|
-void MtkImage::IndexedImage(unsigned char **buffer)
|
271
|
|
-{
|
272
|
|
- int size = _width * _height;
|
273
|
|
- float rgb[3];
|
274
|
|
- int i;
|
275
|
|
-
|
276
|
|
-
|
277
|
|
- *buffer = NULL;
|
278
|
|
-
|
279
|
|
- if (!_image || !_palette)
|
280
|
|
- {
|
281
|
|
- printf("MtkImage::IndexedImage> No image or palette allocated\n");
|
282
|
|
- return;
|
283
|
|
- }
|
284
|
|
-
|
285
|
|
- *buffer = new unsigned char[size];
|
286
|
|
-
|
287
|
|
- for (i = 0; i < size; i++)
|
288
|
|
- {
|
289
|
|
- rgb[0] = _image[i*3]/255.0;
|
290
|
|
- rgb[1] = _image[i*3+1]/255.0;
|
291
|
|
- rgb[2] = _image[i*3+2]/255.0;
|
292
|
|
-
|
293
|
|
- (*buffer)[i] = PaletteMatchColor(rgb);
|
294
|
|
- }
|
295
|
|
-}
|
296
|
|
-
|
297
|
|
-void MtkImage::FlipVertical()
|
298
|
|
-{
|
299
|
|
- int i, j;
|
300
|
|
- unsigned char *swap_row = NULL;
|
301
|
|
-
|
302
|
|
-
|
303
|
|
- if (!_image)
|
304
|
|
- return;
|
305
|
|
-
|
306
|
|
- switch(_color_mode)
|
307
|
|
- {
|
308
|
|
- case COLORMODE_INDEXED:
|
309
|
|
- // We don't store images as indexed, they're computed
|
310
|
|
- break;
|
311
|
|
- case COLORMODE_RGB:
|
312
|
|
- swap_row = new unsigned char[_width*3];
|
313
|
|
-
|
314
|
|
- for (i = 0, j = _height-1; i < _height/2; i++, j--)
|
315
|
|
- {
|
316
|
|
- memcpy(swap_row, &_image[i*_width*3], _width*3);
|
317
|
|
- memcpy(&_image[i*_width*3], &_image[j*_width*3], _width*3);
|
318
|
|
- memcpy(&_image[j*_width*3], swap_row, _width*3);
|
319
|
|
- }
|
320
|
|
-
|
321
|
|
- delete [] swap_row;
|
322
|
|
- break;
|
323
|
|
- case COLORMODE_RGBA:
|
324
|
|
- swap_row = new unsigned char[_width*4];
|
325
|
|
-
|
326
|
|
- for (i = 0, j = _height-1; i < _height/2; i++, j--)
|
327
|
|
- {
|
328
|
|
- memcpy(swap_row, &_image[i*_width*4], _width*4);
|
329
|
|
- memcpy(&_image[i*_width*4], &_image[j*_width*4], _width*4);
|
330
|
|
- memcpy(&_image[j*_width*4], swap_row, _width*4);
|
331
|
|
- }
|
332
|
|
-
|
333
|
|
- delete [] swap_row;
|
334
|
|
- break;
|
335
|
|
- }
|
336
|
|
-}
|
337
|
|
-
|
338
|
|
-// FIXME: static only for now
|
339
|
|
-int MtkImage::Load(char *filename)
|
340
|
|
-{
|
341
|
|
- int (*load)(char *filename, unsigned char **image,
|
342
|
|
- unsigned int *width, unsigned int *height, char *type);
|
343
|
|
- int (*check)(char *filename);
|
344
|
|
- void *handle;
|
345
|
|
- char *error;
|
346
|
|
- FILE *f;
|
347
|
|
- unsigned int i;
|
348
|
|
- char module_filename[128];
|
349
|
|
- char module_check[128];
|
350
|
|
- char module_load[128];
|
351
|
|
- char *name;
|
352
|
|
- unsigned char *image;
|
353
|
|
- unsigned int w, h;
|
354
|
|
- bool loaded = false;
|
355
|
|
- char type;
|
356
|
|
-
|
357
|
|
-
|
358
|
|
- f = fopen(filename, "rb");
|
359
|
|
-
|
360
|
|
- if (!f)
|
361
|
|
- {
|
362
|
|
- perror("MtkImage::Load> File open failed.\n");
|
363
|
|
- return -1;
|
364
|
|
- }
|
365
|
|
-
|
366
|
|
- printf("[MtkImage module loader invoked]\n");
|
367
|
|
-
|
368
|
|
- for (i = 0; i < MTK_IMAGE_MODULE_NAME_COUNT && !loaded; i++)
|
369
|
|
- {
|
370
|
|
- name = (char *)mtk_image_module_names[i];
|
371
|
|
-
|
372
|
|
- snprintf(module_filename, 128, "libmtk_loader-%s.so", name);
|
373
|
|
- snprintf(module_load, 128, "mtk_image__%s_load", name);
|
374
|
|
- snprintf(module_check, 128, "mtk_image__%s_check", name);
|
375
|
|
-
|
376
|
|
- if (!(handle = dlopen (module_filename, RTLD_NOW))) //RTLD_LAZY)))
|
377
|
|
- {
|
378
|
|
- fprintf(stderr, "\tERROR: Couldn't find module '%s'.\n", module_filename);
|
379
|
|
- return -1;
|
380
|
|
- }
|
381
|
|
- else
|
382
|
|
- {
|
383
|
|
- printf("\tModule '%s' opened.\n", module_filename);
|
384
|
|
-
|
385
|
|
- check = (int (*)(char *filename))dlsym(handle, module_check);
|
386
|
|
-
|
387
|
|
- load = (int (*)(char * filename, unsigned char **image,
|
388
|
|
- unsigned int *w, unsigned int *h,
|
389
|
|
- char *type))dlsym(handle, module_load);
|
390
|
|
-
|
391
|
|
- if ((error = dlerror()) != NULL)
|
392
|
|
- {
|
393
|
|
- fprintf (stderr, "\tERROR: %s\n", error);
|
394
|
|
- return -5;
|
395
|
|
- }
|
396
|
|
-
|
397
|
|
- loaded = (loaded || (!(*check)(filename) &&
|
398
|
|
- !(*load)(filename, &image, &w, &h, &type)));
|
399
|
|
-
|
400
|
|
- if ((error = dlerror()) != NULL)
|
401
|
|
- return -10;
|
402
|
|
-
|
403
|
|
- dlclose(handle);
|
404
|
|
- }
|
405
|
|
- }
|
406
|
|
-
|
407
|
|
-
|
408
|
|
- if (loaded)
|
409
|
|
- {
|
410
|
|
- Clear();
|
411
|
|
-
|
412
|
|
- _image = image;
|
413
|
|
- _original_width = _width = w;
|
414
|
|
- _original_height = _height = h;
|
415
|
|
-
|
416
|
|
- switch (type)
|
417
|
|
- {
|
418
|
|
- case 4:
|
419
|
|
- case 2:
|
420
|
|
- _color_mode = COLORMODE_RGBA;
|
421
|
|
- break;
|
422
|
|
- default:
|
423
|
|
- _color_mode = COLORMODE_RGB;
|
424
|
|
- }
|
425
|
|
- return 0;
|
426
|
|
- }
|
427
|
|
- else
|
428
|
|
- return -2;
|
429
|
|
-}
|
430
|
|
-
|
431
|
|
-int MtkImage::Save(char *filename, char *format)
|
432
|
|
-{
|
433
|
|
- extern int mtk_image__tga_save(FILE *f, unsigned char *image,
|
434
|
|
- unsigned int width, unsigned int height,
|
435
|
|
- char bpp);
|
436
|
|
- FILE *f;
|
437
|
|
- int ret;
|
438
|
|
-
|
439
|
|
-
|
440
|
|
- f = fopen(filename, "wb");
|
441
|
|
-
|
442
|
|
-
|
443
|
|
- ret = mtk_image__tga_save(f, _image, _width, _height, _color_mode+1);
|
444
|
|
-
|
445
|
|
- if (f)
|
446
|
|
- fclose(f);
|
447
|
|
-
|
448
|
|
- return ret;
|
449
|
|
-}
|
450
|
|
-
|
451
|
|
-
|
452
|
|
-int MtkImage::Load(unsigned char *image, unsigned int w, unsigned int h,
|
453
|
|
- mtk_colormode mode)
|
454
|
|
-{
|
455
|
|
- switch (mode)
|
456
|
|
- {
|
457
|
|
- case COLORMODE_INDEXED:
|
458
|
|
- return LoadIndexed(image, w, h);
|
459
|
|
- break;
|
460
|
|
- case COLORMODE_RGB:
|
461
|
|
- if (!image || !w || !h || image == _image)
|
462
|
|
- return -1;
|
463
|
|
-
|
464
|
|
- _width = _original_width = w;
|
465
|
|
- _height = _original_height = h;
|
466
|
|
-
|
467
|
|
- if (_image)
|
468
|
|
- delete [] _image;
|
469
|
|
-
|
470
|
|
- _color_mode = COLORMODE_RGB;
|
471
|
|
-
|
472
|
|
- _image = new unsigned char[w * h * 3];
|
473
|
|
- memcpy(_image, image, w * h * 3);
|
474
|
|
-
|
475
|
|
- return 0;
|
476
|
|
- break;
|
477
|
|
- case COLORMODE_RGBA:
|
478
|
|
- if (!image || !w || !h || image == _image)
|
479
|
|
- return -1;
|
480
|
|
-
|
481
|
|
- _width = _original_width = w;
|
482
|
|
- _height = _original_height = h;
|
483
|
|
-
|
484
|
|
- if (_image)
|
485
|
|
- delete [] _image;
|
486
|
|
-
|
487
|
|
- _color_mode = COLORMODE_RGBA;
|
488
|
|
-
|
489
|
|
- _image = new unsigned char[w * h * 4];
|
490
|
|
- memcpy(_image, image, w * h * 4);
|
491
|
|
-
|
492
|
|
- return 0;
|
493
|
|
- break;
|
494
|
|
- }
|
495
|
|
-
|
496
|
|
- return -1;
|
497
|
|
-}
|
498
|
|
-
|
499
|
|
-
|
500
|
|
-// Palette ////////////////////////////////////////////
|
501
|
|
-
|
502
|
|
-// Search palette for matching ( or closest ) rgb color
|
503
|
|
-// Colors *must be 0.0 to 1.0 form!
|
504
|
|
-//
|
505
|
|
-// Note if you have a small palette this may 'wash' to black
|
506
|
|
-// quickly after running through a few times ( 0.0 is default )
|
507
|
|
-int MtkImage::PaletteMatchColor(float rgb[3])
|
508
|
|
-{
|
509
|
|
- float color[3];
|
510
|
|
- float best_weight, current_weight;
|
511
|
|
- int i, best;
|
512
|
|
-
|
513
|
|
-
|
514
|
|
- if (!_palette)
|
515
|
|
- {
|
516
|
|
- printf("MtkImage::MatchColor> ERROR no palette loaded\n");
|
517
|
|
- return -1;
|
518
|
|
- }
|
519
|
|
-
|
520
|
|
- best_weight = current_weight = 0.0;
|
521
|
|
-
|
522
|
|
- for (best = 0, i = 0; i < 256; i++)
|
523
|
|
- {
|
524
|
|
- // Convert from 0-255 to 0.0-1.0 color scale
|
525
|
|
- PaletteColor(i, color);
|
526
|
|
- //color[0] = _palette[i*3]/255.0;
|
527
|
|
- //color[1] = _palette[i*3+1]/255.0;
|
528
|
|
- //color[2] = _palette[i*3+2]/255.0;
|
529
|
|
-
|
530
|
|
- // FIXME: Should be 0.0 (far off) to 1.0 (exact match)
|
531
|
|
- current_weight = 1.0 - ((fabs(rgb[0] - color[0]) +
|
532
|
|
- fabs(rgb[1] - color[1]) +
|
533
|
|
- fabs(rgb[2] - color[2])) / 3.0);
|
534
|
|
-
|
535
|
|
- if (current_weight > best_weight)
|
536
|
|
- {
|
537
|
|
- best_weight = current_weight;
|
538
|
|
- best = i;
|
539
|
|
-
|
540
|
|
- if (best_weight == 1.0)
|
541
|
|
- i = 1024; // break;
|
542
|
|
- }
|
543
|
|
- }
|
544
|
|
-
|
545
|
|
- return best;
|
546
|
|
-}
|
547
|
|
-
|
548
|
|
-void MtkImage::PaletteColor(int i, float rgb[3])
|
549
|
|
-{
|
550
|
|
- if (i > 0 && i < 256 && rgb && _palette)
|
551
|
|
- {
|
552
|
|
- rgb[0] = (float)_palette[i*3]/255.0;
|
553
|
|
- rgb[1] = (float)_palette[i*3+1]/255.0;
|
554
|
|
- rgb[2] = (float)_palette[i*3+2]/255.0;
|
555
|
|
- }
|
556
|
|
-}
|
557
|
|
-
|
558
|
|
-
|
559
|
|
-int MtkImage::PaletteLoadLMP(char *filename)
|
560
|
|
-{
|
561
|
|
- FILE *f;
|
562
|
|
-
|
563
|
|
-
|
564
|
|
- f = fopen(filename, "rb");
|
565
|
|
-
|
566
|
|
- if (f)
|
567
|
|
- {
|
568
|
|
- if (!_palette)
|
569
|
|
- _palette = new unsigned char[768];
|
570
|
|
-
|
571
|
|
- fread(_palette, 1, 768, f);
|
572
|
|
- fclose(f);
|
573
|
|
- return 0;
|
574
|
|
- }
|
575
|
|
-
|
576
|
|
- return -1;
|
577
|
|
-}
|
578
|
|
-
|
579
|
|
-int MtkImage::PaletteLoad(char *filename)
|
580
|
|
-{
|
581
|
|
- FILE *f;
|
582
|
|
- int i;
|
583
|
|
- unsigned int rgb[3];
|
584
|
|
-
|
585
|
|
-
|
586
|
|
- if (!filename)
|
587
|
|
- return -1;
|
588
|
|
-
|
589
|
|
- f = fopen(filename, "r");
|
590
|
|
-
|
591
|
|
- if (f)
|
592
|
|
- {
|
593
|
|
- if (_palette)
|
594
|
|
- delete [] _palette;
|
595
|
|
-
|
596
|
|
- _palette = new unsigned char[768];
|
597
|
|
-
|
598
|
|
- for (i = 0; i < 768; i+=3)
|
599
|
|
- {
|
600
|
|
- fscanf(f, "%u %u %u\n",
|
601
|
|
- &rgb[0],
|
602
|
|
- &rgb[1],
|
603
|
|
- &rgb[2]);
|
604
|
|
-
|
605
|
|
- _palette[i] = rgb[0];
|
606
|
|
- _palette[i+1] = rgb[1];
|
607
|
|
- _palette[i+2] = rgb[2];
|
608
|
|
- }
|
609
|
|
-
|
610
|
|
- fclose(f);
|
611
|
|
- return 0;
|
612
|
|
- }
|
613
|
|
-
|
614
|
|
- return 1;
|
615
|
|
-}
|
616
|
|
-
|
617
|
|
-int MtkImage::PaletteSave(char *filename)
|
618
|
|
-{
|
619
|
|
- FILE *f;
|
620
|
|
- int i;
|
621
|
|
-
|
622
|
|
-
|
623
|
|
- if (!_palette || !filename)
|
624
|
|
- return -1;
|
625
|
|
-
|
626
|
|
- f = fopen(filename, "w");
|
627
|
|
-
|
628
|
|
- if (f)
|
629
|
|
- {
|
630
|
|
- for (i = 0; i < 768; i+=3)
|
631
|
|
- {
|
632
|
|
- fprintf(f, "%i %i %i\n",
|
633
|
|
- (int)_palette[i],
|
634
|
|
- (int)_palette[i+1],
|
635
|
|
- (int)_palette[i+2]);
|
636
|
|
- }
|
637
|
|
-
|
638
|
|
- fclose(f);
|
639
|
|
- return 0;
|
640
|
|
- }
|
641
|
|
-
|
642
|
|
- return 1;
|
643
|
|
-}
|
644
|
|
-
|
645
|
|
-mtk_colormode MtkImage::Mode()
|
646
|
|
-{
|
647
|
|
- return _color_mode;
|
648
|
|
-}
|
649
|
|
-
|
650
|
|
-
|
651
|
|
-/* This code based off on gluScaleImage() */
|
652
|
|
-void MtkImage::Scale()
|
653
|
|
-{
|
654
|
|
- int i, j, k;
|
655
|
|
- float* tempin;
|
656
|
|
- float* tempout;
|
657
|
|
- float sx, sy;
|
658
|
|
- int components = 3;
|
659
|
|
- unsigned char *image;
|
660
|
|
-
|
661
|
|
-
|
662
|
|
- if (!_image || !_width || !_height)
|
663
|
|
- return;
|
664
|
|
-
|
665
|
|
- _height = NextPower(_height);
|
666
|
|
- _width = NextPower(_width);
|
667
|
|
-
|
668
|
|
- if (_height > 256)
|
669
|
|
- _height = 256;
|
670
|
|
-
|
671
|
|
- if (_width > 256)
|
672
|
|
- _width = 256;
|
673
|
|
-
|
674
|
|
-
|
675
|
|
- // Check to see if scaling is needed
|
676
|
|
- if (_height == _original_height && _width == _original_width)
|
677
|
|
- return;
|
678
|
|
-
|
679
|
|
- image = new unsigned char[_height * _width * 3];
|
680
|
|
-
|
681
|
|
- tempin = new float[_original_width * _original_height *
|
682
|
|
- components * sizeof(float)];
|
683
|
|
- tempout = new float[_width * _height * components * sizeof(float)];
|
684
|
|
-
|
685
|
|
- if (!tempout || !tempin)
|
686
|
|
- {
|
687
|
|
- if (tempout)
|
688
|
|
- delete [] tempout;
|
689
|
|
-
|
690
|
|
- if (tempin)
|
691
|
|
- delete [] tempin;
|
692
|
|
-
|
693
|
|
- printf("Oh shit out of memory!\n");
|
694
|
|
- return;
|
695
|
|
- }
|
696
|
|
-
|
697
|
|
- // Copy user data to float format.
|
698
|
|
- for (i = 0; i < _original_height * _original_width * components; i++)
|
699
|
|
- {
|
700
|
|
- tempin[i] = (float)_image[i];
|
701
|
|
- }
|
702
|
|
-
|
703
|
|
- // Determine which filter to use by checking ratios.
|
704
|
|
- if (_width > 1)
|
705
|
|
- {
|
706
|
|
- sx = (float)(_original_width - 1) / (float)(_width - 1);
|
707
|
|
- }
|
708
|
|
- else
|
709
|
|
- {
|
710
|
|
- sx = (float)(_original_width - 1);
|
711
|
|
- }
|
712
|
|
-
|
713
|
|
- if (_height > 1)
|
714
|
|
- {
|
715
|
|
- sy = (float)(_original_height - 1) / (float) (_height - 1);
|
716
|
|
- }
|
717
|
|
- else
|
718
|
|
- {
|
719
|
|
- sy = (float)(_original_height - 1);
|
720
|
|
- }
|
721
|
|
-
|
722
|
|
- if (sx < 1.0 && sy < 1.0)
|
723
|
|
- {
|
724
|
|
- /* Magnify both width and height: use weighted sample of 4 pixels */
|
725
|
|
- int i0, i1, j0, j1;
|
726
|
|
- float alpha, beta;
|
727
|
|
- float* src00;
|
728
|
|
- float* src01;
|
729
|
|
- float* src10;
|
730
|
|
- float* src11;
|
731
|
|
- float s1, s2;
|
732
|
|
- float* dst;
|
733
|
|
-
|
734
|
|
- for(i = 0; i < _height; i++)
|
735
|
|
- {
|
736
|
|
- i0 = (int)(i * sy);
|
737
|
|
- i1 = i0 + 1;
|
738
|
|
-
|
739
|
|
- if (i1 >= _original_height)
|
740
|
|
- {
|
741
|
|
- i1 = _original_height - 1;
|
742
|
|
- }
|
743
|
|
-
|
744
|
|
- alpha = i * sy - i0;
|
745
|
|
-
|
746
|
|
- for (j = 0; j < _width; j++)
|
747
|
|
- {
|
748
|
|
- j0 = (int) (j * sx);
|
749
|
|
- j1 = j0 + 1;
|
750
|
|
-
|
751
|
|
- if (j1 >= _original_width)
|
752
|
|
- {
|
753
|
|
- j1 = _original_width - 1;
|
754
|
|
- }
|
755
|
|
-
|
756
|
|
- beta = j * sx - j0;
|
757
|
|
-
|
758
|
|
- /* Compute weighted average of pixels in rect (i0,j0)-(i1,j1) */
|
759
|
|
- src00 = tempin + (i0 * _original_width + j0) * components;
|
760
|
|
- src01 = tempin + (i0 * _original_width + j1) * components;
|
761
|
|
- src10 = tempin + (i1 * _original_width + j0) * components;
|
762
|
|
- src11 = tempin + (i1 * _original_width + j1) * components;
|
763
|
|
-
|
764
|
|
- dst = tempout + (i * _width + j) * components;
|
765
|
|
-
|
766
|
|
- for (k = 0; k < components; k++)
|
767
|
|
- {
|
768
|
|
- s1 = *src00++ * (1.0 - beta) + *src01++ * beta;
|
769
|
|
- s2 = *src10++ * (1.0 - beta) + *src11++ * beta;
|
770
|
|
- *dst++ = s1 * (1.0 - alpha) + s2 * alpha;
|
771
|
|
- }
|
772
|
|
- }
|
773
|
|
- }
|
774
|
|
- }
|
775
|
|
- else
|
776
|
|
- {
|
777
|
|
- /* Shrink width and/or height: use an unweighted box filter */
|
778
|
|
- int i0, i1;
|
779
|
|
- int j0, j1;
|
780
|
|
- int ii, jj;
|
781
|
|
- float sum;
|
782
|
|
- float* dst;
|
783
|
|
-
|
784
|
|
- for (i = 0; i < _height; i++)
|
785
|
|
- {
|
786
|
|
- i0 = (int) (i * sy);
|
787
|
|
- i1 = i0 + 1;
|
788
|
|
-
|
789
|
|
- if (i1 >= _original_height)
|
790
|
|
- {
|
791
|
|
- i1 = _original_height - 1;
|
792
|
|
- }
|
793
|
|
-
|
794
|
|
- for (j = 0; j < _width; j++)
|
795
|
|
- {
|
796
|
|
- j0 = (int) (j * sx);
|
797
|
|
- j1 = j0 + 1;
|
798
|
|
-
|
799
|
|
- if (j1 >= _original_width)
|
800
|
|
- {
|
801
|
|
- j1 = _original_width - 1;
|
802
|
|
- }
|
803
|
|
-
|
804
|
|
- dst = tempout + (i * _width + j) * components;
|
805
|
|
-
|
806
|
|
- /* Compute average of pixels in the rectangle (i0,j0)-(i1,j1) */
|
807
|
|
- for (k = 0; k < components; k++)
|
808
|
|
- {
|
809
|
|
- sum = 0.0;
|
810
|
|
-
|
811
|
|
- for (ii = i0; ii <= i1; ii++)
|
812
|
|
- {
|
813
|
|
- for (jj = j0; jj <= j1; jj++)
|
814
|
|
- {
|
815
|
|
- sum += *(tempin + (ii * _original_width + jj)
|
816
|
|
- * components + k);
|
817
|
|
- }
|
818
|
|
- }
|
819
|
|
-
|
820
|
|
- sum /= ( j1 - j0 + 1 ) * ( i1 - i0 + 1 );
|
821
|
|
- *dst++ = sum;
|
822
|
|
- }
|
823
|
|
- }
|
824
|
|
- }
|
825
|
|
- }
|
826
|
|
-
|
827
|
|
- // Copy to our results.
|
828
|
|
- for( i = 0; i < _height * _width * components; i++ )
|
829
|
|
- {
|
830
|
|
- image[i] = (unsigned char) tempout[i];
|
831
|
|
- }
|
832
|
|
-
|
833
|
|
- // Delete our temp buffers.
|
834
|
|
- delete[] tempin;
|
835
|
|
- delete[] tempout;
|
836
|
|
- delete[] _image;
|
837
|
|
-
|
838
|
|
- _image = image;
|
839
|
|
-}
|
840
|
|
-
|
841
|
|
-int MtkImage::NextPower(int seed)
|
842
|
|
-{
|
843
|
|
- int i;
|
844
|
|
-
|
845
|
|
- for(i = 1; i < seed; i *= 2)
|
846
|
|
- ;
|
847
|
|
-
|
848
|
|
- return i;
|
849
|
|
-}
|
850
|
|
-
|
851
|
|
-
|
852
|
|
-
|