You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

pixbuf.c 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. /** @file
  25. *
  26. * Pixel buffer
  27. *
  28. */
  29. #include <stdlib.h>
  30. #include <errno.h>
  31. #include <ipxe/umalloc.h>
  32. #include <ipxe/image.h>
  33. #include <ipxe/pixbuf.h>
  34. /**
  35. * Free pixel buffer
  36. *
  37. * @v refcnt Reference count
  38. */
  39. static void free_pixbuf ( struct refcnt *refcnt ) {
  40. struct pixel_buffer *pixbuf =
  41. container_of ( refcnt, struct pixel_buffer, refcnt );
  42. ufree ( pixbuf->data );
  43. free ( pixbuf );
  44. }
  45. /**
  46. * Allocate pixel buffer
  47. *
  48. * @v width Width
  49. * @h height Height
  50. * @ret pixbuf Pixel buffer, or NULL on failure
  51. */
  52. struct pixel_buffer * alloc_pixbuf ( unsigned int width, unsigned int height ) {
  53. struct pixel_buffer *pixbuf;
  54. /* Allocate and initialise structure */
  55. pixbuf = zalloc ( sizeof ( *pixbuf ) );
  56. if ( ! pixbuf )
  57. goto err_alloc_pixbuf;
  58. ref_init ( &pixbuf->refcnt, free_pixbuf );
  59. pixbuf->width = width;
  60. pixbuf->height = height;
  61. pixbuf->len = ( width * height * sizeof ( uint32_t ) );
  62. /* Check for multiplication overflow */
  63. if ( ( width != 0 ) &&
  64. ( ( pixbuf->len / sizeof ( uint32_t ) ) / width ) != height ) {
  65. goto err_overflow;
  66. }
  67. /* Allocate pixel data buffer */
  68. pixbuf->data = umalloc ( pixbuf->len );
  69. if ( ! pixbuf->data )
  70. goto err_alloc_data;
  71. return pixbuf;
  72. err_alloc_data:
  73. err_overflow:
  74. pixbuf_put ( pixbuf );
  75. err_alloc_pixbuf:
  76. return NULL;
  77. }
  78. /**
  79. * Create pixel buffer from image
  80. *
  81. * @v image Image
  82. * @v pixbuf Pixel buffer to fill in
  83. * @ret rc Return status code
  84. */
  85. int image_pixbuf ( struct image *image, struct pixel_buffer **pixbuf ) {
  86. int rc;
  87. /* Check that this image can be used to create a pixel buffer */
  88. if ( ! ( image->type && image->type->pixbuf ) )
  89. return -ENOTSUP;
  90. /* Try creating pixel buffer */
  91. if ( ( rc = image->type->pixbuf ( image, pixbuf ) ) != 0 ) {
  92. DBGC ( image, "IMAGE %s could not create pixel buffer: %s\n",
  93. image->name, strerror ( rc ) );
  94. return rc;
  95. }
  96. return 0;
  97. }
  98. /* Drag in objects via image_pixbuf() */
  99. REQUIRING_SYMBOL ( image_pixbuf );
  100. /* Drag in pixel buffer image formats */
  101. REQUIRE_OBJECT ( config_pixbuf );