My favorites | Sign in
Project Home Downloads Source
Checkout   Browse   Changes    
 
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
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
#include "Draw.h"

NAMESPACE_UPP

#define LTIMING(x) // RTIMING(x)

int ImageBuffer::ScanKind() const
{
bool a255 = false;
bool a0 = false;
const RGBA *s = pixels;
const RGBA *e = s + GetLength();
while(s < e) {
if(s->a == 0)
a0 = true;
else
if(s->a == 255)
a255 = true;
else
return IMAGE_ALPHA;
s++;
}
return a255 ? a0 ? IMAGE_MASK : IMAGE_OPAQUE : IMAGE_EMPTY;
}

void ImageBuffer::SetHotSpots(const Image& src)
{
SetHotSpot(src.GetHotSpot());
Set2ndSpot(src.Get2ndSpot());
}

void ImageBuffer::Create(int cx, int cy)
{
ASSERT(cx >= 0 && cy >= 0);
size.cx = cx;
size.cy = cy;
pixels.Alloc(GetLength());
#ifdef _DEBUG
RGBA *s = pixels;
RGBA *e = pixels + GetLength();
byte a = 0;
while(s < e) {
s->a = a;
a = ~a;
s->r = 255;
s->g = s->b = 0;
s++;
}
#endif
kind = IMAGE_UNKNOWN;
spot2 = hotspot = Point(0, 0);
dots = Size(0, 0);
}

void ImageBuffer::DeepCopy(const ImageBuffer& img)
{
Create(img.GetSize());
SetHotSpot(img.GetHotSpot());
Set2ndSpot(img.Get2ndSpot());
SetDots(img.GetDots());
memcpy(pixels, img.pixels, GetLength() * sizeof(RGBA));
}

void ImageBuffer::Set(Image& img)
{
if(img.data)
if(img.data->refcount == 1) {
size = img.GetSize();
kind = IMAGE_UNKNOWN;
hotspot = img.GetHotSpot();
spot2 = img.Get2ndSpot();
dots = img.GetDots();
pixels = img.data->buffer.pixels;
img.Clear();
}
else {
DeepCopy(img.data->buffer);
kind = IMAGE_UNKNOWN;
img.Clear();
}
else
Create(0, 0);
}


void ImageBuffer::operator=(Image& img)
{
Clear();
Set(img);
}

void ImageBuffer::operator=(ImageBuffer& img)
{
Clear();
Image m = img;
Set(m);
}

ImageBuffer::ImageBuffer(Image& img)
{
Set(img);
}

ImageBuffer::ImageBuffer(ImageBuffer& b)
{
kind = b.kind;
size = b.size;
dots = b.dots;
pixels = b.pixels;
hotspot = b.hotspot;
spot2 = b.spot2;
}

void ImageBuffer::SetDPI(Size dpi)
{
dots.cx = int(600.*size.cx/dpi.cx);
dots.cy = int(600.*size.cy/dpi.cy);
}

Size ImageBuffer::GetDPI()
{
return Size(dots.cx ? int(600.*size.cx/dots.cx) : 0, dots.cy ? int(600.*size.cy/dots.cy) : 0);
}

void (Image::Data::*Image::Data::sSysInit)();
void (Image::Data::*Image::Data::sSysRelease)();
int (Image::Data::*Image::Data::sGetResCount)() const;
void (Image::Data::*Image::Data::sPaint)(SystemDraw& w, int x, int y, const Rect& src, Color c);

void Image::Data::InitSystemImage(
void (Image::Data::*fSysInit)(),
void (Image::Data::*fSysRelease)(),
int (Image::Data::*fGetResCount)() const,
void (Image::Data::*fPaint)(SystemDraw& w, int x, int y, const Rect& src, Color c)
)
{
Image::Data::sSysInit = fSysInit;
Image::Data::sSysRelease = fSysRelease;
Image::Data::sGetResCount = fGetResCount;
Image::Data::sPaint = fPaint;
}

void Image::Data::SysInit()
{
if(sSysInit)
(this->*sSysInit)();
}

void Image::Data::SysRelease()
{
if(sSysRelease)
(this->*sSysRelease)();
}

int Image::Data::GetResCount() const
{
if(sGetResCount)
return (this->*sGetResCount)();
return 0;
}

void Image::Data::Paint(SystemDraw& w, int x, int y, const Rect& src, Color c)
{
if(sPaint)
(this->*sPaint)(w, x, y, src, c);
}

