summaryrefslogtreecommitdiff
path: root/src/lua5/imlua_palette.c
blob: 135fe9ab76bf86600a8d74ec26443b20b54db76b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/** \file
 * \brief IM Lua 5 Binding
 *
 * See Copyright Notice in im_lib.h
 * $Id: imlua_palette.c,v 1.2 2010/06/11 17:43:52 scuri Exp $
 */

#include <string.h>
#include <memory.h>
#include <stdlib.h>

#include "im.h"
#include "im_image.h"
#include "im_util.h"
#include "im_palette.h"

#include <lua.h>
#include <lauxlib.h>

#include "imlua.h"
#include "imlua_aux.h"
#include "imlua_palette.h"


static imluaPalette* imlua_rawcheckpalette(lua_State *L, int param)
{
  void *p = lua_touserdata(L, param);
  if (p != NULL) {  /* value is a userdata? */
    if (lua_getmetatable(L, param)) {  /* does it have a metatable? */
      lua_getfield(L, LUA_REGISTRYINDEX, "imPalette");  /* get correct metatable */
      if (lua_rawequal(L, -1, -2)) {  /* does it have the correct mt? */
        lua_pop(L, 2);  /* remove both metatables */
        return (imluaPalette*)p;
      }
      lua_pop(L, 1);  /* remove previous metatable */

      /* check also for CD palette */
      lua_getfield(L, LUA_REGISTRYINDEX, "cdPalette");  /* get correct metatable */
      if (lua_rawequal(L, -1, -2)) {  /* does it have the correct mt? */
        lua_pop(L, 2);  /* remove both metatables */
        return (imluaPalette*)p;
      }
    }
  }
  luaL_typeerror(L, param, "imPalette");  /* else error */
  return NULL;  /* to avoid warnings */
}

imluaPalette* imlua_checkpalette (lua_State *L, int param)
{
  imluaPalette* pal = imlua_rawcheckpalette(L, param);
  if (!pal->color)
    luaL_argerror(L, param, "destroyed imPalette");

  return pal;
}

void imlua_pushpalette(lua_State *L, long* color, int count)
{
  imluaPalette *pal = (imluaPalette*) lua_newuserdata(L, sizeof(imluaPalette));
  pal->count = count;
  pal->color = color;
  luaL_getmetatable(L, "imPalette");
  lua_setmetatable(L, -2);
}

/***************************************************************************\
* Creates a palette as a "imPalette" userdata. A palette can be          *
* considered and treated as a color table.                                 *
* im.PaletteCreate(count: number) -> (palette: "imPalette")               *
\***************************************************************************/
static int imluaPaletteCreate(lua_State *L)
{
  long* color;

  int count = luaL_optint(L, 1, 256);
  if (count < 1 || count > 256)
    luaL_argerror(L, 1, "palette count should be a positive integer and less then 256");

  color = (long*)malloc(256*sizeof(long));
  memset(color, 0, 256*sizeof(long));

  imlua_pushpalette(L, color, count);
  return 1;
}


/*****************************************************************************\
 im.PaletteFindNearest
\*****************************************************************************/
static int imluaPaletteFindNearest (lua_State *L)
{
  imluaPalette *pal = imlua_checkpalette(L, 1);
  long color = (long int) lua_touserdata(L, 1);

  lua_pushnumber(L, imPaletteFindNearest(pal->color, pal->count, color));
  return 1;
}

/*****************************************************************************\
 im.PaletteFindColor
\*****************************************************************************/
static int imluaPaletteFindColor (lua_State *L)
{
  imluaPalette *pal = imlua_checkpalette(L, 1);
  long color = (long) lua_touserdata(L, 2);
  unsigned char tol = (unsigned char)luaL_checkint(L, 3);

  lua_pushnumber(L, imPaletteFindColor(pal->color, pal->count, color, tol));
  return 1;
}

/*****************************************************************************\
 im.PaletteGray
\*****************************************************************************/
static int imluaPaletteGray (lua_State *L)
{
  imlua_pushpalette(L, imPaletteGray(), 256);
  return 1;
}

/*****************************************************************************\
 im.PaletteRed
\*****************************************************************************/
static int imluaPaletteRed (lua_State *L)
{
  imlua_pushpalette(L, imPaletteRed(), 256);
  return 1;
}

