iPXE
pixbuf.c
Go to the documentation of this file.
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 FILE_SECBOOT ( PERMITTED );
26 
27 /** @file
28  *
29  * Pixel buffer
30  *
31  */
32 
33 #include <stdlib.h>
34 #include <string.h>
35 #include <errno.h>
36 #include <ipxe/umalloc.h>
37 #include <ipxe/image.h>
38 #include <ipxe/pixbuf.h>
39 
40 /**
41  * Free pixel buffer
42  *
43  * @v refcnt Reference count
44  */
45 static void free_pixbuf ( struct refcnt *refcnt ) {
46  struct pixel_buffer *pixbuf =
48 
49  ufree ( pixbuf->data );
50  free ( pixbuf );
51 }
52 
53 /**
54  * Allocate pixel buffer
55  *
56  * @v width Width
57  * @h height Height
58  * @ret pixbuf Pixel buffer, or NULL on failure
59  */
60 struct pixel_buffer * alloc_pixbuf ( unsigned int width, unsigned int height ) {
61  struct pixel_buffer *pixbuf;
62 
63  /* Allocate and initialise structure */
64  pixbuf = zalloc ( sizeof ( *pixbuf ) );
65  if ( ! pixbuf )
66  goto err_alloc_pixbuf;
67  ref_init ( &pixbuf->refcnt, free_pixbuf );
68  pixbuf->width = width;
69  pixbuf->height = height;
70  pixbuf->pixels = ( width * height );
71  pixbuf->len = ( pixbuf->pixels * sizeof ( uint32_t ) );
72 
73  /* Check for multiplication overflow */
74  if ( ( width != 0 ) &&
75  ( ( pixbuf->len / sizeof ( uint32_t ) ) / width ) != height ) {
76  goto err_overflow;
77  }
78 
79  /* Allocate pixel data buffer */
80  pixbuf->data = umalloc ( pixbuf->len );
81  if ( ! pixbuf->data )
82  goto err_alloc_data;
83 
84  return pixbuf;
85 
86  err_alloc_data:
87  err_overflow:
88  pixbuf_put ( pixbuf );
89  err_alloc_pixbuf:
90  return NULL;
91 }
92 
93 /**
94  * Create pixel buffer from image
95  *
96  * @v image Image
97  * @v pixbuf Pixel buffer to fill in
98  * @ret rc Return status code
99  */
100 int image_pixbuf ( struct image *image, struct pixel_buffer **pixbuf ) {
101  int rc;
102 
103  /* Check that this image can be used to create a pixel buffer */
104  if ( ! ( image->type && image->type->pixbuf ) )
105  return -ENOTSUP;
106 
107  /* Try creating pixel buffer */
108  if ( ( rc = image->type->pixbuf ( image, pixbuf ) ) != 0 ) {
109  DBGC ( image, "IMAGE %s could not create pixel buffer: %s\n",
110  image->name, strerror ( rc ) );
111  return rc;
112  }
113 
114  return 0;
115 }
116 
117 /* Drag in objects via image_pixbuf() */
119 
120 /* Drag in pixel buffer image formats */
121 REQUIRE_OBJECT ( config_pixbuf );
int image_pixbuf(struct image *image, struct pixel_buffer **pixbuf)
Create pixel buffer from image.
Definition: pixbuf.c:100
static __always_inline void ufree(void *ptr)
Free external memory.
Definition: umalloc.h:68
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
struct pixel_buffer * alloc_pixbuf(unsigned int width, unsigned int height)
Allocate pixel buffer.
Definition: pixbuf.c:60
static void free_pixbuf(struct refcnt *refcnt)
Free pixel buffer.
Definition: pixbuf.c:45
#define ref_init(refcnt, free)
Initialise a reference counter.
Definition: refcnt.h:65
Error codes.
FILE_SECBOOT(PERMITTED)
unsigned int height
Height.
Definition: pixbuf.h:23
struct image_type * type
Image type, if known.
Definition: image.h:59
#define DBGC(...)
Definition: compiler.h:505
An executable image.
Definition: image.h:24
Pixel buffer.
unsigned int pixels
Total number of pixels.
Definition: pixbuf.h:27
#define ENOTSUP
Operation not supported.
Definition: errno.h:590
A reference counter.
Definition: refcnt.h:27
size_t len
Total length.
Definition: pixbuf.h:29
#define container_of(ptr, type, field)
Get containing structure.
Definition: stddef.h:36
Executable images.
uint32_t * data
32-bit (8:8:8:8) xRGB pixel data, in host-endian order
Definition: pixbuf.h:25
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:79
static void(* free)(struct refcnt *refcnt))
Definition: refcnt.h:55
void * zalloc(size_t size)
Allocate cleared memory.
Definition: malloc.c:662
A pixel buffer.
Definition: pixbuf.h:17
User memory allocation.
struct refcnt refcnt
Reference count.
Definition: pixbuf.h:19
unsigned int uint32_t
Definition: stdint.h:12
static __always_inline void * umalloc(size_t size)
Allocate external memory.
Definition: umalloc.h:57
int(* pixbuf)(struct image *image, struct pixel_buffer **pixbuf)
Create pixel buffer from image.
Definition: image.h:121
REQUIRING_SYMBOL(image_pixbuf)
char * name
Name.
Definition: image.h:38
#define NULL
NULL pointer (VOID *)
Definition: Base.h:322
String functions.
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
unsigned int width
Width.
Definition: pixbuf.h:21
REQUIRE_OBJECT(config_pixbuf)