-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMain.cpp
More file actions
701 lines (610 loc) · 23.1 KB
/
Main.cpp
File metadata and controls
701 lines (610 loc) · 23.1 KB
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
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
#include "ncbind.hpp"
#include <vector>
using namespace std;
#ifndef WIN32
typedef tjs_uint8 BYTE;
typedef tjs_uint16 WORD;
typedef tjs_uint32 DWORD;
#endif
#define ltAddAlpha 12
// レイヤクラスを参照
iTJSDispatch2 *getLayerClass(void)
{
tTJSVariant var;
TVPExecuteExpression(TJS_W("Layer"), &var);
return var.AsObjectNoAddRef();
}
//----------------------------------------------
// レイヤイメージ操作ユーティリティ
// バッファ参照用の型
typedef unsigned char *WrtRefT;
typedef unsigned char const *ReadRefT;
typedef DWORD PixelT;
static tjs_uint32 hasImageHint, imageWidthHint, imageHeightHint;
static tjs_uint32 mainImageBufferHint, mainImageBufferPitchHint, mainImageBufferForWriteHint;
static tjs_uint32 provinceImageBufferHint, provinceImageBufferPitchHint, provinceImageBufferForWriteHint;
static tjs_uint32 clipLeftHint, clipTopHint, clipWidthHint, clipHeightHint;
static tjs_uint32 updateHint;
static tjs_uint32 typeHint;
/**
* レイヤのサイズとバッファを取得する
*/
static bool
GetLayerSize(iTJSDispatch2 *lay, tjs_int32 &w, tjs_int32 &h, tjs_int32 &pitch)
{
iTJSDispatch2 *layerClass = getLayerClass();
// レイヤインスタンス以外ではエラー
if (!lay || TJS_FAILED(lay->IsInstanceOf(0, 0, 0, TJS_W("Layer"), lay))) return false;
// レイヤイメージは在るか?
tTJSVariant val;
if (TJS_FAILED(layerClass->PropGet(0, TJS_W("hasImage"), &hasImageHint, &val, lay)) || (val.AsInteger() == 0)) return false;
// レイヤサイズを取得
if (TJS_FAILED(layerClass->PropGet(0, TJS_W("imageWidth"), &imageWidthHint, &val, lay))) return false;
w = (tjs_int32)val.AsInteger();
if (TJS_FAILED(layerClass->PropGet(0, TJS_W("imageHeight"), &imageHeightHint, &val, lay))) return false;
h = (tjs_int32)val.AsInteger();
// ピッチ取得
if (TJS_FAILED(layerClass->PropGet(0, TJS_W("mainImageBufferPitch"), &mainImageBufferPitchHint, &val, lay))) return false;
pitch = (tjs_int32)val.AsInteger();
// 正常な値かどうか
return (w > 0 && h > 0 && pitch != 0);
}
// 書き込み用
static bool
GetLayerBufferAndSize(iTJSDispatch2 *lay, tjs_int32 &w, tjs_int32 &h, WrtRefT &ptr, tjs_int32 &pitch)
{
iTJSDispatch2 *layerClass = getLayerClass();
if (!GetLayerSize(lay, w, h, pitch)) return false;
// バッファ取得
tTJSVariant val;
if (TJS_FAILED(layerClass->PropGet(0, TJS_W("mainImageBufferForWrite"), &mainImageBufferForWriteHint, &val, lay))) return false;
ptr = reinterpret_cast<WrtRefT>(val.AsInteger());
return (ptr != 0);
}
/**
* クリップ領域のサイズとバッファを取得する
*/
static bool
GetClipSize(iTJSDispatch2 *lay, tjs_int32 &l, tjs_int32 &t, tjs_int32 &w, tjs_int32 &h, tjs_int32 &pitch)
{
iTJSDispatch2 *layerClass = getLayerClass();
// レイヤインスタンス以外ではエラー
if (!lay || TJS_FAILED(lay->IsInstanceOf(0, 0, 0, TJS_W("Layer"), lay))) return false;
// レイヤイメージは在るか?
tTJSVariant val;
if (TJS_FAILED(layerClass->PropGet(0, TJS_W("hasImage"), &hasImageHint, &val, lay)) || (val.AsInteger() == 0)) return false;
// クリップサイズを取得
if (TJS_FAILED(layerClass->PropGet(0, TJS_W("clipLeft"), &clipLeftHint, &val, lay))) return false;
l = (tjs_int32)val.AsInteger();
if (TJS_FAILED(layerClass->PropGet(0, TJS_W("clipTop"), &clipTopHint, &val, lay))) return false;
t = (tjs_int32)val.AsInteger();
if (TJS_FAILED(layerClass->PropGet(0, TJS_W("clipWidth"), &clipWidthHint, &val, lay))) return false;
w = (tjs_int32)val.AsInteger();
if (TJS_FAILED(layerClass->PropGet(0, TJS_W("clipHeight"), &clipHeightHint, &val, lay))) return false;
h = (tjs_int32)val.AsInteger();
// ピッチ取得
if (TJS_FAILED(layerClass->PropGet(0, TJS_W("mainImageBufferPitch"), &mainImageBufferPitchHint, &val, lay))) return false;
pitch = (tjs_int32)val.AsInteger();
// 正常な値かどうか
return (w > 0 && h > 0 && pitch != 0);
}
// 書き込み用
static bool
GetClipBufferAndSize(iTJSDispatch2 *lay, tjs_int32 &l, tjs_int32 &t, tjs_int32 &w, tjs_int32 &h, WrtRefT &ptr, tjs_int32 &pitch)
{
iTJSDispatch2 *layerClass = getLayerClass();
if (!GetClipSize(lay, l, t, w, h, pitch)) return false;
// バッファ取得
tTJSVariant val;
if (TJS_FAILED(layerClass->PropGet(0, TJS_W("mainImageBufferForWrite"), &mainImageBufferForWriteHint, &val, lay))) return false;
ptr = reinterpret_cast<WrtRefT>(val.AsInteger());
if (ptr != 0) {
ptr += pitch * t + l * 4;
return true;
}
return false;
}
/**
* Layer.copyRightBlueToLeftAlpha
* レイヤ右半分の Blue CHANNEL を左半分の Alpha CHANNEL に複製する
*/
static tjs_error TJS_INTF_METHOD
copyRightBlueToLeftAlpha(tTJSVariant *result, tjs_int numparams, tTJSVariant **param, iTJSDispatch2 *lay)
{
// 書き込み先
WrtRefT dbuf = 0;
tjs_int32 dw, dh, dpitch;
if (!GetLayerBufferAndSize(lay, dw, dh, dbuf, dpitch)) {
TVPThrowExceptionMessage(TJS_W("dest must be Layer."));
}
// 半分
dw /= 2;
// コピー
WrtRefT sbuf = dbuf + dw*4;
dbuf += 3;
for (int i=0;i<dh;i++) {
WrtRefT p = sbuf; // B領域
WrtRefT q = dbuf; // A領域
for (int j=0;j<dw;j++) {
*q = *p;
p += 4;
q += 4;
}
sbuf += dpitch;
dbuf += dpitch;
}
ncbPropAccessor layObj(lay);
layObj.FuncCall(0, TJS_W("update"), &updateHint, NULL, (tTVInteger)0, (tTVInteger)0, (tTVInteger)dw, (tTVInteger)dh);
return TJS_S_OK;
}
/**
* Layer.copyBottomBlueToTopAlpha
* レイヤ右半分の Blue CHANNEL を左半分の Alpha CHANNELに複製する
*/
static tjs_error TJS_INTF_METHOD
copyBottomBlueToTopAlpha(tTJSVariant *result, tjs_int numparams, tTJSVariant **param, iTJSDispatch2 *lay)
{
// 書き込み先
WrtRefT dbuf = 0;
tjs_int32 dw, dh, dpitch;
if (!GetLayerBufferAndSize(lay, dw, dh, dbuf, dpitch)) {
TVPThrowExceptionMessage(TJS_W("dest must be Layer."));
}
// 半分
dh /= 2;
// コピー
WrtRefT sbuf = dbuf + dh * dpitch;
dbuf += 3;
for (int i=0;i<dh;i++) {
WrtRefT p = sbuf; // B領域
WrtRefT q = dbuf; // A領域
for (int j=0;j<dw;j++) {
*q = *p;
p += 4;
q += 4;
}
sbuf += dpitch;
dbuf += dpitch;
}
ncbPropAccessor layObj(lay);
layObj.FuncCall(0, TJS_W("update"), &updateHint, NULL, (tTVInteger)0, (tTVInteger)0, (tTVInteger)dw, (tTVInteger)dh);
return TJS_S_OK;
}
static tjs_error TJS_INTF_METHOD
fillAlpha(tTJSVariant *result, tjs_int numparams, tTJSVariant **param, iTJSDispatch2 *lay)
{
// 書き込み先
WrtRefT dbuf = 0;
tjs_int32 l, t, dw, dh, dpitch;
if (!GetClipBufferAndSize(lay, l, t, dw, dh, dbuf, dpitch)) {
TVPThrowExceptionMessage(TJS_W("dest must be Layer."));
}
dbuf += 3;
// 全部 0xffでうめる
for (int i=0;i<dh;i++) {
WrtRefT q = dbuf; // A領域
for (int j=0;j<dw;j++) {
*q = 0xff;
q += 4;
}
dbuf += dpitch;
}
ncbPropAccessor layObj(lay);
layObj.FuncCall(0, TJS_W("update"), &updateHint, NULL, (tTVInteger)l, (tTVInteger)t, (tTVInteger)dw, (tTVInteger)dh);
return TJS_S_OK;
}
static tjs_error TJS_INTF_METHOD
copyAlphaToProvince(tTJSVariant *result, tjs_int numparams, tTJSVariant **param, iTJSDispatch2 *lay)
{
iTJSDispatch2 *layerClass = getLayerClass();
ReadRefT sbuf = 0;
WrtRefT dbuf = 0;
tjs_int32 l, t, w, h, spitch, dpitch, threshold = -1, matched = 1, otherwise = 0;
if (TJS_PARAM_EXIST(0)) {
threshold = (tjs_int32)(param[0]->AsInteger());
}
if (TJS_PARAM_EXIST(1)) {
matched = (tjs_int32)(param[1]->AsInteger());
}
if (TJS_PARAM_EXIST(2)) {
otherwise = (tjs_int32)(param[2]->AsInteger());
}
if (!GetClipSize(lay, l, t, w, h, spitch)) {
TVPThrowExceptionMessage(TJS_W("src must be Layer."));
}
tTJSVariant val;
if (TJS_FAILED(layerClass->PropGet(0, TJS_W("mainImageBuffer"), &mainImageBufferHint, &val, lay)) ||
(sbuf = reinterpret_cast<ReadRefT>(val.AsInteger())) == NULL) {
TVPThrowExceptionMessage(TJS_W("src has no image."));
}
sbuf += spitch * t + l * 4;
val.Clear();
if (TJS_FAILED(layerClass->PropGet(0, TJS_W("provinceImageBufferForWrite"), &provinceImageBufferForWriteHint, &val, lay)) ||
(dbuf = reinterpret_cast<WrtRefT>(val.AsInteger())) == NULL) {
TVPThrowExceptionMessage(TJS_W("dst has no province image."));
}
val.Clear();
if (TJS_FAILED(layerClass->PropGet(0, TJS_W("provinceImageBufferPitch"), &provinceImageBufferPitchHint, &val, lay)) ||
(dpitch = (tjs_int32)val.AsInteger()) == 0) {
TVPThrowExceptionMessage(TJS_W("dst has no province pitch."));
}
dbuf += dpitch * t + l;
sbuf += 3;
unsigned char th = (unsigned char)threshold;
unsigned char on = (unsigned char)matched;
unsigned char off = (unsigned char)otherwise;
int mode = 0;
if (threshold >= 0 && threshold < 256) {
bool enmatch = (matched >= 0 && matched < 256);
bool enother = (otherwise >= 0 && otherwise < 256);
if (!enmatch && !enother) return TJS_S_OK; // 変更なし
mode = (enmatch && enother) ? 1 : enmatch ? 3 : 4;
} else if (threshold >= 256) {
if (otherwise >= 0 && otherwise < 256) mode = 2;
else return TJS_S_OK; // 変更なし
}
for (int y = 0; y < h; y++) {
WrtRefT p = dbuf;
ReadRefT q = sbuf;
switch (mode) {
case 0:
for (int x = 0; x < w; x++, q+=4) *p++ = *q;
break;
case 1:
for (int x = 0; x < w; x++, q+=4) *p++ = (*q >= th) ? on : off;
break;
case 2:
for (int x = 0; x < w; x++, q+=4) *p++ = off;
break;
case 3:
for (int x = 0; x < w; x++, q+=4, p++) if (*q >= th) *p = on;
break;
case 4:
for (int x = 0; x < w; x++, q+=4, p++) if (*q < th) *p = off;
break;
}
sbuf += spitch;
dbuf += dpitch;
}
ncbPropAccessor layObj(lay);
layObj.FuncCall(0, TJS_W("update"), &updateHint, NULL, (tTVInteger)l, (tTVInteger)t, (tTVInteger)w, (tTVInteger)h);
return TJS_S_OK;
}
static inline PixelT ClipAddAlpha(PixelT const pixel, PixelT alpha) {
const PixelT mask = 0xFF;
if (!((~alpha) & mask)) return pixel; // alpha == 255
if (!( alpha & mask)) return 0; // alpha == 0
const PixelT col_a = ((pixel >> 24) & mask) * alpha;
const PixelT col_r = ((pixel >> 16) & mask) * alpha;
const PixelT col_g = ((pixel >> 8) & mask) * alpha;
const PixelT col_b = ((pixel ) & mask) * alpha;
return ( (((col_a + (col_a >> 7)) >> 8) << 24) |
(((col_r + (col_r >> 7)) >> 8) << 16) |
(((col_g + (col_g >> 7)) >> 8) << 8) |
(((col_b + (col_b >> 7)) >> 8) ) );
}
static inline bool CalcClipArea(
tjs_int32 &dx, tjs_int32 &dy, const tjs_int32 diw, const tjs_int32 dih,
tjs_int32 &sx, tjs_int32 &sy, const tjs_int32 siw, const tjs_int32 sih,
tjs_int32 &w, tjs_int32 &h)
{
// srcが範囲外
if (sx+w <= 0 || sy+h <= 0 ||
sx >= siw || sy >= sih) return true;
// srcの負方向のカット
if (sx < 0) { w += sx; dx -= sx; sx = 0; }
if (sy < 0) { h += sy; dy -= sy; sy = 0; }
// srcの正方向のカット
tjs_int32 cut;
if ((cut = sx + w - siw) > 0) w -= cut;
if ((cut = sy + h - sih) > 0) h -= cut;
// dstが範囲外
if (dx+w <= 0 || dy+h <= 0 ||
dx >= diw || dy >= dih) return true;
// dstの負方向のカット
if (dx < 0) { w += dx; sx -= dx; dx = 0; }
if (dy < 0) { h += dy; sy -= dy; dy = 0; }
// dstの正方向のカット
if ((cut = dx + w - diw) > 0) w -= cut;
if ((cut = dy + h - dih) > 0) h -= cut;
return (w <= 0 || h <= 0);
}
static inline iTJSDispatch2 *GetSrcDstLayerInfo(
iTJSDispatch2 *src, tjs_int32 &siw, tjs_int32 &sih, tjs_int32 &spitch, ReadRefT &sbuf,
iTJSDispatch2 *dst, tjs_int32 &dl, tjs_int32 &dt, tjs_int32 &diw, tjs_int32 &dih, tjs_int32 &dpitch, WrtRefT &dbuf)
{
iTJSDispatch2 *layerClass = getLayerClass();
tTJSVariant val;
// 描画先クリッピング領域
if (!GetClipSize(dst, dl, dt, diw, dih, dpitch)) {
TVPThrowExceptionMessage(TJS_W("dest must be Layer."));
}
if (!GetLayerSize(src, siw, sih, spitch)) {
TVPThrowExceptionMessage(TJS_W("src must be Layer."));
}
// バッファ取得
if (TJS_FAILED(layerClass->PropGet(0, TJS_W("mainImageBuffer"), &mainImageBufferHint, &val, src))) sbuf = 0;
else sbuf = reinterpret_cast<ReadRefT>(val.AsInteger());
if (TJS_FAILED(layerClass->PropGet(0, TJS_W("mainImageBufferForWrite"), &mainImageBufferForWriteHint, &val, dst))) dbuf = 0;
else dbuf = reinterpret_cast<WrtRefT>(val.AsInteger());
if (!sbuf || !dbuf) TVPThrowExceptionMessage(TJS_W("Layer has no images."));
return layerClass;
}
static tjs_error TJS_INTF_METHOD
clipAlphaRect(tTJSVariant *result, tjs_int numparams, tTJSVariant **param, iTJSDispatch2 *dst)
{
ncbPropAccessor layObj(dst);
ReadRefT sbuf = 0;
WrtRefT dbuf = 0;
iTJSDispatch2 *src = 0;
tjs_int32 w, h;
tjs_int32 dx, dy, dl, dt, diw, dih, dpitch;
tjs_int32 sx, sy, siw, sih, spitch;
unsigned char clrval = 0;
bool clr = false, addalpha = false;
if (numparams < 7) return TJS_E_BADPARAMCOUNT;
dx = (tjs_int32)param[0]->AsInteger();
dy = (tjs_int32)param[1]->AsInteger();
src = param[2]->AsObjectNoAddRef();
sx = (tjs_int32)param[3]->AsInteger();
sy = (tjs_int32)param[4]->AsInteger();
w = (tjs_int32)param[5]->AsInteger();
h = (tjs_int32)param[6]->AsInteger();
if (numparams >= 8 && param[7]->Type() != tvtVoid) {
tjs_int32 n = (tjs_int32)param[7]->AsInteger();
clr = (n >= 0 && n < 256);
clrval = (unsigned char)(n & 255);
}
if (w <= 0|| h <= 0) return TJS_E_INVALIDPARAM;
iTJSDispatch2 *layerClass = GetSrcDstLayerInfo(src, siw,sih,spitch,sbuf,
dst, dl,dt,diw,dih,dpitch,dbuf);
{
tTJSVariant val;
if (TJS_FAILED(layerClass->PropGet(0, TJS_W("type"), &typeHint, &val, dst))) return false;
addalpha = val.AsInteger() == ltAddAlpha;
}
dbuf += dpitch * dt + dl * 4;
// 描画領域のクリッピング対応
dx -= dl;
dy -= dt;
// クリッピング
if (CalcClipArea(dx,dy,diw,dih, sx,sy,siw,sih, w,h)) goto none;
tjs_int32 x, y;
WrtRefT p;
ReadRefT q;
if (!addalpha) {
if (clr) {
for (y = 0; y < dy; y++) for ((x=0, p=dbuf+y*dpitch+3); x < diw; x++, p+=4) *p = clrval;
for (y = dy+h; y < dih; y++) for ((x=0, p=dbuf+y*dpitch+3); x < diw; x++, p+=4) *p = clrval;
}
for (y = 0; y < h; y++) {
if (clr) for ((x=0, p=dbuf+(y+dy)*dpitch+3); x < dx; x++, p+=4) *p = clrval;
p = dbuf + (y + dy) * dpitch + 3 + (dx*4);
q = sbuf + (y + sy) * spitch + 3 + (sx*4);
for (x = 0; x < w; x++, p+=4, q+=4) {
tjs_uint32 n = (tjs_uint32)(*p) * (tjs_uint32)(*q);
*p = (unsigned char)((n + (n >> 7)) >> 8);
}
if (clr) for (x = dx+w; x < diw; x++, p+=4) *p = clrval;
}
} else {
// for ltAddAlpha methods
PixelT *pp;
if (clr && clrval < 255) {
if (!clrval) {
for (y = 0; y < dy; y++) for ((x=0, pp=(PixelT*)(dbuf+y*dpitch)); x < diw; x++) *pp++ = 0;
for (y = dy+h; y < dih; y++) for ((x=0, pp=(PixelT*)(dbuf+y*dpitch)); x < diw; x++) *pp++ = 0;
for (y = 0; y < h; y++) {
for ((x=0, pp=(PixelT*)(dbuf+(y+dy)*dpitch)); x < dx; x++) *pp++ = 0;
pp = (PixelT*)(dbuf + (y + dy) * dpitch + (dx*4));
q = sbuf + (y + sy) * spitch + 3 + (sx*4);
for (x = 0; x < w; x++, q+=4) *pp++ = ClipAddAlpha(*pp, (PixelT)*q);
for (x = dx+w; x < diw; x++ ) *pp++ = 0;
}
} else {
PixelT cval = (PixelT)clrval;
for (y = 0; y < dy; y++) for ((x=0, pp=(PixelT*)(dbuf+y*dpitch)); x < diw; x++) *pp++ = ClipAddAlpha(*pp, cval);
for (y = dy+h; y < dih; y++) for ((x=0, pp=(PixelT*)(dbuf+y*dpitch)); x < diw; x++) *pp++ = ClipAddAlpha(*pp, cval);
for (y = 0; y < h; y++) {
for ((x=0, pp=(PixelT*)(dbuf+(y+dy)*dpitch)); x < dx; x++) *pp++ = ClipAddAlpha(*pp, cval);
pp = (PixelT*)(dbuf + (y + dy) * dpitch + (dx*4));
q = sbuf + (y + sy) * spitch + 3 + (sx*4);
for (x = 0; x < w; x++, q+=4) *pp++ = ClipAddAlpha(*pp, (PixelT)*q);
for (x = dx+w; x < diw; x++ ) *pp++ = ClipAddAlpha(*pp, cval);
}
}
} else {
for (y = 0; y < h; y++) {
pp = (PixelT*)(dbuf + (y + dy) * dpitch + (dx*4));
q = sbuf + (y + sy) * spitch + 3 + (sx*4);
for (x = 0; x < w; x++, q+=4) *pp++ = ClipAddAlpha(*pp, (PixelT)*q);
}
}
}
if (clr) {
layObj.FuncCall(0, TJS_W("update"), &updateHint, NULL, (tTVInteger)dl, (tTVInteger)dt, (tTVInteger)diw, (tTVInteger)dih);
} else {
layObj.FuncCall(0, TJS_W("update"), &updateHint, NULL, (tTVInteger)(dl+dx), (tTVInteger)(dt+dy), (tTVInteger)w, (tTVInteger)h);
}
return TJS_S_OK;
none:
// 領域範囲外で演算が行われない場合
if (clr) {
for (tjs_int32 y = 0; y < dih; y++) {
WrtRefT p = dbuf + y * dpitch + 3;
for (tjs_int32 x = 0; x < diw; x++, p+=4) *p = clrval;
}
layObj.FuncCall(0, TJS_W("update"), &updateHint, NULL, (tTVInteger)dl, (tTVInteger)dt, (tTVInteger)diw, (tTVInteger)dih);
}
return TJS_S_OK;
}
static tjs_error TJS_INTF_METHOD
overwrapRect(tTJSVariant *result, tjs_int numparams, tTJSVariant **param, iTJSDispatch2 *dst)
{
iTJSDispatch2 *layerClass = getLayerClass();
ncbPropAccessor layObj(dst);
ReadRefT sbuf = 0;
WrtRefT dbuf = 0;
iTJSDispatch2 *src = 0;
tTJSVariant val;
tjs_int32 w, h;
tjs_int32 dx, dy, dl, dt, diw, dih, dpitch;
tjs_int32 sx, sy, siw, sih, spitch;
unsigned char threshold = 1;
if (numparams < 7) return TJS_E_BADPARAMCOUNT;
dx = (tjs_int32)param[0]->AsInteger();
dy = (tjs_int32)param[1]->AsInteger();
src = param[2]->AsObjectNoAddRef();
sx = (tjs_int32)param[3]->AsInteger();
sy = (tjs_int32)param[4]->AsInteger();
w = (tjs_int32)param[5]->AsInteger();
h = (tjs_int32)param[6]->AsInteger();
if (numparams >= 8 && param[7]->Type() != tvtVoid) {
tjs_int32 n = (tjs_int32)param[7]->AsInteger();
if (n >= 0 && n < 256) threshold = (unsigned char)(n);
}
if (w <= 0|| h <= 0) return TJS_E_INVALIDPARAM;
GetSrcDstLayerInfo(src, siw,sih,spitch,sbuf,
dst, dl,dt,diw,dih,dpitch,dbuf);
dbuf += dpitch * dt + dl * 4;
// 描画領域のクリッピング対応
dx -= dl;
dy -= dt;
// クリッピング
if (CalcClipArea(dx,dy,diw,dih, sx,sy,siw,sih, w,h)) return TJS_S_OK;
for (tjs_int32 y = 0; y < h; y++) {
WrtRefT p = dbuf + (y + dy) * dpitch + (dx*4);
ReadRefT q = sbuf + (y + sy) * spitch + (sx*4);
for (tjs_int32 x = 0; x < w; x++, p+=4, q+=4) {
if (q[3] >= threshold) *(reinterpret_cast<PixelT*>(p)) = *(reinterpret_cast<const PixelT*>(q));
}
}
return TJS_S_OK;
}
static tjs_error TJS_INTF_METHOD
fillByProvince(tTJSVariant *result, tjs_int numparams, tTJSVariant **param, iTJSDispatch2 *lay)
{
iTJSDispatch2 *layerClass = getLayerClass();
if (numparams < 2) return TJS_E_BADPARAMCOUNT;
unsigned char index = (int)*param[0];
PixelT color = (int)*param[1];
// 書き込み先
WrtRefT dbuf = 0;
tjs_int32 l, t, dw, dh, dpitch;
if (!GetClipBufferAndSize(lay, l, t, dw, dh, dbuf, dpitch)) {
TVPThrowExceptionMessage(TJS_W("must be Layer."));
}
ReadRefT sbuf = 0;
tjs_int32 spitch;
{
tTJSVariant val;
if (TJS_FAILED(layerClass->PropGet(0, TJS_W("provinceImageBuffer"), &provinceImageBufferHint, &val, lay)) ||
(sbuf = reinterpret_cast<ReadRefT>(val.AsInteger())) == NULL) {
TVPThrowExceptionMessage(TJS_W("no province image."));
}
if (TJS_FAILED(layerClass->PropGet(0, TJS_W("provinceImageBufferPitch"), &provinceImageBufferPitchHint, &val, lay)) ||
(spitch = (tjs_int32)val.AsInteger()) == 0) {
TVPThrowExceptionMessage(TJS_W("no province pitch."));
}
}
sbuf += t * spitch + l;
for (int y = 0; y < dh; y++) {
ReadRefT q = sbuf;
PixelT *p = (PixelT*)dbuf;
for (int x = 0; x < dw; x++) {
if (*q == index) {
*(PixelT*)p = color;
}
q++;
p++;
}
sbuf += spitch;
dbuf += dpitch;
}
ncbPropAccessor layObj(lay);
layObj.FuncCall(0, TJS_W("update"), &updateHint, NULL, (tTVInteger)l, (tTVInteger)t, (tTVInteger)dw, (tTVInteger)dh);
return TJS_S_OK;
}
static tjs_error TJS_INTF_METHOD
fillToProvince(tTJSVariant *result, tjs_int numparams, tTJSVariant **param, iTJSDispatch2 *lay)
{
iTJSDispatch2 *layerClass = getLayerClass();
if (numparams < 2) return TJS_E_BADPARAMCOUNT;
tjs_uint32 rcolor = ((tjs_uint32)(param[0])->AsInteger());
PixelT color = rcolor & 0xFFFFFF;
bool allmatch = rcolor < 0;
unsigned char index = (int)(tTVInteger)*param[1];
tjs_int32 threshold = 64;
if (TJS_PARAM_EXIST(2)) {
threshold = (tjs_int32)(param[2]->AsInteger());
}
ReadRefT sbuf = 0;
tjs_int32 l, t, dw, dh, spitch;
if (!GetClipSize(lay, l, t, dw, dh, spitch)) {
TVPThrowExceptionMessage(TJS_W("src must be Layer."));
}
tTJSVariant val;
if (TJS_FAILED(layerClass->PropGet(0, TJS_W("mainImageBuffer"), &mainImageBufferHint, &val, lay)) ||
(sbuf = reinterpret_cast<ReadRefT>(val.AsInteger())) == NULL) {
TVPThrowExceptionMessage(TJS_W("src has no image."));
}
sbuf += spitch * t + l * 4;
WrtRefT dbuf = 0;
tjs_int32 dpitch;
val.Clear();
if (TJS_FAILED(layerClass->PropGet(0, TJS_W("provinceImageBufferForWrite"), &provinceImageBufferForWriteHint, &val, lay)) ||
(dbuf = reinterpret_cast<WrtRefT>(val.AsInteger())) == NULL) {
TVPThrowExceptionMessage(TJS_W("dst has no province image."));
}
val.Clear();
if (TJS_FAILED(layerClass->PropGet(0, TJS_W("provinceImageBufferPitch"), &provinceImageBufferPitchHint, &val, lay)) ||
(dpitch = (tjs_int32)val.AsInteger()) == 0) {
TVPThrowExceptionMessage(TJS_W("dst has no province pitch."));
}
dbuf += dpitch * t + l;
int minx = -1, miny = -1, maxx = -1, maxy = -1;
for (int y = 0; y < dh; y++) {
const PixelT *q = (const PixelT*)sbuf;
WrtRefT p = dbuf;
for (int x = 0; x < dw; x++) {
if ((allmatch || ((*q & 0xFFFFFF) == color)) && ((tjs_int32)(*q >> 24)) >= threshold) {
*p = index;
if (minx < 0 || minx > x) minx = x;
if (miny < 0 || miny > y) miny = y;
if (maxx < 0 || maxx < x) maxx = x;
if (maxy < 0 || maxy < y) maxy = y;
}
q++;
p++;
}
sbuf += spitch;
dbuf += dpitch;
}
if (result) {
result->Clear();
if (minx >= 0) {
iTJSDispatch2 *arr = TJSCreateArrayObject();
if (arr) {
tTJSVariant num;
num = (minx + l); arr->PropSetByNum(TJS_MEMBERENSURE, 0, &num, arr);
num = (miny + t); arr->PropSetByNum(TJS_MEMBERENSURE, 1, &num, arr);
num = (maxx + l - minx + 1); arr->PropSetByNum(TJS_MEMBERENSURE, 2, &num, arr);
num = (maxy + t - miny + 1); arr->PropSetByNum(TJS_MEMBERENSURE, 3, &num, arr);
tTJSVariant v(arr, arr);
arr->Release();
*result = v;
}
}
}
ncbPropAccessor layObj(lay);
layObj.FuncCall(0, TJS_W("update"), &updateHint, NULL, l, t, dw, dh);
return TJS_S_OK;
}
NCB_ATTACH_FUNCTION(copyRightBlueToLeftAlpha, Layer, copyRightBlueToLeftAlpha);
NCB_ATTACH_FUNCTION(copyBottomBlueToTopAlpha, Layer, copyBottomBlueToTopAlpha);
NCB_ATTACH_FUNCTION(fillAlpha, Layer, fillAlpha);
NCB_ATTACH_FUNCTION(copyAlphaToProvince, Layer, copyAlphaToProvince);
NCB_ATTACH_FUNCTION(clipAlphaRect, Layer, clipAlphaRect);
NCB_ATTACH_FUNCTION(overwrapRect, Layer, overwrapRect);
NCB_ATTACH_FUNCTION(fillByProvince, Layer, fillByProvince);
NCB_ATTACH_FUNCTION(fillToProvince, Layer, fillToProvince);