/*****************************************************************************\
 im.PaletteGreen
\*****************************************************************************/
static int imluaPaletteGreen (lua_State *L)
{
  imlua_pushpalette(L, imPaletteGreen(), 256);
  return 1;
}

/*****************************************************************************\
 im.PaletteBlue
\*****************************************************************************/
static int imluaPaletteBlue (lua_State *L)
{
  imlua_pushpalette(L, imPaletteBlue(), 256);
  return 1;
}

/*****************************************************************************\
 im.PaletteYellow
\*****************************************************************************/
static int imluaPaletteYellow (lua_State *L)
{
  imlua_pushpalette(L, imPaletteYellow(), 256);
  return 1;
}

/*****************************************************************************\
 im.PaletteMagenta
\*****************************************************************************/
static int imluaPaletteMagenta (lua_State *L)
{
  imlua_pushpalette(L, imPaletteMagenta(), 256);
  return 1;
}

/*****************************************************************************\
 im.PaletteCian
\*****************************************************************************/
static int imluaPaletteCian (lua_State *L)
{
  imlua_pushpalette(L, imPaletteCian(), 256);
  return 1;
}

/*****************************************************************************\
 im.PaletteRainbow
\*****************************************************************************/
static int imluaPaletteRainbow (lua_State *L)
{
  imlua_pushpalette(L, imPaletteRainbow(), 256);
  return 1;
}

/*****************************************************************************\
 im.PaletteHues
\*****************************************************************************/
static int imluaPaletteHues (lua_State *L)
{
  imlua_pushpalette(L, imPaletteHues(), 256);
  return 1;
}

/*****************************************************************************\
 im.PaletteBlueIce
\*****************************************************************************/
static int imluaPaletteBlueIce (lua_State *L)
{
  imlua_pushpalette(L, imPaletteBlueIce(), 256);
  return 1;
}

/*****************************************************************************\
 im.PaletteHotIron
\*****************************************************************************/
static int imluaPaletteHotIron (lua_State *L)
{
  imlua_pushpalette(L, imPaletteHotIron(), 256);
  return 1;
}

/*****************************************************************************\
 im.PaletteBlackBody
\*****************************************************************************/
static int imluaPaletteBlackBody (lua_State *L)
{
  imlua_pushpalette(L, imPaletteBlackBody(), 256);
  return 1;
}

/*****************************************************************************\
 im.PaletteHighContrast
\*****************************************************************************/
static int imluaPaletteHighContrast (lua_State *L)
{
  imlua_pushpalette(L, imPaletteHighContrast(), 256);
  return 1;
}

/*****************************************************************************\
 im.PaletteUniform
\*****************************************************************************/
static int imluaPaletteUniform (lua_State *L)
{
  imlua_pushpalette(L, imPaletteUniform(), 256);
  return 1;
}

/*****************************************************************************\
 im.PaletteUniformIndex
\*****************************************************************************/
static int imluaPaletteUniformIndex (lua_State *L)
{
  lua_pushnumber(L, imPaletteUniformIndex((long int) lua_touserdata(L, 1)));
  return 1;
}

/*****************************************************************************\
 im.PaletteUniformIndexHalftoned
\*****************************************************************************/
static int imluaPaletteUniformIndexHalftoned (lua_State *L)
{
  long color = (long) lua_touserdata(L, 1);
  int x = luaL_checkint(L, 2);
  int y = luaL_checkint(L, 3);

  lua_pushnumber(L, imPaletteUniformIndexHalftoned(color, x, y));
  return 1;
}

/***************************************************************************\
* Frees a previously allocated palette                                      *
* im.PaletteDestroy(palette: "imPalette")                                      *
\***************************************************************************/
static int imluaPaletteDestroy (lua_State *L)
{
  imluaPalette *pal = imlua_rawcheckpalette(L, 1);
  if (!pal->color)
    luaL_argerror(L, 1, "destroyed imPalette");

  free(pal->color);
  pal->color = NULL;  /* mark as destroyed */
  pal->count = 0;

  return 0;
}

