GCC Code Coverage Report


Directory: ./
File: image.c
Date: 2026-01-16 15:19:28
Exec Total Coverage
Lines: 22 26 84.6%
Functions: 2 2 100.0%
Branches: 14 18 77.8%

Line Branch Exec Source
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
1/2
✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 2 times.
2 if (image == NULL) {
17 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
7/8
✓ Branch 0 (2→3) taken 5 times.
✓ Branch 1 (2→6) taken 1 times.
✓ Branch 2 (3→4) taken 4 times.
✓ Branch 3 (3→6) taken 1 times.
✓ Branch 4 (4→5) taken 3 times.
✓ Branch 5 (4→6) taken 1 times.
✗ Branch 6 (5→6) not taken.
✓ Branch 7 (5→7) taken 3 times.
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
5/6
✓ Branch 0 (7→8) taken 2 times.
✓ Branch 1 (7→11) taken 1 times.
✓ Branch 2 (8→9) taken 2 times.
✗ Branch 3 (8→11) not taken.
✓ Branch 4 (9→10) taken 1 times.
✓ Branch 5 (9→11) taken 1 times.
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
1/2
✗ Branch 0 (12→13) not taken.
✓ Branch 1 (12→14) taken 2 times.
2 if (image.pixels == NULL) {
71 image.width = 0;
72 image.height = 0;
73 image.bpp = 0;
74 }
75 2 return image;
76 }
77
78