1 | /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ |
---|
2 | /* |
---|
3 | * example_02.c: sample gnome-print code |
---|
4 | * |
---|
5 | * This program is free software; you can redistribute it and/or |
---|
6 | * modify it under the terms of the GNU Library General Public License |
---|
7 | * as published by the Free Software Foundation; either version 2 of |
---|
8 | * the License, or (at your option) any later version. |
---|
9 | * |
---|
10 | * This program is distributed in the hope that it will be useful, |
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
13 | * GNU Library General Public License for more details. |
---|
14 | * |
---|
15 | * You should have received a copy of the GNU Library General Public |
---|
16 | * License along with this program; if not, write to the Free Software |
---|
17 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
18 | * |
---|
19 | * Authors: |
---|
20 | * Chema Celorio <chema@ximian.com> |
---|
21 | * |
---|
22 | * Copyright (C) 2002 Ximian Inc. and authors |
---|
23 | * |
---|
24 | */ |
---|
25 | |
---|
26 | /* |
---|
27 | * See README |
---|
28 | */ |
---|
29 | |
---|
30 | #include <libgnomeprint/gnome-print.h> |
---|
31 | #include <libgnomeprint/gnome-print-job.h> |
---|
32 | #include <gdk-pixbuf/gdk-pixbuf.h> |
---|
33 | |
---|
34 | #define NUMBER_OF_PIXELS 256 |
---|
35 | |
---|
36 | static void |
---|
37 | my_print_image_from_pixbuf (GnomePrintContext *gpc, GdkPixbuf *pixbuf) |
---|
38 | { |
---|
39 | guchar *raw_image; |
---|
40 | gboolean has_alpha; |
---|
41 | gint rowstride, height, width; |
---|
42 | |
---|
43 | raw_image = gdk_pixbuf_get_pixels (pixbuf); |
---|
44 | has_alpha = gdk_pixbuf_get_has_alpha (pixbuf); |
---|
45 | rowstride = gdk_pixbuf_get_rowstride (pixbuf); |
---|
46 | height = gdk_pixbuf_get_height (pixbuf); |
---|
47 | width = gdk_pixbuf_get_width (pixbuf); |
---|
48 | |
---|
49 | if (has_alpha) |
---|
50 | gnome_print_rgbaimage (gpc, (char *)raw_image, width, height, rowstride); |
---|
51 | else |
---|
52 | gnome_print_rgbimage (gpc, (char *)raw_image, width, height, rowstride); |
---|
53 | } |
---|
54 | |
---|
55 | static void |
---|
56 | my_print_image_from_disk (GnomePrintContext *gpc) |
---|
57 | { |
---|
58 | GdkPixbuf *pixbuf; |
---|
59 | GError *error; |
---|
60 | |
---|
61 | /* Load the image into a pixbuf */ |
---|
62 | error = NULL; |
---|
63 | pixbuf = gdk_pixbuf_new_from_file ("sample-image.png", &error); |
---|
64 | if (!pixbuf) { |
---|
65 | g_print ("Could not load sample_image.png.\n"); |
---|
66 | return; |
---|
67 | } |
---|
68 | |
---|
69 | /* Save the graphic context, scale, print the image and restore */ |
---|
70 | gnome_print_gsave (gpc); |
---|
71 | gnome_print_scale (gpc, 144, 144); |
---|
72 | my_print_image_from_pixbuf (gpc, pixbuf); |
---|
73 | gnome_print_grestore (gpc); |
---|
74 | |
---|
75 | g_object_unref (G_OBJECT (pixbuf)); |
---|
76 | } |
---|
77 | |
---|
78 | static void |
---|
79 | my_print_image_from_memory (GnomePrintContext *gpc) |
---|
80 | { |
---|
81 | gchar color_image [NUMBER_OF_PIXELS] [NUMBER_OF_PIXELS] [3]; |
---|
82 | gint pixels = NUMBER_OF_PIXELS; |
---|
83 | gint x, y; |
---|
84 | |
---|
85 | /* Create the image in memory */ |
---|
86 | for (y = 0; y < pixels; y++) { |
---|
87 | for (x = 0; x < pixels; x++) { |
---|
88 | color_image [y][x][0] = (x + y) >> 1; |
---|
89 | color_image [y][x][1] = (x + (pixels - 1 - y)) >> 1; |
---|
90 | color_image [y][x][2] = ((pixels - 1 - x) + y) >> 1; |
---|
91 | } |
---|
92 | } |
---|
93 | |
---|
94 | /* All images in postscript are printed on a 1 x 1 square, since we |
---|
95 | * want an image which has a size of 2" by 2" inches, we have to scale |
---|
96 | * the CTM (Current Transformation Matrix). Save the graphic state and |
---|
97 | * restore it after we are done so that our scaling does not affect the |
---|
98 | * drawing calls that follow. |
---|
99 | */ |
---|
100 | gnome_print_gsave (gpc); |
---|
101 | gnome_print_scale (gpc, 144, 144); |
---|
102 | gnome_print_rgbimage (gpc, (char *)color_image, pixels, pixels, pixels * 3); |
---|
103 | gnome_print_grestore (gpc); |
---|
104 | } |
---|
105 | |
---|
106 | static void |
---|
107 | my_draw (GnomePrintContext *gpc) |
---|
108 | { |
---|
109 | gnome_print_beginpage (gpc, "1"); |
---|
110 | |
---|
111 | gnome_print_translate (gpc, 200, 100); |
---|
112 | my_print_image_from_memory (gpc); |
---|
113 | |
---|
114 | gnome_print_translate (gpc, 0, 150); |
---|
115 | my_print_image_from_disk (gpc); |
---|
116 | |
---|
117 | gnome_print_showpage (gpc); |
---|
118 | } |
---|
119 | |
---|
120 | static void |
---|
121 | my_print (void) |
---|
122 | { |
---|
123 | GnomePrintJob *job; |
---|
124 | GnomePrintContext *gpc; |
---|
125 | |
---|
126 | job = gnome_print_job_new (NULL); |
---|
127 | gpc = gnome_print_job_get_context (job); |
---|
128 | |
---|
129 | my_draw (gpc); |
---|
130 | |
---|
131 | gnome_print_job_close (job); |
---|
132 | gnome_print_job_print (job); |
---|
133 | |
---|
134 | g_object_unref (gpc); |
---|
135 | g_object_unref (job); |
---|
136 | } |
---|
137 | |
---|
138 | int |
---|
139 | main (int argc, char * argv[]) |
---|
140 | { |
---|
141 | g_type_init (); |
---|
142 | |
---|
143 | my_print (); |
---|
144 | |
---|
145 | g_print ("Done...\n"); |
---|
146 | |
---|
147 | return 0; |
---|
148 | } |
---|