-
Notifications
You must be signed in to change notification settings - Fork 4
/
vera_tileset.c
1455 lines (1230 loc) · 35.2 KB
/
vera_tileset.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
#include <libgimp/gimp.h>
#include <libgimp/gimpui.h>
#include <errno.h>
#include <string.h>
#include <stdio.h>
#include <libxml/encoding.h>
#include <libxml/xmlwriter.h>
#define SAVE_PROC "file-vera-save"
#define PLUG_IN_BINARY "file-vera"
#define VERA_DEFAULTS_PARASITE "vera-save-defaults"
#define VERA_COLORMAP_CONVERT "plug-in-vera-colormap-convert"
static void query(void);
static void run(const gchar *name,
gint nparams,
const GimpParam *param,
gint *nreturn_vals,
GimpParam **return_vals);
static gboolean save_tile_set(const gchar *filename,
gint32 image_id,
gint32 drawable_id,
GError **error);
static gboolean save_bitmap(const gchar *filename,
gint32 image_id,
gint32 drawable_id,
GError **error);
static gboolean save_tsx(const gchar *filename,
const gchar *bmp_filename,
GimpRunMode run_mode,
gint32 image_id,
gint32 drawable_id,
GError **error);
static gboolean save_palette(const gchar *filename,
const guchar *cmap,
const gint palsize,
GError **error);
static void shift_color_map(guchar* orig,
guchar** shifted,
gint palsize,
gint offset);
typedef enum
{
TILESET = 0,
BITMAP = 1
} VeraExport;
typedef enum
{
TILE_1BPP = 1,
TILE_2BPP = 2,
TILE_4BPP = 4,
TILE_8BPP = 8
} TileBpp;
typedef enum
{
TILE_WIDTH_8 = 8,
TILE_WIDTH_16 = 16,
TILE_WIDTH_32 = 32,
TILE_WIDTH_64 = 64
} TileWidth;
typedef enum
{
TILE_HEIGHT_8 = 8,
TILE_HEIGHT_16 = 16,
TILE_HEIGHT_32 = 32,
TILE_HEIGHT_64 = 64
} TileHeight;
typedef struct
{
gboolean file_header;
VeraExport export_type; /* tileset or bitmap */
TileBpp tile_bpp; /* Bits per pixel format for tiles */
TileWidth tile_width;
TileHeight tile_height;
gboolean tiled_file;
gboolean bmp_file;
gboolean pal_file;
} VeraSaveVals;
typedef struct
{
gboolean run;
// tile dialog
GtkWidget *tile_1bpp;
GtkWidget *tile_2bpp;
GtkWidget *tile_4bpp;
GtkWidget *tile_8bpp;
GtkWidget *tile_width_8;
GtkWidget *tile_width_16;
GtkWidget *tile_width_32;
GtkWidget *tile_width_64;
GtkWidget *tile_height_8;
GtkWidget *tile_height_16;
GtkWidget *tile_height_32;
GtkWidget *tile_height_64;
GtkWidget *no_tiled_file;
GtkWidget *tiled_file;
GtkWidget *bmp_file;
GtkWidget *pal_file;
// selector dialog
GtkWidget *file_header;
GtkWidget *tileset_export;
GtkWidget *bitmap_export;
} VeraSaveGui;
GimpPlugInInfo PLUG_IN_INFO =
{
NULL,
NULL,
query,
run
};
static const VeraSaveVals defaults =
{
TILESET,
TILE_4BPP,
TILE_WIDTH_8,
TILE_HEIGHT_8,
TRUE,
TRUE,
TRUE
};
static VeraSaveVals veravals;
static gboolean save_tiles_dialog(gint32 image_id);
static gboolean save_bitmap_dialog(gint32 image_id);
static gboolean save_selector_dialog(gint32 image_id);
static void save_dialog_response(GtkWidget *widget,
gint response_id,
gpointer data);
static void load_defaults(void);
static void save_defaults(void);
static void load_gui_defaults(VeraSaveGui *vg);
MAIN()
static void query (void)
{
static const GimpParamDef save_args[] =
{
{ GIMP_PDB_INT32, "run-mode", "The run mode { RUN-INTERACTIVE (0), RUN-NONINTERACTIVE (1) }" },
{ GIMP_PDB_IMAGE, "image", "Input image" },
{ GIMP_PDB_DRAWABLE, "drawable", "Drawable to export" },
{ GIMP_PDB_STRING, "filename", "The name of the file to export the image to" },
{ GIMP_PDB_STRING, "raw-filename", "The name of the file to export the image to" },
{ GIMP_PDB_INT32, "export-type", "0 - Tileset, 1 - Bitmap" },
{ GIMP_PDB_INT32, "file-header", "0 - no 2-byte header, 1 - 2-byte header" },
{ GIMP_PDB_INT32, "tile-bpp", "Bits per pixel" },
{ GIMP_PDB_INT32, "tile-width", "Tile width" },
{ GIMP_PDB_INT32, "tile-height", "Tile height" },
{ GIMP_PDB_INT32, "Tiled-file", "Create a Tiled tile set file" },
{ GIMP_PDB_INT32, "BMP-file", "Create a BMP output file" },
{ GIMP_PDB_INT32, "PAL-file", "Create a PAL palette file" }
};
gimp_install_procedure (SAVE_PROC,
"Exports files in VERA compatible binaries",
"This plug-in exports binary files for VERA chips.",
"Jestin Stoffel <[email protected]>",
"Copyright 2021-2022 by Jestin Stoffel",
"0.0.1 - 2021",
"VERA tile set",
"INDEXED*",
GIMP_PLUGIN,
G_N_ELEMENTS (save_args),
0,
save_args,
NULL);
gimp_register_file_handler_mime (SAVE_PROC, "application/octet-stream");
gimp_register_save_handler (SAVE_PROC, "BIN", "");
static const GimpParamDef vera_convert_args[] =
{
{ GIMP_PDB_INT32, "run-mode", "The run mode { RUN-INTERACTIVE (0), RUN-NONINTERACTIVE (1) }" },
{ GIMP_PDB_IMAGE, "image", "Input image" },
{ GIMP_PDB_DRAWABLE, "drawable", "Drawable to export" }
};
gimp_install_procedure (VERA_COLORMAP_CONVERT,
"Aligns the colormap to nearest colors that can be displayed on the VERA chip",
"This procedure takes an indexed image and converts every color "
"in the colormap for proper viewing when converted to 12 bit color depth."
"This allows for more accurate viewing when exported for use on the VERA chip.",
"Jestin Stoffel <[email protected]>",
"Copyright 2022 by Jestin Stoffel",
"0.0.1 2022",
"Convert to VERA compatible colors",
"INDEXED*",
GIMP_PLUGIN,
G_N_ELEMENTS (vera_convert_args), 0,
vera_convert_args, NULL);
gimp_plugin_menu_register (VERA_COLORMAP_CONVERT, "<Image>/Colors/Map/Colormap");
gimp_plugin_menu_register (VERA_COLORMAP_CONVERT, "<Colormap>");
gimp_plugin_icon_register (VERA_COLORMAP_CONVERT, GIMP_ICON_TYPE_ICON_NAME,
(const guint8 *) GIMP_ICON_COLORMAP);
}
static void run (const gchar *name,
gint nparams,
const GimpParam *param,
gint *nreturn_vals,
GimpParam **return_vals)
{
static GimpParam values[3];
GimpRunMode run_mode;
GimpPDBStatusType status = GIMP_PDB_SUCCESS;
GError *error = NULL;
gint32 image_id;
gint32 drawable_id;
gchar* filename;
GimpExportReturn export = GIMP_EXPORT_CANCEL;
// INIT_I18N ();
gegl_init (NULL, NULL);
*nreturn_vals = 1;
*return_vals = values;
values[0].type = GIMP_PDB_STATUS;
values[0].data.d_status = GIMP_PDB_EXECUTION_ERROR;
run_mode = param[0].data.d_int32;
image_id = param[1].data.d_int32;
drawable_id = param[2].data.d_int32;
if (strcmp (name, SAVE_PROC) == 0)
{
filename = param[3].data.d_string;
load_defaults ();
/* export the image */
export = gimp_export_image (&image_id, &drawable_id, "VERA",
GIMP_EXPORT_CAN_HANDLE_INDEXED);
if (export == GIMP_EXPORT_CANCEL)
{
*nreturn_vals = 1;
values[0].data.d_status = GIMP_PDB_CANCEL;
return;
}
switch (run_mode)
{
case GIMP_RUN_INTERACTIVE:
/*
* Possibly retrieve data...
*/
gimp_get_data (SAVE_PROC, &veravals);
/*
* Then acquire information with a dialog...
*/
if (!save_selector_dialog (image_id))
{
values[0].data.d_status = GIMP_PDB_CANCEL;
return;
}
switch(veravals.export_type)
{
case TILESET:
if (!save_tiles_dialog (image_id))
status = GIMP_PDB_CANCEL;
break;
case BITMAP:
if (!save_bitmap_dialog (image_id))
status = GIMP_PDB_CANCEL;
break;
}
break;
case GIMP_RUN_NONINTERACTIVE:
/*
* Make sure all the arguments are there!
*/
if (nparams != 13)
{
status = GIMP_PDB_CALLING_ERROR;
}
else
{
veravals.export_type = param[5].data.d_int32;
veravals.file_header = param[6].data.d_int32;
veravals.tile_bpp = param[7].data.d_int32;
veravals.tile_width = param[8].data.d_int32;
veravals.tile_height = param[9].data.d_int32;
veravals.tiled_file = param[10].data.d_int32;
veravals.bmp_file = param[11].data.d_int32;
veravals.pal_file = param[12].data.d_int32;
}
break;
case GIMP_RUN_WITH_LAST_VALS:
/*
* Possibly retrieve data...
*/
gimp_get_data (SAVE_PROC, &veravals);
break;
default:
break;
}
if (status == GIMP_PDB_SUCCESS)
{
gint palsize;
guchar *cmap = gimp_image_get_colormap (image_id, &palsize);
if (cmap && veravals.pal_file)
{
if(!save_palette(filename, cmap, palsize, &error))
{
status = GIMP_PDB_EXECUTION_ERROR;
}
}
gchar *bmp_filename = g_strconcat (filename, ".bmp", NULL);
switch(veravals.export_type)
{
case TILESET:
// generate images for all palettes when using 2bpp or 4bpp
if(veravals.tile_bpp == TILE_4BPP || veravals.tile_bpp == TILE_2BPP)
{
guchar* shifted_map = (guchar*)malloc(sizeof(guchar) * palsize * 3);
guchar num_palettes = palsize / 16;
for (int i = 0; i < num_palettes; i++)
{
shift_color_map(cmap, &shifted_map, palsize, i*16);
gimp_image_set_colormap(image_id, shifted_map, palsize);
gchar* number_string = malloc(sizeof(gchar) * 3);
sprintf(number_string, "%d", i);
gchar *numbered_filename = g_strconcat(filename, ".", number_string, NULL);
gchar *numbered_bmp_filename = g_strconcat (numbered_filename, ".bmp", NULL);
if (veravals.bmp_file)
{
// write out a bitmap to be used with the .tsx file
gimp_file_save(GIMP_RUN_NONINTERACTIVE,
image_id,
drawable_id,
numbered_bmp_filename,
numbered_bmp_filename);
}
if(veravals.tiled_file)
{
if(!save_tsx(numbered_filename,
numbered_bmp_filename,
GIMP_RUN_NONINTERACTIVE,
image_id,
drawable_id,
&error))
{
status = GIMP_PDB_EXECUTION_ERROR;
}
}
g_free(number_string);
g_free(numbered_filename);
g_free(numbered_bmp_filename);
}
gimp_image_set_colormap(image_id, cmap, palsize);
g_free(shifted_map);
}
else // NOT 4bpp or 2BPP
{
if (veravals.bmp_file)
{
// write out a bitmap to be used with the .tsx file
gimp_file_save(run_mode, image_id, drawable_id, bmp_filename, bmp_filename);
}
if(veravals.tiled_file)
{
if(!save_tsx(filename, bmp_filename, GIMP_RUN_NONINTERACTIVE, image_id, drawable_id, &error))
{
status = GIMP_PDB_EXECUTION_ERROR;
}
}
}
if (save_tile_set (filename, image_id, drawable_id, &error))
{
gimp_set_data (SAVE_PROC, &veravals, sizeof (veravals));
}
else
{
status = GIMP_PDB_EXECUTION_ERROR;
}
break;
case BITMAP:
if (save_bitmap (filename, image_id, drawable_id, &error))
{
gimp_set_data (SAVE_PROC, &veravals, sizeof (veravals));
}
else
{
status = GIMP_PDB_EXECUTION_ERROR;
}
break;
}
g_free(bmp_filename);
}
if (export == GIMP_EXPORT_EXPORT)
gimp_image_delete (image_id);
}
else if (strcmp (name, VERA_COLORMAP_CONVERT) == 0)
{
/* Make sure that the image is indexed */
if (gimp_image_base_type (image_id) != GIMP_INDEXED)
status = GIMP_PDB_EXECUTION_ERROR;
if (status == GIMP_PDB_SUCCESS)
{
gint palsize;
guchar * cmap;
guchar* adjusted_map;
gint cur_pal_val;
switch (run_mode)
{
case GIMP_RUN_INTERACTIVE:
case GIMP_RUN_NONINTERACTIVE:
case GIMP_RUN_WITH_LAST_VALS:
if (nparams != 2)
{
status = GIMP_PDB_CALLING_ERROR;
}
cmap = gimp_image_get_colormap (image_id, &palsize);
adjusted_map = (guchar*)malloc(sizeof(guchar) * palsize * 3);
for (int i = 0; i < palsize * 3; i++)
{
// adjusted_map[i] = ((cmap[i] & 0xf0) >> 4) | (cmap[i] & 0xf0);
cur_pal_val = cmap[i];
cur_pal_val = ((cur_pal_val * 15) + 135) >> 8;
adjusted_map[i] = ((cur_pal_val & 0x0f) << 4) | (cur_pal_val & 0x0f);
}
break;
}
if (status == GIMP_PDB_SUCCESS)
{
gimp_image_set_colormap(image_id, adjusted_map, palsize);
if (run_mode == GIMP_RUN_INTERACTIVE)
gimp_set_data (VERA_COLORMAP_CONVERT, cmap, sizeof (cmap));
if (run_mode != GIMP_RUN_NONINTERACTIVE)
gimp_displays_flush ();
}
g_free(adjusted_map);
}
}
else
{
status = GIMP_PDB_CALLING_ERROR;
}
if (status != GIMP_PDB_SUCCESS && error)
{
*nreturn_vals = 2;
values[1].type = GIMP_PDB_STRING;
values[1].data.d_string = error->message;
}
values[0].data.d_status = status;
}
static void shift_color_map(guchar* orig,
guchar** shifted,
gint palsize,
gint offset)
{
gint offset_base = offset * 3;
for(int i = 0; i < palsize*3; i+=3)
{
if(i + offset_base >= palsize * 3)
{
// this is the last <offset> values in the color map
(*shifted)[i] = orig[(i - offset_base) % palsize];
(*shifted)[i+1] = orig[(i+1 - offset_base) % palsize];
(*shifted)[i+2] = orig[(i+2 - offset_base) % palsize];
continue;
}
(*shifted)[i] = orig[i+offset_base];
(*shifted)[i+1] = orig[i+1+offset_base];
(*shifted)[i+2] = orig[i+2+offset_base];
}
}
static gboolean save_tsx (const gchar *filename,
const gchar *bmp_filename,
GimpRunMode run_mode,
gint32 image_id,
gint32 drawable_id,
GError **error)
{
// write out the tsx file
gchar *tsx_filename = g_strconcat (filename, ".tsx", NULL);
int rc;
xmlTextWriterPtr writer;
writer = xmlNewTextWriterFilename(tsx_filename, 0);
if(writer == NULL)
{
printf("could not create writer\n");
g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (errno),
"Could not open '%s' for writing: %s",
gimp_filename_to_utf8 (filename), g_strerror (errno));
return FALSE;
}
rc = xmlTextWriterStartDocument(writer, NULL, "UTF-8", NULL);
if (rc < 0)
{
printf("could not create document\n");
g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (errno),
"Error starting document '%s': %s",
gimp_filename_to_utf8 (filename), g_strerror (errno));
return FALSE;
}
GeglBuffer *buffer;
gint32 width, height, tile_count, columns;
/* get info about the current image */
buffer = gimp_drawable_get_buffer (drawable_id);
width = gegl_buffer_get_width (buffer);
height = gegl_buffer_get_height (buffer);
tile_count = (width * height) / (veravals.tile_width * veravals.tile_height);
columns = width / veravals.tile_width;
printf("writing tsx document\n");
gchar* val_string;
xmlTextWriterStartElement(writer, BAD_CAST "tileset");
xmlTextWriterWriteAttribute(writer, BAD_CAST "version", BAD_CAST "1.5");
xmlTextWriterWriteAttribute(writer, BAD_CAST "tiledversion", BAD_CAST "1.8.0");
xmlTextWriterWriteAttribute(writer, BAD_CAST "name", BAD_CAST filename);
val_string = g_strdup_printf("%d", veravals.tile_width);
xmlTextWriterWriteAttribute(writer,BAD_CAST "tilewidth", BAD_CAST val_string);
g_free(val_string);
val_string = g_strdup_printf("%d", veravals.tile_height);
xmlTextWriterWriteAttribute(writer, BAD_CAST "tileheight", BAD_CAST val_string);
g_free(val_string);
val_string = g_strdup_printf("%d", tile_count);
xmlTextWriterWriteAttribute(writer, BAD_CAST "tilecount", BAD_CAST val_string);
g_free(val_string);
val_string = g_strdup_printf("%d", columns);
xmlTextWriterWriteAttribute(writer, BAD_CAST "columns", BAD_CAST val_string);
g_free(val_string);
xmlTextWriterStartElement(writer, BAD_CAST "image");
xmlTextWriterWriteAttribute(writer, BAD_CAST "source", BAD_CAST bmp_filename);
xmlTextWriterWriteAttribute(writer, BAD_CAST "trans", BAD_CAST "000000");
val_string = g_strdup_printf("%d", width);
xmlTextWriterWriteAttribute(writer, BAD_CAST "width", BAD_CAST val_string);
g_free(val_string);
val_string = g_strdup_printf("%d", height);
xmlTextWriterWriteAttribute(writer, BAD_CAST "height", BAD_CAST val_string);
g_free(val_string);
xmlTextWriterEndElement(writer); // image
xmlTextWriterEndElement(writer); // tileset
xmlTextWriterEndDocument(writer);
xmlFreeTextWriter(writer);
printf("finished writing tsx document\n");
return TRUE;
}
static gboolean save_tile_set (const gchar *filename,
gint32 image_id,
gint32 drawable_id,
GError **error)
{
GeglBuffer *buffer;
const Babl *format = NULL;
guchar *buf;
guchar *tile_buf;
gint32 width, height, bpp;
FILE *fp = NULL;
gboolean ret = FALSE;
/* get info about the current image */
buffer = gimp_drawable_get_buffer (drawable_id);
switch (gimp_drawable_type (drawable_id))
{
case GIMP_INDEXED_IMAGE:
case GIMP_INDEXEDA_IMAGE:
format = gimp_drawable_get_format (drawable_id);
break;
case GIMP_RGB_IMAGE:
case GIMP_RGBA_IMAGE:
case GIMP_GRAY_IMAGE:
case GIMP_GRAYA_IMAGE:
default:
// TODO set error message
return FALSE;
}
bpp = babl_format_get_bytes_per_pixel (format);
width = gegl_buffer_get_width (buffer);
height = gegl_buffer_get_height (buffer);
buf = g_new (guchar, width * height * bpp);
gegl_buffer_get (buffer, GEGL_RECTANGLE (0, 0, width, height), 1.0,
format, buf,
GEGL_AUTO_ROWSTRIDE, GEGL_ABYSS_NONE);
g_object_unref (buffer);
int tile_width = veravals.tile_width;
int tile_height = veravals.tile_height;
int t_width = width / tile_width;
int t_height = height / tile_height;
int tile_buf_length = ((width * height * bpp) / (8 / veravals.tile_bpp));
int tile_buf_index = 0;
if(veravals.file_header)
{
tile_buf_length += 2;
tile_buf_index += 2;
}
tile_buf = g_new (guchar, tile_buf_length);
if(veravals.file_header)
{
// 2 byte header
tile_buf[0] = 0;
tile_buf[1] = 0;
}
for(int y = 0; y < t_height; y++)
{
int yoff = y * tile_height;
for(int x = 0; x < t_width; x++)
{
int xoff = x * tile_width;
// write out a single tile
for(int ty = 0; ty < tile_height; ty++)
{
for(int tx = 0; tx < tile_width; tx++)
{
// get the color from the buffer
int buf_index = ((yoff + ty) * width) + xoff + tx;
guchar color = buf[buf_index];
switch(veravals.tile_bpp)
{
case TILE_1BPP:
switch(buf_index % 8)
{
case 0:
tile_buf[tile_buf_index] = color << 7;
break;
case 1:
tile_buf[tile_buf_index] |= color << 6;
break;
case 2:
tile_buf[tile_buf_index] |= color << 5;
break;
case 3:
tile_buf[tile_buf_index] |= color << 4;
break;
case 4:
tile_buf[tile_buf_index] |= color << 3;
break;
case 5:
tile_buf[tile_buf_index] |= color << 2;
break;
case 6:
tile_buf[tile_buf_index] |= color << 1;
break;
case 7:
tile_buf[tile_buf_index] |= color;
tile_buf_index++;
break;
}
break;
case TILE_2BPP:
switch(buf_index % 4)
{
case 0:
tile_buf[tile_buf_index] = color << 6;
break;
case 1:
tile_buf[tile_buf_index] |= color << 4;
break;
case 2:
tile_buf[tile_buf_index] |= color << 2;
break;
case 3:
tile_buf[tile_buf_index] |= color;
tile_buf_index++;
break;
}
break;
case TILE_4BPP:
switch(buf_index % 2)
{
case 0:
tile_buf[tile_buf_index] = color << 4;
break;
case 1:
tile_buf[tile_buf_index] |= color;
tile_buf_index++;
break;
}
break;
case TILE_8BPP:
tile_buf[tile_buf_index] = color;
tile_buf_index++;
break;
}
}
}
}
}
fp = fopen (filename, "wb");
if (! fp)
{
g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (errno),
"Could not open '%s' for writing: %s",
gimp_filename_to_utf8 (filename), g_strerror (errno));
return FALSE;
}
ret = TRUE;
if (! fwrite (tile_buf, tile_buf_length, 1, fp))
{
return FALSE;
}
fclose (fp);
g_free(tile_buf);
return ret;
}
static gboolean save_bitmap (const gchar *filename,
gint32 image_id,
gint32 drawable_id,
GError **error)
{
GeglBuffer *buffer;
const Babl *format = NULL;
guchar *buf;
guchar *bitmap_buf;
gint32 width, height, bpp;
FILE *fp = NULL;
gboolean ret = FALSE;
/* get info about the current image */
buffer = gimp_drawable_get_buffer (drawable_id);
switch (gimp_drawable_type (drawable_id))
{
case GIMP_INDEXED_IMAGE:
case GIMP_INDEXEDA_IMAGE:
format = gimp_drawable_get_format (drawable_id);
break;
case GIMP_RGB_IMAGE:
case GIMP_RGBA_IMAGE:
case GIMP_GRAY_IMAGE:
case GIMP_GRAYA_IMAGE:
default:
// TODO set error message
return FALSE;
}
bpp = babl_format_get_bytes_per_pixel (format);
width = gegl_buffer_get_width (buffer);
height = gegl_buffer_get_height (buffer);
buf = g_new (guchar, width * height * bpp);
gegl_buffer_get (buffer, GEGL_RECTANGLE (0, 0, width, height), 1.0,
format, buf,
GEGL_AUTO_ROWSTRIDE, GEGL_ABYSS_NONE);
g_object_unref (buffer);
int bitmap_buf_length = ((width * height * bpp) / (8 / veravals.tile_bpp));
int bitmap_buf_index = 0;
if(veravals.file_header)
{
bitmap_buf_length += 2;
bitmap_buf_index += 2;
}
bitmap_buf = g_new (guchar, bitmap_buf_length);
if(veravals.file_header)
{
// 2 byte header
bitmap_buf[0] = 0;
bitmap_buf[1] = 0;
}
// TODO: populate bitmap_buf
// write the whole file
for(int y = 0; y < height; y++)
{
for(int x = 0; x < width; x++)
{
// get the color from the buffer
int buf_index = (width * y) + x;
guchar color = buf[buf_index];
switch(veravals.tile_bpp)
{
case TILE_1BPP:
switch(buf_index % 8)
{
case 0:
bitmap_buf[bitmap_buf_index] = color << 7;
break;
case 1:
bitmap_buf[bitmap_buf_index] |= color << 6;
break;
case 2:
bitmap_buf[bitmap_buf_index] |= color << 5;
break;
case 3:
bitmap_buf[bitmap_buf_index] |= color << 4;
break;
case 4:
bitmap_buf[bitmap_buf_index] |= color << 3;
break;
case 5:
bitmap_buf[bitmap_buf_index] |= color << 2;
break;
case 6:
bitmap_buf[bitmap_buf_index] |= color << 1;
break;
case 7:
bitmap_buf[bitmap_buf_index] |= color;
bitmap_buf_index++;
break;
}
break;
case TILE_2BPP:
switch(buf_index % 4)
{
case 0:
bitmap_buf[bitmap_buf_index] = color << 6;
break;
case 1:
bitmap_buf[bitmap_buf_index] |= color << 4;
break;
case 2:
bitmap_buf[bitmap_buf_index] |= color << 2;
break;
case 3:
bitmap_buf[bitmap_buf_index] |= color;
bitmap_buf_index++;
break;
}
break;
case TILE_4BPP:
if (buf_index % 2)
{
// odd byte
bitmap_buf[bitmap_buf_index] |= color;
bitmap_buf_index++;
}
else
{
// even byte
bitmap_buf[bitmap_buf_index] = color << 4;
}
break;
case TILE_8BPP:
bitmap_buf[bitmap_buf_index] = color;
bitmap_buf_index++;
break;
}
}
}
fp = fopen (filename, "wb");
if (! fp)
{
g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (errno),
"Could not open '%s' for writing: %s",
gimp_filename_to_utf8 (filename), g_strerror (errno));
return FALSE;
}
ret = TRUE;
if (! fwrite (bitmap_buf, bitmap_buf_length, 1, fp))
{
return FALSE;
}
fclose (fp);
g_free(bitmap_buf);
return ret;
}
static gboolean save_palette(const gchar *filename,
const guchar *cmap,
const gint palsize,
GError **error)
{
FILE *fp = NULL;
guchar *pal_buf;
int pal_buf_length = palsize * 2;
int pal_buf_index = 0; // start past the 2 byte header
if(veravals.file_header)
{
pal_buf_length += 2;
pal_buf_index += 2;
}
pal_buf = g_new (guchar, pal_buf_length); // 2 bytes per color, 2 byte header
if(veravals.file_header)
{
// 2 byte header
pal_buf[0] = 0;
pal_buf[1] = 0;
}
for(int i = 0; i < palsize*3; i+=3)
{
// read rgb values from colormap
gint r = ((cmap[i] * 15) + 135) >> 8;
gint g = ((cmap[i+1] * 15) + 135) >> 8;
gint b = ((cmap[i+2] * 15) + 135) >> 8;
// write out packed g and b values
pal_buf[pal_buf_index] = (g & 0x0f) << 4 | b;
// write out r value in lower nibble
pal_buf[pal_buf_index+1] = r & 0x0f;
pal_buf_index += 2;
}
/* we have colormap too, write it into filename+PAL.BIN */