void Image::Set(ImageBuffer& b)
{
if(b.GetWidth() == 0 || b.GetHeight() == 0)
data = NULL;
else
data = new Data(b);
}

void Image::Clear()
{
if(data)
data->Release();
data = NULL;
}

Image& Image::operator=(ImageBuffer& img)
{
if(data)
data->Release();
Set(img);
return *this;
}

Image& Image::operator=(const Image& img)
{
Data *d = data;
data = img.data;
if(data)
data->Retain();
if(d)
d->Release();
return *this;
}

const RGBA* Image::operator~() const
{
return data ? ~data->buffer : NULL;
}

Image::operator const RGBA*() const
{
return data ? ~data->buffer : NULL;
}

const RGBA* Image::operator[](int i) const
{
ASSERT(data);
return data->buffer[i];
}

Size Image::GetSize() const
{
return data ? data->buffer.GetSize() : Size(0, 0);
}

int Image::GetLength() const
{
return data ? data->buffer.GetLength() : 0;
}

Point Image::GetHotSpot() const
{
return data ? data->buffer.GetHotSpot() : Point(0, 0);
}

Point Image::Get2ndSpot() const
{
return data ? data->buffer.Get2ndSpot() : Point(0, 0);
}

Size Image::GetDots() const
{
return data ? data->buffer.GetDots() : Size(0, 0);
}

Size Image::GetDPI()
{
Size size = GetSize();
Size dots = GetDots();
return data ? Size(int(600.*size.cx/dots.cx), int(600.*size.cy/dots.cy)): Size(0, 0);
}

int Image::GetKindNoScan() const
{
return data ? data->buffer.GetKind() : IMAGE_EMPTY;
}

int Image::Data::GetKind()
{
int k = buffer.GetKind();
if(k != IMAGE_UNKNOWN)
return k;
k = buffer.ScanKind();
buffer.SetKind(k);
return k;
}

int Image::GetKind() const
{
return data ? data->GetKind() : IMAGE_EMPTY;
}

void Image::PaintImage(SystemDraw& w, int x, int y, const Rect& src, Color c) const
{
if(data)
data->Paint(w, x, y, src, c);
}

void Image::Serialize(Stream& s)
{
int version = 0;
s / version;
Size sz = GetSize();
Point p = GetHotSpot();
Size dots = GetDots();
s % sz % p % dots;
int len = sz.cx * sz.cy;
if(s.IsLoading())
if(len) {
ImageBuffer b(sz);
if(!s.GetAll(~b, len * sizeof(RGBA)))
s.SetError();
b.SetDots(dots);
b.SetHotSpot(p);
*this = b;
}
else
Clear();
else
s.Put(~*this, len * sizeof(RGBA));
}

INITBLOCK {
Value::Register<Image>("Image");
}

bool Image::operator==(const Image& img) const
{
if(GetLength() != img.GetLength())
return false;
return memcmp(~*this, ~img, GetLength() * sizeof(RGBA)) == 0;
}

bool Image::operator!=(const Image& img) const
{
return !operator==(img);
}

dword Image::GetHashValue() const
{
return memhash(~*this, GetLength() * sizeof(RGBA));
}

Image::Image(const Image& img)
{
data = img.data;
if(data)
data->Retain();
}

Image::Image(Image (*fn)())
{
data = NULL;
*this = (*fn)();
}

Image::Image(const Value& src)
{
data = NULL;
if(!IsNull(src))
*this = RawValue<Image>::Extract(src);
}

Image::Image(ImageBuffer& b)
{
Set(b);
}

Image::~Image()
{
if(data)
data->Release();
}

Image::Image(const Init& init)
{
ASSERT(init.info[0] >= 1);
Size sz;
sz.cx = Peek32le(init.info + 1);
sz.cy = Peek32le(init.info + 5);
ImageBuffer b(sz);
int i = 0;
while(i < init.scan_count) {
UnpackRLE(b[i], (const byte *)init.scans[i], sz.cx);
i++;
}
while(i < sz.cy)
memset(b[i++], 0, sizeof(RGBA) * sz.cx);
b.SetHotSpot(Point(Peek32le(init.info + 9), Peek32le(init.info + 13)));
Set(b);
}

String Image::ToString() const
{
return String("Image ").Cat() << GetSize();
}

Link<Image::Data> Image::Data::ResData[1];
int Image::Data::ResCount;

