Rename GP_Context -> GP_Pixmap
[gfxprim.git] / libs / filters / GP_ApplyTables.c
blobc61a9b99bb51d61ff44a67784aa54d209e609fc1
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-2013 Cyril Hrubis <metan@ucw.cz> *
20 * *
21 *****************************************************************************/
23 #include "core/GP_Debug.h"
25 #include "filters/GP_ApplyTables.h"
27 static GP_Pixel *create_table(const GP_PixelTypeChannel *chan)
29 size_t table_size = (1 << chan->size);
30 GP_Pixel *table = malloc(table_size * sizeof(GP_Pixel));
31 GP_Pixel i;
33 GP_DEBUG(2, "Table for channel '%s' size %zu (%p)",
34 chan->name, table_size, table);
36 if (!table) {
37 GP_DEBUG(1, "Malloc failed :(");
38 return NULL;
41 for (i = 0; i < table_size; i++)
42 table[i] = i;
44 return table;
47 static void free_tables(GP_FilterTables *self)
49 unsigned int i;
51 for (i = 0; i < GP_PIXELTYPE_MAX_CHANNELS; i++) {
53 if (!self->table[i])
54 break;
56 GP_DEBUG(2, "Freeing table (%p)", self->table[i]);
57 free(self->table[i]);
61 int GP_FilterTablesInit(GP_FilterTables *self, const GP_Pixmap *pixmap)
63 unsigned int i;
64 const GP_PixelTypeDescription *desc;
66 GP_DEBUG(2, "Allocating tables for pixel %s",
67 GP_PixelTypeName(pixmap->pixel_type));
69 for (i = 0; i < GP_PIXELTYPE_MAX_CHANNELS; i++)
70 self->table[i] = NULL;
72 desc = GP_PixelTypeDesc(pixmap->pixel_type);
74 for (i = 0; i < desc->numchannels; i++) {
75 self->table[i] = create_table(&desc->channels[i]);
76 if (!self->table[i]) {
77 free_tables(self);
78 return 1;
82 self->free_table = 0;
84 return 0;
87 GP_FilterTables *GP_FilterTablesAlloc(const GP_Pixmap *pixmap)
89 GP_FilterTables *tables = malloc(sizeof(GP_FilterTables));
91 GP_DEBUG(1, "Allocating point filter (%p)", tables);
93 if (!tables) {
94 GP_DEBUG(1, "Malloc failed :(");
95 return NULL;
98 if (GP_FilterTablesInit(tables, pixmap)) {
99 free(tables);
100 return NULL;
103 tables->free_table = 1;
105 return tables;
108 void GP_FilterTablesFree(GP_FilterTables *self)
110 GP_DEBUG(1, "Freeing point filter and tables (%p)", self);
112 free_tables(self);
114 if (self->free_table) {
115 GP_DEBUG(2, "Freeing table itself");
116 free(self);