-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscr2.class.php
663 lines (552 loc) · 17.8 KB
/
scr2.class.php
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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
<?php
/*
Copyright 2007 Rafael de Oliveira Jannone
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
the Free Software Foundation; either version 2 of the License, or
(at your option) 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
require_once 'msxconv.inc.php';
class Surf2 extends Surf {
var $attr;
var $shape;
function Surf2($w, $h) {
$sz = ceil($w * $h / 8);
$this->width = $w & (~7);
$this->height = $h & (~7);
$this->attr = array_fill(0, $sz, 0);
$this->shape = array_fill(0, $sz, 0);
$this->sections = array(
'shapes' => array(0, 6144),
'chars' => array(6144, 768),
'attributes' => array(8192, 6144)
);
}
/*
function fromImageDirect(&$img, &$palette, $cb = NULL) {
$msx = imagecreate(1,1);
foreach ($palette as $rgb) {
imagecolorallocate($msx, $rgb[0], $rgb[1], $rgb[2]);
}
$byte = imagecreate(1,1);
$w = imagesx($img);
$h = imagesy($img);
$output = new Surf2($w, $h);
$output->palette = &$palette;
$addr = 0;
for ($y = 0; $y < $h; $y++) {
if ($cb && $y % 10 == 0)
call_user_func($cb, intval($y * 100/ $h));
for ($x = 0; $x < $w; $x += 8) {
// find most frequent colors
$freq = array();
for ($i = 0; $i < 8; $i++) {
$v = imagecolorat($img, $x + $i, $y);
$v = imagecolorsforindex($img, $v);
$v = imagecolorclosest($msx, $v['red'], $v['green'], $v['blue']);
$freq[$v]++;
}
arsort($freq);
// now we have the 2 most frequent colors
$attrs = array_keys($freq);
if (count($attrs) == 1)
$attrs[] = $attrs[0];
// use the 'byte' image to allocate 2 colors and match the 8 pixels
@imagecolordeallocate($byte, 0);
@imagecolordeallocate($byte, 1);
for ($i = 0; $i < 2; $i++) {
$v = imagecolorsforindex($msx, $attrs[$i]);
$v = array($v['red'], $v['green'], $v['blue']);
imagecolorallocate($byte, $v[0], $v[1], $v[2]);
}
// build the shape+attrs byte
$b = 128;
$shape = 0;
for ($i = 0; $i < 8; $i++) {
$v = imagecolorat($img, $x + $i, $y);
$v = imagecolorsforindex($img, $v);
$v = imagecolorclosest($byte, $v['red'], $v['green'], $v['blue']);
$shape |= ($v ? $b : 0);
$b >>= 1;
}
$output->attr[$addr] = chr(($attrs[1] << 4) | $attrs[0]);
$output->shape[$addr] = chr($shape);
++$addr;
}
}
$output->attr = implode('', $output->attr);
$output->shape = implode('', $output->shape);
return $output;
}
*/
function fromImageDirect(&$img, $cb = NULL) {
$palette = $this->palette_rgb;
$msx = imagecreate(1,1);
foreach ($palette as $rgb) {
imagecolorallocate($msx, $rgb[0], $rgb[1], $rgb[2]);
}
$byte = imagecreate(1,1);
$w = imagesx($img);
$h = imagesy($img);
$addr = 0;
for ($y = 0; $y < $h; $y++) {
if ($cb && $y % 10 == 0)
call_user_func($cb, intval($y * 100/ $h));
for ($x = 0; $x < $w; $x += 8) {
// find most frequent colors
$freq = array();
for ($i = 0; $i < 8; $i++) {
$v = imagecolorat($img, $x + $i, $y);
$v = imagecolorclosest($msx, ($v >> 16) & 0xFF, ($v >> 8) & 0xFF, $v & 0xFF);
$freq[$v]++;
}
arsort($freq);
// we need at least 2 colors
$attrs = array_keys($freq);
if (count($attrs) == 1)
$attrs[] = $attrs[0];
$best_e = MAX_PIXEL_DIST;
// test combinations
for ($v1 = 0; $v1 < count($attrs); $v1++) {
$c1 = $attrs[$v1];
for ($v2 = $v1 + 1; $v2 < count($attrs); $v2++) {
$c2 = $attrs[$v2];
// use the 'byte' image to allocate 2 colors and match the 8 pixels
@imagecolordeallocate($byte, 0);
@imagecolordeallocate($byte, 1);
$v = imagecolorsforindex($msx, $c1);
$a1 = array($v['red'], $v['green'], $v['blue']);
imagecolorallocate($byte, $a1[0], $a1[1], $a1[2]);
$v = imagecolorsforindex($msx, $c2);
$a2 = array($v['red'], $v['green'], $v['blue']);
imagecolorallocate($byte, $a2[0], $a2[1], $a2[2]);
// build the shape+attrs byte, find error
$e = 0;
$b = 128;
$shape = 0;
for ($i = 0; $i < 8; $i++) {
$v = imagecolorat($img, $x + $i, $y);
$v = array(($v >> 16) & 0xFF, ($v >> 8) & 0xFF, $v & 0xFF);
$p = imagecolorclosest($byte, $v[0], $v[1], $v[2]);
$pp = imagecolorsforindex($byte, $p);
$pp = array($p['red'], $p['green'], $p['blue']);
$dist = pixel_dist($v, $pp);
$e += $dist;
if ($e >= $best_e)
break;
$shape |= ($p ? $b : 0);
$b >>= 1;
}
if ($e < $best_e) {
$best_e = $e;
$best_c1 = $c1;
$best_c2 = $c2;
$best_shape = $shape;
}
}
}
$this->attr[$addr] = chr(($best_c2 << 4) | $best_c1);
$this->shape[$addr] = chr($best_shape);
++$addr;
}
}
$this->attr = implode('', $this->attr);
$this->shape = implode('', $this->shape);
}
function fromImageDitherLowQuality($input, $cb = NULL) {
$palette = $this->palette_rgb;
$first_color = ($this->palette_type == 'msx1') ? 1 : 0;
$msx = imagecreate(1,1);
foreach ($palette as $c) {
imagecolorallocate($msx, $c[0], $c[1], $c[2]);
}
/*
this conversion uses floyd-steinberg error diffusion
if you're not certain of how it works, check this link:
http://www.visgraf.impa.br/Courses/ip00/proj/Dithering1/floyd_steinberg_dithering.html
*/
/* output pointers */
$dest_shape = 0;
$dest_attr = 0;
/* init output surface */
$w = imagesx($input);
$h = imagesy($input);
/* error propagation line buffers */
$dither_jmp = ($w + 2);
$dither_sz = $dither_jmp; // sizeof(pixel)
$dither_line1 = array_fill(0, $dither_sz, array(0,0,0));
$dither_line2 = array_fill(0, $dither_sz, array(0,0,0));
/* init first line buffer */
if ($this->height > 0) {
$ow = $this->width;
for ($i=0; $i < $ow; $i++)
$dither_line1[$i + 1] = pixel_split(ImageColorAt($input, $i, 0));
}
$oh = $this->height;
for ($y = 0; $y < $oh; $y++) {
if ($cb && $y % 10 == 0)
call_user_func($cb, intval($y * 100/ $oh));
/* buffer line ahead, we'll need those neighbour pixels later */
if ($y < $oh - 1) {
for ($i=0; $i < $ow; $i++)
$dither_line2[$i + 1] = pixel_split(ImageColorAt($input, $i, $y + 1));
}
$src = 0;
for ($x = 0; $x < $ow; $x += 8, $src += 8, $dest_shape++, $dest_attr++) {
$g8 = imagecreatetruecolor(8, 1);
for ($i = 0; $i < 8; $i++) {
$c = $dither_line1[$x + $i + 1];
pixel_clamp($c);
imagesetpixel($g8, $i, 0, ($c[0] << 16) | ($c[1] << 8) | $c[2]);
}
imagetruecolortopalette($g8, true, 8);
$c1 = @imagecolorsforindex($g8, 0);
$c2 = @imagecolorsforindex($g8, 1);
imagedestroy($g8);
$c1 = imagecolorclosest($msx, $c1['red'], $c1['green'], $c1['blue']);
$c2 = imagecolorclosest($msx, $c2['red'], $c2['green'], $c2['blue']);
/* get the RGB color of bits 0 and 1 */
$bit0 = &$palette[$c1];
$bit1 = &$palette[$c2];
/* let's generate the bitmask using those colors */
/* reset current shape */
$v = 0;
/* for each of the 8 horizontal pixels... */
$bp = 128; /* bit pointer */
for ($b = 0; $b < 8; $b++, $bp >>= 1)
{
$dpos = $x + $b + 1; /* dither buffer position */
/* ...we must decide for bit 0 (off) or bit 1 (on) */
/* get pixel */
$dv1 = &$dither_line1[$dpos];
/* clamp pixel (inlined to be faster) */
$_v = $dv1[0];
$dv1[0] = ($_v < 0) ? 0 : (($_v > 255) ? 255 : $_v);
$_v = $dv1[1];
$dv1[1] = ($_v < 0) ? 0 : (($_v > 255) ? 255 : $_v);
$_v = $dv1[2];
$dv1[2] = ($_v < 0) ? 0 : (($_v > 255) ? 255 : $_v);
/* calculate distance from source to each considered color */
$_r1 = $dv1[0] - $bit0[0];
$_g1 = $dv1[1] - $bit0[1];
$_b1 = $dv1[2] - $bit0[2];
$d1 = $_r1 * $_r1 + $_g1 * $_g1 + $_b1 * $_b1;
$_r2 = $dv1[0] - $bit1[0];
$_g2 = $dv1[1] - $bit1[1];
$_b2 = $dv1[2] - $bit1[2];
$d2 = $_r2 * $_r2 + $_g2 * $_g2 + $_b2 * $_b2;
/* evaluate the closest pixel */
if ($d1 < $d2) {
/* bit0 wins */
//$v |= 0;
} else {
/* bit1 wins */
/* activate bit */
$v |= $bp;
}
if ($d1 < $d2) {
$bit = &$bit0;
$e = array($_r1, $_g1, $_b1);
} else {
$bit = &$bit1;
$e = array($_r2, $_g2, $_b2);
}
// propagate error to the right neighbour pixel
$p = &$dither_line1[$dpos];
$p[0] += $e[0] * 7 >> 4;
$p[1] += $e[1] * 7 >> 4;
$p[2] += $e[2] * 7 >> 4;
$p = &$dither_line2[$dpos - 1];
$p[0] += $e[0] * 3 >> 4;
$p[1] += $e[1] * 3 >> 4;
$p[2] += $e[2] * 3 >> 4;
$p = &$dither_line2[$dpos];
$p[0] += $e[0] * 5 >> 4;
$p[1] += $e[1] * 5 >> 4;
$p[2] += $e[2] * 5 >> 4;
$p = &$dither_line2[$dpos + 1];
$p[0] += $e[0] >> 4;
$p[1] += $e[1] >> 4;
$p[2] += $e[2] >> 4;
}
/* output winner combo */
$this->shape[$dest_shape] = chr($v);
$this->attr[$dest_attr] = chr(($c2 << 4) | $c1); /* combine attributes */
}
/* swap line buffers (avoiding reallocation) */
$swap = $dither_line1;
$dither_line1 = $dither_line2;
$dither_line2 = $swap;
/* the "ahead line" containing the propagated errors, is now the primary line (source) */
}
$this->attr = implode('', $this->attr);
$this->shape = implode('', $this->shape);
imagedestroy($msx);
}
function fromImageDither($input, $cb = NULL) {
$palette = $this->palette_rgb;
$first_color = ($this->palette_type == 'msx1') ? 1 : 0;
/*
this conversion uses floyd-steinberg error diffusion
if you're not certain of how it works, check this link:
http://www.visgraf.impa.br/Courses/ip00/proj/Dithering1/floyd_steinberg_dithering.html
*/
/* output pointers */
$dest_shape = 0;
$dest_attr = 0;
/* init output surface */
$w = imagesx($input);
$h = imagesy($input);
/* error propagation line buffers */
$dither_jmp = ($w + 2);
$dither_sz = $dither_jmp; // sizeof(pixel)
//dither_line1 = (pixel*)malloc(dither_sz);
$dither_line1 = array_fill(0, $dither_sz, array(0,0,0));
//dither_line2 = (pixel*)malloc(dither_sz);
$dither_line2 = array_fill(0, $dither_sz, array(0,0,0));
/* init first line buffer */
if ($this->height > 0) {
$ow = $this->width;
for ($i=0; $i < $ow; $i++)
$dither_line1[$i + 1] = pixel_split(ImageColorAt($input, $i, 0));
}
$oh = $this->height;
for ($y = 0; $y < $oh; $y++) {
if ($cb && $y % 10 == 0)
call_user_func($cb, intval($y * 100/ $oh));
/* buffer line ahead, we'll need those neighbour pixels later */
if ($y < $oh - 1) {
for ($i=0; $i < $ow; $i++)
$dither_line2[$i + 1] = pixel_split(ImageColorAt($input, $i, $y + 1));
}
$src = 0;
for ($x = 0; $x < $ow; $x += 8, $src += 8, $dest_shape++, $dest_attr++) {
/* shape value and colors [current combo] */
/*
byte v;
byte c1, c2;
pixel dit1[10];
pixel dit2[10];
*/
/* shape value and colors [winner combo] */
/*
byte wv;
byte wc1, wc2;
pixel wdit1[10];
pixel wdit2[10];
*/
/* distance from the source pixel */
$dist = MAX_PIXEL_DIST;
/* our objective is to find the combo (v / c1 / c2) that is closer
to our source pixels on the image */
/* considering all possible combinations of colors */
$dit1_bkp = array_slice($dither_line1, $src, 10);
for ($c1 = $first_color; $c1 < 16; $c1++) {
/* get the RGB color of bit 1 */
$bit0 = &$palette[$c1];
/* avoiding duplicate combinations */
for ($c2 = $c1 + 1; $c2 < 16; $c2++)
{
/* get the RGB color of bit 1 */
$bit1 = &$palette[$c2];
/* init micro-dither buffer, this one will be restored
for each combination */
$dit1 = $dit1_bkp;
/* let's generate the bitmask using those colors */
/* distance between generated pixel and source pixel */
$pdist = 0;
/* reset current shape */
$v = 0;
/* error list */
$el = array();
/* for each of the 8 horizontal pixels... */
$bp = 128; /* bit pointer */
for ($b = 1; $b < 9; $b++, $bp >>= 1)
{
/* ...we must decide for bit 0 (off) or bit 1 (on) */
/* get pixel */
$dv1 = &$dit1[$b];
/* clamp pixel (inlined to be faster) */
$_v = $dv1[0];
$dv1[0] = ($_v < 0) ? 0 : (($_v > 255) ? 255 : $_v);
$_v = $dv1[1];
$dv1[1] = ($_v < 0) ? 0 : (($_v > 255) ? 255 : $_v);
$_v = $dv1[2];
$dv1[2] = ($_v < 0) ? 0 : (($_v > 255) ? 255 : $_v);
/* calculate distance from source to each considered color */
$_r1 = $dv1[0] - $bit0[0];
$_g1 = $dv1[1] - $bit0[1];
$_b1 = $dv1[2] - $bit0[2];
$d1 = $_r1 * $_r1 + $_g1 * $_g1 + $_b1 * $_b1;
$_r2 = $dv1[0] - $bit1[0];
$_g2 = $dv1[1] - $bit1[1];
$_b2 = $dv1[2] - $bit1[2];
$d2 = $_r2 * $_r2 + $_g2 * $_g2 + $_b2 * $_b2;
/* evaluate the closest pixel */
if ($d1 < $d2) {
/* bit0 wins */
$pdist += $d1;
} else {
/* bit1 wins */
/* activate bit */
$v |= $bp;
$pdist += $d2;
}
if ($pdist >= $dist)
break;
//if ($floyd) {
// calculate error of the chosen color
if ($d1 < $d2) {
$bit = &$bit0;
$e = array($_r1, $_g1, $_b1);
} else {
$bit = &$bit1;
$e = array($_r2, $_g2, $_b2);
}
// propagate error to the right neighbour pixel
$p = &$dit1[$b+1];
$p[0] += $e[0] * 7 >> 4;
$p[1] += $e[1] * 7 >> 4;
$p[2] += $e[2] * 7 >> 4;
$el[] = $e;
//}
}
/* is this combo closer to the source pixels? */
if ($pdist < $dist) {
/* set as the current winner */
$dist = $pdist;
$wv = $v;
$wc1 = $c1;
$wc2 = $c2;
$wdit1 = $dit1;
$wel = $el;
}
}
}
/* output winner combo */
$this->shape[$dest_shape] = chr($wv);
$this->attr[$dest_attr] = chr(($wc2 << 4) | $wc1); /* combine attributes */
$wdit2 = array_slice($dither_line2, $src, 10);
//if ($floyd) {
/* once decided the 8 bits, we should propagate errors to the line below as well */
for ($b = 1; $b < 9; $b++) {
$e = $wel[$b-1];
$p = &$wdit2[$b-1];
$p[0] += $e[0] * 3 >> 4;
$p[1] += $e[1] * 3 >> 4;
$p[2] += $e[2] * 3 >> 4;
$p = &$wdit2[$b];
$p[0] += $e[0] * 5 >> 4;
$p[1] += $e[1] * 5 >> 4;
$p[2] += $e[2] * 5 >> 4;
$p = &$wdit2[$b+1];
$p[0] += $e[0] >> 4;
$p[1] += $e[1] >> 4;
$p[2] += $e[2] >> 4;
}
//}
/* update line buffers */
$cnt = count($wdit1);
for ($i=0; $i < $cnt; $i++) {
$dither_line1[$i + $src] = $wdit1[$i];
$dither_line2[$i + $src] = $wdit2[$i];
}
//array_splice($dither_line1, $src, $cnt, $wdit1);
//array_splice($dither_line2, $src, $cnt, $wdit2);
}
/* swap line buffers (avoiding reallocation) */
$swap = $dither_line1;
$dither_line1 = $dither_line2;
$dither_line2 = $swap;
/* the "ahead line" containing the propagated errors, is now the primary line (source) */
}
$this->attr = implode('', $this->attr);
$this->shape = implode('', $this->shape);
}
/* converts msx screen 2 surface to RGB surface */
function preview()
{
$palette = $this->palette_rgb;
$src_shape = 0;
$src_attr = 0;
$w8 = floor($this->width / 8);
$output = imagecreatetruecolor($this->width, $this->height);
for ($y = 0; $y < $this->height; $y++) {
for ($x = 0; $x < $w8; $x++, $src_shape++, $src_attr++, $dest += 8) {
$v = ord(substr($this->shape, $src_shape, 1));
$at = ord(substr($this->attr, $src_attr, 1));
$c1 = $at & 15;
$c2 = $at >> 4;
$bit0 = &$palette[$c1];
$bit1 = &$palette[$c2];
$ix = $x * 8;
for ($b = 0; $b < 8; $b++) {
$bit = ($v & 128) ? $bit1 : $bit0;
imagesetpixel($output, $ix + $b, $y, ($bit[0] << 16) | ($bit[1] << 8) | $bit[2]);
$v <<= 1;
}
}
}
return $output;
}
function _outputBinary() {
$file = fopen("php://output", "wb");
$w = $this->width;
$h = $this->height;
$w8 = $w >> 3;
/* write shape (VRAM @ 0) */
/* for each row of 8 pixels */
for ($r = 0; $r < $h; $r += 8) {
$base = $r * $w8;
/* for each column of 8 pixels */
for ($x = 0; $x < $w; $x += 8) {
$v = $base;
/* write block */
for ($y = 0; $y < 8; $y++, $v += $w8) {
fputs($file, substr($this->shape, $v, 1), 1);
}
++$base;
}
}
/* cover space between shape and attributes */
if ($this->tilemap) {
foreach ($this->tilemap as $val) {
fputs($file, chr($val), 1);
}
} else {
for ($r = 0; $r < 3; $r++) {
for ($x = 0; $x < 256; $x++)
fputs($file, chr($x), 1);
}
}
for ($r = 0; $r < 32; $r++) {
fputs($file, chr(0xD1) . chr(0) . chr($r) . chr(0x0F), 4);
}
for ($r = 0; $r < 1152; $r++)
fputs($file, chr(0), 1);
/* write attributes (VRAM @ 8192) */
/* for each row of 8 pixels */
for ($r = 0; $r < $h; $r += 8) {
$base = $r * $w8;
/* for each column of 8 pixels */
for ($x = 0; $x < $w; $x += 8)
{
$v = $base;
/* write block */
for ($y = 0; $y < 8; $y++, $v += $w8) {
fputs($file, substr($this->attr, $v, 1), 1);
}
++$base;
}
}
fclose($file);
}
}
?>