Add support for PNG images in .spv files.
[pspp] / src / output / image-item.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2020 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17 #include <config.h>
18
19 #include "output/image-item.h"
20
21 #include <stdarg.h>
22 #include <stdlib.h>
23
24 #include "libpspp/cast.h"
25 #include "libpspp/compiler.h"
26 #include "output/driver.h"
27 #include "output/output-item-provider.h"
28
29 #include "gl/xalloc.h"
30 #include "gl/xvasprintf.h"
31
32 #ifdef HAVE_CAIRO
33 /* Creates and returns a new image item containing IMAGE.  Takes ownership of
34    IMAGE. */
35 struct image_item *
36 image_item_create (cairo_surface_t *image)
37 {
38   struct image_item *item = xmalloc (sizeof *item);
39   *item = (struct image_item) {
40     .output_item = OUTPUT_ITEM_INITIALIZER (&image_item_class),
41     .image = image,
42   };
43   return item;
44 }
45 #endif
46
47 /* Submits ITEM to the configured output drivers, and transfers ownership to
48    the output subsystem. */
49 void
50 image_item_submit (struct image_item *item)
51 {
52   output_submit (&item->output_item);
53 }
54
55 struct image_item *
56 image_item_unshare (struct image_item *old)
57 {
58   assert (old->output_item.ref_cnt > 0);
59   if (!image_item_is_shared (old))
60     return old;
61   image_item_unref (old);
62
63   struct image_item *new = xmalloc (sizeof *new);
64   *new = (struct image_item) {
65     .output_item = OUTPUT_ITEM_CLONE_INITIALIZER (&old->output_item),
66 #ifdef HAVE_CAIRO
67     .image = cairo_surface_reference (old->image),
68 #endif
69   };
70   return new;
71 }
72 \f
73 static const char *
74 image_item_get_label (const struct output_item *output_item UNUSED)
75 {
76   return "Image";
77 }
78
79 static void
80 image_item_destroy (struct output_item *output_item)
81 {
82   struct image_item *item = to_image_item (output_item);
83 #ifdef HAVE_CAIRO
84   cairo_surface_destroy (item->image);
85 #endif
86   free (item);
87 }
88
89 const struct output_item_class image_item_class =
90   {
91     image_item_get_label,
92     image_item_destroy,
93   };