+gboolean pdf_draw_image(PdfImage *image, struct XrefTable *xref, GString *pdfbuf)
+{
+ char *buf, *p1, *p2;
+ int height, width, stride, x, y, chan;
+ GString *zpix;
+
+ if (gdk_pixbuf_get_bits_per_sample(image->pixbuf) != 8 ||
+ gdk_pixbuf_get_colorspace(image->pixbuf) != GDK_COLORSPACE_RGB) {
+ return FALSE;
+ }
+
+ width = gdk_pixbuf_get_width(image->pixbuf);
+ height = gdk_pixbuf_get_height(image->pixbuf);
+ stride = gdk_pixbuf_get_rowstride(image->pixbuf);
+ chan = gdk_pixbuf_get_n_channels(image->pixbuf);
+ if (!((chan==3 && !image->has_alpha) || (chan==4 && image->has_alpha))) {
+ return FALSE;
+ }
+
+ p2 = buf = (char *)g_malloc(3*width*height);
+ for (y=0; y<height; y++) {
+ p1 = (char *)gdk_pixbuf_get_pixels(image->pixbuf)+stride*y;
+ for (x=0; x<width; x++) {
+ *(p2++)=*(p1++); *(p2++)=*(p1++); *(p2++)=*(p1++);
+ if (chan==4) p1++;
+ }
+ }
+ zpix = do_deflate(buf, 3*width*height);
+ g_free(buf);
+
+ xref->data[image->n_obj] = pdfbuf->len;
+ g_string_append_printf(pdfbuf,
+ "%d 0 obj\n<< /Length %d /Filter /FlateDecode /Type /Xobject "
+ "/Subtype /Image /Width %d /Height %d /ColorSpace /DeviceRGB "
+ "/BitsPerComponent 8 ",
+ image->n_obj, zpix->len, width, height);
+ if (image->has_alpha) {
+ g_string_append_printf(pdfbuf,
+ "/SMask %d 0 R ",
+ image->n_obj_smask);
+ }
+ g_string_append_printf(pdfbuf, " >> stream\n");
+
+ g_string_append_len(pdfbuf, zpix->str, zpix->len);
+ g_string_free(zpix, TRUE);
+ g_string_append(pdfbuf, "endstream\nendobj\n");
+
+ if (image->has_alpha) {
+ p2 = buf = (char *)g_malloc(width*height);
+ for (y=0; y<height; y++) {
+ p1 = (char *)gdk_pixbuf_get_pixels(image->pixbuf)+stride*y;
+ for (x=0; x<width; x++) {
+ p1+=3; /* skip the RGB */
+ *(p2++)=*(p1++); /* just copy the alpha */
+ }
+ }
+ zpix = do_deflate(buf, width*height);
+ g_free(buf);
+
+ xref->data[image->n_obj_smask] = pdfbuf->len;
+ g_string_append_printf(pdfbuf,
+ "%d 0 obj\n<< /Length %d /Filter /FlateDecode /Type /Xobject "
+ "/Subtype /Image /Width %d /Height %d /ColorSpace /DeviceGray "
+ "/BitsPerComponent 8 >> stream\n",
+ image->n_obj_smask, zpix->len, width, height);
+
+ g_string_append_len(pdfbuf, zpix->str, zpix->len);
+ g_string_free(zpix, TRUE);
+ g_string_append(pdfbuf, "endstream\nendobj\n");
+ }
+
+ return TRUE;
+}
+
+