2009-01-17 00:12:10 +01:00
|
|
|
/***************************************************************************
|
|
|
|
|
|
|
|
CImage.c
|
|
|
|
|
2009-08-17 12:41:51 +02:00
|
|
|
(c) 2000-2009 Benoît Minisini <gambas@users.sourceforge.net>
|
2009-01-17 00:12:10 +01:00
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
2009-08-17 12:41:51 +02:00
|
|
|
the Free Software Foundation; either version 2, or (at your option)
|
2009-01-17 00:12:10 +01:00
|
|
|
any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#define __CIMAGE_C
|
|
|
|
|
|
|
|
#include "main.h"
|
|
|
|
#include "image.h"
|
|
|
|
#include "CImage.h"
|
|
|
|
|
2009-01-26 13:25:35 +01:00
|
|
|
BEGIN_METHOD(CIMAGE_new, GB_INTEGER w; GB_INTEGER h; GB_INTEGER col; GB_INTEGER format)
|
2009-01-17 00:12:10 +01:00
|
|
|
|
2009-01-21 02:25:13 +01:00
|
|
|
int format = IMAGE_get_default_format();
|
|
|
|
|
2009-01-21 20:57:53 +01:00
|
|
|
if (VARGOPT(format, 0) == 1)
|
2009-01-21 02:25:13 +01:00
|
|
|
format = GB_IMAGE_FMT_SET_PREMULTIPLIED(format);
|
2009-01-17 00:12:10 +01:00
|
|
|
|
|
|
|
IMAGE_create(THIS_IMAGE, VARGOPT(w, 0), VARGOPT(h, 0), format);
|
|
|
|
|
|
|
|
if (!MISSING(col))
|
|
|
|
IMAGE_fill(THIS_IMAGE, VARG(col));
|
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
|
|
|
BEGIN_METHOD_VOID(CIMAGE_free)
|
|
|
|
|
|
|
|
IMAGE_delete(THIS_IMAGE);
|
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
|
|
|
BEGIN_PROPERTY(CIMAGE_width)
|
|
|
|
|
|
|
|
GB.ReturnInteger(THIS_IMAGE->width);
|
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
|
|
|
BEGIN_PROPERTY(CIMAGE_height)
|
|
|
|
|
|
|
|
GB.ReturnInteger(THIS_IMAGE->height);
|
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
|
|
|
BEGIN_PROPERTY(CIMAGE_depth)
|
|
|
|
|
|
|
|
GB.ReturnInteger(32);
|
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
|
|
|
BEGIN_METHOD(CIMAGE_fill, GB_INTEGER col)
|
|
|
|
|
|
|
|
IMAGE_fill(THIS_IMAGE, VARG(col));
|
2009-09-01 11:29:40 +02:00
|
|
|
GB.ReturnObject(THIS);
|
2009-01-17 00:12:10 +01:00
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
2010-06-15 08:54:38 +02:00
|
|
|
BEGIN_METHOD(Image_FillRect, GB_INTEGER x; GB_INTEGER y; GB_INTEGER width; GB_INTEGER height; GB_INTEGER col)
|
|
|
|
|
|
|
|
IMAGE_fill_rect(THIS_IMAGE, VARG(x), VARG(y), VARG(width), VARG(height), VARG(col));
|
|
|
|
GB.ReturnObject(THIS);
|
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
2009-01-17 00:12:10 +01:00
|
|
|
BEGIN_METHOD(CIMAGE_get, GB_INTEGER x; GB_INTEGER y)
|
|
|
|
|
|
|
|
GB.ReturnInteger(IMAGE_get_pixel(THIS_IMAGE, VARG(x), VARG(y)));
|
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
|
|
|
BEGIN_METHOD(CIMAGE_put, GB_INTEGER col; GB_INTEGER x; GB_INTEGER y)
|
|
|
|
|
|
|
|
IMAGE_set_pixel(THIS_IMAGE, VARG(x), VARG(y), VARG(col));
|
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
|
|
|
BEGIN_PROPERTY(CIMAGE_data)
|
|
|
|
|
|
|
|
GB.ReturnPointer((void *)THIS_IMAGE->data);
|
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
2010-08-13 21:57:06 +02:00
|
|
|
BEGIN_METHOD_VOID(Image_Desaturate)
|
2009-01-17 00:12:10 +01:00
|
|
|
|
|
|
|
IMAGE_make_gray(THIS_IMAGE);
|
2009-09-01 11:29:40 +02:00
|
|
|
GB.ReturnObject(THIS);
|
2009-01-17 00:12:10 +01:00
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
2010-08-13 21:57:06 +02:00
|
|
|
BEGIN_METHOD_VOID(CIMAGE_gray)
|
|
|
|
|
|
|
|
fprintf(stderr, "warning: Image.Gray is deprecated, use Image.Desaturate instead.\n");
|
|
|
|
Image_Desaturate(_object, _param);
|
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
2009-01-17 00:12:10 +01:00
|
|
|
BEGIN_METHOD_VOID(CIMAGE_clear)
|
|
|
|
|
|
|
|
IMAGE_delete(THIS_IMAGE);
|
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
|
|
|
BEGIN_METHOD(CIMAGE_replace, GB_INTEGER src; GB_INTEGER dst; GB_BOOLEAN noteq)
|
|
|
|
|
|
|
|
IMAGE_replace(THIS_IMAGE, VARG(src), VARG(dst), VARGOPT(noteq, FALSE));
|
2009-09-01 11:29:40 +02:00
|
|
|
GB.ReturnObject(THIS);
|
2009-01-17 00:12:10 +01:00
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
2010-08-13 21:57:06 +02:00
|
|
|
BEGIN_METHOD(Image_Erase, GB_INTEGER color)
|
2009-01-17 02:11:36 +01:00
|
|
|
|
|
|
|
IMAGE_make_transparent(THIS_IMAGE, VARGOPT(color, 0xFFFFFF));
|
2009-09-01 11:29:40 +02:00
|
|
|
GB.ReturnObject(THIS);
|
2009-01-17 02:11:36 +01:00
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
2010-08-13 21:57:06 +02:00
|
|
|
BEGIN_METHOD_VOID(CIMAGE_transparent)
|
|
|
|
|
|
|
|
fprintf(stderr, "warning: Image.Transparent is deprecated, use Image.Erase instead.\n");
|
|
|
|
Image_Erase(_object, _param);
|
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
2009-02-13 01:12:07 +01:00
|
|
|
BEGIN_METHOD(CIMAGE_colorize, GB_INTEGER color)
|
|
|
|
|
|
|
|
IMAGE_colorize(THIS_IMAGE, VARG(color));
|
2009-09-01 11:29:40 +02:00
|
|
|
GB.ReturnObject(THIS);
|
2009-02-13 01:12:07 +01:00
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
2009-12-29 12:56:43 +01:00
|
|
|
BEGIN_METHOD(CIMAGE_mask, GB_INTEGER color)
|
|
|
|
|
|
|
|
IMAGE_mask(THIS_IMAGE, VARG(color));
|
|
|
|
GB.ReturnObject(THIS);
|
|
|
|
|
|
|
|
END_METHOD
|
2009-02-13 01:12:07 +01:00
|
|
|
|
2009-02-03 00:46:01 +01:00
|
|
|
BEGIN_METHOD(CIMAGE_copy, GB_INTEGER x; GB_INTEGER y; GB_INTEGER w; GB_INTEGER h)
|
|
|
|
|
|
|
|
CIMAGE *image;
|
|
|
|
int x = VARGOPT(x, 0);
|
|
|
|
int y = VARGOPT(y, 0);
|
|
|
|
int w = VARGOPT(w, THIS_IMAGE->width);
|
|
|
|
int h = VARGOPT(h, THIS_IMAGE->height);
|
|
|
|
|
|
|
|
GB.New(POINTER(&image), GB.FindClass("Image"), NULL, NULL);
|
|
|
|
|
|
|
|
IMAGE_create(&image->image, w, h, THIS_IMAGE->format);
|
|
|
|
IMAGE_bitblt(&image->image, 0, 0, THIS_IMAGE, x, y, w, h);
|
|
|
|
|
|
|
|
GB.ReturnObject(image);
|
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
2009-08-17 00:07:48 +02:00
|
|
|
BEGIN_METHOD(CIMAGE_resize, GB_INTEGER width; GB_INTEGER height)
|
|
|
|
|
|
|
|
GB_IMG tmp;
|
|
|
|
int w = VARG(width);
|
|
|
|
int h = VARG(height);
|
|
|
|
|
|
|
|
if (w < 0) w = THIS_IMAGE->width;
|
|
|
|
if (h < 0) h = THIS_IMAGE->height;
|
|
|
|
|
|
|
|
//IMAGE_convert(THIS_IMAGE, IMAGE_get_default_format());
|
|
|
|
//fprintf(stderr, "format = %d\n", THIS_IMAGE->format);
|
|
|
|
tmp.ob = THIS_IMAGE->ob;
|
|
|
|
IMAGE_create(&tmp, w, h, THIS_IMAGE->format);
|
|
|
|
IMAGE_bitblt(&tmp, 0, 0, THIS_IMAGE, 0, 0, w, h);
|
|
|
|
|
|
|
|
IMAGE_delete(THIS_IMAGE);
|
|
|
|
*THIS_IMAGE = tmp;
|
2009-08-31 18:25:13 +02:00
|
|
|
GB.ReturnObject(THIS);
|
2009-08-17 00:07:48 +02:00
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
|
|
|
BEGIN_METHOD(CIMAGE_mirror, GB_BOOLEAN horz; GB_BOOLEAN vert)
|
|
|
|
|
2009-08-31 18:25:13 +02:00
|
|
|
GB_IMG tmp;
|
2009-08-17 00:07:48 +02:00
|
|
|
|
2009-08-31 20:04:36 +02:00
|
|
|
tmp.ob = THIS_IMAGE->ob;
|
2009-08-31 18:25:13 +02:00
|
|
|
IMAGE_create(&tmp, THIS_IMAGE->width, THIS_IMAGE->height, THIS_IMAGE->format);
|
|
|
|
IMAGE_mirror(THIS_IMAGE, &tmp, VARG(horz), VARG(vert));
|
2009-08-17 00:07:48 +02:00
|
|
|
|
2009-08-31 18:25:13 +02:00
|
|
|
IMAGE_delete(THIS_IMAGE);
|
|
|
|
*THIS_IMAGE = tmp;
|
|
|
|
GB.ReturnObject(THIS);
|
2009-08-17 00:07:48 +02:00
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
2009-08-21 21:43:40 +02:00
|
|
|
#if 0
|
2009-08-18 00:36:54 +02:00
|
|
|
BEGIN_METHOD(CIMAGE_transform, GB_FLOAT sx; GB_FLOAT sy; GB_FLOAT dx; GB_FLOAT dy)
|
|
|
|
|
|
|
|
CIMAGE *image;
|
|
|
|
int w, h;
|
|
|
|
double dx = VARG(dx);
|
|
|
|
double dy = VARG(dy);
|
|
|
|
double f, s;
|
|
|
|
|
|
|
|
GB.New(POINTER(&image), GB.FindClass("Image"), NULL, NULL);
|
|
|
|
GB.ReturnObject(image);
|
|
|
|
|
|
|
|
f = fabs(dx);
|
|
|
|
if (f > fabs(dy))
|
|
|
|
f = fabs(dy);
|
|
|
|
|
|
|
|
s = 1 / sqrt(dx * dx + dy * dy);
|
|
|
|
|
|
|
|
w = (int)(THIS_IMAGE->width * (1.0 + f) * s + 0.5);
|
|
|
|
h = (int)(THIS_IMAGE->height * (1.0 + f) * s + 0.5);
|
|
|
|
|
|
|
|
if (!w || !h)
|
|
|
|
return;
|
|
|
|
|
|
|
|
IMAGE_create(&image->image, w, h, THIS_IMAGE->format);
|
|
|
|
IMAGE_transform(&image->image, THIS_IMAGE, VARG(sx), VARG(sy), VARG(dx), VARG(dy));
|
|
|
|
|
|
|
|
END_METHOD
|
2009-08-21 21:43:40 +02:00
|
|
|
#endif
|
2009-08-18 00:36:54 +02:00
|
|
|
|
2010-08-12 21:13:44 +02:00
|
|
|
BEGIN_METHOD(Image_DrawAlpha, GB_OBJECT image; GB_INTEGER x; GB_INTEGER y; GB_INTEGER srcx; GB_INTEGER srcy; GB_INTEGER srcw; GB_INTEGER srch)
|
|
|
|
|
|
|
|
CIMAGE *image = VARG(image);
|
|
|
|
|
|
|
|
if (GB.CheckObject(image))
|
|
|
|
return;
|
|
|
|
|
|
|
|
IMAGE_draw_alpha(THIS_IMAGE, VARGOPT(x, 0), VARGOPT(y, 0), &image->image, VARGOPT(srcx, 0), VARGOPT(srcy, 0), VARGOPT(srcw, -1), VARGOPT(srch, -1));
|
|
|
|
GB.ReturnObject(THIS);
|
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
2009-01-17 00:12:10 +01:00
|
|
|
GB_DESC CImageDesc[] =
|
|
|
|
{
|
|
|
|
GB_DECLARE("Image", sizeof(CIMAGE)),
|
|
|
|
|
|
|
|
GB_CONSTANT("Standard", "i", 0),
|
|
|
|
GB_CONSTANT("Premultiplied", "i", 1),
|
|
|
|
|
2009-01-26 13:25:35 +01:00
|
|
|
GB_METHOD("_new", NULL, CIMAGE_new, "[(Width)i(Height)i(Color)i(Format)i]"),
|
2009-01-17 00:12:10 +01:00
|
|
|
GB_METHOD("_free", NULL, CIMAGE_free, NULL),
|
|
|
|
|
|
|
|
GB_METHOD("_get", "i", CIMAGE_get, "(X)i(Y)i"),
|
|
|
|
GB_METHOD("_put", NULL, CIMAGE_put, "(Color)i(X)i(Y)i"),
|
|
|
|
|
|
|
|
//GB_PROPERTY_READ("Format", "i", CIMAGE_format),
|
|
|
|
GB_PROPERTY_READ("Width", "i", CIMAGE_width),
|
|
|
|
GB_PROPERTY_READ("Height", "i", CIMAGE_height),
|
|
|
|
GB_PROPERTY_READ("W", "i", CIMAGE_width),
|
|
|
|
GB_PROPERTY_READ("H", "i", CIMAGE_height),
|
|
|
|
GB_PROPERTY_READ("Depth", "i", CIMAGE_depth),
|
2009-08-17 00:07:48 +02:00
|
|
|
GB_PROPERTY_READ("Data", "p", CIMAGE_data),
|
2009-01-17 00:12:10 +01:00
|
|
|
|
2009-08-31 18:25:13 +02:00
|
|
|
GB_METHOD("Clear", "Image", CIMAGE_clear, NULL),
|
|
|
|
GB_METHOD("Fill", "Image", CIMAGE_fill, "(Color)i"),
|
|
|
|
GB_METHOD("Gray", "Image", CIMAGE_gray, NULL),
|
|
|
|
GB_METHOD("Transparent", "Image", CIMAGE_transparent, "[(Color)i]"),
|
2010-08-13 21:57:06 +02:00
|
|
|
GB_METHOD("Desaturate", "Image", Image_Desaturate, NULL),
|
|
|
|
GB_METHOD("Erase", "Image", Image_Erase, "[(Color)i]"),
|
2009-08-31 18:25:13 +02:00
|
|
|
GB_METHOD("Replace", "Image", CIMAGE_replace, "(OldColor)i(NewColor)i[(NotEqual)b]"),
|
|
|
|
GB_METHOD("Colorize", "Image", CIMAGE_colorize, "(Color)i"),
|
2009-12-29 12:56:43 +01:00
|
|
|
GB_METHOD("Mask", "Image", CIMAGE_mask, "(Color)i"),
|
2009-01-17 00:12:10 +01:00
|
|
|
|
2009-02-03 00:46:01 +01:00
|
|
|
GB_METHOD("Copy", "Image", CIMAGE_copy, "[(X)i(Y)i(Width)i(Height)i]"),
|
2009-08-31 18:25:13 +02:00
|
|
|
GB_METHOD("Resize", "Image", CIMAGE_resize, "(Width)i(Height)i"),
|
2009-08-17 00:07:48 +02:00
|
|
|
|
|
|
|
GB_METHOD("Mirror", "Image", CIMAGE_mirror, "(Horizontal)b(Vertical)b"),
|
2010-06-15 08:54:38 +02:00
|
|
|
|
|
|
|
GB_METHOD("FillRect", "Image", Image_FillRect, "(X)i(Y)i(Width)i(Height)i(Color)i"),
|
2010-08-12 21:13:44 +02:00
|
|
|
//GB_METHOD("FillAlpha", "Image", Image_FillAlpha, "(X)i(Y)i(Width)i(Height)i(Alpha)i"),
|
|
|
|
GB_METHOD("DrawAlpha", "Image", Image_DrawAlpha, "(Image)Image;[(X)i(Y)i(SrcX)i(SrcY)i(SrcWidth)i(SrcHeight)i]"),
|
|
|
|
|
2009-08-21 21:43:40 +02:00
|
|
|
//GB_METHOD("Transform", "Image", CIMAGE_transform, "(SrcX)f(SrcY)f(DepX)f(DepY)f"),
|
2009-08-17 00:07:48 +02:00
|
|
|
//GB_METHOD("Draw", NULL, CIMAGE_draw, "(Image)Image;(X)i(Y)i[(Width)i(Height)i(SrcX)i(SrcY)i(SrcWidth)i(SrcHeight)i]"),
|
2009-02-03 00:46:01 +01:00
|
|
|
|
2009-01-17 00:12:10 +01:00
|
|
|
GB_END_DECLARE
|
|
|
|
};
|
|
|
|
|