GFXprim
2D bitmap graphics library with emphasis on speed and correctness
Loading...
Searching...
No Matches
gp_pixmap.h
Go to the documentation of this file.
1// SPDX-License-Identifier: LGPL-2.1-or-later
2/*
3 * Copyright (C) 2009-2011 Jiri "BlueBear" Dluhos
4 * <jiri.bluebear.dluhos@gmail.com>
5 *
6 * Copyright (C) 2009-2024 Cyril Hrubis <metan@ucw.cz>
7 */
8
15#ifndef CORE_GP_PIXMAP_H
16#define CORE_GP_PIXMAP_H
17
18#include <stdint.h>
19#include <unistd.h>
20
21#include <core/gp_common.h>
22#include <core/gp_types.h>
23#include <core/gp_pixel.h>
25
33struct gp_pixmap {
35 uint8_t *pixels;
42 uint32_t bytes_per_row;
44 uint32_t w;
46 uint32_t h;
56 uint8_t offset;
57
64
71
79 uint8_t axes_swap:1;
87 uint8_t x_swap:1;
95 uint8_t y_swap:1;
96
106
113};
114
123#define GP_PIXEL_ADDR(pixmap, x, y) ((pixmap)->pixels \
124 + (y) * (pixmap)->bytes_per_row \
125 + ((x + (pixmap)->offset) * gp_pixel_size((pixmap)->pixel_type)) / 8)
126
127#define GP_CALC_ROW_SIZE(pixel_type, width) \
128 ((gp_pixel_size(pixel_type) * width) / 8 + \
129 !!((gp_pixel_size(pixel_type) * width) % 8))
130
131/* Performs a series of sanity checks on pixmap, aborting if any fails. */
132#define GP_CHECK_PIXMAP(pixmap) do { \
133 GP_CHECK(pixmap, "NULL passed as pixmap"); \
134 GP_CHECK(pixmap->pixels || pixmap->w == 0 || pixmap->h == 0, "invalid pixmap: pixels NULL on nonzero w h"); \
135} while (0)
136
145#define GP_PIXEL_IS_CLIPPED(pixmap, x, y) \
146 ((x) < 0 || x >= (typeof(x)) pixmap->w \
147 || (y) < 0 || y >= (typeof(y)) pixmap->h) \
148
184 uint32_t stride);
185
200{
201 return gp_pixmap_alloc_ex(w, h, type, 0);
202}
203
218
238static inline int gp_pixmap_gamma_set(gp_pixmap *self, float gamma)
239{
240 gp_correction_desc desc = {
241 .corr_type = GP_CORRECTION_TYPE_GAMMA,
242 .gamma = gamma,
243 };
244
245 return gp_pixmap_correction_set(self, &desc);
246}
247
265static inline int gp_pixmap_srgb_set(gp_pixmap *self)
266{
267 gp_correction_desc desc = {
268 .corr_type = GP_CORRECTION_TYPE_SRGB,
269 };
270
271 return gp_pixmap_correction_set(self, &desc);
272}
273
284
294
318 gp_pixel_type type, void *pixels,
319 enum gp_pixmap_init_flags flags);
320
341 gp_pixel_type type, uint32_t bpr,
342 void *pixels, enum gp_pixmap_init_flags flags);
343
363 gp_pixel_type type, void *pixels,
364 enum gp_pixmap_init_flags flags)
365{
366 gp_pixmap *ret = malloc(sizeof(gp_pixmap));
367 if (!ret)
368 return NULL;
369
370 return gp_pixmap_init(ret, w, h, type, pixels, flags | GP_PIXMAP_FREE_PIXMAP);
371}
372
392
404
415
434 gp_coord x, gp_coord y, gp_size w, gp_size h);
435
454 gp_coord x, gp_coord y, gp_size w, gp_size h);
455
456
457/*
458 * Converts pixmap to a different pixel type.
459 * Returns a newly allocated pixmap.
460 *
461 * This is naive implementation that doesn't do any ditherings or error
462 * diffusions.
463 */
464gp_pixmap *gp_pixmap_convert_alloc(const gp_pixmap *src,
465 gp_pixel_type dst_pixel_type);
466
467/*
468 * Converts pixmap to a different pixel type.
469 *
470 * This is naive implementation that doesn't do any ditherings or error
471 * diffusions.
472 */
473gp_pixmap *gp_pixmap_convert(const gp_pixmap *src, gp_pixmap *dst);
474
481
492
501static inline void gp_pixmap_rotate_cw(gp_pixmap *self)
502{
504}
505
514static inline void gp_pixmap_rotate_ccw(gp_pixmap *self)
515{
517}
518
527static inline int gp_pixmap_rotation_equal(const gp_pixmap *c1,
528 const gp_pixmap *c2)
529{
530 return c1->axes_swap == c2->axes_swap &&
531 c1->x_swap == c2->x_swap &&
532 c1->y_swap == c2->y_swap;
533}
534
543static inline void gp_pixmap_rotation_set(gp_pixmap *self, int axes_swap,
544 int x_swap, int y_swap)
545{
546 self->axes_swap = axes_swap;
547 self->x_swap = x_swap;
548 self->y_swap = y_swap;
549}
550
557static inline void gp_pixmap_rotation_copy(const gp_pixmap *src,
558 gp_pixmap *dst)
559{
560 dst->axes_swap = src->axes_swap;
561 dst->x_swap = src->x_swap;
562 dst->y_swap = src->y_swap;
563}
564
571static inline gp_size gp_pixmap_w(const gp_pixmap *self)
572{
573 if (self->axes_swap)
574 return self->h;
575 else
576 return self->w;
577}
578
585static inline gp_size gp_pixmap_h(const gp_pixmap *self)
586{
587 if (self->axes_swap)
588 return self->w;
589 else
590 return self->h;
591}
592
593/*
594 * Compare two pixmaps. Returns true only if all of types, sizes and
595 * bitmap data match. Takes transformations into account.
596 *
597 * For now ignores gamma tables.
598 *
599 * Currently rather slow (getpixel).
600 * TODO: speed up for same rotation and same bit-offset data (per-row memcpy).
601 */
602
603int gp_pixmap_equal(const gp_pixmap *pixmap1, const gp_pixmap *pixmap2);
604
605#endif /* CORE_GP_PIXMAP_H */
606
Common macros.
A common types.
int gp_coord
Integer type for coordinates i.e. x, y, ...
Definition gp_types.h:19
unsigned int gp_size
Integer type for sizes i.e. w, h, ...
Definition gp_types.h:24
gp_symmetry
Rotation and mirroring flags.
Definition gp_types.h:47
@ GP_ROTATE_CCW
Rotate counter clockwise. Alias for GP_ROTATE_270.
Definition gp_types.h:57
@ GP_ROTATE_CW
Rotate clockwise. Alias for GP_ROTATE_90.
Definition gp_types.h:51
Gamma and sRGB corrections.
gp_pixel_type
List of all pixel types.
A pixel description.
int gp_pixmap_resize(gp_pixmap *self, gp_size w, gp_size h)
Resizes pixmap.
gp_pixmap * gp_pixmap_init(gp_pixmap *pixmap, gp_size w, gp_size h, gp_pixel_type type, void *pixels, enum gp_pixmap_init_flags flags)
Initializes allocated pixmap structure.
static void gp_pixmap_rotation_copy(const gp_pixmap *src, gp_pixmap *dst)
Copies rotation flags from one pixmap to another.
Definition gp_pixmap.h:557
gp_pixmap * gp_pixmap_init_ex(gp_pixmap *pixmap, gp_size w, gp_size h, gp_pixel_type type, uint32_t bpr, void *pixels, enum gp_pixmap_init_flags flags)
Initializes allocated pixmap structure.
static int gp_pixmap_rotation_equal(const gp_pixmap *c1, const gp_pixmap *c2)
Compares rotation flags for two pixmaps.
Definition gp_pixmap.h:527
void gp_pixmap_rotate(gp_pixmap *self, gp_symmetry symmetry)
Rotates pixmap accordingly to rotate flags.
static void gp_pixmap_rotation_set(gp_pixmap *self, int axes_swap, int x_swap, int y_swap)
Sets pixmap rotation flags.
Definition gp_pixmap.h:543
static void gp_pixmap_rotate_cw(gp_pixmap *self)
Rotates pixmap flags clockwise.
Definition gp_pixmap.h:501
void gp_pixmap_print_info(const gp_pixmap *self)
Prints pixmap information into stdout.
gp_pixmap_copy_flags
A pixmap copy flags.
Definition gp_pixmap.h:396
@ GP_PIXMAP_COPY_PIXELS
Definition gp_pixmap.h:398
@ GP_PIXMAP_COPY_ROTATION
Definition gp_pixmap.h:400
@ GP_PIXMAP_COPY_GAMMA
Definition gp_pixmap.h:402
gp_pixmap_init_flags
A pixmap init flags.
Definition gp_pixmap.h:288
@ GP_PIXMAP_FREE_PIXMAP
Definition gp_pixmap.h:290
@ GP_PIXMAP_FREE_PIXELS
Definition gp_pixmap.h:292
gp_pixmap * gp_sub_pixmap_alloc(const gp_pixmap *src, gp_coord x, gp_coord y, gp_size w, gp_size h)
Allocate and initalize a subpixmap.
static gp_size gp_pixmap_w(const gp_pixmap *self)
Returns pixmap width taking axes swap into account.
Definition gp_pixmap.h:571
gp_pixmap * gp_sub_pixmap(const gp_pixmap *src, gp_pixmap *subpixmap, gp_coord x, gp_coord y, gp_size w, gp_size h)
Initializes a subpixmap.
static void gp_pixmap_rotate_ccw(gp_pixmap *self)
Rotates pixmap flags counter clock wise.
Definition gp_pixmap.h:514
static gp_size gp_pixmap_h(const gp_pixmap *self)
Returns pixmap height taking axes swap into account.
Definition gp_pixmap.h:585
gp_pixmap * gp_pixmap_copy(const gp_pixmap *src, enum gp_pixmap_copy_flags flags)
Copies a pixmap.
@ GP_CORRECTION_TYPE_GAMMA
Classical gamma correction.
@ GP_CORRECTION_TYPE_SRGB
Standard RGB.
static int gp_pixmap_srgb_set(gp_pixmap *self)
Sets a sRGB correction for the pixmap.
Definition gp_pixmap.h:265
static int gp_pixmap_gamma_set(gp_pixmap *self, float gamma)
Sets a gamma correction for the pixmap.
Definition gp_pixmap.h:238
static gp_pixmap * gp_pixmap_from_data(gp_size w, gp_size h, gp_pixel_type type, void *pixels, enum gp_pixmap_init_flags flags)
Creates a pixmap from a buffer allocated by malloc().
Definition gp_pixmap.h:362
static gp_pixmap * gp_pixmap_alloc(gp_size w, gp_size h, gp_pixel_type type)
Allocates a pixmap.
Definition gp_pixmap.h:199
int gp_pixmap_correction_set(gp_pixmap *self, gp_correction_desc *corr_desc)
Sets a correction for the pixmap.
gp_pixmap * gp_pixmap_alloc_ex(gp_size w, gp_size h, gp_pixel_type type, uint32_t stride)
Allocates a pixmap.
void gp_pixmap_free(gp_pixmap *self)
Frees a pixmap.
A correction description.
A correction tables for all pixel channels.
A pixmap buffer.
Definition gp_pixmap.h:33
uint32_t h
Pixmap height in pixels.
Definition gp_pixmap.h:46
uint8_t * pixels
A pointer to image pixels.
Definition gp_pixmap.h:35
uint8_t x_swap
Mirrors image x axis for drawing.
Definition gp_pixmap.h:87
uint8_t pixels_allocated
Set if pixels were allocatd by malloc().
Definition gp_pixmap.h:105
uint8_t y_swap
Mirrors image y axis for drawing.
Definition gp_pixmap.h:95
enum gp_pixel_type pixel_type
A pixel format.
Definition gp_pixmap.h:63
uint32_t bytes_per_row
Number of bytes per row.
Definition gp_pixmap.h:42
gp_gamma * gamma
A pointer to a gamma correction table.
Definition gp_pixmap.h:70
uint32_t w
Pixmap width in pixels.
Definition gp_pixmap.h:44
uint8_t offset
An offset to apply before the start of the pixel row.
Definition gp_pixmap.h:56
uint8_t axes_swap
Swaps image x and y axes for drawing.
Definition gp_pixmap.h:79
uint8_t pixmap_allocated
Set if pixmap was allocated by malloc().
Definition gp_pixmap.h:112