-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPICTResource.cpp
1208 lines (1045 loc) · 24.1 KB
/
PICTResource.cpp
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
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* PICTResource.cpp
Copyright (C) 2008 by Gregory Smith
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.
This license is contained in the file "COPYING", which is included
with this source code; it is available online at
http://www.gnu.org/licenses/gpl.html
*/
#include "ferro/AStream.h"
#include "PICTResource.h"
#include <bitset>
#include <fstream>
#include <sstream>
#include <boost/algorithm/string/predicate.hpp>
#include <string.h>
using namespace atque;
namespace algo = boost::algorithm;
/*
void PICTResource::Load(const std::vector<uint8>& data)
{
data_.clear();
AIStreamBE stream(&data[0], data.size());
int16 size;
stream >> size;
stream >> top_;
stream >> left_;
stream >> bottom_;
stream >> right_;
data_.resize(data.size() - 10);
stream.read(&data_[0], data_.size());
}*/
void PICTResource::Load(const std::vector<uint8>& data)
{
bitmap_.SetSize(1, 1);
data_.clear();
jpeg_.clear();
AIStreamBE stream(&data[0], data.size());
int16 size;
stream >> size;
Rect rect;
rect.Load(stream);
try
{
bool done = false;
while (!done)
{
uint16 opcode;
stream >> opcode;
switch (opcode) {
case 0x0000: // NOP
case 0x0011: // VersionOp
case 0x001c: // HiliteMode
case 0x001e: // DefHilite
case 0x0038: // FrameSameRect
case 0x0039: // PaintSameRect
case 0x003a: // EraseSameRect
case 0x003b: // InvertSameRect
case 0x003c: // FillSameRect
case 0x02ff: // Version
break;
case 0x00ff: // OpEndPic
done = true;
break;
case 0x0001: { // Clipping region
uint16 size;
stream >> size;
if (size & 1)
size++;
stream.ignore(size - 2);
break;
}
case 0x0003: // TxFont
case 0x0004: // TxFace
case 0x0005: // TxMode
case 0x0008: // PnMode
case 0x000d: // TxSize
case 0x0015: // PnLocHFrac
case 0x0016: // ChExtra
case 0x0023: // ShortLineFrom
case 0x00a0: // ShortComment
stream.ignore(2);
break;
case 0x0006: // SpExtra
case 0x0007: // PnSize
case 0x000b: // OvSize
case 0x000c: // Origin
case 0x000e: // FgColor
case 0x000f: // BgColor
case 0x0021: // LineFrom
stream.ignore(4);
break;
case 0x001a: // RGBFgCol
case 0x001b: // RGBBkCol
case 0x001d: // HiliteColor
case 0x001f: // OpColor
case 0x0022: // ShortLine
stream.ignore(6);
break;
case 0x0002: // BkPat
case 0x0009: // PnPat
case 0x000a: // FillPat
case 0x0010: // TxRatio
case 0x0020: // Line
case 0x0030: // FrameRect
case 0x0031: // PaintRect
case 0x0032: // EraseRect
case 0x0033: // InvertRect
case 0x0034: // FillRect
stream.ignore(8);
break;
case 0x0c00: { // HeaderOp
HeaderOp headerOp;
headerOp.Load(stream);
break;
}
case 0x00a1: { // LongComment
stream.ignore(2);
int16 size;
stream >> size;
if (size & 1)
size++;
stream.ignore(size);
break;
}
case 0x0098: // Packed CopyBits
case 0x0099: // Packed CopyBits with clipping region
case 0x009a: // Direct CopyBits
case 0x009b: { // Direct CopyBits with clipping region
bool packed = (opcode == 0x0098 || opcode == 0x0099);
bool clipped = (opcode == 0x0099 || opcode == 0x009b);
LoadCopyBits(stream, packed, clipped);
if (jpeg_.size())
{
bitmap_.SetSize(1, 1);
}
else if (bitmap_.TellWidth() != rect.width() && bitmap_.TellWidth() == 614)
{
throw ParseError("PICT appears to use Cinemascope hack");
}
break;
}
case 0x8200: { // Compressed QuickTime image (we only handle JPEG compression)
if (jpeg_.size())
{
throw ParseError("PICT contains banded JPEG");
}
LoadJPEG(stream);
break;
}
default:
if (opcode >= 0x0300 && opcode < 0x8000)
stream.ignore((opcode >> 8) * 2);
else if (opcode >= 0x8000 && opcode < 0x8100)
break;
else
{
std::ostringstream s;
s << "Unimplemented opcode " << std::hex << opcode;
throw ParseError(s.str());
}
break;
}
}
}
catch (const ParseError& e)
{
bitmap_.SetSize(1, 1);
jpeg_.clear();
data_ = data;
why_unparsed_ = e.what();
}
catch (const AStream::failure& e)
{
bitmap_.SetSize(1, 1);
jpeg_.clear();
data_ = data;
why_unparsed_ = "Error parsing PICT";
}
}
template <class T>
static std::vector<T> UnpackRow(AIStreamBE& stream, int row_bytes)
{
std::vector<T> result;
int row_length;
if (row_bytes > 250)
{
uint16 length;
stream >> length;
row_length = length;
}
else
{
uint8 length;
stream >> length;
row_length = length;
}
uint32 end = stream.tellg() + row_length;
while (stream.tellg() < end)
{
int8 c;
stream >> c;
if (c < 0)
{
int size = -c + 1;
T data;
stream >> data;
for (int i = 0; i < size; ++i)
{
result.push_back(data);
}
}
else if (c != -128)
{
int size = c + 1;
T data;
for (int i = 0; i < size; ++i)
{
stream >> data;
result.push_back(data);
}
}
}
return result;
}
static std::vector<uint8> ExpandPixels(const std::vector<uint8>& scan_line, int depth)
{
std::vector<uint8> result;
for (std::vector<uint8>::const_iterator it = scan_line.begin(); it != scan_line.end(); ++it)
{
if (depth == 4)
{
result.push_back((*it) >> 4);
result.push_back((*it) & 0xf);
}
else if (depth == 2)
{
result.push_back((*it) >> 6);
result.push_back(((*it) >> 4) & 0x3);
result.push_back(((*it) >> 2) & 0x3);
result.push_back((*it) & 0x3);
}
else if (depth == 1)
{
std::bitset<8> bits(*it);
for (int i = 0; i < 8; ++i)
{
result.push_back(bits[i] ? 1 : 0);
}
}
}
return result;
}
void PICTResource::LoadCopyBits(AIStreamBE& stream, bool packed, bool clipped)
{
if (!packed)
stream.ignore(4); // pmBaseAddr
uint16 row_bytes;
stream >> row_bytes;
bool is_pixmap = (row_bytes & 0x8000);
row_bytes &= 0x3fff;
Rect rect;
rect.Load(stream);
uint16 width = rect.width();
uint16 height = rect.height();
uint16 pack_type, pixel_size;
if (is_pixmap)
{
stream.ignore(2); // pmVersion
stream >> pack_type;
stream.ignore(14); // packSize/hRes/vRes/pixelType
stream >> pixel_size;
stream.ignore(16); // cmpCount/cmpSize/planeBytes/pmTable/pmReserved
}
else
{
pack_type = 0;
pixel_size = 1;
}
bitmap_.SetSize(width, height);
if (pixel_size <= 8)
bitmap_.SetBitDepth(8);
else
bitmap_.SetBitDepth(pixel_size);
// read the color table
if (is_pixmap && packed)
{
stream.ignore(4); // ctSeed
uint16 flags, num_colors;
stream >> flags
>> num_colors;
num_colors++;
for (int i = 0; i < num_colors; ++i)
{
uint16 index, red, green, blue;
stream >> index
>> red
>> green
>> blue;
if (flags & 0x8000)
index = i;
else
index &= 0xff;
RGBApixel pixel = { static_cast<ebmpBYTE>(blue >> 8), static_cast<ebmpBYTE>(green >> 8), static_cast<ebmpBYTE>(red >> 8), 0xff };
bitmap_.SetColor(index, pixel);
}
}
// src/dst/transfer mode
stream.ignore(18);
// clipping region
if (clipped)
{
uint16 size;
stream >> size;
stream.ignore(size - 2);
}
// the picture itself
if (pixel_size <= 8)
{
for (int y = 0; y < height; ++y)
{
std::vector<uint8> scan_line;
if (row_bytes < 8)
{
scan_line.resize(row_bytes);
stream.read(&scan_line[0], scan_line.size());
}
else
{
scan_line = UnpackRow<uint8>(stream, row_bytes);
}
if (pixel_size == 8)
{
for (int x = 0; x < width; ++x)
{
bitmap_.SetPixel(x, y, bitmap_.GetColor(scan_line[x]));
}
}
else
{
std::vector<uint8> pixels = ExpandPixels(scan_line, pixel_size);
for (int x = 0; x < width; ++x)
{
bitmap_.SetPixel(x, y, bitmap_.GetColor(pixels[x]));
}
}
}
}
else if (pixel_size == 16)
{
for (int y = 0; y < height; ++y)
{
std::vector<uint16> scan_line;
if (row_bytes < 8 || pack_type == 1)
{
for (int x = 0; x < width; ++x)
{
uint16 pixel;
stream >> pixel;
scan_line.push_back(pixel);
}
}
else if (pack_type == 0 || pack_type == 3)
{
scan_line = UnpackRow<uint16>(stream, row_bytes);
}
for (int x = 0; x < width; ++x)
{
RGBApixel pixel;
pixel.Red = (scan_line[x] >> 10) & 0x1f;
pixel.Green = (scan_line[x] >> 5) & 0x1f;
pixel.Blue = scan_line[x] & 0x1f;
pixel.Red = (pixel.Red * 255 + 16) / 31;
pixel.Green = (pixel.Green * 255 + 16) / 31;
pixel.Blue = (pixel.Blue * 255 + 16) / 31;
pixel.Alpha = 0xff;
bitmap_.SetPixel(x, y, pixel);
}
}
}
else if (pixel_size == 32)
{
for (int y = 0; y < height; ++y)
{
std::vector<uint8> scan_line;
if (row_bytes < 8 || pack_type == 1)
{
scan_line.resize(width * 3);
for (int x = 0; x < width; ++x)
{
uint32 pixel;
stream >> pixel;
scan_line[x] = pixel >> 16;
scan_line[x + width] = pixel >> 8;
scan_line[x + width * 2] = pixel;
}
}
else if (pack_type == 0 || pack_type == 4)
{
scan_line = UnpackRow<uint8>(stream, row_bytes);
}
for (int x = 0; x < width; ++x)
{
RGBApixel pixel;
pixel.Red = scan_line[x];
pixel.Green = scan_line[x + width];
pixel.Blue = scan_line[x + width * 2];
pixel.Alpha = 0xff;
bitmap_.SetPixel(x, y, pixel);
}
}
}
if (stream.tellg() & 1)
stream.ignore(1);
}
void PICTResource::LoadJPEG(AIStreamBE& stream)
{
uint32 opcode_size;
stream >> opcode_size;
if (opcode_size & 1)
opcode_size++;
uint32 opcode_start = stream.tellg();
stream.ignore(26); // version/matrix (hom. part)
int16 offset_x, offset_y;
stream >> offset_x;
stream.ignore(2);
stream >> offset_y;
stream.ignore(2);
stream.ignore(4); // rest of matrix
if (offset_x != 0 || offset_y != 0)
{
throw ParseError("PICT contains banded JPEG");
}
uint32 matte_size;
stream >> matte_size;
stream.ignore(22); // matte rect/srcRect/accuracy
uint32 mask_size;
stream >> mask_size;
if (matte_size)
{
uint32 matte_id_size;
stream >> matte_id_size;
stream.ignore(matte_id_size - 4);
}
stream.ignore(matte_size);
stream.ignore(mask_size);
uint32 id_size, codec_type;
stream >> id_size
>> codec_type;
if (codec_type != FOUR_CHARS_TO_INT('j','p','e','g'))
{
throw ParseError("Unsupported QuickTime compression codec");
}
stream.ignore(36); // resvd1/resvd2/dataRefIndex/version/revisionLevel/vendor/temporalQuality/spatialQuality/width/height/hRes/vRes
uint32 data_size;
stream >> data_size;
stream.ignore(38); // frameCount/name/depth/clutID
jpeg_.resize(data_size);
stream.read(&jpeg_[0], jpeg_.size());
stream.ignore(opcode_start + opcode_size - stream.tellg());
}
template <class T>
static std::vector<uint8> PackRow(const std::vector<T>& scan_line)
{
std::vector<uint8> result(scan_line.size() * sizeof(T) * 2);
AOStreamBE stream(&result[0], result.size());
typename std::vector<T>::const_iterator run = scan_line.begin();
typename std::vector<T>::const_iterator start = scan_line.begin();
typename std::vector<T>::const_iterator end = scan_line.begin() + 1;
while (end != scan_line.end())
{
if (*end != *(end - 1))
{
run = end;
}
end++;
if (end - run == 3)
{
if (run > start)
{
uint8 block_length = run - start - 1;
stream << block_length;
while (start < run)
{
stream << *start++;
}
}
while (end != scan_line.end() && *end == *(end - 1) && end - run < 128)
{
++end;
}
uint8 run_length = 1 - (end - run);
stream << run_length;
stream << *run;
run = end;
start = end;
}
else if (end - start == 128)
{
uint8 block_length = end - start - 1;
stream << block_length;
while (start < end)
{
stream << *start++;
}
run = end;
}
}
if (end > start)
{
uint8 block_length = end - start - 1;
stream << block_length;
while (start < end)
{
stream << *start++;
}
}
result.resize(stream.tellp());
return result;
}
bool PICTResource::LoadRaw(const std::vector<uint8>& data, const std::vector<uint8>& clut)
{
AIStreamBE stream(&data[0], data.size());
Rect rect;
rect.Load(stream);
int height = rect.height();
int width = rect.width();
int16 depth;
stream >> depth;
if (depth != 8 && depth != 16)
return false;
bitmap_.SetBitDepth(depth);
bitmap_.SetSize(width, height);
if (depth == 8)
{
if (clut.size() != 6 + 256 * 6)
return false;
AIStreamBE clut_stream(&clut[0], clut.size());
clut_stream.ignore(6);
for (int i = 0; i < 256; ++i)
{
uint16 red, green, blue;
clut_stream >> red
>> green
>> blue;
RGBApixel color = { static_cast<ebmpBYTE>(blue >> 8), static_cast<ebmpBYTE>(green >> 8), static_cast<ebmpBYTE>(red >> 8), 0xff };
bitmap_.SetColor(i, color);
}
for (int y = 0; y < height; ++y)
{
for (int x = 0; x < width; ++x)
{
uint8 pixel;
stream >> pixel;
bitmap_.SetPixel(x, y, bitmap_.GetColor(pixel));
}
}
}
else
{
for (int y = 0; y < height; ++y)
{
for (int x = 0; x < width; ++x)
{
uint16 color;
stream >> color;
RGBApixel pixel;
pixel.Red = (color >> 10) & 0x1f;
pixel.Green = (color >> 5) & 0x1f;
pixel.Blue = color & 0x1f;
pixel.Red = (pixel.Red * 255 + 16) / 31;
pixel.Green = (pixel.Green * 255 + 16) / 31;
pixel.Blue = (pixel.Blue * 255 + 16) / 31;
pixel.Alpha = 0xff;
bitmap_.SetPixel(x, y, pixel);
}
}
}
return true;
}
std::vector<uint8> PICTResource::SaveBMP() const
{
std::vector<uint8> result;
int depth = const_cast<BMP&>(bitmap_).TellBitDepth();
int width = const_cast<BMP&>(bitmap_).TellWidth();
int height = const_cast<BMP&>(bitmap_).TellHeight();
if (depth < 8)
depth = 8;
else if (depth == 24)
depth = 32;
// size(2), rect(8), versionOp(2), version(2), headerOp(26), clip(12)
int output_length = 10 + 2 + 2 + HeaderOp::kSize + 12;
int row_bytes;
if (depth == 8)
{
row_bytes = width;
// opcode(2), pixmap(46), colorTable(8+256*8), srcRect/dstRect/mode(18)
output_length += 2 + PixMap::kSize + 8 + 256 * 8 + 18;
}
else
{
row_bytes = width * (depth == 16 ? 2 : 4);
// opcode(2), pmBaseAddr(4), pixmap(46), srcRect/dstRect/mode(18)
output_length += 2 + 4 + PixMap::kSize + 18;
}
// data is variable--allocate twice what we need
output_length += height * row_bytes * 2;
result.resize(output_length);
AOStreamBE ostream(&result[0], result.size());
int16 size = 0;
Rect clipRect(width, height);
ostream << size;
clipRect.Save(ostream);
int16 versionOp = 0x0011;
int16 version = 0x02ff;
ostream << versionOp
<< version;
HeaderOp headerOp;
headerOp.srcRect = clipRect;
headerOp.Save(ostream);
int16 clip = 0x0001;
int16 clipSize = 10;
ostream << clip
<< clipSize;
clipRect.Save(ostream);
if (depth == 8)
{
int16 PackBitsRectOp = 0x0098;
ostream << PackBitsRectOp;
}
else
{
int16 DirectBitsRectOp = 0x009a;
uint32 pmBaseAddr = 0x000000ff;
ostream << DirectBitsRectOp
<< pmBaseAddr;
}
PixMap pixMap(depth, row_bytes);
pixMap.bounds = clipRect;
pixMap.Save(ostream);
// color table
if (depth == 8)
{
int32 seed = 0;
int16 flags = 0;
int16 size = 255;
ostream << seed
<< flags
<< size;
for (int16 index = 0; index < 256; ++index)
{
RGBApixel pixel = const_cast<BMP&>(bitmap_).GetColor(index);
uint16 red = pixel.Red << 8;
uint16 green = pixel.Green << 8;
uint16 blue = pixel.Blue << 8;
ostream << index
<< red
<< green
<< blue;
}
}
// source
clipRect.Save(ostream);
// destination
clipRect.Save(ostream);
int16 transfer_mode = 0;
ostream << transfer_mode;
std::map<RGBApixel, int> color_map; // for faster saving of 8-bit images
if (depth == 8)
{
for (int i = 255; i >= 0; --i)
{
color_map[const_cast<BMP&>(bitmap_).GetColor(i)] = i;
}
}
for (int y = 0; y < height; ++y)
{
std::vector<uint8> scan_line;
if (depth == 8)
{
std::vector<uint8> pixels(width);
for (int x = 0; x < width; ++x)
{
pixels[x] = color_map[bitmap_.GetPixel(x, y)];
}
scan_line = PackRow(pixels);
}
else if (depth == 16)
{
std::vector<uint16> pixels(width);
for (int x = 0; x < width; ++x)
{
uint16 red = bitmap_.GetPixel(x, y).Red >> 3;
uint16 green = bitmap_.GetPixel(x, y).Green >> 3;
uint16 blue = bitmap_.GetPixel(x, y).Blue >> 3;
pixels[x] = (red << 10) | (green << 5) | blue;
}
scan_line = PackRow(pixels);
}
else
{
std::vector<uint8> pixels(width * 3);
for (int x = 0; x < width; ++x)
{
pixels[x] = bitmap_.GetPixel(x, y).Red;
pixels[x + width] = bitmap_.GetPixel(x, y).Green;
pixels[x + width * 2] = bitmap_.GetPixel(x, y).Blue;
}
scan_line = PackRow(pixels);
}
if (row_bytes > 250)
ostream << static_cast<uint16>(scan_line.size());
else
ostream << static_cast<uint8>(scan_line.size());
ostream.write(&scan_line[0], scan_line.size());
}
if (ostream.tellp() & 1)
ostream.ignore(1);
int16 endPic = 0x00ff;
ostream << endPic;
result.resize(ostream.tellp());
return result;
}
static bool ParseJPEGDimensions(const std::vector<uint8>& data, int16& width, int16& height)
{
try
{
AIStreamBE stream(&data[0], data.size());
uint16 magic;
stream >> magic;
if (magic != 0xffd8)
return false;
while (stream.tellg() < stream.maxg())
{
// eat until we find 0xff
uint8 c;
do
{
stream >> c;
}
while (c != 0xff);
// eat 0xffs until we find marker code
do
{
stream >> c;
}
while (c == 0xff);
switch (c)
{
case 0xd9: // end of image
case 0xda: // start of scan
return false;
break;
case 0xc0:
case 0xc1:
case 0xc2:
case 0xc3:
case 0xc5:
case 0xc6:
case 0xc7:
case 0xc8:
case 0xc9:
case 0xca:
case 0xcb:
case 0xcd:
case 0xce:
case 0xcf: {
// start of frame
uint16 length;
uint8 precision;
stream >> length
>> precision
>> height
>> width;
return true;
break;
}
default:
{
uint16 length;
stream >> length;
if (length < 2)
return false;
else
stream.ignore(length - 2);
break;
}
}
}
return false;
}
catch (const AStream::failure&)
{
return false;
}
}
std::vector<uint8> PICTResource::SaveJPEG() const
{
std::vector<uint8> result;
int16 width, height;
if (!ParseJPEGDimensions(jpeg_, width, height))
return result;
// size(2), rect(8), versionOp(2), version(2), headerOp(26), clip(12)
int output_length = 10 + 2 + 2 + HeaderOp::kSize + 12;
// PICT opcode
output_length += 76;
// image description
output_length += 86;
output_length += jpeg_.size();
// end opcode
output_length += 2;
if (output_length & 1)
output_length++;
result.resize(output_length);
AOStreamBE ostream(&result[0], result.size());
int16 size = 0;
Rect clipRect(width, height);
ostream << size;
clipRect.Save(ostream);
int16 versionOp = 0x0011;
int16 version = 0x02ff;
ostream << versionOp
<< version;
HeaderOp headerOp;
headerOp.srcRect = clipRect;
headerOp.Save(ostream);
int16 clip = 0x0001;
int16 clipSize = 10;
ostream << clip
<< clipSize;
clipRect.Save(ostream);
uint16 opcode = 0x8200;
uint32 opcode_size = 154 + jpeg_.size();
ostream << opcode
<< opcode_size;
ostream.ignore(2); // version
std::vector<int16> matrix(18);
matrix[0] = 1;
matrix[8] = 1;
matrix[16] = 0x4000;
for (int i = 0; i < matrix.size(); ++i)
{
ostream << matrix[i];
}
ostream.ignore(4); // matte size
ostream.ignore(8); // matte rect
uint16 transfer_mode = 0x0040;
ostream << transfer_mode;
clipRect.Save(ostream);
uint32 accuracy = 768;
ostream << accuracy;
ostream.ignore(4); // mask size
uint32 id_size = 86;