/**
* Copyright hacker_c0kgmotg ( http://wonderfl.net/user/hacker_c0kgmotg )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/jbGi
*/
wonderfl build flash online logo
* signin
* codes
page view ranking favorite ranking forked count ranking
* users
page view ranking favorite ranking forked count ranking
* tags
Flash/Actionscript keywords
* Q&A
new questions
* wonderfl?
what is wonderfl? help! libraries wiki APIs developer's blog contact
code search
二値化処理一覧
お気に入りに追加
icon rsakane
* forked : 0
* favorite : 46
* lines : 529
* license : All rights reserved
* modified : 2009/12/03 17:53:11
ブログに貼る
1. package
2. {
3. import flash.display.*;
4. import flash.events.*;
5. import flash.geom.*;
6. import flash.net.URLRequest;
7. import flash.system.*;
8. import flash.filters.*;
9. import frocessing.color.ColorRGB;
10. import com.bit101.components.*;
11.
12. public class Main extends Sprite
13. {
14. private const WIDTH:int = 250;
15. private const HEIGHT:int = 250;
16.
17. private var window:Window;
18. private var bitmap:Bitmap;
19. private var copybd:BitmapData;
20.
21. public function Main()
22. {
23. window = new Window(this, 0, 0, "Binarization");
24. window.width = window.height = 465;
25.
26. Security.loadPolicyFile("http://farm3.static.flickr.com/crossdomain.xml");
27. var loader:Loader = new Loader();
28. loader.contentLoaderInfo.addEventListener(Event.INIT, initHandler);
29. loader.load(new URLRequest("http://farm1.static.flickr.com/208/503553011_82716cc9b9.jpg"), new LoaderContext(true));
30. }
31.
32. private function initHandler(event:Event):void
33. {
34. var loader:Loader = event.currentTarget.loader;
35.
36. var matrix:Matrix = new Matrix();
37. matrix.scale(WIDTH / loader.width, HEIGHT / loader.height);
38.
39. var bd:BitmapData = new BitmapData(WIDTH, HEIGHT);
40. bd.draw(loader, matrix);
41. copybd = bd.clone();
42. bitmap = new Bitmap(bd);
43. bitmap.x = (465 - bitmap.width) / 2;
44. window.content.addChild(bitmap);
45.
46. var button1:PushButton = new PushButton(window.content, 5, 300, "Original", originalHander);
47. var button2:PushButton = new PushButton(window.content, 5, 320, "Threshold", binarization);
48. var button3:PushButton = new PushButton(window.content, 5, 340, "Halftone", halfTone);
49. var button4:PushButton = new PushButton(window.content, 120, 300, "Random", randomDither);
50. var button5:PushButton = new PushButton(window.content, 120, 320, "Bayer", bayerDither);
51. var button6:PushButton = new PushButton(window.content, 235, 300, "Repeat", errorDiffusion);
52. var button7:PushButton = new PushButton(window.content, 235, 320, "Floyd–Steinberg", errorDiffusion2);
53. var button8:PushButton = new PushButton(window.content, 235, 340, "Jarvis, Judice & Ninke", errorDiffusion3);
54. var button9:PushButton = new PushButton(window.content, 235, 360, "Stucki", errorDiffusion4);
55. var button10:PushButton = new PushButton(window.content, 235, 380, "Burkes", errorDiffusion5);
56. var button11:PushButton = new PushButton(window.content, 350, 300, "Sierra", errorDiffusion6);
57. var button12:PushButton = new PushButton(window.content, 350, 320, "Two-row Sierra", errorDiffusion7);
58. var button13:PushButton = new PushButton(window.content, 350, 340, "Filter Lite", errorDiffusion8);
59. }
60.
61. private function originalHander(event:Event = null):void
62. {
63. bitmap.bitmapData = copybd.clone();
64. }
65.
66. private function binarization(event:Event = null):void
67. {
68. bitmap.bitmapData = copybd.clone();
69. var bd:BitmapData = bitmap.bitmapData;
70.
71. bd.threshold(bd, bd.rect, new Point(), "<", 0x7FFFFF, 0xFF000000, 0xFFFFFF);
72. bd.threshold(bd, bd.rect, new Point(), "!=", 0x0, 0xFFFFFFFF, 0xFFFFFF);
73. }
74.
75. private function halfTone(event:Event = null):void
76. {
77. bitmap.bitmapData = copybd.clone();
78. var bd:BitmapData = bitmap.bitmapData;
79.
80. const PSIZE:int = 80;
81. if (bd.width < PSIZE || bd.height < PSIZE) return;
82.
83. var SIZE:int = (bd.width > bd.height) ? bd.width / PSIZE : bd.height / PSIZE;
84. var matrix:Array = new Array();
85. for (var i:int = 0; i < SIZE * SIZE; i++) matrix.push(1);
86. var filter:ConvolutionFilter = new ConvolutionFilter(SIZE, SIZE, matrix, SIZE * SIZE);
87. bd.applyFilter(bd, bd.rect, new Point(0, 0), filter);
88.
89. var canvas:Sprite = new Sprite();
90. for (var y:int = Math.floor(SIZE / 2); y < bd.height; y += SIZE)
91. {
92. for (var x:int = Math.floor(SIZE / 2); x < bd.width; x += SIZE)
93. {
94. canvas.graphics.beginFill(0x0);
95. canvas.graphics.drawCircle(x, y, bd.getPixel(x, y) / (255 * 255 * 255 / SIZE));
96. canvas.graphics.endFill();
97. }
98. }
99.
100. bitmap.bitmapData = new BitmapData(WIDTH, HEIGHT, false);
101. bitmap.bitmapData.draw(canvas);
102.
103. // ハーフトーンの色が変わる謎現象対策(本来ならこれは外したほうがいいです)
104. bitmap.bitmapData.threshold(bitmap.bitmapData, bd.rect, new Point(), "<", 0x7FFFFF, 0xFF000000, 0xFFFFFF);
105. bitmap.bitmapData.threshold(bitmap.bitmapData, bd.rect, new Point(), "!=", 0x0, 0xFFFFFFFF, 0xFFFFFF);
106. }
107.
108. private function randomDither(event:Event = null):void
109. {
110. bitmap.bitmapData = copybd.clone();
111. var bd:BitmapData = bitmap.bitmapData;
112.
113. var color:ColorRGB = new ColorRGB();
114.
115. for (var y:int = 0; y < bd.height; y++)
116. {
117. for (var x:int = 0; x < bd.width; x++)
118. {
119. color.value = bd.getPixel(x, y);
120. var gray:int = (color.r + color.g + color.b) / 3;
121. if (gray < Math.random() * 256)
122. {
123. color.r = color.g = color.b = 0;
124. }
125. else
126. {
127. color.r = color.g = color.b = 255;
128. }
129. bd.setPixel(x, y, color.value);
130. }
131. }
132. }
133.
134. private function bayerDither(event:Event = null):void
135. {
136. bitmap.bitmapData = copybd.clone();
137. var bd:BitmapData = bitmap.bitmapData;
138. var color:ColorRGB = new ColorRGB();
139.
140. var pattern:Array =
141. [
142. [ 0, 8, 2, 10],
143. [12, 4, 14, 6],
144. [ 3, 11, 1, 9],
145. [15, 7, 13, 5]
146. ];
147.
148. for (var y:int = 0; y < bd.height; y++)
149. {
150. for (var x:int = 0; x < bd.width; x++)
151. {
152. var threshold:int = pattern[x % 4][y % 4];
153. color.value = bd.getPixel(x, y);
154.
155. color.r /= 16;
156. color.g /= 16;
157. color.b /= 16;
158.
159. var avg:int = (color.r + color.g + color.b) / 3;
160. if (avg < threshold) color.r = color.g = color.b = 0;
161. else color.r = color.g = color.b = 255;
162.
163. bd.setPixel(x, y, color.value);
164. }
165. }
166. }
167.
168. private function errorDiffusion(event:Event = null):void
169. {
170. bitmap.bitmapData = copybd.clone();
171. var bd:BitmapData = bitmap.bitmapData;
172. var color:ColorRGB = new ColorRGB();
173.
174. for (var y:int = 0; y < bd.height; y++)
175. {
176. var error:int = 0;
177. for (var x:int = 0; x < bd.width; x++)
178. {
179. color.value = bd.getPixel(x, y);
180. var avg:int = (color.r + color.g + color.b) / 3;
181. avg -= error;
182.
183. if (avg < 128)
184. {
185. error = -avg;
186. avg = 0;
187. }
188. else
189. {
190. error = 255 - avg;
191. avg = 255;
192. }
193.
194. color.r = color.g = color.b = avg;
195. bd.setPixel(x, y, color.value);
196. }
197. }
198. }
199.
200. private function errorDiffusion2(event:Event = null):void
201. {
202. bitmap.bitmapData = copybd.clone();
203. var bd:BitmapData = bitmap.bitmapData;
204.
205. var color:ColorRGB = new ColorRGB();
206. var error:Array = new Array(bd.height);
207. var filter:Array =
208. [
209. [ 0, 0, 0, 7/48, 5/48],
210. [3/48, 5/48, 7/48, 5/48, 3/48],
211. [1/48, 3/48, 5/48, 3/48, 1/48],
212. ];
213.
214. for (var y:int = 0; y < bd.height; y++)
215. {
216. error[y] = new Array(bd.width);
217. for (var x:int = 0; x < bd.width; x++)
218. {
219. error[y][x] = 0;
220. }
221. }
222.
223. for (y = 0; y < bd.height; y++)
224. {
225. for (x = 0; x < bd.width; x++)
226. {
227. color.value = bd.getPixel(x, y);
228. var avg:int = (color.r + color.g + color.b) / 3;
229.
230. avg -= error[y][x];
231.
232. var e:int = 0;
233. if (avg < 128)
234. {
235. e = -avg;
236. avg = 0;
237. }
238. else
239. {
240. e = 255 - avg;
241. avg = 255;
242. }
243.
244. color.r = color.g = color.b = avg;
245.
246. for (var yy:int = 0; yy < 3; yy++)
247. {
248. for (var xx:int = -2; xx <= 2; xx++)
249. {
250. if (y + yy < 0 || bd.height <= y + yy ||
251. x + xx < 0 || bd.width <= x + xx) continue;
252.
253. error[y + yy][x + xx] += e * filter[yy][xx + 2];
254. }
255. }
256.
257. bd.setPixel(x, y, color.value);
258. }
259. }
260. }
261.
262. private function errorDiffusion3(event:Event = null):void
263. {
264. bitmap.bitmapData = copybd.clone();
265. var bd:BitmapData = bitmap.bitmapData;
266.
267. var color:ColorRGB = new ColorRGB();
268. var error:Array = new Array(bd.height);
269. var filter:Array =
270. [
271. [ 0, 0, 7 / 16],
272. [3 / 16, 5 / 16, 1 / 16]
273. ];
274.
275. for (var y:int = 0; y < bd.height; y++)
276. {
277. error[y] = new Array(bd.width);
278. for (var x:int = 0; x < bd.width; x++)
279. {
280. error[y][x] = 0;
281. }
282. }
283.
284. for (y = 0; y < bd.height; y++)
285. {
286. for (x = 0; x < bd.width; x++)
287. {
288. color.value = bd.getPixel(x, y);
289. var avg:int = (color.r + color.g + color.b) / 3;
290.
291. avg -= error[y][x];
292.
293. var e:int = 0;
294. if (avg < 128)
295. {
296. e = -avg;
297. avg = 0;
298. }
299. else
300. {
301. e = 255 - avg;
302. avg = 255;
303. }
304.
305. color.r = color.g = color.b = avg;
306.
307. for (var yy:int = 0; yy < 2; yy++)
308. {
309. for (var xx:int = -1; xx <= 1; xx++)
310. {
311. if (y + yy < 0 || bd.height <= y + yy ||
312. x + xx < 0 || bd.width <= x + xx) continue;
313.
314. error[y + yy][x + xx] += e * filter[yy][xx + 1];
315. }
316. }
317.
318. bd.setPixel(x, y, color.value);
319. }
320. }
321. }
322.
323. private function errorDiffusion4(event:Event = null):void
324. {
325. bitmap.bitmapData = copybd.clone();
326. var bd:BitmapData = bitmap.bitmapData;
327.
328. var color:ColorRGB = new ColorRGB();
329. var error:Array = new Array(bd.height);
330. var filter:Array =
331. [
332. [ 0, 0, 0, 8/42, 4/42],
333. [2/42, 4/42, 8/42, 4/42, 2/42],
334. [1/42, 2/42, 4/42, 2/42, 1/42]
335. ];
336.
337. for (var y:int = 0; y < bd.height; y++)
338. {
339. error[y] = new Array(bd.width);
340. for (var x:int = 0; x < bd.width; x++)
341. {
342. error[y][x] = 0;
343. }
344. }
345.
346. for (y = 0; y < bd.height; y++)
347. {
348. for (x = 0; x < bd.width; x++)
349. {
350. color.value = bd.getPixel(x, y);
351. var avg:int = (color.r + color.g + color.b) / 3;
352.
353. avg -= error[y][x];
354.
355. var e:int = 0;
356. if (avg < 128)
357. {
358. e = -avg;
359. avg = 0;
360. }
361. else
362. {
363. e = 255 - avg;
364. avg = 255;
365. }
366.
367. color.r = color.g = color.b = avg;
368.
369. for (var yy:int = 0; yy < 3; yy++)
370. {
371. for (var xx:int = -2; xx <= 2; xx++)
372. {
373. if (y + yy < 0 || bd.height <= y + yy ||
374. x + xx < 0 || bd.width <= x + xx) continue;
375.
376. error[y + yy][x + xx] += e * filter[yy][xx + 2];
377. }
378. }
379.
380. bd.setPixel(x, y, color.value);
381. }
382. }
383. }
384.
385. private function errorDiffusion5(event:Event = null):void
386. {
387. bitmap.bitmapData = copybd.clone();
388. var bd:BitmapData = bitmap.bitmapData;
389.
390. var color:ColorRGB = new ColorRGB();
391. var error:Array = new Array(bd.height);
392. var filter:Array =
393. [
394. [ 0, 0, 0, 8/32, 4/32],
395. [2/32, 4/32, 8/32, 4/32, 2/32]
396. ];
397.
398. for (var y:int = 0; y < bd.height; y++)
399. {
400. error[y] = new Array(bd.width);
401. for (var x:int = 0; x < bd.width; x++)
402. {
403. error[y][x] = 0;
404. }
405. }
406.
407. for (y = 0; y < bd.height; y++)
408. {
409. for (x = 0; x < bd.width; x++)
410. {
411. color.value = bd.getPixel(x, y);
412. var avg:int = (color.r + color.g + color.b) / 3;
413.
414. avg -= error[y][x];
415.
416. var e:int = 0;
417. if (avg < 128)
418. {
419. e = -avg;
420. avg = 0;
421. }
422. else
423. {
424. e = 255 - avg;
425. avg = 255;
426. }
427.
428. color.r = color.g = color.b = avg;
429.
430. for (var yy:int = 0; yy < 2; yy++)
431. {
432. for (var xx:int = -2; xx <= 2; xx++)
433. {
434. if (y + yy < 0 || bd.height <= y + yy ||
435. x + xx < 0 || bd.width <= x + xx) continue;
436.
437. error[y + yy][x + xx] += e * filter[yy][xx + 2];
438. }
439. }
440.
441. bd.setPixel(x, y, color.value);
442. }
443. }
444. }
445.
446. private function errorDiffusion6(event:Event = null):void
447. {
448. bitmap.bitmapData = copybd.clone();
449. var bd:BitmapData = bitmap.bitmapData;
450.
451. var color:ColorRGB = new ColorRGB();
452. var error:Array = new Array(bd.height);
453. var filter:Array =
454. [
455. [ 0, 0, 0, 5/32, 3/32],
456. [2/32, 4/32, 5/32, 4/32, 2/32],
457. [ 0, 2/32, 3/32, 2/32, 0]
458. ];
459.
460. for (var y:int = 0; y < bd.height; y++)
461. {
462. error[y] = new Array(bd.width);
463. for (var x:int = 0; x < bd.width; x++)
464. {
465. error[y][x] = 0;
466. }
467. }
468.
469. for (y = 0; y < bd.height; y++)
470. {
471. for (x = 0; x < bd.width; x++)
472. {
473. color.value = bd.getPixel(x, y);
474. var avg:int = (color.r + color.g + color.b) / 3;
475.
476. avg -= error[y][x];
477.
478. var e:int = 0;
479. if (avg < 128)
480. {
481. e = -avg;
482. avg = 0;
483. }
484. else
485. {
486. e = 255 - avg;
487. avg = 255;
488. }
489.
490. color.r = color.g = color.b = avg;
491.
492. for (var yy:int = 0; yy < 3; yy++)
493. {
494. for (var xx:int = -2; xx <= 2; xx++)
495. {
496. if (y + yy < 0 || bd.height <= y + yy ||
497. x + xx < 0 || bd.width <= x + xx) continue;
498.
499. error[y + yy][x + xx] += e * filter[yy][xx + 2];
500. }
501. }
502.
503. bd.setPixel(x, y, color.value);
504. }
505. }
506. }
507.
508. private function errorDiffusion7(event:Event = null):void
509. {
510. bitmap.bitmapData = copybd.clone();
511. var bd:BitmapData = bitmap.bitmapData;
512.
513. var color:ColorRGB = new ColorRGB();
514. var error:Array = new Array(bd.height);
515. var filter:Array =
516. [
517. [ 0, 0, 0, 4/16, 3/16],
518. [1/16, 2/16, 3/16, 2/16, 1/16]
519. ];
520.
521. for (var y:int = 0; y < bd.height; y++)
522. {
523. error[y] = new Array(bd.width);
524. for (var x:int = 0; x < bd.width; x++)
525. {
526. error[y][x] = 0;
527. }
528. }
529.
530. for (y = 0; y < bd.height; y++)
531. {
532. for (x = 0; x < bd.width; x++)
533. {
534. color.value = bd.getPixel(x, y);
535. var avg:int = (color.r + color.g + color.b) / 3;
536.
537. avg -= error[y][x];
538.
539. var e:int = 0;
540. if (avg < 128)
541. {
542. e = -avg;
543. avg = 0;
544. }
545. else
546. {
547. e = 255 - avg;
548. avg = 255;
549. }
550.
551. color.r = color.g = color.b = avg;
552.
553. for (var yy:int = 0; yy < 2; yy++)
554. {
555. for (var xx:int = -2; xx <= 2; xx++)
556. {
557. if (y + yy < 0 || bd.height <= y + yy ||
558. x + xx < 0 || bd.width <= x + xx) continue;
559.
560. error[y + yy][x + xx] += e * filter[yy][xx + 2];
561. }
562. }
563.
564. bd.setPixel(x, y, color.value);
565. }
566. }
567. }
568.
569. private function errorDiffusion8(event:Event = null):void
570. {
571. bitmap.bitmapData = copybd.clone();
572. var bd:BitmapData = bitmap.bitmapData;
573.
574. var color:ColorRGB = new ColorRGB();
575. var error:Array = new Array(bd.height);
576. var filter:Array =
577. [
578. [ 0, 0, 2/4],
579. [1/4, 1/4, 0]
580. ];
581.
582. for (var y:int = 0; y < bd.height; y++)
583. {
584. error[y] = new Array(bd.width);
585. for (var x:int = 0; x < bd.width; x++)
586. {
587. error[y][x] = 0;
588. }
589. }
590.
591. for (y = 0; y < bd.height; y++)
592. {
593. for (x = 0; x < bd.width; x++)
594. {
595. color.value = bd.getPixel(x, y);
596. var avg:int = (color.r + color.g + color.b) / 3;
597.
598. avg -= error[y][x];
599.
600. var e:int = 0;
601. if (avg < 128)
602. {
603. e = -avg;
604. avg = 0;
605. }
606. else
607. {
608. e = 255 - avg;
609. avg = 255;
610. }
611.
612. color.r = color.g = color.b = avg;
613.
614. for (var yy:int = 0; yy < 2; yy++)
615. {
616. for (var xx:int = -1; xx <= 1; xx++)
617. {
618. if (y + yy < 0 || bd.height <= y + yy ||
619. x + xx < 0 || bd.width <= x + xx) continue;
620.
621. error[y + yy][x + xx] += e * filter[yy][xx + 1];
622. }
623. }
624.
625. bd.setPixel(x, y, color.value);
626. }
627. }
628. }
629. }
630. }
noswf download
download fullscreen
TALK
ハーフトーンが白黒にならない謎現象を発見したので無理やり修正しました。
at 2009/12/03 17:55:34 by icon rsakane
TAGS
BitmapData beautifl binary color dithering effect filter graphics halftone
FAVORITE BY
icon tkinjo
icon ferv
icon bongiovi015
icon nui
icon motikawa_rgm
icon savage69kr
icon mogera
icon dizgid
icon KinkumaDesign
icon coppieee
icon PESakaTFM
icon nki2
icon DS729_aka_KGC
icon PROT830
icon luar
icon alumican_net
icon OKASUKE
icon ngtn
icon y_tti
icon a24
icon Aquioux
icon osamX
icon paq
icon uwi
icon FlashFit
icon miniapp
icon skn
icon clockmaker
icon hikipuro
icon ep91ckok
icon nanlow
icon sw_lucchini
icon digitrick
icon yanbaka
icon ninehundred
icon miyaoka
icon yd_niku
icon norichika2
icon ozguraltay :
halftoneeffect
icon zmaxlin :
BitmapDatabinaryFilter
icon sr_forest :
BitmapDataFiltereffect
icon makc3d :
ditheringha ha awesome, does it work for >2 colors?
icon nitoyon :
いろんな二値化を紹介
icon a440hlz :
beautifleffect
icon nicoptere :
BitmapDatacoloreffectfiltergraphicsbinary
icon keim_at_Si :
すごく参考になる
FORKED
error ColorRGB bitmapData height value filter content clone setPixel getPixel color gray matrix BitmapData Array threshold ConvolutionFilter Event rect loader
ad
about wonderfl | FAQ | 利用規約 | プライバシーポリシー
KAYAC Project
* 建築家と注文住宅を建てる ハウスコ
* 母の日に絵のプレゼント ART-Meter
* 湘南情報 湘南Clip
* 総務情報 総務の森
* ブログパーツ BlogDeco
* Flash情報 _level0
* 鎌倉でのランチなら bowls
* 声優・ナレーター・おしゃべり好きのコミュニティ こえ部
* ポケットフレンズ コンチ
* DIYから手芸まで 手作りコミュニティ dododay
* build flash online wonderfl
* flash ゲーム wonderfl Flash-Games
* お笑い動画 配信サイト Owarai.tv
* _ twitterプロモーション
* _ iPhone企画 制作
* _ mixiアプリ 企画 制作
Copyright © KAYAC Inc. All Rights Reserved.
Get Adobe Flash Player