Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 <ipxe/umalloc.h>
  31. #include <ipxe/pixbuf.h>
  32. /**
  33. * Free pixel buffer
  34. *
  35. * @v refcnt Reference count
  36. */
  37. static void free_pixbuf ( struct refcnt *refcnt ) {
  38. struct pixel_buffer *pixbuf =
  39. container_of ( refcnt, struct pixel_buffer, refcnt );
  40. ufree ( pixbuf->data );
  41. free ( pixbuf );
  42. }
  43. /**
  44. * Allocate pixel buffer
  45. *
  46. * @v width Width
  47. * @h height Height
  48. * @ret pixbuf Pixel buffer, or NULL on failure
  49. */
  50. struct pixel_buffer * alloc_pixbuf ( unsigned int width, unsigned int height ) {
  51. struct pixel_buffer *pixbuf;
  52. /* Allocate and initialise structure */
  53. pixbuf = zalloc ( sizeof ( *pixbuf ) );
  54. if ( ! pixbuf )
  55. goto err_alloc_pixbuf;
  56. ref_init ( &pixbuf->refcnt, free_pixbuf );
  57. pixbuf->width = width;
  58. pixbuf->height = height;
  59. pixbuf->len = ( width * height * sizeof ( uint32_t ) );
  60. /* Check for multiplication overflow */
  61. if ( ( ( pixbuf->len / sizeof ( uint32_t ) ) / width ) != height )
  62. goto err_overflow;
  63. /* Allocate pixel data buffer */
  64. pixbuf->data = umalloc ( pixbuf->len );
  65. if ( ! pixbuf->data )
  66. goto err_alloc_data;
  67. return pixbuf;
  68. err_alloc_data:
  69. err_overflow:
  70. pixbuf_put ( pixbuf );
  71. err_alloc_pixbuf:
  72. return NULL;
  73. }