20090113
[gdash.git] / include / cells2c.c
blobff910d02599c277b1cf03c2ead54a1b84b8ff097
1 /*
2 * Copyright (c) 2007, 2008 Czirkos Zoltan <cirix@fw.hu>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 #include <glib.h>
17 #include <gtk/gtk.h>
18 #include <stdlib.h>
19 #include <string.h>
21 #include "c64_png_colors.h"
24 GdkPixbuf *pixbuf=NULL;
25 int width, height, rowstride, n_channels;
26 guchar *pixels;
29 static int
30 get_pixel (int x, int y)
32 guchar *p;
33 int r, g, b, a;
35 g_assert (x >= 0 && x < width);
36 g_assert (y >= 0 && y < height);
38 p = pixels + y * rowstride + x * n_channels;
39 r=p[0];
40 g=p[1];
41 b=p[2];
42 a=p[3];
43 return c64_png_colors(r, g, b, a);
47 int
48 main(int argc, char *argv[])
50 int i;
51 int w, h, x, y;
53 gtk_init_check(&argc, &argv);
54 if (argc!=2) {
55 g_critical("Usage: %s <filename.png>", argv[0]);
57 return -1;
59 pixbuf=gdk_pixbuf_new_from_file(argv[1], NULL);
60 g_assert (gdk_pixbuf_get_colorspace (pixbuf) == GDK_COLORSPACE_RGB);
61 g_assert (gdk_pixbuf_get_bits_per_sample (pixbuf) == 8);
62 g_assert (gdk_pixbuf_get_has_alpha (pixbuf));
64 n_channels = gdk_pixbuf_get_n_channels (pixbuf);
65 width = gdk_pixbuf_get_width (pixbuf);
66 height = gdk_pixbuf_get_height (pixbuf);
67 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
68 pixels = gdk_pixbuf_get_pixels (pixbuf);
70 g_assert (n_channels == 4);
72 w=gdk_pixbuf_get_width(pixbuf);
73 h=gdk_pixbuf_get_height(pixbuf);
74 g_assert (w % 16 == 0);
75 g_assert (h % 16 == 0);
77 if (strchr(argv[1], '.'))
78 *strchr(argv[1], '.')=0;
80 g_print("static const guchar %s[]={\n", argv[1]);
81 /* magic number 8 is NUM_OF_CELLS_X, the number of cells in a row */
82 g_print("/* cell size %d */\n", w/8);
83 g_print("%d, \n", w/8);
84 g_print("/* image data */\n");
85 i=0;
86 for (y=0; y<h; y++)
87 for (x=0; x<w; x++) {
88 g_print("%d, ", get_pixel(x, y));
89 if (i++ >25) {
90 g_print("\n");
91 i=0;
94 if (i!=0)
95 g_print("\n");
96 g_print("};\n");
97 return 0;