1 /*****************************************************************************
2 * This file is part of gfxprim library. *
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. *
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. *
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 *
19 * Copyright (C) 2009-2010 Jiri "BlueBear" Dluhos *
20 * <jiri.bluebear.dluhos@gmail.com> *
22 * Copyright (C) 2009-2010 Cyril Hrubis <metan@ucw.cz> *
24 *****************************************************************************/
32 #include "GP_Common.h"
35 /* This structure holds all information needed for drawing into an image. */
36 typedef struct GP_Context
{
37 uint8_t *pixels
; /* pointer to image pixels */
38 uint8_t bpp
; /* values: 1, 2, 4, 8, 16, 24, 32 */
39 uint32_t bytes_per_row
;
40 uint32_t w
; /* width */
41 uint32_t h
; /* height */
43 GP_PixelType pixel_type
; /* hardware pixel format */
45 /* image orientation. Most common is landscape (0, 0, 0),
46 * portrait with normal topleft corner is (1, 0, 0).
48 int axes_swap
:1; /* swap axes so that x is y and y is x */
49 int x_swap
:1; /* swap direction on x */
50 int y_swap
:1; /* swap direction on y */
52 /* clipping rectangle; drawing functions only affect the inside */
59 /* Returns the pixel type used by the context. */
60 inline GP_PixelType
GP_GetContextPixelType(const GP_Context
*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 { \
77 "NULL passed as context"); \
78 GP_CHECK(context->pixels, \
79 "invalid context: NULL image pointer"); \
80 GP_CHECK(context->bpp <= 32, \
81 "invalid context: unsupported bits-per-pixel count"); \
82 GP_CHECK(context->w > 0 && context->h > 0, \
83 "invalid context: invalid image size"); \
84 GP_CHECK(context->clip_w_min <= context->clip_w_max \
85 && context->clip_h_min <= context->clip_h_max, \
86 "invalid context: invalid clipping rectangle"); \
87 GP_CHECK(context->clip_w_max < context->w \
88 && context->clip_h_max < context->h, \
89 "invalid context: clipping rectangle larger than image"); \
93 * Is true, when pixel is clipped.
95 #define GP_PIXEL_IS_CLIPPED(context, x, y) \
96 (x < (int) context->clip_w_min \
97 || x > (int) context->clip_w_max \
98 || y < (int) context->clip_h_min \
99 || y > (int) context->clip_h_max) \
104 GP_Context
*GP_ContextAlloc(uint32_t w
, uint32_t h
, GP_PixelType type
);
107 * If passed the pixels are copied to newly created context, otherwise
108 * the pixels are allocated but uninitalized.
110 #define GP_COPY_WITH_PIXELS 1
115 GP_Context
*GP_ContextCopy(GP_Context
*context
, int flag
);
120 void GP_ContextFree(GP_Context
*context
);
123 * Dump context into file
125 GP_RetCode
GP_ContextDump(GP_Context
*context
, const char *path
);
128 * Rotates context flags.
130 void GP_ContextFlagsRotateCW(GP_Context
*context
);
131 void GP_ContextFlagsRotateCCW(GP_Context
*context
);
134 * Returns context width and height.
136 static inline uint32_t GP_ContextW(GP_Context
*context
)
141 static inline uint32_t GP_ContextH(GP_Context
*context
)
146 static inline uint32_t GP_TContextW(GP_Context
*context
)
148 if (context
->axes_swap
)
154 static inline uint32_t GP_TContextH(GP_Context
*context
)
156 if (context
->axes_swap
)
162 #endif /* GP_CONTEXT_H */