iPXE
pixbuf.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2013 Michael Brown <mbrown@fensystems.co.uk>.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License as
00006  * published by the Free Software Foundation; either version 2 of the
00007  * License, or any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017  * 02110-1301, USA.
00018  *
00019  * You can also choose to distribute this program under the terms of
00020  * the Unmodified Binary Distribution Licence (as given in the file
00021  * COPYING.UBDL), provided that you have satisfied its requirements.
00022  */
00023 
00024 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00025 
00026 /** @file
00027  *
00028  * Pixel buffer
00029  *
00030  */
00031 
00032 #include <stdlib.h>
00033 #include <errno.h>
00034 #include <ipxe/umalloc.h>
00035 #include <ipxe/image.h>
00036 #include <ipxe/pixbuf.h>
00037 
00038 /**
00039  * Free pixel buffer
00040  *
00041  * @v refcnt            Reference count
00042  */
00043 static void free_pixbuf ( struct refcnt *refcnt ) {
00044         struct pixel_buffer *pixbuf =
00045                 container_of ( refcnt, struct pixel_buffer, refcnt );
00046 
00047         ufree ( pixbuf->data );
00048         free ( pixbuf );
00049 }
00050 
00051 /**
00052  * Allocate pixel buffer
00053  *
00054  * @v width             Width
00055  * @h height            Height
00056  * @ret pixbuf          Pixel buffer, or NULL on failure
00057  */
00058 struct pixel_buffer * alloc_pixbuf ( unsigned int width, unsigned int height ) {
00059         struct pixel_buffer *pixbuf;
00060 
00061         /* Allocate and initialise structure */
00062         pixbuf = zalloc ( sizeof ( *pixbuf ) );
00063         if ( ! pixbuf )
00064                 goto err_alloc_pixbuf;
00065         ref_init ( &pixbuf->refcnt, free_pixbuf );
00066         pixbuf->width = width;
00067         pixbuf->height = height;
00068         pixbuf->len = ( width * height * sizeof ( uint32_t ) );
00069 
00070         /* Check for multiplication overflow */
00071         if ( ( width != 0 ) &&
00072              ( ( pixbuf->len / sizeof ( uint32_t ) ) / width ) != height ) {
00073                 goto err_overflow;
00074         }
00075 
00076         /* Allocate pixel data buffer */
00077         pixbuf->data = umalloc ( pixbuf->len );
00078         if ( ! pixbuf->data )
00079                 goto err_alloc_data;
00080 
00081         return pixbuf;
00082 
00083  err_alloc_data:
00084  err_overflow:
00085         pixbuf_put ( pixbuf );
00086  err_alloc_pixbuf:
00087         return NULL;
00088 }
00089 
00090 /**
00091  * Create pixel buffer from image
00092  *
00093  * @v image             Image
00094  * @v pixbuf            Pixel buffer to fill in
00095  * @ret rc              Return status code
00096  */
00097 int image_pixbuf ( struct image *image, struct pixel_buffer **pixbuf ) {
00098         int rc;
00099 
00100         /* Check that this image can be used to create a pixel buffer */
00101         if ( ! ( image->type && image->type->pixbuf ) )
00102                 return -ENOTSUP;
00103 
00104         /* Try creating pixel buffer */
00105         if ( ( rc = image->type->pixbuf ( image, pixbuf ) ) != 0 ) {
00106                 DBGC ( image, "IMAGE %s could not create pixel buffer: %s\n",
00107                        image->name, strerror ( rc ) );
00108                 return rc;
00109         }
00110 
00111         return 0;
00112 }
00113 
00114 /* Drag in objects via image_pixbuf() */
00115 REQUIRING_SYMBOL ( image_pixbuf );
00116 
00117 /* Drag in pixel buffer image formats */
00118 REQUIRE_OBJECT ( config_pixbuf );