aboutsummaryrefslogtreecommitdiff
path: root/img-save-dialog.c
blob: ce7c3ef92f5bf630af03fff7c53a431546b468e3 (plain)
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
#include "plugin-img.h"
#include <libgimp/gimpui.h>

static GtkWidget *fmt_frame, *ckey_frame, *fmt_vbox, *ckey_vbox, *ckey_align,
    *fmt_align, *hbox, *fmt_rgb565, *fmt_rgb, *fmt_rgba, *ckey_image, *ckey_use,
    *gimp_color;
static GimpRGB color;
static GdkPixbuf *pixbuf;

static GtkWidget *
img_preview_new(gint32 image_ID)
{
    GtkWidget *image, *event_box, *align;
    guint32 width, height;
    gint *frames, nframes, layer;

    frames = gimp_image_get_layers(image_ID, &nframes);
    layer = frames[g_random_int() % nframes];
    g_free(frames);
    width = gimp_drawable_width(layer);
    height = gimp_drawable_height(layer);

    pixbuf =
        gimp_drawable_get_thumbnail(layer, MIN(width, 128), MIN(height, 128),
                                    GIMP_PIXBUF_SMALL_CHECKS);

    image = gtk_image_new_from_pixbuf(pixbuf);
    g_object_unref(pixbuf);

    event_box = gtk_event_box_new();
    gtk_widget_set_events(event_box, GDK_ALL_EVENTS_MASK);

    align = gtk_alignment_new(0.5, 0.5, 0, 0);

    gtk_widget_show(image);
    gtk_widget_show(event_box);
    gtk_container_add(GTK_CONTAINER(event_box), image);
    gtk_container_add(GTK_CONTAINER(align), event_box);

    return align;
}

static void
on_RGB(GtkWidget * widget, gpointer data)
{
    gtk_widget_set_sensitive(ckey_vbox,
                             gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON
                                                          (fmt_rgb)) ||
                             gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON
                                                          (fmt_rgba)));
}

static void
on_ckey_use(GtkWidget * widget, gpointer data)
{
    gboolean active;

    active = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));
    gtk_widget_set_sensitive(ckey_image, active);
    gtk_widget_set_sensitive(gimp_color, active);
}

static gboolean
on_image_click(GtkWidget * widget, GdkEventButton * event, gpointer data)
{
    int width, height, rowstride, n_channels;
    guchar *pixels, *p;

    n_channels = gdk_pixbuf_get_n_channels(pixbuf);
    width = gdk_pixbuf_get_width(pixbuf);
    height = gdk_pixbuf_get_height(pixbuf);
    rowstride = gdk_pixbuf_get_rowstride(pixbuf);
    pixels = gdk_pixbuf_get_pixels(pixbuf);
    p = pixels + (int)(event->y * rowstride) + (int)(event->x * n_channels);
    gimp_rgb_set_uchar(&color, p[0], p[1], p[2]);
    gimp_color_button_set_color((GimpColorButton *) gimp_color, &color);
    return TRUE;
}

