LCOV - code coverage report
Current view: top level - image - image.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 84.6 % 26 22
Test Date: 2026-01-18 16:11:12 Functions: 100.0 % 2 2

            Line data    Source code
       1              : #include <stdlib.h>
       2              : 
       3              : #include "image.h"
       4              : 
       5              : 
       6              : 
       7              : /**
       8              :  * Compute the total size in bytes of an image's pixel buffer.
       9              :  *
      10              :  * @param image Pointer to the image whose buffer size will be computed.
      11              :  *
      12              :  * @returns Total number of bytes required for the image's pixel buffer
      13              :  *          (width * height * bpp).
      14              :  */
      15            2 : size_t image_size(const image_t *image) {
      16            2 :     if (image == NULL) {
      17            0 :         return 0;
      18              :     }
      19              :     /* cast to size_t before multiplication to prevent overflow */
      20            2 :     return (size_t)image->width * (size_t)image->height * (size_t)image->bpp;
      21              : }
      22              : 
      23              : 
      24              : 
      25              : /**
      26              :  * Create an image_t with the given width, height, and bytes-per-pixel,
      27              :  * allocating a pixel buffer.
      28              :  *
      29              :  * The returned image_t fields width, height, and bpp are initialized and
      30              :  * pixels points to a newly allocated buffer of size width * height * bpp. If
      31              :  * allocation fails, pixels will be NULL.
      32              :  *
      33              :  * @param width  Image width specified in pixels.
      34              :  * @param height Image height specified in pixels.
      35              :  * @param bpp    Bytes per pixel (bytes used to store a single pixel).
      36              :  *
      37              :  * @returns The initialized image_t; its `pixels` member points to the
      38              :  *          allocated buffer or NULL on allocation failure.
      39              :  */
      40            6 : image_t image_create(const unsigned int width, const unsigned int height, const unsigned int bpp) {
      41              :     image_t image;
      42              : 
      43              :     /* validate image size */
      44            6 :     if (width == 0 || height == 0 || width > MAX_WIDTH || height > MAX_HEIGHT) {
      45            3 :         image.width = 0;
      46            3 :         image.height = 0;
      47            3 :         image.bpp = 0;
      48            3 :         image.pixels = NULL;
      49            3 :         return image;
      50              :     }
      51              : 
      52              :     /* validate image type */
      53            3 :     if (bpp != GRAYSCALE && bpp != RGB && bpp != RGBA) {
      54            1 :         image.width = 0;
      55            1 :         image.height = 0;
      56            1 :         image.bpp = 0;
      57            1 :         image.pixels = NULL;
      58            1 :         return image;
      59              :     }
      60              : 
      61              :     /* initialize image */
      62            2 :     image.width = width;
      63            2 :     image.height = height;
      64            2 :     image.bpp = bpp;
      65              : 
      66              :     /* callers must check that image.pixels != NULL */
      67            2 :     image.pixels = (unsigned char *)malloc(image_size(&image));
      68              : 
      69              :     /* make sure the image will be 'zero value' when pixels are not allocated */
      70            2 :     if (image.pixels == NULL) {
      71            0 :         image.width = 0;
      72            0 :         image.height = 0;
      73            0 :         image.bpp = 0;
      74              :     }
      75            2 :     return image;
      76              : }
      77              : 
        

Generated by: LCOV version 2.0-1