/*****************************************************************************\
 gc
\*****************************************************************************/
static int imluaPalette_gc(lua_State *L)
{
  imluaPalette *pal = (imluaPalette*)lua_touserdata(L, 1);
  if (pal && pal->color)
  {
    free(pal->color);
    pal->color = NULL;  /* mark as destroyed */
    pal->count = 0;
  }

  return 0;
}

/***************************************************************************\
* color = palette[i]                                                        *
\***************************************************************************/
static int imluaPalette_index(lua_State *L)
{
  imluaPalette *pal = imlua_checkpalette(L, 1);
  int index_i = luaL_checkint(L, 2);

  if (index_i < 0 || index_i >= pal->count)
    luaL_argerror(L, 2, "index is out of bounds");

  lua_pushlightuserdata(L, (void*) pal->color[index_i]);
  return 1;
}

/***************************************************************************\
* palette[i] = color                                                        *
\***************************************************************************/
static int imluaPalette_newindex(lua_State *L)
{
  long color_i;
  imluaPalette *pal = imlua_checkpalette(L, 1);
  int index_i = luaL_checkint(L, 2);

  if (index_i < 0 || index_i >= pal->count)
    luaL_argerror(L, 2, "index is out of bounds");

  if (!lua_islightuserdata(L, 3))
    luaL_argerror(L, 3, "color must be a light user data");

  color_i = (long int) lua_touserdata(L, 3);

  pal->color[index_i] = color_i;
  return 0;
}

/*****************************************************************************\
 len
\*****************************************************************************/
static int imluaPalette_len(lua_State *L)
{
  imluaPalette *pal = (imluaPalette*)lua_touserdata(L, 1);
  lua_pushinteger(L, pal->count);
  return 1;
}

/*****************************************************************************\
 tostring
\*****************************************************************************/
static int imluaPalette_tostring (lua_State *L)
{
  imluaPalette *pal = (imluaPalette*)lua_touserdata(L, 1);
  lua_pushfstring(L, "imPalette(%p)%s", pal, (pal->color)? "": "-destroyed");
  return 1;
}

static const luaL_reg impalette_lib[] = {
  {"PaletteFindNearest", imluaPaletteFindNearest},
  {"PaletteFindColor", imluaPaletteFindColor},
  {"PaletteGray", imluaPaletteGray },
  {"PaletteRed", imluaPaletteRed },
  {"PaletteGreen", imluaPaletteGreen },
  {"PaletteBlue", imluaPaletteBlue },
  {"PaletteYellow", imluaPaletteYellow },
  {"PaletteMagenta", imluaPaletteMagenta },
  {"PaletteCian", imluaPaletteCian },
  {"PaletteRainbow", imluaPaletteRainbow },
  {"PaletteHues", imluaPaletteHues },
  {"PaletteBlueIce", imluaPaletteBlueIce },
  {"PaletteHotIron", imluaPaletteHotIron },
  {"PaletteBlackBody", imluaPaletteBlackBody },
  {"PaletteHighContrast", imluaPaletteHighContrast },
  {"PaletteUniform", imluaPaletteUniform },
  {"PaletteUniformIndex", imluaPaletteUniformIndex },
  {"PaletteUniformIndexHalftoned", imluaPaletteUniformIndexHalftoned },

  {"PaletteDestroy", imluaPaletteDestroy},
  {"PaletteCreate", imluaPaletteCreate},

  {NULL, NULL}
};

static const luaL_reg impalette_metalib[] = {
  {"__gc", imluaPalette_gc},
  {"__tostring", imluaPalette_tostring},
  {"__index", imluaPalette_index},
  {"__newindex", imluaPalette_newindex},
  {"__len", imluaPalette_len},

  {NULL, NULL}
};

static void createmeta (lua_State *L) 
{
  /* there is no object orientation for imPalette, only array access */
  luaL_newmetatable(L, "imPalette");  /* create new metatable for imPalette handles */
  luaL_register(L, NULL, impalette_metalib);     /* register methods */
  lua_pop(L, 1);   /* removes the metatable from the top of the stack */
}

void imlua_open_palette (lua_State *L)
{
  /* "im" table is at the top of the stack */
  createmeta(L);
  luaL_register(L, NULL, impalette_lib);
}