Context docs, yay!
[gfxprim.git] / include / core / GP_Context.h
blobd6b0f6af644c66c82030244e05b80c841d336ddf
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-2011 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 * Allocate context.
92 GP_Context *GP_ContextAlloc(GP_Size w, GP_Size h, GP_PixelType type);
95 * If passed the pixels are copied to newly created context, otherwise
96 * the pixels are allocated but uninitalized.
98 #define GP_COPY_WITH_PIXELS 1
101 * Copy context.
103 GP_Context *GP_ContextCopy(const GP_Context *src, int flag);
106 * Create subcontext.
108 * If pointer to subcontext is NULL, new context is allocated
109 * otherwise context pointed by subcontext pointer is initalized.
111 * The free_pixels flag is set to 0 upon subcontext initalization so the
112 * GP_ContextFree() would not call free() upon the subcontext->pixels pointer.
114 GP_Context *GP_ContextSubContext(GP_Context *context, GP_Context *subcontext,
115 GP_Coord x, GP_Coord y, GP_Size w, GP_Size h);
118 * Converts context to different pixel type.
120 * This is naive implementation that doesn't do any ditherings or error
121 * diffusions.
123 GP_Context *GP_ContextConvert(const GP_Context *context, GP_PixelType res_type);
126 * Free context.
128 void GP_ContextFree(GP_Context *context);
131 * Dump context into file
133 GP_RetCode GP_ContextDump(GP_Context *context, const char *path);
136 * Rotates context flags clock wise.
138 void GP_ContextFlagsRotateCW(GP_Context *context);
141 * Rotates context flags counter clock wise.
143 void GP_ContextFlagsRotateCCW(GP_Context *context);
146 * Returns context width and height taking the rotation flags into a account.
148 static inline GP_Size GP_ContextW(const GP_Context *context)
150 if (context->axes_swap)
151 return context->h;
152 else
153 return context->w;
156 static inline GP_Size GP_ContextH(const GP_Context *context)
158 if (context->axes_swap)
159 return context->w;
160 else
161 return context->h;
164 #endif /* CORE_GP_CONTEXT_H */