gboolean
img_save_dialog(gint32 image, ImageParasite * plugin)
{
    GtkWidget *dialog;
    gint response;

    gimp_ui_init(PLUG_IN_BINARY, TRUE);
    dialog =
        gimp_dialog_new("Save as IMG", PLUG_IN_BINARY, NULL, 0,
                        gimp_standard_help_func, "plug-in-img",
                        GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, GTK_STOCK_SAVE,
                        GTK_RESPONSE_OK, NULL);
    gimp_window_set_transient(GTK_WINDOW(dialog));
    gtk_widget_show(dialog);

    /*
     * Radio buttons to select file format 
     */
    fmt_frame = gimp_frame_new("Image format");
    fmt_rgb565 = gtk_radio_button_new_with_label(NULL, FMT[FMT_RGB565]);
    fmt_rgb =
        gtk_radio_button_new_with_label_from_widget(GTK_RADIO_BUTTON
                                                    (fmt_rgb565), FMT[FMT_RGB]);
    fmt_rgba =
        gtk_radio_button_new_with_label_from_widget(GTK_RADIO_BUTTON(fmt_rgb),
                                                    FMT[FMT_RGBA]);
    gtk_widget_show(fmt_frame);
    gtk_widget_show(fmt_rgb565);
    gtk_widget_show(fmt_rgb);
    gtk_widget_show(fmt_rgba);

    /*
     * Horizontal main box 
     */
    hbox = gtk_hbox_new(TRUE, 2);
    gtk_widget_show(hbox);

    /*
     * Vertical box for radio buttons 
     */
    fmt_align = gtk_alignment_new(0.5, 0, 0, 0);
    gtk_widget_show(fmt_align);
    fmt_vbox = gtk_vbox_new(TRUE, 12);
    gtk_container_add(GTK_CONTAINER(fmt_align), fmt_vbox);
    gtk_container_set_border_width(GTK_CONTAINER(fmt_vbox), 12);
    gtk_widget_show(fmt_vbox);
    gtk_box_pack_start(GTK_BOX(fmt_vbox), fmt_frame, TRUE, TRUE, 0);
    gtk_box_pack_start(GTK_BOX(fmt_vbox), fmt_rgb565, TRUE, TRUE, 0);
    gtk_box_pack_start(GTK_BOX(fmt_vbox), fmt_rgb, TRUE, TRUE, 0);
    gtk_box_pack_start(GTK_BOX(fmt_vbox), fmt_rgba, TRUE, TRUE, 0);

    /*
     * Allow color key for RGB and RGBA 
     */
    g_signal_connect(G_OBJECT(fmt_rgb), "clicked", G_CALLBACK(on_RGB), NULL);
    g_signal_connect(G_OBJECT(fmt_rgba), "clicked", G_CALLBACK(on_RGB), NULL);

    /*
     * Vertical box for color key 
     */
    ckey_align = gtk_alignment_new(0.5, 0, 0, 0);
    gtk_widget_show(ckey_align);
    ckey_vbox = gtk_vbox_new(FALSE, 2);
    gtk_container_add(GTK_CONTAINER(ckey_align), ckey_vbox);
    gtk_container_set_border_width(GTK_CONTAINER(ckey_vbox), 12);
    ckey_frame = gimp_frame_new("Transparent color");
    ckey_use = gtk_check_button_new_with_label("Set transparent color");
    ckey_image = img_preview_new(image);
    gimp_rgb_set_uchar(&color, plugin->ckey.R, plugin->ckey.G, plugin->ckey.B);
    gimp_color =
        gimp_color_button_new("Choose color", 64, 64, &color,
                              GIMP_COLOR_AREA_FLAT);
    gtk_box_pack_start(GTK_BOX(ckey_vbox), ckey_frame, TRUE, TRUE, 0);
    gtk_box_pack_start(GTK_BOX(ckey_vbox), ckey_use, TRUE, TRUE, 0);
    gtk_box_pack_start(GTK_BOX(ckey_vbox), ckey_image, TRUE, TRUE, 0);
    gtk_box_pack_start(GTK_BOX(ckey_vbox), gimp_color, TRUE, TRUE, 0);
    gtk_widget_show(gimp_color);
    gtk_widget_show(ckey_vbox);
    gtk_widget_show(ckey_frame);
    gtk_widget_show(ckey_use);
    gtk_widget_show(ckey_image);
    g_signal_connect(G_OBJECT(ckey_use), "clicked", G_CALLBACK(on_ckey_use),
                     NULL);
    g_signal_connect(G_OBJECT(ckey_image), "button-press-event",
                     G_CALLBACK(on_image_click), NULL);
    /*
     * Assemble all together 
     */
    gtk_box_pack_start(GTK_BOX(hbox), fmt_align, TRUE, TRUE, 0);
    gtk_box_pack_start(GTK_BOX(hbox), ckey_align, TRUE, TRUE, 0);
    gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), hbox, TRUE, TRUE, 0);

    /*
     * Set initial parameters 
     */
    switch (plugin->format)
    {
      case FMT_RGB565:
          gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(fmt_rgb565), TRUE);
          break;
      case FMT_RGB:
          gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(fmt_rgb), TRUE);
          gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(ckey_use),
                                       plugin->ckey.is != 0);
          break;
      case FMT_RGBA:
          gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(fmt_rgba), TRUE);
          gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(ckey_use),
                                       plugin->ckey.is != 0);
          break;
    }

    on_RGB(fmt_rgb, NULL);
    on_ckey_use(ckey_use, NULL);

    /*
     * Run the dialog 
     */
    response = gimp_dialog_run(GIMP_DIALOG(dialog));

    /*
     * Get new parameters 
     */
    if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(fmt_rgb565)))
        plugin->format = FMT_RGB565;
    else if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(fmt_rgb)))
        plugin->format = FMT_RGB;
    else if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(fmt_rgba)))
        plugin->format = FMT_RGBA;
    plugin->ckey.is =
        gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(ckey_use)) ? 1 : 0;
    gimp_color_button_get_color((GimpColorButton *) gimp_color, &color);
    gimp_rgb_get_uchar(&color, &plugin->ckey.R, &plugin->ckey.G,
                       &plugin->ckey.B);
    gtk_widget_destroy(dialog);
    return (response == GTK_RESPONSE_OK);
}