Image::Data::Data(ImageBuffer& b)
: buffer(b)
{
paintcount = 0;
paintonly = false;
refcount = 1;
INTERLOCKED {
static int64 gserial;
serial = ++gserial;
}
SysInit();
}

Image::Data::~Data()
{
DrawLock __;
SysRelease();
Unlink();
}

void Image::Data::PaintOnlyShrink()
{
if(paintonly) {
LTIMING("PaintOnlyShrink");
DrawLock __;
DropPixels___(buffer);
ResCount -= GetResCount();
Unlink();
}
}

static void sMultiply(ImageBuffer& b, int (*op)(RGBA *t, const RGBA *s, int len))
{
if(b.GetKind() != IMAGE_OPAQUE && b.GetKind() != IMAGE_EMPTY)
(*op)(~b, ~b, b.GetLength());
}

void Premultiply(ImageBuffer& b)
{
sMultiply(b, Premultiply);
}

void Unmultiply(ImageBuffer& b)
{
sMultiply(b, Unmultiply);
}

static Image sMultiply(const Image& img, int (*op)(RGBA *t, const RGBA *s, int len))
{
int k = img.GetKind();
if(k == IMAGE_OPAQUE || k == IMAGE_EMPTY)
return img;
ImageBuffer ib(img.GetSize());
ib.SetHotSpot(img.GetHotSpot());
ib.Set2ndSpot(img.Get2ndSpot());
ib.SetKind(Premultiply(~ib, ~img, ib.GetLength()));
return ib;
}

Image Premultiply(const Image& img)
{
return sMultiply(img, Premultiply);
}

Image Unmultiply(const Image& img)
{
return sMultiply(img, Unmultiply);
}

void SetPaintOnly___(Image& m)
{
if(m.data && m.data->refcount == 1)
m.data->paintonly = true;
}

void Iml::Init(int n)
{
for(int i = 0; i < n; i++)
map.Add(name[i]);
}

void Iml::Reset()
{
int n = map.GetCount();
map.Clear();
Init(n);
}

void Iml::Set(int i, const Image& img)
{
map[i].image = img;
map[i].loaded = true;
}

Image Iml::Get(int i)
{
IImage& m = map[i];
if(!m.loaded) {
DrawLock __;
if(data.GetCount()) {
int ii = 0;
for(;;) {
const Data& d = data[ii];
if(i < d.count) {
static const char *cached_data;
static Vector<Image> cached;
if(cached_data != d.data) {
cached_data = d.data;
cached = UnpackImlData(d.data, d.len);
if(premultiply)
for(int i = 0; i < cached.GetCount(); i++)
cached[i] = Premultiply(cached[i]);
}
m.image = cached[i];
break;
}
i -= d.count;
ii++;
}
}
else
m.image = Premultiply(Image(img_init[i]));
m.loaded = true;
}
return m.image;
}

#ifdef _DEBUG
int Iml::GetBinSize() const
{
int size = 0;
for(int i = 0; i < map.GetCount(); i++) {
const Image::Init& init = img_init[i];
size += (int)strlen(name[i]) + 1 + 24;
for(int q = 0; q < init.scan_count; q++)
size += (int)strlen(init.scans[q]);
}
return size;
}
#endif

Iml::Iml(const Image::Init *img_init, const char **name, int n)
: img_init(img_init),
name(name)
{
#ifdef flagCHECKINIT
RLOG("Constructing iml " << *name);
#endif
premultiply = true;
Init(n);
}

void Iml::AddData(const byte *_data, int len, int count)
{
Data& d = data.Add();
d.data = (const char *)_data;
d.len = len;
d.count = count;
data.Shrink();
}

static StaticCriticalSection sImgMapLock;

static VectorMap<String, Iml *>& sImgMap()
{
static VectorMap<String, Iml *> x;
return x;
}

void Register(const char *imageclass, Iml& list)
{
#ifdef flagCHECKINIT
RLOG("Registering iml " << imageclass);
#endif
INTERLOCKED_(sImgMapLock)
sImgMap().GetAdd(imageclass) = &list;
}

int GetImlCount()
{
int q;
INTERLOCKED_(sImgMapLock)
q = sImgMap().GetCount();
return q;
}

Iml& GetIml(int i)
{
return *sImgMap()[i];
}

String GetImlName(int i)
{
DrawLock __;
return sImgMap().GetKey(i);
}

