371578ba222ecb00faef561e5eba46f20ec561db
[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 /* Creates and returns a new image item containing IMAGE.  Takes ownership of
33    IMAGE. */
34 struct image_item *
35 image_item_create (cairo_surface_t *image)
36 {
37   struct image_item *item = xmalloc (sizeof *item);
38   *item = (struct image_item) {
39     .output_item = OUTPUT_ITEM_INITIALIZER (&image_item_class),
40     .image = image,
41   };
42   return item;
43 }
44
45 /* Submits ITEM to the configured output drivers, and transfers ownership to
46    the output subsystem. */
47 void
48 image_item_submit (struct image_item *item)
49 {
50   output_submit (&item->output_item);
51 }
52
53 struct image_item *
54 image_item_unshare (struct image_item *old)
55 {
56   assert (old->output_item.ref_cnt > 0);
57   if (!image_item_is_shared (old))
58     return old;
59   image_item_unref (old);
60
61   struct image_item *new = xmalloc (sizeof *new);
62   *new = (struct image_item) {
63     .output_item = OUTPUT_ITEM_CLONE_INITIALIZER (&old->output_item),
64     .image = cairo_surface_reference (old->image),
65   };
66   return new;
67 }
68 \f
69 static const char *
70 image_item_get_label (const struct output_item *output_item UNUSED)
71 {
72   return "Image";
73 }
74
75 static void
76 image_item_destroy (struct output_item *output_item)
77 {
78   struct image_item *item = to_image_item (output_item);
79   cairo_surface_destroy (item->image);
80   free (item);
81 }
82
83 const struct output_item_class image_item_class =
84   {
85     image_item_get_label,
86     image_item_destroy,
87   };