Update with current status
[gnash.git] / testsuite / misc-ming.all / loading / LoadBitmapTest.c
blob16a2206c5230ff91da024059cdeb66ba14e98634
1 /*
2 * Copyright (C) 2007, 2009, 2010, 2011 Free Software Foundation, Inc.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program 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
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 */
19 #include <stdlib.h>
20 #include <stdio.h>
21 #include <ming.h>
22 #include <string.h>
24 #include "ming_utils.h"
26 #define OUTPUT_VERSION 8
27 #define OUTPUT_FILENAME "LoadBitmapTest.swf"
29 const char* mediadir=".";
32 /// The test shows the following:
34 /// The MovieClip itself is drawn with no transformation, i.e. identity matrix.
35 /// Any contained MovieClips keep their transformation.
36 /// BitmapData.draw draws on top of what's in the BitmapData already.
37 /// Dynamically loaded images are not drawn.
38 int
39 main(int argc, char** argv)
41 SWFMovie mo;
42 SWFMovieClip dejagnuclip;
43 SWFBitmap bp;
44 SWFInput inp;
45 FILE* imgfile;
47 if (argc > 1) mediadir = argv[1];
48 else {
49 fprintf(stderr, "Usage: %s <mediadir>\n", argv[0]);
50 return EXIT_FAILURE;
53 Ming_init();
54 Ming_useSWFVersion (OUTPUT_VERSION);
56 mo = newSWFMovie();
57 SWFMovie_setDimension(mo, 800, 600);
59 SWFMovie_setRate(mo, 12);
60 dejagnuclip = get_dejagnu_clip(
61 (SWFBlock)get_default_font(mediadir), 10, 10, 150, 800, 600);
62 SWFMovie_add(mo, (SWFBlock)dejagnuclip);
64 SWFMovie_nextFrame(mo);
66 // Add a MovieClip with an image.
68 char file[] = "/green.jpg";
69 if (strlen(mediadir) > 1024) {
70 fprintf(stderr, "Path to media dir too long! Fix the testcase");
72 char path[1024 + sizeof file];
73 strcpy(path, mediadir);
74 strcat(path, file);
76 imgfile = fopen(path, "rb");
77 if (!imgfile) {
78 fprintf(stderr, "Failed to open bitmap file");
79 return EXIT_FAILURE;
82 // Note that recent ming version have the more convenient
83 // newSWFInput_filename() function, but we want to support
84 // older versions.
85 inp = newSWFInput_file(imgfile);
86 bp = newSWFBitmap_fromInput(inp);
87 if (!bp) {
88 return EXIT_FAILURE;
90 SWFMovie_addExport(mo, (SWFBlock)bp, "img1");
92 SWFMovie_writeExports(mo);
94 // For some reason some values are very slightly different, even though
95 // the bytes should be taken directly from the embedded DefineBits tag.
96 // We'd better not worry about it too much; they're very close.
97 add_actions(mo,
98 // 24-bit RGB checker
99 "near = function(bitmap, x, y, val) {"
100 " tol = 2;"
101 " col = bitmap.getPixel(x, y);"
102 " col_r = (col & 0xff0000) >> 16;"
103 " col_g = (col & 0xff00) >> 8;"
104 " col_b = (col & 0xff);"
105 " val_r = (val & 0xff0000) >> 16;"
106 " val_g = (val & 0xff00) >> 8;"
107 " val_b = (val & 0xff);"
108 " if (Math.abs(col_r - val_r) > tol) return false;"
109 " if (Math.abs(col_b - val_b) > tol) return false;"
110 " if (Math.abs(col_g - val_g) > tol) return false;"
111 " return true;"
112 "};"
115 add_actions(mo,
116 "f = flash.display.BitmapData.loadBitmap('img1');");
117 check_equals(mo, "typeof(f)", "'object'");
118 check_equals(mo, "f.__proto__", "flash.display.BitmapData.prototype");
119 check(mo, "f.__proto__ === flash.display.BitmapData.prototype");
120 check_equals(mo, "f.transparent", "false");
122 // Pixel checking
123 check(mo, "near(f, 85, 10, 0x00ff00)");
124 check(mo, "near(f, 85, 30, 0x008800)");
125 check(mo, "near(f, 85, 50, 0x004400)");
126 check(mo, "near(f, 85, 70, 0x002200)");
127 check(mo, "near(f, 85, 85, 0x000000)");
129 // Now do weird things with the class to see what's called where.
130 add_actions(mo,
132 // A static property of an object
133 "o = {};"
134 "ASSetPropFlags(o, null, 0, 1);"
135 "o.func = flash.display.BitmapData.loadBitmap;"
137 // Plain function
138 "func = flash.display.BitmapData.loadBitmap;"
140 // Overwrite flash.display.BitmapData
141 "backup = flash.display.BitmapData;"
142 "_global.flash.display.BitmapData = 67;"
144 // Works
145 "c = o.func('img1');"
147 // Doesn't work. Not sure why; maybe because 'this' is a level.
148 "d = func('img1');"
150 "o.prototype = backup.prototype;"
151 "e = o.func('img1');"
153 "_root.attachBitmap(c, 67);"
157 check_equals(mo, "typeof(c)", "'object'");
158 check_equals(mo, "c.__proto__", "undefined");
159 xcheck_equals(mo, "typeof(d)", "'undefined'");
161 // The __proto__ member is the prototype of the parent object.
162 check_equals(mo, "typeof(e)", "'object'");
163 check_equals(mo, "typeof(e.__proto__)", "'object'");
164 check_equals(mo, "e.__proto__", "backup.prototype");
165 check_equals(mo, "typeof(e.constructor)", "'function'");
166 check(mo, "e.constructor != backup.constructor");
168 add_actions(mo, "_global.flash.display.BitmapData = backup;");
170 add_actions(mo, "stop();");
172 // Output movie
173 puts("Saving " OUTPUT_FILENAME);
174 SWFMovie_save(mo, OUTPUT_FILENAME);
176 fclose(imgfile);
178 return EXIT_SUCCESS;