summaryrefslogtreecommitdiff
path: root/tests/testupload.c
blob: 747c94211f9b8caccf9258702fea74baf58f9271 (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
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
#include <gtk/gtk.h>

static void
add_to_grid (GtkWidget *grid,
             int        left,
             int        top,
             int        n_channels,
             gboolean   premul,
             int        width,
             int        height,
             int        stride,
             GtkWidget *picture)
{
  char *text;

  gtk_grid_attach (GTK_GRID (grid), gtk_label_new ("Channels"), left, top, 1, 1);
  gtk_grid_attach (GTK_GRID (grid), gtk_label_new ("Width"), left, top + 1, 1, 1);
  gtk_grid_attach (GTK_GRID (grid), gtk_label_new ("Height"), left, top + 2, 1, 1);
  gtk_grid_attach (GTK_GRID (grid), gtk_label_new ("Stride"), left, top + 3, 1, 1);

  text = g_strdup_printf ("%d%s", n_channels, premul ? " (premul)" : "");
  gtk_grid_attach (GTK_GRID (grid), gtk_label_new (text), left + 1, top, 1, 1);
  g_free (text);
  text = g_strdup_printf ("%d", width);
  gtk_grid_attach (GTK_GRID (grid), gtk_label_new (text), left + 1, top + 1, 1, 1);
  g_free (text);
  text = g_strdup_printf ("%d", height);
  gtk_grid_attach (GTK_GRID (grid), gtk_label_new (text), left + 1, top + 2, 1, 1);
  g_free (text);
  text = g_strdup_printf ("%d", stride);
  gtk_grid_attach (GTK_GRID (grid), gtk_label_new (text), left + 1, top + 3, 1, 1);
  g_free (text);

  gtk_grid_attach (GTK_GRID (grid), picture, left + 2, top + 0, 1, 4);
}

int
main (int argc, char *argv[])
{
  GtkWidget *window, *grid;
  GdkPixbuf *source, *pb, *pb2;
  GError *error = NULL;
  cairo_t *cr;
  cairo_surface_t *surface;
  GBytes *bytes;
  GdkTexture *texture;

  gtk_init ();

  window = gtk_window_new ();
  grid = gtk_grid_new ();
  gtk_widget_set_margin_top (grid, 10);
  gtk_widget_set_margin_bottom (grid, 10);
  gtk_widget_set_margin_start (grid, 10);
  gtk_widget_set_margin_end (grid, 10);
  gtk_grid_set_row_spacing (GTK_GRID (grid), 10);
  gtk_grid_set_column_spacing (GTK_GRID (grid), 10);
  gtk_window_set_child (GTK_WINDOW (window), grid);

  source = gdk_pixbuf_new_from_file_at_scale ("tests/portland-rose.jpg",
                                              200, 200, TRUE,
                                              &error);
  if (!source)
    g_error ("%s", error->message);

  add_to_grid (grid, 0, 0,
               gdk_pixbuf_get_n_channels (source),
               FALSE,
               gdk_pixbuf_get_width (source),
               gdk_pixbuf_get_height (source),
               gdk_pixbuf_get_rowstride (source),
               gtk_picture_new_for_pixbuf (source));

  g_object_unref (source);

  source = gdk_pixbuf_new_from_file_at_scale ("tests/portland-rose.jpg",
                                              199, 199, TRUE,
                                              &error);
  if (!source)
    g_error ("%s", error->message);

  add_to_grid (grid, 4, 0,
               gdk_pixbuf_get_n_channels (source),
               FALSE,
               gdk_pixbuf_get_width (source),
               gdk_pixbuf_get_height (source),
               gdk_pixbuf_get_rowstride (source),
               gtk_picture_new_for_pixbuf (source));

  g_object_unref (source);

  source = gdk_pixbuf_new_from_file_at_scale ("tests/portland-rose.jpg",
                                              201, 201, TRUE,
                                              &error);

  if (!source)
    g_error ("%s", error->message);

  pb = gdk_pixbuf_new_subpixbuf (source, 0, 0, 200, 200);

  add_to_grid (grid, 8, 0,
               gdk_pixbuf_get_n_channels (pb),
               FALSE,
               gdk_pixbuf_get_width (pb),
               gdk_pixbuf_get_height (pb),
               gdk_pixbuf_get_rowstride (pb),
               gtk_picture_new_for_pixbuf (pb));

  g_object_unref (source);
  g_object_unref (pb);


  source = gdk_pixbuf_new_from_file_at_scale ("tests/portland-rose.jpg",
                                              200, 200, TRUE,
                                              &error);
  if (!source)
    g_error ("%s", error->message);

  pb = gdk_pixbuf_add_alpha (source, FALSE, 0, 0, 0);

  add_to_grid (grid, 0, 5,
               gdk_pixbuf_get_n_channels (pb),
               FALSE,
               gdk_pixbuf_get_width (pb),
               gdk_pixbuf_get_height (pb),
               gdk_pixbuf_get_rowstride (pb),
               gtk_picture_new_for_pixbuf (pb));

  g_object_unref (source);
  g_object_unref (pb);

  source = gdk_pixbuf_new_from_file_at_scale ("tests/portland-rose.jpg",
                                              199, 199, TRUE,
                                              &error);
  if (!source)
    g_error ("%s", error->message);

  pb = gdk_pixbuf_add_alpha (source, FALSE, 0, 0, 0);

  add_to_grid (grid, 4, 5,
               gdk_pixbuf_get_n_channels (pb),
               FALSE,
               gdk_pixbuf_get_width (pb),
               gdk_pixbuf_get_height (pb),
               gdk_pixbuf_get_rowstride (pb),
               gtk_picture_new_for_pixbuf (pb));

  g_object_unref (source);
  g_object_unref (pb);

  source = gdk_pixbuf_new_from_file_at_scale ("tests/portland-rose.jpg",
                                              201, 201, TRUE,
                                              &error);
  if (!source)
    g_error ("%s", error->message);

  pb = gdk_pixbuf_add_alpha (source, FALSE, 0, 0, 0);
  pb2 = gdk_pixbuf_new_subpixbuf (pb, 0, 0, 200, 200);

  add_to_grid (grid, 8, 5,
               gdk_pixbuf_get_n_channels (pb2),
               FALSE,
               gdk_pixbuf_get_width (pb2),
               gdk_pixbuf_get_height (pb2),
               gdk_pixbuf_get_rowstride (pb2),
               gtk_picture_new_for_pixbuf (pb2));

  g_object_unref (source);
  g_object_unref (pb);
  g_object_unref (pb2);

  source = gdk_pixbuf_new_from_file_at_scale ("tests/portland-rose.jpg",
                                              200, 200, TRUE,
                                              &error);
  if (!source)
    g_error ("%s", error->message);

  pb = gdk_pixbuf_add_alpha (source, FALSE, 0, 0, 0);
  surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, gdk_pixbuf_get_width (pb), gdk_pixbuf_get_height (pb));
  cr = cairo_create (surface);
  gdk_cairo_set_source_pixbuf (cr, pb, 0, 0);
  cairo_paint (cr);
  cairo_destroy (cr);
  bytes = g_bytes_new (cairo_image_surface_get_data (surface),
                       cairo_image_surface_get_stride (surface) *
                       cairo_image_surface_get_height (surface));
  texture = gdk_memory_texture_new (cairo_image_surface_get_width (surface),
                                    cairo_image_surface_get_height (surface),
                                    GDK_MEMORY_DEFAULT,
                                    bytes,
                                    cairo_image_surface_get_stride (surface));
  g_bytes_unref (bytes);

  add_to_grid (grid, 0, 10,
               4, TRUE,
               cairo_image_surface_get_width (surface),
               cairo_image_surface_get_height (surface),
               cairo_image_surface_get_stride (surface),
               gtk_picture_new_for_paintable (GDK_PAINTABLE (texture)));

  g_object_unref (source);
  g_object_unref (pb);
  cairo_surface_destroy (surface);

  source = gdk_pixbuf_new_from_file_at_scale ("tests/portland-rose.jpg",
                                              199, 199, TRUE,
                                              &error);
  if (!source)
    g_error ("%s", error->message);

  pb = gdk_pixbuf_add_alpha (source, FALSE, 0, 0, 0);
  surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, gdk_pixbuf_get_width (pb), gdk_pixbuf_get_height (pb));
  cr = cairo_create (surface);
  gdk_cairo_set_source_pixbuf (cr, pb, 0, 0);
  cairo_paint (cr);
  cairo_destroy (cr);
  bytes = g_bytes_new (cairo_image_surface_get_data (surface),
                       cairo_image_surface_get_stride (surface) *
                       cairo_image_surface_get_height (surface));
  texture = gdk_memory_texture_new (cairo_image_surface_get_width (surface),
                                    cairo_image_surface_get_height (surface),
                                    GDK_MEMORY_DEFAULT,
                                    bytes,
                                    cairo_image_surface_get_stride (surface));
  g_bytes_unref (bytes);

  add_to_grid (grid, 4, 10,
               4, TRUE,
               cairo_image_surface_get_width (surface),
               cairo_image_surface_get_height (surface),
               cairo_image_surface_get_stride (surface),
               gtk_picture_new_for_paintable (GDK_PAINTABLE (texture)));

  g_object_unref (source);
  g_object_unref (pb);
  cairo_surface_destroy (surface);

  source = gdk_pixbuf_new_from_file_at_scale ("tests/portland-rose.jpg",
                                              201, 201, TRUE,
                                              &error);
  if (!source)
    g_error ("%s", error->message);

  pb = gdk_pixbuf_add_alpha (source, FALSE, 0, 0, 0);
  surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, gdk_pixbuf_get_width (pb), gdk_pixbuf_get_height (pb));
  cr = cairo_create (surface);
  gdk_cairo_set_source_pixbuf (cr, pb, 0, 0);
  cairo_paint (cr);
  cairo_destroy (cr);
  bytes = g_bytes_new (cairo_image_surface_get_data (surface),
                       cairo_image_surface_get_stride (surface) *
                       cairo_image_surface_get_height (surface));
  texture = gdk_memory_texture_new (cairo_image_surface_get_width (surface) - 1,
                                    cairo_image_surface_get_height (surface) - 1,
                                    GDK_MEMORY_DEFAULT,
                                    bytes,
                                    cairo_image_surface_get_stride (surface));
  g_bytes_unref (bytes);

  add_to_grid (grid, 8, 10,
               4, TRUE,
               gdk_texture_get_width (texture),
               gdk_texture_get_height (texture),
               cairo_image_surface_get_stride (surface),
               gtk_picture_new_for_paintable (GDK_PAINTABLE (texture)));

  g_object_unref (source);
  g_object_unref (pb);
  cairo_surface_destroy (surface);

  gtk_window_present (GTK_WINDOW (window));

  while (g_list_model_get_n_items (gtk_window_get_toplevels ()) > 0)
    g_main_context_iteration (NULL, TRUE);

  return 0;
}