int FindIml(const char *name)
{
DrawLock __;
return sImgMap().Find(name);
}

Image GetImlImage(const char *name)
{
Image m;
const char *w = strchr(name, ':');
if(w) {
DrawLock __;
int q = FindIml(String(name, w));
if(q >= 0) {
Iml& iml = *sImgMap()[q];
while(*w == ':')
w++;
q = iml.Find(w);
if(q >= 0)
m = iml.Get(q);
}
}
return m;
}

void SetImlImage(const char *name, const Image& m)
{
const char *w = strchr(name, ':');
if(w) {
DrawLock __;
int q = FindIml(String(name, w));
if(q >= 0) {
Iml& iml = *sImgMap()[q];
while(*w == ':')
w++;
q = iml.Find(w);
if(q >= 0)
iml.Set(q, m);
}
}
}

String StoreImageAsString(const Image& img)
{
if(img.GetKind() == IMAGE_EMPTY)
return Null;
int type = img.GetKind() == IMAGE_OPAQUE ? 3 : 4;
StringStream ss;
ss.Put(type);
Size sz = img.GetSize();
ss.Put16le(sz.cx);
ss.Put16le(sz.cy);
Point p = img.GetHotSpot();
ss.Put16le(p.x);
ss.Put16le(p.y);
Size dots = img.GetDots();
ss.Put16le(dots.cx);
ss.Put16le(dots.cy);
const RGBA *s = img;
const RGBA *e = s + img.GetLength();
Buffer<byte> b(type * img.GetLength());
byte *t = b;
if(type == 3)
while(s < e) {
*t++ = s->r;
*t++ = s->g;
*t++ = s->b;
s++;
}
else
while(s < e) {
*t++ = s->r;
*t++ = s->g;
*t++ = s->b;
*t++ = s->a;
s++;
}
MemReadStream m(b, type * img.GetLength());
ZCompress(ss, m);
return ss;
}

Image LoadImageFromString(const String& src)
{
if(src.GetLength() < 13)
return Null;
StringStream ss(src);
int type = ss.Get();
Size sz;
sz.cx = ss.Get16le();
sz.cy = ss.Get16le();
if(sz.cx < 0 || sz.cy < 0)
return Null;
Point p;
p.x = ss.Get16le();
p.y = ss.Get16le();
if(p.x < 0 || p.y < 0)
return Null;
Size dots;
dots.cx = ss.Get16le();
dots.cy = ss.Get16le();
if(dots.cx < 0 || dots.cy < 0)
return Null;
StringStream out;
ZDecompress(out, ss);
String data = out;
if(data.GetLength() != type * sz.cx * sz.cy)
return Image();
ImageBuffer ib(sz);
ib.SetHotSpot(p);
ib.SetDots(dots);
RGBA *t = ib;
const RGBA *e = t + ib.GetLength();
const byte *s = data;
if(type == 3)
while(t < e) {
t->r = *s++;
t->g = *s++;
t->b = *s++;
t->a = 255;
t++;
}
else
if(type == 4)
while(t < e) {
t->r = *s++;
t->g = *s++;
t->b = *s++;
t->a = *s++;
t++;
}
else
return Image();
return ib;
}

Size GetImageStringSize(const String& src)
{
if(src.GetLength() < 13)
return Size(0, 0);
StringStream ss(src);
ss.Get();
Size sz;
sz.cx = ss.Get16le();
sz.cy = ss.Get16le();
return sz;
}

Size GetImageStringDots(const String& src)
{
if(src.GetLength() < 13)
return Size(0, 0);
StringStream ss(src);
ss.SeekCur(9);
Size sz;
sz.cx = ss.Get16le();
sz.cy = ss.Get16le();
return sz;
}

END_UPP_NAMESPACE

Change log

r4950 by cxl on May 14, 2012   Diff
Draw: Font, Drawing, Painting, Image now
support Xmlize and Jsonize
Go to: 
Project members, sign in to write a code review

Older revisions

r4453 by cxl on Jan 21, 2012   Diff
Fixed some warning
r2932 by cxl on Dec 26, 2010   Diff
Draw: ImageOps hotspots, CtrlLib:
Splitter Zoom fix
r2698 by cxl on Sep 16, 2010   Diff
Vector, Array: All Adds now returning
T&
All revisions of this file

File info

Size: 13549 bytes, 735 lines
Powered by Google Project Hosting