-
Notifications
You must be signed in to change notification settings - Fork 17
/
gtk.c
1991 lines (1865 loc) · 79.1 KB
/
gtk.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (c) 2015 Harm Hanemaaijer <fgenfb@yahoo.com>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#define _USE_MATH_DEFINES
#include <math.h>
#ifdef __GNUC__
#include <sys/time.h>
#else
#include <time.h>
#include <windows.h>
#endif
#include <gtk/gtk.h>
#include <cairo/cairo.h>
#include "texgenpack.h"
#include "decode.h"
#include "packing.h"
#include "viewer.h"
#ifndef __GNUC__
#include "strcasecmp.h"
#endif
#if GTK_MAJOR_VERSION == 2
static GtkWidget *gtk_box_new(GtkOrientation orientation, gint spacing) {
if (orientation == GTK_ORIENTATION_HORIZONTAL)
return gtk_hbox_new(FALSE, spacing);
else
return gtk_vbox_new(FALSE, spacing);
}
#endif
#if defined(_WIN32) && !defined(__GNUC__)
#if defined(_MSC_VER) || defined(_MSC_EXTENSIONS)
#define DELTA_EPOCH_IN_MICROSECS 11644473600000000Ui64
#else
#define DELTA_EPOCH_IN_MICROSECS 11644473600000000ULL
#endif
static void gettimeofday(struct timeval *tv, struct timezone *tz) {
FILETIME ft;
uint64_t tmpres = 0;
if (NULL != tv) {
GetSystemTimeAsFileTime(&ft);
tmpres |= ft.dwHighDateTime;
tmpres <<= 32;
tmpres |= ft.dwLowDateTime;
tmpres /= 10; /*convert into microseconds*/
/*converting file time to unix epoch*/
tmpres -= DELTA_EPOCH_IN_MICROSECS;
tv->tv_sec = (long)(tmpres / 1000000UL);
tv->tv_usec = (long)(tmpres % 1000000UL);
}
}
#endif
/*
* A note about internal image storage:
*
* RGB(A) images are stored in cairo's AGRB8 format.
* half-float images are stored in regular RGBA half-float format.
* 8-bit and 16-bit images with two or less components are stored in native (non-cairo) format.
*/
#define SET_BORDER_WIDTH 16.0
typedef struct {
int index;
GtkWidget *image_drawing_area;
cairo_surface_t *surface;
cairo_surface_t *base_surface[2];
double area_width, area_height;
} Window;
static GtkWidget *gtk_window;
static Window *window;
static int GUI_initialized = 0;
static int window_width = 1024;
static int window_height = 800;
static double zoom = 1.0;
static double zoom_origin_x = 0;
static double zoom_origin_y = 0;
static int difference_mode = 0;
static int compression_active = 0;
static gboolean delete_event_cb(GtkWidget *widget, GdkEvent *event, gpointer data) {
return FALSE;
}
static void destroy_cb(GtkWidget *widget, gpointer data) {
gtk_main_quit();
exit(0);
}
static gboolean area_configure_event_cb(GtkWidget *widget, GdkEventConfigure *event, Window *window) {
if (!GUI_initialized) {
return TRUE;
}
window->area_width = event->width;
window->area_height = event->height;
cairo_surface_destroy(window->surface);
for (int i = 0; i < current_nu_image_sets; i++)
gui_create_base_surface(i);
window->surface = cairo_image_surface_create(CAIRO_FORMAT_RGB24, window->area_width, window->area_height);
gui_draw_window();
/* We've handled the configure event, no need for further processing. */
return TRUE;
}
#if GTK_MAJOR_VERSION == 2
static gboolean area_expose_cb(GtkWidget *widget, GdkEventExpose *event, Window *window) {
cairo_t *cr = gdk_cairo_create(widget->window);
cairo_set_source_surface(cr, window->surface, 0, 0);
cairo_paint(cr);
return TRUE;
}
#else
static gboolean area_draw_cb(GtkWidget *widget, cairo_t *cr, Window *window) {
cairo_set_source_surface(cr, window->surface, 0, 0);
cairo_paint(cr);
return TRUE;
}
#endif
static void gui_update_drawing_area(Window *window) {
gtk_widget_queue_draw(GTK_WIDGET(window->image_drawing_area));
}
void gui_initialize(int *argc, char ***argv) {
gtk_init(argc, argv);
}
static double last_press_event_x = 0;
static double last_press_event_y = 0;
static double last_press_time;
static gboolean area_button_press_event_cb(GtkWidget *widget, GdkEventButton *event, Window *window) {
if (event->button == 1) {
last_press_event_x = event->x;
last_press_event_y = event->y;
struct timeval tv;
gettimeofday(&tv, NULL);
last_press_time = tv.tv_sec * 1000000 + tv.tv_usec;
}
return FALSE;
}
static gboolean area_button_release_event_cb(GtkWidget *widget, GdkEventButton *event, Window *window) {
if (event->button == 1) {
struct timeval tv;
gettimeofday(&tv, NULL);
double release_time = tv.tv_sec * 1000000 + tv.tv_usec;
double dx = event->x - last_press_event_x;
double dy = event->y - last_press_event_y;
if (release_time - last_press_time < 100000 || (dx * dx + dy * dy < 100)) {
// Click centers on mouse position.
double borders_x = 16 + SET_BORDER_WIDTH * (current_nu_image_sets - 1);
double image_set_width = (window->area_width - borders_x) / current_nu_image_sets;
// Figure out on which image set the click is.
int j = - 1;
for (int i = 0; i < current_nu_image_sets; i++) {
double start_x = 8 + ((window->area_width - borders_x) / current_nu_image_sets
+ SET_BORDER_WIDTH) * i;
if (event->x >= start_x && event->x < start_x + image_set_width)
j = i;
}
if (j == - 1)
// Click on the border.
return FALSE;
double center_x = 8 + ((window->area_width - borders_x) / current_nu_image_sets +
SET_BORDER_WIDTH) * j + (image_set_width / 2);
double center_y = 32 + (window->area_height - 32 - 8) / 2;
zoom_origin_x -= (event->x - center_x); // * zoom;
zoom_origin_y -= (event->y - center_y); // * zoom;
}
else {
// Pan moves image.
zoom_origin_x += dx;
zoom_origin_y += dy;
}
gui_draw_and_show_window();
}
return FALSE;
}
static gboolean area_scroll_event_cb(GtkWidget *widget, GdkEventScroll *event, Window *window) {
double borders_x = 16 + SET_BORDER_WIDTH * (current_nu_image_sets - 1);
double image_set_width = (window->area_width - borders_x) / current_nu_image_sets;
double center_x = (image_set_width / 2);
double center_y = (window->area_height - 32 - 8) / 2;
if (event->direction == GDK_SCROLL_UP && zoom < 32) {
// Zoom in.
// Center defined by
// zoom * x_orig + origin_x = area_width / 2
// zoom * y_orig + origin_y = area_height / 2
// new_zoom * x_orig + new_origin_x = area_width / 2
// new_zoom * y_orig + new_origin_y = area_height / 2
//
zoom_origin_x = center_x - 1.2 * (center_x - zoom_origin_x);
zoom_origin_y = center_y - 1.2 * (center_y - zoom_origin_y);
zoom *= 1.2;
}
else
if (event->direction == GDK_SCROLL_DOWN && zoom > (double)1 / 32) {
// Zoom out.
zoom_origin_x = center_x - (1 / 1.2) * (center_x - zoom_origin_x);
zoom_origin_y = center_y - (1 / 1.2) * (center_y - zoom_origin_y);
zoom *= 1 / 1.2;
}
else
return FALSE;
gui_draw_and_show_window();
return FALSE;
}
void gui_zoom_fit_to_window() {
zoom_origin_x = zoom_origin_y = 0;
// Determine the scaling factor so that the image fits neatly within the window.
double factorx, factory;
double max_w = 0;
double h = 0;
for (int i = 0; i < current_nu_image_sets; i++) {
if (current_nu_mipmaps[i] > 1) {
if (current_image[i][0].width + 8 + current_image[i][0].width / 2 > max_w)
max_w = current_image[i][0].width + 8 + current_image[i][0].width / 2;
}
else
if (current_image[i][0].width > max_w)
max_w = current_image[i][0].width;
if (current_image[i][0].height > h)
h = current_image[i][0].height;
}
double w = max_w * current_nu_image_sets;
factorx = ((double)window->area_width - 16 + SET_BORDER_WIDTH * (current_nu_image_sets - 1)) / w;
factory = ((double)window->area_height - 32 - 8) / h;
double factor;
if (factorx > factory) {
factor = factory;
}
else {
factor = factorx;
}
zoom = factor;
}
static void menu_item_reset_zoom_activate_cb(GtkMenuItem *menu_item, gpointer data) {
gui_zoom_fit_to_window();
gui_draw_and_show_window();
}
static void menu_item_reset_zoom_real_size_activate_cb(GtkMenuItem *menu_item, gpointer data) {
zoom = 1.0;
zoom_origin_x = zoom_origin_y = 0;
gui_draw_and_show_window();
}
static void check_menu_item_show_difference_toggled_cb(GtkMenuItem *menu_item, gpointer data) {
if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(menu_item)))
difference_mode = 1;
else
difference_mode = 0;
gui_create_base_surface(1);
gui_draw_and_show_window();
}
static void check_menu_item_hdr_toggled_cb(GtkMenuItem *menu_item, gpointer data) {
if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(menu_item)))
option_hdr = 1;
else
option_hdr = 0;
}
static void menu_item_generate_mipmaps_activate_cb(GtkMenuItem *menu_item, gpointer data) {
if (compression_active)
return;
for (int i = 1; i < current_nu_mipmaps[0]; i++)
free(current_image[0][i].pixels);
current_nu_mipmaps[0] = 1;
int n = count_mipmap_levels(¤t_image[0][0]);
for (int i = 1; i < n; i++) {
generate_mipmap_level_from_previous_level(¤t_image[0][i - 1], ¤t_image[0][i]);
}
current_nu_mipmaps[0] = n;
gui_zoom_fit_to_window();
gui_create_base_surface(0);
gui_draw_and_show_window();
}
static int mipmap_level_being_compressed;
static int stop_compression;
static double compress_start_time;
static double last_compress_progress_time;
static double compress_progress; // [0, 1]
static const double compression_update_frequency_table[5] =
{ 1.0 / 60.0, 0.1, 1, 10.0, 1E40 };
// Update screen at most every 1.0 seconds during compression by default.
// (Some GTK implementations, such as under Windows, may be slow, while GUI updates
// for large textures may impact compression speed on any system since the whole
// texture is potentially scaled/redrawn with cairo).
#ifdef _WIN32
static int compression_update_frequency_selection = 2;
#else
static int compression_update_frequency_selection = 2;
#endif
// Calculate progress and return true if the window needs to be updated.
static int current_pass;
static int calculate_progress(Texture *texture, int compressed_block_index, int pass) {
struct timeval tv;
gettimeofday(&tv, NULL);
double current_time = tv.tv_sec + tv.tv_usec * 0.000001;
if (pass == 2 && current_pass == 1) {
compress_start_time = current_time;
last_compress_progress_time = current_time;
current_pass = 2;
}
if (current_time - last_compress_progress_time >=
compression_update_frequency_table[compression_update_frequency_selection]) {
compress_progress = (double)compressed_block_index /
((texture->extended_height / texture->block_height) *
(texture->extended_width / texture->block_width));
last_compress_progress_time = current_time;
return 1;
}
return 0;
}
// This function is called by the compress function after each compressed block.
static void compress_callback(BlockUserData *user_data) {
Texture *texture = user_data->texture;
int x = user_data->x_offset;
int y = user_data->y_offset;
// Decompress the block.
uint32_t image_buffer[16];
int compressed_block_index = (y / texture->block_height) * (texture->extended_width / texture->block_width)
+ x / texture->block_width;
int flags = 0;
if (texture->type == TEXTURE_TYPE_BPTC)
flags = BPTC_MODE_ALLOWED_ALL;
else if (texture->type & TEXTURE_TYPE_ETC_BIT)
flags = ETC2_MODE_ALLOWED_ALL;
texture->decoding_function((unsigned char *)&texture->pixels[compressed_block_index *
(texture->info->internal_bits_per_block / 32)], image_buffer, flags);
// Copy it to image[1].
int h = 4;
if (y + h > current_image[1][mipmap_level_being_compressed].height)
h = current_image[1][mipmap_level_being_compressed].height - y;
int w = 4;
if (x + w > current_image[1][mipmap_level_being_compressed].width)
w = current_image[1][mipmap_level_being_compressed].width - x;
for (int i = 0; i < h; i++)
for (int j = 0; j < w; j++) {
uint32_t pixel = image_buffer[i * 4 + j];
// Put the pixel in cairo's ARGB32 format.
current_image[1][mipmap_level_being_compressed].pixels[(y + i) *
current_image[1][mipmap_level_being_compressed].extended_width + (x + j)] =
pack_rgba(pixel_get_b(pixel), pixel_get_g(pixel), pixel_get_r(pixel),
pixel_get_a(pixel));
}
if (calculate_progress(texture, compressed_block_index, user_data->pass)) {
gui_create_base_surface(1);
gui_draw_and_show_window();
}
gui_handle_events();
if (stop_compression)
user_data->stop_signalled = 1;
}
// This function is called by the compress function after each compressed block. Half-float version.
static void compress_half_float_callback(BlockUserData *user_data) {
Texture *texture = user_data->texture;
int x = user_data->x_offset;
int y = user_data->y_offset;
// Decompress the block.
uint64_t image_buffer[16]; // 64-bit pixels
int compressed_block_index = (y / texture->block_height) * (texture->extended_width / texture->block_width)
+ x / texture->block_width;
texture->decoding_function((unsigned char *)&texture->pixels[compressed_block_index *
(texture->info->internal_bits_per_block / 32)], (unsigned int *)image_buffer,
BPTC_FLOAT_MODE_ALLOWED_ALL);
// Copy it to image[1].
int h = 4;
if (y + h > current_image[1][mipmap_level_being_compressed].height)
h = current_image[1][mipmap_level_being_compressed].height - y;
int w = 4;
if (x + w > current_image[1][mipmap_level_being_compressed].width)
w = current_image[1][mipmap_level_being_compressed].width - x;
for (int i = 0; i < h; i++)
for (int j = 0; j < w; j++) {
uint64_t pixel64 = image_buffer[i * 4 + j];
*(uint64_t *)¤t_image[1][mipmap_level_being_compressed].pixels[((y + i) *
current_image[1][mipmap_level_being_compressed].extended_width + (x + j)) * 2] =
pixel64;
}
if (calculate_progress(texture, compressed_block_index, user_data->pass)) {
gui_create_base_surface(1);
gui_draw_and_show_window();
}
gui_handle_events();
if (stop_compression)
user_data->stop_signalled = 1;
}
// This function is called by the compress function after each compressed block. 16-bit component version.
static void compress_rg16_callback(BlockUserData *user_data) {
Texture *texture = user_data->texture;
int x = user_data->x_offset;
int y = user_data->y_offset;
// Decompress the block.
uint32_t image_buffer[16];
int compressed_block_index = (y / texture->block_height) * (texture->extended_width / texture->block_width)
+ x / texture->block_width;
texture->decoding_function((unsigned char *)&texture->pixels[compressed_block_index *
(texture->info->internal_bits_per_block / 32)], image_buffer, 0);
// Copy it to image[1].
int h = 4;
if (y + h > current_image[1][mipmap_level_being_compressed].height)
h = current_image[1][mipmap_level_being_compressed].height - y;
int w = 4;
if (x + w > current_image[1][mipmap_level_being_compressed].width)
w = current_image[1][mipmap_level_being_compressed].width - x;
for (int i = 0; i < h; i++)
for (int j = 0; j < w; j++) {
uint32_t pixel = image_buffer[i * 4 + j];
current_image[1][mipmap_level_being_compressed].pixels[(y + i) *
current_image[1][mipmap_level_being_compressed].extended_width + (x + j)] =
pixel;
}
if (calculate_progress(texture, compressed_block_index, user_data->pass)) {
gui_create_base_surface(1);
gui_draw_and_show_window();
}
gui_handle_events();
if (stop_compression)
user_data->stop_signalled = 1;
}
// This function is called by the compress function after each compressed block. 8-bit component version.
static void compress_rg8_callback(BlockUserData *user_data) {
compress_rg16_callback(user_data);
}
static void popup_message(const char *message) {
GtkWidget *dialog = gtk_message_dialog_new(GTK_WINDOW(gtk_window), (GtkDialogFlags)(GTK_DIALOG_MODAL |
GTK_DIALOG_DESTROY_WITH_PARENT), GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE, message);
gtk_dialog_run(GTK_DIALOG(dialog));
gtk_widget_destroy(dialog);
}
static void menu_item_compress_activate_cb(GtkMenuItem *menu_item, gpointer data) {
if (compression_active)
return;
int format = TEXTURE_TYPE_ETC1;
if (option_texture_format != - 1)
format = option_texture_format;
// Compression from half-float source to RGBA texture type is now allowed.
// if (current_image[0][0].is_half_float && !(format & TEXTURE_TYPE_HALF_FLOAT_BIT)) {
// popup_message("Error -- texture compression format is unsuited for half-float images. "
// "Use a half-float compression format or convert the image to regular format.");
// return;
// }
if (!current_image[0][0].is_half_float && (format & TEXTURE_TYPE_HALF_FLOAT_BIT)) {
popup_message("Error -- half-float texture compression format is unsuited for regular images. "
"Convert the image to half-float format or use a regular RGB texture format.");
return;
}
if (current_image[0][0].bits_per_component != 16 && (format & TEXTURE_TYPE_16_BIT_COMPONENTS_BIT)) {
popup_message("Error -- 16-bit component texture compression is unsuited for regular images or "
"images with 8-bit components.");
return;
}
// Compression from 16-bit source to 8-bit texture type is now allowed.
// if (current_image[0][0].bits_per_component == 16 && !(format & TEXTURE_TYPE_16_BIT_COMPONENTS_BIT)) {
// if (!current_image[0][0].is_half_float) {
// popup_message("Error -- texture compression format is unsuited for 16-bit component images.");
// return;
// }
// }
TextureInfo *info = match_texture_type(format);
if (current_image[0][0].nu_components <= 2 && current_image[0][0].nu_components != info->nu_components) {
popup_message("Error -- mismatch in the number of components.");
return;
}
if (current_image[0][0].nu_components >= 3 && info->nu_components < 3) {
popup_message("Error -- mismatch in the number of components.");
return;
}
if ((current_image[0][0].is_signed && !(format & TEXTURE_TYPE_SIGNED_BIT)) ||
(!(current_image[0][0].is_signed) && (format & TEXTURE_TYPE_SIGNED_BIT))) {
popup_message("Error -- image type signedness does not match texture type.");
return;
}
compression_active = 1;
stop_compression = 0;
destroy_image_set(1);
current_file_type[1] = FILE_TYPE_UNDEFINED;
for (int i = 0; i < current_nu_mipmaps[0]; i++) {
// Allocate and clear image[1][i].
current_image[1][i] = current_image[0][i]; // Copy fields.
if (current_image[0][i].is_half_float && !(format & TEXTURE_TYPE_HALF_FLOAT_BIT)) {
current_image[1][i].is_half_float = 0;
current_image[1][i].bits_per_component = 8;
if (current_image[0][i].alpha_bits > 0)
current_image[1][i].alpha_bits = 8;
}
if (current_image[0][i].bits_per_component == 16 && !(format & TEXTURE_TYPE_16_BIT_COMPONENTS_BIT)) {
current_image[1][i].bits_per_component = 8;
}
if (current_image[0][i].nu_components > info->nu_components)
// If the source image has more components (such as alpha) but the texture format does not,
// exclude the extra components from the target image.
current_image[1][i].nu_components = info->nu_components;
if (current_image[1][i].alpha_bits > 0 && (format & TEXTURE_TYPE_ALPHA_BIT) == 0)
current_image[1][i].alpha_bits = 0;
int bpp = 4;
if (current_image[1][i].is_half_float)
bpp = 8;
current_image[1][i].pixels = (unsigned int *)malloc(current_image[1][i].height *
current_image[1][i].extended_width * bpp);
clear_image(¤t_image[1][i]);
current_nu_image_sets = 2;
current_nu_mipmaps[1] = i + 1;
gui_zoom_fit_to_window();
mipmap_level_being_compressed = i;
struct timeval tv;
gettimeofday(&tv, NULL);
compress_start_time = tv.tv_sec + tv.tv_usec * 0.000001;
compress_progress = 0;
last_compress_progress_time = compress_start_time;
current_pass = 1;
if (format & TEXTURE_TYPE_ASTC_BIT) {
// Make sure the window shows compression has started.
current_file_type[1] = FILE_TYPE_IMAGE_UNKNOWN;
gui_create_base_surface(1);
gui_draw_and_show_window();
gui_handle_events();
current_file_type[1] = FILE_TYPE_UNDEFINED;
}
if (current_image[1][i].bits_per_component == 16 && !current_image[1][i].is_half_float) {
// The image is in r16 or rg16 format, or the signed version.
compress_image(¤t_image[0][i], format, compress_rg16_callback,
¤t_texture[1][i], 0, 0, 0);
}
else
if (current_image[1][i].bits_per_component == 8 && current_image[1][i].nu_components <= 2) {
// The image is in r8 or rg8 format, or signed r8 or signed rg8.
compress_image(¤t_image[0][i], format, compress_rg8_callback,
¤t_texture[1][i], 0, 0, 0);
}
else
if (!current_image[1][i].is_half_float && current_image[0][i].is_half_float) {
// The source image is in half-float format, the destination is in regular RGB(A)8 format.
compress_image(¤t_image[0][i], format, compress_callback,
¤t_texture[1][i], 0, 0, 0);
}
else
if (!current_image[1][i].is_half_float) {
// The image is a regular RGB(A)8 image.
// Create a temporary image in non-cairo format to compress from.
Image image;
image = current_image[0][i];
image.pixels = (unsigned int *)malloc(current_image[0][i].height * current_image[0][i].extended_width * 4);
memcpy(image.pixels, current_image[0][i].pixels, current_image[0][i].height *
current_image[0][i].extended_width * 4);
convert_image_to_or_from_cairo_format(&image);
compress_image(&image, format, compress_callback, ¤t_texture[1][i], 0, 0, 0);
free(image.pixels);
}
else {
// The image is in half-float format.
compress_image(¤t_image[0][i], format, compress_half_float_callback,
¤t_texture[1][i], 0, 0, 0);
}
// Uncompressed textures didn't generate compress_callback so the image hasn't been filled in.
if (current_texture[1][i].type & TEXTURE_TYPE_UNCOMPRESSED_BIT) {
convert_texture_to_image(¤t_texture[1][i], ¤t_image[1][i]);
if (!(current_texture[1][i].type & TEXTURE_TYPE_HALF_FLOAT_BIT))
convert_image_to_or_from_cairo_format(¤t_image[1][i]);
}
if (current_texture[1][i].type & TEXTURE_TYPE_ASTC_BIT) {
// ASTC compression is handled via a system call to astenc.
// This generates a half-float image.
convert_texture_to_image(¤t_texture[1][i], ¤t_image[1][i]);
// If the first image (on the left) is in regular RGB, convert the second image to RGB.
if (!current_image[0][0].is_half_float) {
convert_image_from_half_float(¤t_image[1][i], 0, 1.0, 1.0);
convert_image_to_or_from_cairo_format(¤t_image[1][i]);
}
}
// For two RGB(A)8 images, the comparison will be performed in cairo's pixel format,
// which doesn't matter for the RMSE calculation (R and B swapped in both images).
// However, if the first image is in half-float format and the second in RGB(A)8 cairo
// format, we have to temporarily convert the second image to non-cairo format for the
// comparison.
if (!current_image[1][i].is_half_float && current_image[0][i].is_half_float) {
convert_image_to_or_from_cairo_format(¤t_image[1][i]);
current_rmse[i] = compare_images(¤t_image[0][i], ¤t_image[1][i]);
convert_image_to_or_from_cairo_format(¤t_image[1][i]);
}
else
current_rmse[i] = compare_images(¤t_image[0][i], ¤t_image[1][i]);
if (stop_compression)
break;
}
compression_active = 0;
gui_create_base_surface(1);
gui_draw_and_show_window();
}
static void menu_item_stop_compression_activate_cb(GtkMenuItem *menu_item, gpointer data) {
stop_compression = 1;
}
static void menu_item_flip_vertical_right_activate_cb(GtkMenuItem *menu_item, gpointer data) {
if (compression_active)
return;
for (int i = 0; i < current_nu_mipmaps[1]; i++)
flip_image_vertical(¤t_image[1][i]);
gui_create_base_surface(1);
gui_draw_and_show_window();
}
static GtkWidget *file_save_dialog;
static char *current_filename = NULL;
static void gui_save_texture(int image_index) {
const char *extension;
TextureInfo *info = match_texture_type(current_texture[image_index][0].type);
// Guess the preferred texture container format.
// Check whether saving/loading is supported for KTX or DDS.
// Usually DDS will be preferable for DXT1/3/5.
if (current_texture[image_index][0].type & TEXTURE_TYPE_DXTC_BIT)
extension = "dds";
// Otherwise prioritize depending on the platform.
#ifdef _WIN32
else if (info->dds_support)
extension = "dds";
else if (info->ktx_support)
extension = "ktx";
#else
else if (info->ktx_support)
extension = "ktx";
else if (info->dds_support)
extension = "dds";
#endif
// KTX supports most formats (fail-safe).
else
extension = "ktx";
char *filename;
if (current_filename == NULL)
filename = strdup("untitled.EXT");
else {
int n = strlen(current_filename);
int i;
for (i = n - 1; i > 0; i--)
if (current_filename[i] == '.')
break;
if (i == 0)
i = n;
filename = (char *)malloc(i + 4 + 1);
strncpy(filename, current_filename, i);
strcpy(&filename[i], ".EXT");
}
strcpy(&filename[strlen(filename) - 3], extension);
gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(file_save_dialog),
filename);
free(filename);
again : ;
int r = gtk_dialog_run(GTK_DIALOG(file_save_dialog));
if (r == GTK_RESPONSE_ACCEPT) {
char *filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(file_save_dialog));
int type = determine_filename_type(filename);
if (type & FILE_TYPE_TEXTURE_BIT) {
if (type == FILE_TYPE_KTX)
if (!match_texture_type(current_texture[image_index][0].type)->ktx_support) {
popup_message("Texture format not supported by KTX file.");
g_free(filename);
goto again;
}
if (type == FILE_TYPE_DDS)
if (!match_texture_type(current_texture[image_index][0].type)->dds_support) {
popup_message("Texture format not supported by DDS file.");
g_free(filename);
goto again;
}
if (type == FILE_TYPE_PKM)
if (current_texture[image_index][0].type != TEXTURE_TYPE_ETC1) {
popup_message("Texture format not supported by PKM file.");
g_free(filename);
goto again;
}
if (type == FILE_TYPE_ASTC)
if (!(current_texture[image_index][0].type & TEXTURE_TYPE_ASTC_BIT)) {
popup_message("Texture format not supported by ASTC file.");
g_free(filename);
goto again;
}
save_texture(¤t_texture[image_index][0], current_nu_mipmaps[image_index],
filename, type);
}
else
if (type & FILE_TYPE_IMAGE_BIT) {
convert_image_to_or_from_cairo_format(¤t_image[image_index][0]);
save_image(¤t_image[image_index][0], filename, type);
convert_image_to_or_from_cairo_format(¤t_image[image_index][0]);
}
else {
popup_message("Can only save to filename with an extension corresponding to "
" a KTX, DDS, PKM or PNG file.");
g_free(filename);
goto again;
}
g_free(filename);
}
gtk_widget_hide(file_save_dialog);
}
static void gui_save_image(int image_index) {
if (current_image[image_index][0].is_half_float ||
current_image[image_index][0].bits_per_component != 8) {
popup_message("Cannot save image to PNG file (need 8-bit components).");
return;
}
if (!(current_image[image_index][0].nu_components == 1 ||
current_image[image_index][0].nu_components == 3)) {
popup_message("Cannot save image to PNG file "
"(must have one or three components, or three with alpha).");
return;
}
const char *extension;
extension = "png";
char *filename;
if (current_filename == NULL)
filename = strdup("untitled.EXT");
else {
int n = strlen(current_filename);
int i;
for (i = n - 1; i > 0; i--)
if (current_filename[i] == '.')
break;
if (i == 0)
i = n;
filename = (char *)malloc(i + 4 + 1);
strncpy(filename, current_filename, i);
strcpy(&filename[i], ".EXT");
}
strcpy(&filename[strlen(filename) - 3], extension);
gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(file_save_dialog),
filename);
free(filename);
again : ;
int r = gtk_dialog_run(GTK_DIALOG(file_save_dialog));
if (r == GTK_RESPONSE_ACCEPT) {
char *filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(file_save_dialog));
int type = determine_filename_type(filename);
if (type & FILE_TYPE_TEXTURE_BIT) {
popup_message("Can't save source image to texture file (convert to result first).");
g_free(filename);
goto again;
}
else if (type & FILE_TYPE_IMAGE_BIT) {
convert_image_to_or_from_cairo_format(¤t_image[image_index][0]);
save_image(¤t_image[image_index][0], filename, type);
convert_image_to_or_from_cairo_format(¤t_image[image_index][0]);
}
else {
popup_message("Can only save to filename with an extension corresponding to "
" a PNG file.");
g_free(filename);
goto again;
}
g_free(filename);
}
gtk_widget_hide(file_save_dialog);
}
static void menu_item_save_source_activate_cb(GtkMenuItem *menu_item, gpointer data) {
if (current_nu_image_sets < 1) {
GtkWidget *dialog = gtk_message_dialog_new(GTK_WINDOW(gtk_window),
(GtkDialogFlags)(GTK_DIALOG_MODAL |
GTK_DIALOG_DESTROY_WITH_PARENT), GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
"No image defined.");
gtk_dialog_run(GTK_DIALOG(dialog));
gtk_widget_destroy(dialog);
return;
}
gui_save_image(0);
}
static void menu_item_save_result_activate_cb(GtkMenuItem *menu_item, gpointer data) {
if (compression_active)
return;
if (current_nu_image_sets < 2) {
GtkWidget *dialog = gtk_message_dialog_new(GTK_WINDOW(gtk_window), (GtkDialogFlags)(GTK_DIALOG_MODAL |
GTK_DIALOG_DESTROY_WITH_PARENT), GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
"No image or texture defined as second image.");
gtk_dialog_run(GTK_DIALOG(dialog));
gtk_widget_destroy(dialog);
return;
}
gui_save_texture(1);
}
static GtkWidget *file_load_dialog;
static void menu_item_load_activate_cb(GtkMenuItem *menu_item, gpointer data) {
if (compression_active)
return;
again : ;
int r = gtk_dialog_run(GTK_DIALOG(file_load_dialog));
if (r == GTK_RESPONSE_ACCEPT) {
char *filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(file_load_dialog));
int type = determine_filename_type(filename);
if (type & FILE_TYPE_TEXTURE_BIT) {
destroy_image_set(0);
current_nu_mipmaps[0] = load_texture(filename, type, 32, ¤t_texture[0][0]);
for (int i = 0 ; i < current_nu_mipmaps[0]; i++) {
convert_texture_to_image(¤t_texture[0][i], ¤t_image[0][i]);
if (!(current_texture[0][i].type & TEXTURE_TYPE_HALF_FLOAT_BIT))
convert_image_to_or_from_cairo_format(¤t_image[0][i]);
}
current_file_type[0] = type;
if (current_nu_image_sets == 0)
current_nu_image_sets = 1;
}
else
if (type & FILE_TYPE_IMAGE_BIT) {
destroy_image_set(0);
load_image(filename, type, ¤t_image[0][0]);
current_nu_mipmaps[0] = 1;
if (current_image[0][0].bits_per_component == 8 && current_image[0][0].nu_components > 2)
convert_image_to_or_from_cairo_format(¤t_image[0][0]);
current_file_type[0] = type;
if (current_nu_image_sets == 0)
current_nu_image_sets = 1;
}
else {
GtkWidget *dialog = gtk_message_dialog_new(GTK_WINDOW(gtk_window), (GtkDialogFlags)(GTK_DIALOG_MODAL |
GTK_DIALOG_DESTROY_WITH_PARENT), GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
"Can only load a file with an extension corresponding to a "
"KTX, DDS, PKM or PNG file.");
gtk_dialog_run(GTK_DIALOG(dialog));
gtk_widget_destroy(dialog);
g_free(filename);
goto again;
}
const char *title_str = "texgenpack-gui texture conversion utility";
char *s = (char *)malloc(strlen(filename) + strlen(title_str) + 3 + 1);
sprintf(s, "%s (%s)", title_str, filename);
gtk_window_set_title(GTK_WINDOW(gtk_window), s);
free(s);
if (current_filename != NULL)
free(current_filename);
current_filename = strdup(filename);
g_free(filename);
current_rmse[0] = - 1.0;
gui_zoom_fit_to_window();
gui_create_base_surface(0);
gui_create_base_surface(1);
gui_draw_and_show_window();
}
gtk_widget_hide(file_load_dialog);
}
static void menu_item_clear_activate_cb(GtkMenuItem *menu_item, gpointer data) {
if (compression_active)
return;
if (current_nu_image_sets < 2)
return;
destroy_image_set(1);
current_nu_image_sets = 1;
gui_zoom_fit_to_window();
gui_draw_and_show_window();
}
static void menu_item_quit_activate_cb(GtkMenuItem *menu_item, gpointer data) {
gtk_main_quit();
exit(0);
}
static void menu_item_flip_vertical_left_activate_cb(GtkMenuItem *menu_item, gpointer data) {
if (compression_active)
return;
for (int i = 0; i < current_nu_mipmaps[0]; i++)
flip_image_vertical(¤t_image[0][i]);
gui_create_base_surface(0);
gui_draw_and_show_window();
}
static void menu_item_remove_alpha_activate_cb(GtkMenuItem *menu_item, gpointer data) {
if (compression_active)
return;
if (current_image[0][0].alpha_bits == 0) {
popup_message("Image doesn't have alpha component.");
return;
}
// Free pixel buffers associated with current_texture[0][i].
if (current_file_type[0] & FILE_TYPE_TEXTURE_BIT)
for (int i = 0; i < current_nu_mipmaps[0]; i++)
free(current_texture[0][i].pixels);
current_file_type[0] = FILE_TYPE_IMAGE_UNKNOWN;
for (int i = 0; i < current_nu_mipmaps[0]; i++) {
remove_alpha_from_image(¤t_image[0][i]);
}
gui_create_base_surface(0);
gui_draw_and_show_window();
}
static void menu_item_add_alpha_activate_cb(GtkMenuItem *menu_item, gpointer data) {
if (compression_active)
return;
if (current_image[0][0].alpha_bits > 0) {
popup_message("Image already has an alpha component.");
return;
}
// Free pixel buffers associated with current_texture[0][i].
if (current_file_type[0] & FILE_TYPE_TEXTURE_BIT)
for (int i = 0; i < current_nu_mipmaps[0]; i++)
free(current_texture[0][i].pixels);
current_file_type[0] = FILE_TYPE_IMAGE_UNKNOWN;
for (int i = 0; i < current_nu_mipmaps[0]; i++) {
add_alpha_to_image(¤t_image[0][i]);
}
gui_create_base_surface(0);
gui_draw_and_show_window();
}
static void menu_item_convert_from_half_float_activate_cb(GtkMenuItem *menu_item, gpointer data) {
if (compression_active)
return;
if (!current_image[0][0].is_half_float) {
popup_message("Image is not in half-float format.");
return;
}
// Free pixel buffers associated with current_texture[0][i].
if (current_file_type[0] & FILE_TYPE_TEXTURE_BIT)
for (int i = 0; i < current_nu_mipmaps[0]; i++)
free(current_texture[0][i].pixels);
current_file_type[0] = FILE_TYPE_IMAGE_UNKNOWN;
for (int i = 0; i < current_nu_mipmaps[0]; i++) {
// Convert from half_float to RGB.
convert_image_from_half_float(¤t_image[0][i], 0, 1.0, 1.0);
if (current_image[0][i].nu_components >= 3)
// Convert to cairo format.
convert_image_to_or_from_cairo_format(¤t_image[0][i]);
}
gui_create_base_surface(0);
gui_draw_and_show_window();
}
static void menu_item_convert_to_half_float_activate_cb(GtkMenuItem *menu_item, gpointer data) {
if (compression_active)
return;
if (current_image[0][0].is_half_float || (current_image[0][0].nu_components <= 2 &&
current_image[0][0].is_signed)) {
popup_message("Image is not in regular RGB format or unsigned 8-bit or 16-bit component format.");
return;
}
// Free pixel buffers associated with current_texture[0][i].
if (current_file_type[0] & FILE_TYPE_TEXTURE_BIT)
for (int i = 0; i < current_nu_mipmaps[0]; i++)
free(current_texture[0][i].pixels);
current_file_type[0] = FILE_TYPE_IMAGE_UNKNOWN;