-
Notifications
You must be signed in to change notification settings - Fork 1
/
imageext.c
148 lines (104 loc) · 3.73 KB
/
imageext.c
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
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <png.h>
#include "flirt-debug.h"
void
savePNG(char* filename, ddImage* image, int width, int height)
{
FILE* f = fopen(filename, "wb");
int i;
png_bytep* row_pointers;
char* imagebytes = ddImage_getData(image);
int rowstride = ddImage_getRowstride(image);
png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, png_voidp_NULL, png_voidp_NULL, png_voidp_NULL);
png_infop info_ptr = png_create_info_struct(png_ptr);
if ( setjmp(png_jmpbuf(png_ptr)) )
{
png_destroy_write_struct(&png_ptr, &info_ptr);
fclose(f);
return;
}
png_init_io(png_ptr, f);
png_set_write_status_fn(png_ptr, png_write_status_ptr_NULL);
png_set_IHDR(png_ptr, info_ptr, width, height,
8, PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
row_pointers = png_malloc(png_ptr, height * sizeof(png_bytep));
for ( i = 0; i < height; ++i )
row_pointers[i] = (unsigned char*)imagebytes + i * rowstride;
png_set_rows(png_ptr, info_ptr, row_pointers);
png_set_swap_alpha(png_ptr);
// XXX - should be endian check
#ifndef DD_PLATFORM_MAC_OS_X
png_set_swap_alpha(png_ptr);
png_set_bgr(png_ptr);
#endif
png_write_png(png_ptr, info_ptr, 0, NULL);
png_destroy_info_struct(png_ptr, &info_ptr);
png_destroy_write_struct(&png_ptr, &info_ptr);
fclose(f);
}
struct imageObject
{
ddActionObject parent;
ddDrawClip* clip;
int width;
int height;
};
typedef struct imageObject ImageObject;
ddActionValue
Image_constructor(ddActionObject* classref, ddActionContext* context, int nargs)
{
ddActionValue value = ddActionContext_popValue(context);
ddActionObject* object = ddActionValue_getObjectValue(value);
ImageObject* image;
if ( !ddActionObject_isKindOfClass(object, ddActionMovieClipClass) )
return ddNullValue;
image = malloc(sizeof(struct imageObject));
ddActionObject_init((ddActionObject*)image);
ddActionObject_setClass((ddActionObject*)image, (ddActionClass*)ddActionObject_getPrototype(classref));
image->clip = ddActionMovieClip_getDrawClip((ddActionMovieClip*)ddActionObject_retain(object));
image->width = 0;
image->height = 0;
return dd_newActionValue_object((ddActionObject*)image);
}
ddActionValue
Image_setWidth(ddActionObject* object, ddActionContext* context, int nargs)
{
ddActionValue val = ddActionContext_popValue(context);
int width = ddActionValue_getIntValue(val);
ImageObject* image = (ImageObject*)object;
image->width = width;
return ddNullValue;
}
ddActionValue
Image_setHeight(ddActionObject* object, ddActionContext* context, int nargs)
{
ddActionValue val = ddActionContext_popValue(context);
int height = ddActionValue_getIntValue(val);
ImageObject* image = (ImageObject*)object;
image->height = height;
return ddNullValue;
}
ddActionValue
Image_render(ddActionObject* object, ddActionContext* context, int nargs)
{
ddActionValue val = ddActionContext_popValue(context);
char* filename = ddActionValue_getStringValue(context, val);
ImageObject* image = (ImageObject*)object;
ddImage* bitmap = dd_newImage(image->width, image->height);
if ( filename == NULL || image->width == 0 || image->height == 0 )
return ddFalseValue;
ddDrawClip_renderInImage(image->clip, bitmap, ddPlayer_getBackgroundColor(ddContext_getPlayer(context)));
savePNG(filename, bitmap, image->width, image->height);
return ddTrueValue;
}
void
setupImageClass(ddPlayer* player)
{
ddActionClass* class = ddPlayer_addClass(player, ddActionObjectClass, "Image", Image_constructor, 1);
ddActionClass_addNativeMethod(class, "render", Image_render, 1);
ddActionClass_addNativeMethod(class, "setWidth", Image_setWidth, 1);
ddActionClass_addNativeMethod(class, "setHeight", Image_setHeight, 1);
}