[pixbuf] Enable PNG format by default
[ipxe.git] / src / core / pixbuf.c
1 /*
2 * Copyright (C) 2013 Michael Brown <mbrown@fensystems.co.uk>.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * 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, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA.
18 *
19 * You can also choose to distribute this program under the terms of
20 * the Unmodified Binary Distribution Licence (as given in the file
21 * COPYING.UBDL), provided that you have satisfied its requirements.
22 */
23
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25
26 /** @file
27 *
28 * Pixel buffer
29 *
30 */
31
32 #include <stdlib.h>
33 #include <errno.h>
34 #include <ipxe/umalloc.h>
35 #include <ipxe/image.h>
36 #include <ipxe/pixbuf.h>
37
38 /**
39 * Free pixel buffer
40 *
41 * @v refcnt Reference count
42 */
43 static void free_pixbuf ( struct refcnt *refcnt ) {
44 struct pixel_buffer *pixbuf =
45 container_of ( refcnt, struct pixel_buffer, refcnt );
46
47 ufree ( pixbuf->data );
48 free ( pixbuf );
49 }
50
51 /**
52 * Allocate pixel buffer
53 *
54 * @v width Width
55 * @h height Height
56 * @ret pixbuf Pixel buffer, or NULL on failure
57 */
58 struct pixel_buffer * alloc_pixbuf ( unsigned int width, unsigned int height ) {
59 struct pixel_buffer *pixbuf;
60
61 /* Allocate and initialise structure */
62 pixbuf = zalloc ( sizeof ( *pixbuf ) );
63 if ( ! pixbuf )
64 goto err_alloc_pixbuf;
65 ref_init ( &pixbuf->refcnt, free_pixbuf );
66 pixbuf->width = width;
67 pixbuf->height = height;
68 pixbuf->len = ( width * height * sizeof ( uint32_t ) );
69
70 /* Check for multiplication overflow */
71 if ( ( ( pixbuf->len / sizeof ( uint32_t ) ) / width ) != height )
72 goto err_overflow;
73
74 /* Allocate pixel data buffer */
75 pixbuf->data = umalloc ( pixbuf->len );
76 if ( ! pixbuf->data )
77 goto err_alloc_data;
78
79 return pixbuf;
80
81 err_alloc_data:
82 err_overflow:
83 pixbuf_put ( pixbuf );
84 err_alloc_pixbuf:
85 return NULL;
86 }
87
88 /**
89 * Create pixel buffer from image
90 *
91 * @v image Image
92 * @v pixbuf Pixel buffer to fill in
93 * @ret rc Return status code
94 */
95 int image_pixbuf ( struct image *image, struct pixel_buffer **pixbuf ) {
96 int rc;
97
98 /* Check that this image can be used to create a pixel buffer */
99 if ( ! ( image->type && image->type->pixbuf ) )
100 return -ENOTSUP;
101
102 /* Try creating pixel buffer */
103 if ( ( rc = image->type->pixbuf ( image, pixbuf ) ) != 0 ) {
104 DBGC ( image, "IMAGE %s could not create pixel buffer: %s\n",
105 image->name, strerror ( rc ) );
106 return rc;
107 }
108
109 return 0;
110 }
111
112 /* Drag in objects via image_pixbuf() */
113 REQUIRING_SYMBOL ( image_pixbuf );
114
115 /* Drag in pixel buffer image formats */
116 REQUIRE_OBJECT ( config_pixbuf );