core: Remove GP_ContextDump().
[gfxprim.git] / include / core / GP_Context.h
blob505748170737825db9cb7921f80b523f16908c19
1 /*****************************************************************************
2 * This file is part of gfxprim library. *
3 * *
4 * Gfxprim is free software; you can redistribute it and/or *
5 * modify it under the terms of the GNU Lesser General Public *
6 * License as published by the Free Software Foundation; either *
7 * version 2.1 of the License, or (at your option) any later version. *
8 * *
9 * Gfxprim is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
12 * Lesser General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU Lesser General Public *
15 * License along with gfxprim; if not, write to the Free Software *
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, *
17 * Boston, MA 02110-1301 USA *
18 * *
19 * Copyright (C) 2009-2011 Jiri "BlueBear" Dluhos *
20 * <jiri.bluebear.dluhos@gmail.com> *
21 * *
22 * Copyright (C) 2009-2012 Cyril Hrubis <metan@ucw.cz> *
23 * *
24 *****************************************************************************/
26 #ifndef CORE_GP_CONTEXT_H
27 #define CORE_GP_CONTEXT_H
29 #include <stdint.h>
30 #include <unistd.h>
32 #include "GP_Common.h"
33 #include "GP_Types.h"
34 #include "GP_Pixel.h"
36 /* This structure holds all information needed for drawing into an image. */
37 typedef struct GP_Context {
38 uint8_t *pixels; /* pointer to image pixels */
39 uint8_t bpp; /* pixel length in bits */
40 uint32_t bytes_per_row;
41 uint32_t w; /* width in pixels */
42 uint32_t h; /* height in pixels */
43 /*
44 * Row bit offset. The offset is ignored for byte aligned pixels.
45 * Basically it's used for non aligned pixels with combination
46 * with subcontextes.
48 uint8_t offset;
50 GP_PixelType pixel_type; /* hardware pixel format */
52 /* image orientation. Most common is landscape (0, 0, 0),
53 * portrait with normal topleft corner is (1, 0, 0).
55 uint8_t axes_swap:1; /* swap axes so that x is y and y is x */
56 uint8_t x_swap:1; /* swap direction on x */
57 uint8_t y_swap:1; /* swap direction on y */
58 uint8_t bit_endian:1; /* GP_BIT_ENDIAN */
59 uint8_t free_pixels:1; /* If set pixels are freed on GP_ContextFree */
60 } GP_Context;
62 /* Determines the address of a pixel within the context's image.
63 * Rows and columns are specified in the image's orientation
64 * (i.e. they might not be XY if the image is rotated).
66 #define GP_PIXEL_ADDR(context, x, y) (((context)->pixels \
67 + y * (context)->bytes_per_row \
68 + (x * (context)->bpp) / 8))
70 #define GP_CALC_ROW_SIZE(pixel_type, width) \
71 ((GP_PixelSize(pixel_type) * width) / 8 + \
72 !!((GP_PixelSize(pixel_type) * width) % 8))
74 /* Performs a series of sanity checks on context, aborting if any fails. */
75 #define GP_CHECK_CONTEXT(context) do { \
76 GP_CHECK(context, "NULL passed as context"); \
77 GP_CHECK(context->pixels, "invalid context: NULL image pointer"); \
78 GP_CHECK(context->bpp <= 32, "invalid context: unsupported bits-per-pixel count"); \
79 GP_CHECK(context->w > 0 && context->h > 0, "invalid context: invalid image size"); \
80 } while (0)
83 * Is true, when pixel is clipped out of context.
85 #define GP_PIXEL_IS_CLIPPED(context, x, y) \
86 ((x) < 0 || x >= (typeof(x)) context->w \
87 || (y) < 0 || y >= (typeof(y)) context->h) \
90 * Check for exactly same rotation flags.
92 #define GP_CONTEXT_ROTATION_EQUAL(c1, c2) \
93 ((c1)->axes_swap == (c2)->axes_swap && \
94 (c1)->x_swap == (c2)->x_swap && \
95 (c1)->y_swap == (c2)->y_swap)
98 * Allocate context.
100 GP_Context *GP_ContextAlloc(GP_Size w, GP_Size h, GP_PixelType type);
103 * Initalize context, pixels pointer is not dereferenced so it's safe to pass
104 * NULL there and allocate it later with size context->bpr * context->h.
106 void GP_ContextInit(GP_Context *context, GP_Size w, GP_Size h,
107 GP_PixelType type, void *pixels);
110 * Resizes context->pixels array and changes metadata to match.
112 * Returns non-zero on failure (remalloc() has failed).
114 int GP_ContextResize(GP_Context *context, GP_Size w, GP_Size h);
117 * If passed the pixels are copied to newly created context, otherwise
118 * the pixels are allocated but uninitalized.
120 #define GP_COPY_WITH_PIXELS 1
123 * Copy context.
125 GP_Context *GP_ContextCopy(const GP_Context *src, int flag);
128 * Create subcontext.
130 * If pointer to subcontext is NULL, new context is allocated
131 * otherwise context pointed by subcontext pointer is initalized.
133 * The free_pixels flag is set to 0 upon subcontext initalization so the
134 * GP_ContextFree() would not call free() upon the subcontext->pixels pointer.
136 GP_Context *GP_ContextSubContext(GP_Context *context, GP_Context *subcontext,
137 GP_Coord x, GP_Coord y, GP_Size w, GP_Size h);
140 * Converts context to a different pixel type.
141 * Returns a newly allocated context.
143 * This is naive implementation that doesn't do any ditherings or error
144 * diffusions.
146 GP_Context *GP_ContextConvert(const GP_Context *src,
147 GP_PixelType dst_pixel_type);
150 * Free context.
152 * If context->free_pixels, also free pixel data.
154 void GP_ContextFree(GP_Context *context);
157 * Rotates context flags clock wise.
159 void GP_ContextFlagsRotateCW(GP_Context *context);
162 * Rotates context flags counter clock wise.
164 void GP_ContextFlagsRotateCCW(GP_Context *context);
167 * Returns context width and height taking the rotation flags into a account.
169 static inline GP_Size GP_ContextW(const GP_Context *context)
171 if (context->axes_swap)
172 return context->h;
173 else
174 return context->w;
177 static inline GP_Size GP_ContextH(const GP_Context *context)
179 if (context->axes_swap)
180 return context->w;
181 else
182 return context->h;
185 #endif /* CORE_GP_CONTEXT_H */