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-2013 Cyril Hrubis <metan@ucw.cz> *
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
));
33 GP_DEBUG(2, "Table for channel '%s' size %zu (%p)",
34 chan
->name
, table_size
, table
);
37 GP_DEBUG(1, "Malloc failed :(");
41 for (i
= 0; i
< table_size
; i
++)
47 static void free_tables(GP_FilterTables
*self
)
51 for (i
= 0; i
< GP_PIXELTYPE_MAX_CHANNELS
; i
++) {
56 GP_DEBUG(2, "Freeing table (%p)", self
->table
[i
]);
61 int GP_FilterTablesInit(GP_FilterTables
*self
, const GP_Pixmap
*pixmap
)
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
]) {
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
);
94 GP_DEBUG(1, "Malloc failed :(");
98 if (GP_FilterTablesInit(tables
, pixmap
)) {
103 tables
->free_table
= 1;
108 void GP_FilterTablesFree(GP_FilterTables
*self
)
110 GP_DEBUG(1, "Freeing point filter and tables (%p)", self
);
114 if (self
->free_table
) {
115 GP_DEBUG(2, "Freeing table itself");