-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathPictureFile.java
More file actions
1166 lines (1097 loc) · 33.2 KB
/
PictureFile.java
File metadata and controls
1166 lines (1097 loc) · 33.2 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
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
import java.awt.AlphaComposite;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.awt.image.DataBuffer;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import javax.swing.JFileChooser;
public class PictureFile {
//-------------------------------
//PPMファイル保存
//-------------------------------
//保存→読み込みで画像が崩れるので使ってはいけない
public static void saveAsPpm(BufferedImage img, String path){
if(img.getType()==BufferedImage.TYPE_INT_ARGB){
//main
}
else if(img.getType()==BufferedImage.TYPE_INT_RGB){
//bg
}
else return;
if(path==null){
JFileChooser chooser = new JFileChooser(new File("./"));
chooser.setDialogTitle(PCARDFrame.pc.intl.getDialogText("Save File"));
chooser.setSelectedFile(new File("./.ppm"));
int ret = chooser.showSaveDialog(PCARDFrame.pc);
if(ret != JFileChooser.APPROVE_OPTION){
//保存しない
return;
}
path = chooser.getSelectedFile().getName();
}
File file = new File(path);
if(file.exists()&&!file.canWrite()){
//書き込めない
return;
}
try {
BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(file));
stream.write('P');
stream.write('6');
stream.write('\n');
String str;
str = Integer.toString(img.getWidth());
for(int i=0; i<str.length(); i++){
stream.write(str.charAt(i));
}
stream.write(' ');
str = Integer.toString(img.getHeight());
for(int i=0; i<str.length(); i++){
stream.write(str.charAt(i));
}
stream.write('\n');
stream.write('2');
stream.write('5');
stream.write('5');
stream.write('\n');
for(int y=0; y<img.getHeight(); y++){
for(int x=0; x<img.getWidth(); x++){
int c = img.getRaster().getDataBuffer().getElem(0,x+img.getWidth()*y);
int r = (c>>16)&0xFF;
int g = (c>>8)&0xFF;
int b = (c>>0)&0xFF;
stream.write(r);
stream.write(g);
stream.write(b);
}
}
if(img.getType()==BufferedImage.TYPE_INT_ARGB){
//アルファデータ
stream.write('\n');
stream.write('P');
stream.write('5');
stream.write('\n');
str = Integer.toString(img.getWidth());
for(int i=0; i<str.length(); i++){
stream.write(str.charAt(i));
}
stream.write(' ');
str = Integer.toString(img.getHeight());
for(int i=0; i<str.length(); i++){
stream.write(str.charAt(i));
}
stream.write('\n');
stream.write('2');
stream.write('5');
stream.write('5');
stream.write('\n');
for(int y=0; y<img.getHeight(); y++){
for(int x=0; x<img.getWidth(); x++){
int c = img.getRaster().getDataBuffer().getElem(0,x+img.getWidth()*y);
int a = (c>>24)&0xFF;
stream.write(a);
}
}
}
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//-------------------------------
//PBM/PPMファイル読み込み
//-------------------------------
public static BufferedImage loadPbm(String path){
File file = new File(path);
if(!file.exists() || file.isDirectory()) return null;
BufferedInputStream stream;
try {
stream = new BufferedInputStream(new FileInputStream(path));
} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
}
//サイズ取得
int ver = readPbmVer(stream);
if(ver==0) return null;
Dimension size = readPbmSize(stream);
if(size==null) return null;
int depth = 0;
if(ver>=5){
depth = readPpmDepth(stream);
}
byte[] b = null;
if(ver==4){
//PBM画像データ
b = new byte[size.width/8*size.height];
try {
for(int i=0; i<b.length; i++){
b[i]=(byte)stream.read();
}
stream.read();
} catch (IOException e) {
e.printStackTrace();
}
}else if(ver==6){
//PPM画像データ
b = new byte[size.width*3*size.height];
try {
for(int i=0; i<b.length; i++){
b[i]=(byte)stream.read();
}
stream.read();
} catch (IOException e) {
e.printStackTrace();
}
}
//マスクサイズ取得
int maskver = readPbmVer(stream);
Dimension masksize = null;
int maskdepth = 0;
if(maskver==4||maskver==5){
masksize = readPbmSize(stream);
if(maskver>=5){
maskdepth = readPpmDepth(stream);
}
}
//PBMマスク画像データ
byte[] msk = null;
if(maskver==4){
//PBM画像データ
msk = new byte[masksize.width/8*masksize.height];
try {
for(int i=0; i<msk.length; i++){
msk[i]=(byte)stream.read();
}
stream.read();
} catch (IOException e) {
e.printStackTrace();
}
}else if(maskver==5){
//PPM画像データ
msk = new byte[masksize.width*masksize.height];
try {
for(int i=0; i<msk.length; i++){
msk[i]=(byte)stream.read();
}
stream.read();
} catch (IOException e) {
e.printStackTrace();
}
}
BufferedImage img = null;
if(ver==4 /*&& maskver==4*/){
//BufferedImageに変換
img = new BufferedImage(size.width, size.height, BufferedImage.TYPE_INT_ARGB);
img.getGraphics().setColor(Color.WHITE);
img.getGraphics().fillRect(0, 0, size.width, size.height);
DataBuffer db = img.getRaster().getDataBuffer();
for(int v=0; v<size.height; v++){
for(int h=0; h<size.width; h++){
int pix=0x0, mpix=0xFF000000;
switch(h%8){
case 0: pix=(b[(v*size.width+h)/8]>>7)&0x01; break;
case 1: pix=(b[(v*size.width+h)/8]>>6)&0x01; break;
case 2: pix=(b[(v*size.width+h)/8]>>5)&0x01; break;
case 3: pix=(b[(v*size.width+h)/8]>>4)&0x01; break;
case 4: pix=(b[(v*size.width+h)/8]>>3)&0x01; break;
case 5: pix=(b[(v*size.width+h)/8]>>2)&0x01; break;
case 6: pix=(b[(v*size.width+h)/8]>>1)&0x01; break;
case 7: pix=(b[(v*size.width+h)/8] )&0x01; break;
}
if(pix==0) pix = 0x00FFFFFF;
if(msk!=null){
switch(h%8){
case 0: mpix=(msk[(v*masksize.width+h)/8]>>7)&0x01; break;
case 1: mpix=(msk[(v*masksize.width+h)/8]>>6)&0x01; break;
case 2: mpix=(msk[(v*masksize.width+h)/8]>>5)&0x01; break;
case 3: mpix=(msk[(v*masksize.width+h)/8]>>4)&0x01; break;
case 4: mpix=(msk[(v*masksize.width+h)/8]>>3)&0x01; break;
case 5: mpix=(msk[(v*masksize.width+h)/8]>>2)&0x01; break;
case 6: mpix=(msk[(v*masksize.width+h)/8]>>1)&0x01; break;
case 7: mpix=(msk[(v*masksize.width+h)/8] )&0x01; break;
}
if(mpix!=0) mpix = 0xFF000000;
}
db.setElem(v*size.width+h, mpix+pix);
}
}
}
else if(ver==6 && /*maskver==5 &&*/ depth==255 /*&& maskdepth==255*/){
//BufferedImageに変換
img = new BufferedImage(size.width, size.height, BufferedImage.TYPE_INT_ARGB);
img.getGraphics().setColor(Color.WHITE);
img.getGraphics().fillRect(0, 0, size.width, size.height);
DataBuffer db = img.getRaster().getDataBuffer();
for(int v=0; v<size.height; v++){
for(int h=0; h<size.width; h++){
int pix=0x0, mpix=0xFF000000;
pix=(b[v*size.width*3+h]<<16)+(b[v*size.width*3+h+1]<<8)+(b[v*size.width*3+h+2]);
if(msk!=null && maskdepth==255){
mpix=msk[v*masksize.width+h]<<24;
}
db.setElem(v*size.width+h, mpix+pix);
}
}
}
return img;
}
//PBMヘッダ読み込み
private static int readPbmVer(BufferedInputStream stream){
String tmpstr="";
int ver = 0;
try {
//バージョン
for(int i=0; i<2; i++){
tmpstr += (char)stream.read();
}
if(tmpstr.equals("P4")) ver = 4;
if(tmpstr.equals("P5")) ver = 5;
if(tmpstr.equals("P6")) ver = 6;
stream.read();
} catch (IOException e) {
e.printStackTrace();
}
return ver;
}
//PBMヘッダ読み込み
private static Dimension readPbmSize(BufferedInputStream stream){
String tmpstr="";
int width=0, height=0;
try {
//サイズ
tmpstr="";
while(true){
char c = (char)stream.read();
if(c==' ') break;
if(c<'0'||c>'9') return null;
tmpstr += c;
}
width = Integer.valueOf(tmpstr);
tmpstr="";
while(true){
char c = (char)stream.read();
if(c=='\n') break;
tmpstr += c;
}
height = Integer.valueOf(tmpstr);
} catch (IOException e) {
e.printStackTrace();
}
return new Dimension(width,height);
}
//PBMヘッダ読み込み
private static int readPpmDepth(BufferedInputStream stream){
String tmpstr="";
int depth = 0;
try {
//バージョン
for(int i=0; i<3; i++){
tmpstr += (char)stream.read();
}
if(tmpstr.equals("255")) depth = 255;
stream.read();
} catch (IOException e) {
e.printStackTrace();
}
return depth;
}
//-------------------------------
//PICTファイル読み込み
//-------------------------------
public static BufferedImage loadPICT(String path){
File file = new File(path);
if(!file.exists()) return null;
BufferedInputStream stream;
try {
stream = new BufferedInputStream(new FileInputStream(path));
} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
}
//サイズ取得
Dimension size = readPICTv1Size(stream);
int version = readPICTVer(stream);
if(version==2) size = readPICTv2Size(stream,version);
if(size==null) return null;
if(size.width<=0 || size.height<=0){
System.out.println("PICT size error");
return null;
}
//各データ
BufferedImage img = null;
BufferedImage jpegimg = null;
int jpgHeight = 0;
Graphics2D g = null;
int px = 0;
int py = 10;
int fontsize = 12;
System.out.println("-------");
while(true){
int opcode;
try {
opcode = readOpcode2(stream,version);
} catch (IOException e1) {
break;
}
if(opcode==0x0000){ //アライメントずれの暫定措置
opcode = readOpcode(stream,1);
}
boolean bitmap_flag=false;
boolean packbits_flag=false;
boolean fullcolor_flag=false;
boolean rgnmask_flag=false;
System.out.println("opcode:0x"+ Integer.toHexString( opcode ));
if(opcode==0x8200||opcode==0x8201){
//JPEG
int filelen = (readOpcode(stream,2)<<16)+readOpcode(stream,2);//00 00 53 60
for(int i=0; i<100; i++){ //macファイルヘッダ
readOpcode(stream,1);
}
/*int width2 =*/ readOpcode(stream,2);
/*int height2 =*/ readOpcode(stream,2);
for(int i=0; i<50; i++){ //macファイルヘッダ2
readOpcode(stream,1);
}
byte[] b = new byte[filelen-154];
try {
stream.read(b, 0, filelen-154);
} catch (IOException e1) {
e1.printStackTrace();
}
try {
jpegimg = javax.imageio.ImageIO.read(new ByteArrayInputStream(b));
jpgHeight+=jpegimg.getHeight();
} catch (Exception e) {
e.printStackTrace();
}
continue;
}
else if(opcode==0x0090||opcode==0x0091){
bitmap_flag = true;
}
else if(opcode==0x0098||opcode==0x0099){
packbits_flag = true;
}
else if(opcode==0x009A||opcode==0x009B){
packbits_flag = true;
fullcolor_flag = true;
}
else if(opcode==0x00a1){
//ロングコメント
opcode = readOpcode(stream,2);
int length = readOpcode(stream,2);
byte[] b = new byte[length];
for(int i=0;i<length;i++){b[i] = (byte)readOpcode(stream,1);}
String s="";
try {
s = new String(b, "US-ASCII");
} catch (UnsupportedEncodingException e) {
}
System.out.println("long comment:"+s);
continue;
}
else if(opcode==0x00ff||opcode==0xffff){
break; //終了コード
}
else if(opcode==0x001e){
continue; //デフォルトのハイライトカラー使用、らしい
}
else if(opcode==0x0001){
//領域
int length = readOpcode(stream,2);
for(int i=0;i<length-2;i++){readOpcode(stream,1);}
continue;
}
else if(opcode==0x001f){
//OpColor
readOpcode(stream,2);//r
readOpcode(stream,2);//g
readOpcode(stream,2);//b
continue;
}
else if(opcode==0x001e){
//defHilite
continue;
}
else if(opcode==0x00a0){
//ショートコメント
readOpcode(stream,2);
continue;
}
else if(opcode==0x0009){
//ペンパターン
int[]penptn = new int[8];
for(int i=0;i<8;i++){penptn[i] = readOpcode(stream,1);}
continue;
}
else if(opcode==0x0022){
//ペン位置
int penX = (short)readOpcode(stream,2);
int penY = (short)readOpcode(stream,2);
byte penX2 = (byte)readOpcode(stream,1);
byte penY2 = (byte)readOpcode(stream,1);
System.out.println("penX:"+penX+" penY:"+penY+" penX2:"+penX2+" penY2:"+penY2);
if(img==null) {
img = new BufferedImage(size.width, size.height, BufferedImage.TYPE_INT_ARGB);
g = img.createGraphics();
}
g.drawLine(penX, penY, penX+penX2, penY+penY2);
px = penX+penX2;
py = penY+penY2;
continue;
}
else if(opcode==0x0007){
//ペンサイズ
int penSizeX = readOpcode(stream,2);
int penSizeY = readOpcode(stream,2);
g.setStroke(new BasicStroke((penSizeX+penSizeY)/2, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
continue;
}
else if(opcode==0x001a){
//RGBcolor|前景色(RGB)
int cr = readOpcode(stream,2);
int cg = readOpcode(stream,2);
int cb = readOpcode(stream,2);
g.setColor(new Color(cr/256,cg/256,cb/256));
continue;
}
else if(opcode==0x001b){
//RGBcolor|背景色(RGB)
int cr = readOpcode(stream,2);
int cg = readOpcode(stream,2);
int cb = readOpcode(stream,2);
g.setBackground(new Color(cr/256,cg/256,cb/256));
continue;
}
else if(opcode==0x002c){
//フォント (2+データ長)
int length = readOpcode(stream,2);
byte[] b = new byte[length];
for(int i=0;i<length;i++){b[i] = (byte)readOpcode(stream,1);}
String s="";
try {
s = new String(b, "SJIS");
} catch (UnsupportedEncodingException e) {
}
System.out.println("font:'"+s+"' at "+px+","+py);
//g.drawString(s,px,py);
continue;
}
else if(opcode==0x0003){
//書体ID
readOpcode(stream,2);
continue;
}
else if(opcode==0x0004){
//文字形状
readOpcode(stream,2);
continue;
}
else if(opcode==0x000d){
//文字サイズ
fontsize = readOpcode(stream,2);
g.setFont(new Font("", 0, fontsize));
continue;
}
else if(opcode==0x002e){
//? 文字に関する何か
int length = readOpcode(stream,2);
for(int i=0;i<length;i++){readOpcode(stream,1);}
px=0;py=0;//###
continue;
}
else if(opcode==0x0028){
//文字列描画
int ddx = readOpcode(stream,2);
int ddy = readOpcode(stream,2);
int count = readOpcode(stream,1);
byte[] b = new byte[count];
for(int i=0;i<count;i++){b[i] = (byte)readOpcode(stream,1);}
String s="";
try {
s = new String(b, "SJIS");
} catch (UnsupportedEncodingException e) {
}
System.out.println("drawString28:'"+s+"' at "+(ddx)+","+(ddy));
do{
String s2 = s;
if((s.indexOf('\r')>-1)) s2 = s.substring(0,s.indexOf('\r'));
g.drawString(s2,ddx,ddy+fontsize);
s = s.substring(s.indexOf('\r')+1);
ddy = ddy+fontsize;
}while((s.indexOf('\r')>-1));
continue;
}
else if(opcode==0x0029){
//文字列描画(水平相対座標)
int dx = readOpcode(stream,1);
int count = readOpcode(stream,1);
byte[] b = new byte[count];
for(int i=0;i<count;i++){b[i] = (byte)readOpcode(stream,1);}
String s="";
try {
s = new String(b, "SJIS");
} catch (UnsupportedEncodingException e) {
}
System.out.println("drawString29:'"+s+"' at "+(px+dx)+","+(py));
do{
String s2 = s;
if((s.indexOf('\r')>-1)) s2 = s.substring(0,s.indexOf('\r'));
g.drawString(s2,px+dx,py+fontsize);
s = s.substring(s.indexOf('\r')+1);
py = py+fontsize;
}while((s.indexOf('\r')>-1));
continue;
}
else if(opcode==0x002a){
//文字列描画(垂直相対座標)
int dy = readOpcode(stream,1);
int count = readOpcode(stream,1);
byte[] b = new byte[count];
for(int i=0;i<count;i++){b[i] = (byte)readOpcode(stream,1);}
String s="";
try {
s = new String(b, "SJIS");
} catch (UnsupportedEncodingException e) {
}
System.out.println("drawString2a:'"+s+"' at "+(px)+","+(py+dy));
do{
String s2 = s;
if((s.indexOf('\r')>-1)) s2 = s.substring(0,s.indexOf('\r'));
g.drawString(s2,px,py+dy+fontsize);
s = s.substring(s.indexOf('\r')+1);
py = py+fontsize;
}while((s.indexOf('\r')>-1));
continue;
}
else if(opcode==0x002b){
//文字列描画(水平垂直相対座標)
int dx = readOpcode(stream,1);
int dy = readOpcode(stream,1);
int count = readOpcode(stream,1);
byte[] b = new byte[count];
for(int i=0;i<count;i++){b[i] = (byte)readOpcode(stream,1);}
String s="";
try {
s = new String(b, "SJIS");
} catch (UnsupportedEncodingException e) {
}
System.out.println("drawString2b:'"+s+"' at "+(px+dx)+","+(py+dy));
do{
String s2 = s;
if((s.indexOf('\r')>-1)) s2 = s.substring(0,s.indexOf('\r'));
g.drawString(s2,px+dx,py+dy+fontsize);
s = s.substring(s.indexOf('\r')+1);
py = py+fontsize;
}while((s.indexOf('\r')>-1));
continue;
}
else{
//不明
continue;
}
if(opcode==0x0091||opcode==0x0099||opcode==0x009B){
rgnmask_flag = true;
}
if(img==null) {
img = new BufferedImage(size.width, size.height, BufferedImage.TYPE_INT_ARGB);
g = img.createGraphics();
g.setColor(new Color(255,255,255));
g.setComposite(AlphaComposite.getInstance(AlphaComposite.CLEAR, 0.0f));
g.fillRect(0,0, size.width, size.height);
}
if(jpegimg!=null){
//Graphics2D g = img.createGraphics();
g.drawImage(jpegimg, 0,jpgHeight-jpegimg.getHeight(),null);
}
DataBuffer db = img.getRaster().getDataBuffer();
if(fullcolor_flag){
readOpcode(stream,2);//ベースアドレス
readOpcode(stream,2);
}
int rowBytes = 0x3fff & readOpcode(stream,2);
int btop = readOpcode(stream,2);//左上Y座標
int bleft = readOpcode(stream,2);
int bbottom = readOpcode(stream,2);
int bright = readOpcode(stream,2);
int bpp = 1;
int[] palette = null;
if(version==1 || jpegimg!=null){
palette = new int[]{0xFFFFFFFF, 0xFF000000};
}
if(!bitmap_flag&&version==2&&jpegimg==null){
readOpcode(stream,2);//バージョン
readOpcode(stream,2);//圧縮タイプ
readOpcode(stream,2);//圧縮サイズ
readOpcode(stream,2);//圧縮サイズ
readOpcode(stream,2);//水平解像度
readOpcode(stream,2);//水平解像度
readOpcode(stream,2);//垂直解像度
readOpcode(stream,2);//垂直解像度
readOpcode(stream,2);//ピクセルタイプ
bpp = readOpcode(stream,2);//1ピクセルあたりのビット数
/*int byteoff =*/ readOpcode(stream,2);//次のピクセルまでのバイトオフセット
/*int pixelbytes =*/ readOpcode(stream,2);//コンポーネントサイズ
readOpcode(stream,2);//次のカラープレーンまでのオフセット
readOpcode(stream,2);//次のカラープレーンまでのオフセット
readOpcode(stream,2);//反転
readOpcode(stream,2);//反転
readOpcode(stream,2);//カラーテーブル識別番号
readOpcode(stream,2);//カラーテーブル識別番号
if(!fullcolor_flag){
readOpcode(stream,2);//カラーテーブルID
readOpcode(stream,2);//カラーテーブルID
readOpcode(stream,2);//カラーテーブルフラグ
int palette_cnt = 1+readOpcode(stream,2);//登録されているパレット数
//if(palette_cnt > 256) return null;
palette = new int[palette_cnt];
for(int i=0; i<palette_cnt; i++){
/*int pidx =*/ readOpcode(stream,2);//パレット番号
int cr = readOpcode(stream,2)>>8;//パレット色データR
int cg = readOpcode(stream,2)>>8;//パレット色データG
int cb = readOpcode(stream,2)>>8;//パレット色データB
palette[i] = 0xFF000000+(cr<<16)+(cg<<8)+cb;
}
}
}
if(rowBytes==0) rowBytes = (bright-bleft)*bpp/8;
if(rowBytes<8) packbits_flag = false;
int dtop = readOpcode(stream,2);//元解像度での左上Y座標
int dleft = readOpcode(stream,2);
int dbottom = readOpcode(stream,2);
int dright = readOpcode(stream,2);
if(dright>size.width || dbottom>size.height){ //無理矢理対応・・・
dright -= dleft;
dleft = 0;
dbottom -= dtop;
dtop = 0;
if(dright>size.width || dbottom>size.height){
break; //無理
}
}
readOpcode(stream,2);//72dpiでの左上Y座標
readOpcode(stream,2);
readOpcode(stream,2);
readOpcode(stream,2);
int trans_mode = readOpcode(stream,2);//転送モード
if(trans_mode!=0){
//System.out.println("trans_mode:"+trans_mode);
}
if(rgnmask_flag){
//System.out.println("rgnmask_flag:"+rgnmask_flag);
int len = readOpcode(stream,2);
int rtop = readOpcode(stream,2);//top
int rleft = readOpcode(stream,2);//left
readOpcode(stream,2);//bottom
readOpcode(stream,2);//right
if(len==10){
for(int y=0; y<size.height; y++){
for(int x=0; x<size.width; x++){
db.setElem(y*size.width+x, 0xFFFFFFFF);
}
}
}
//for(int i=0; i<len-10;i++){readOpcode(stream,1);}
//リージョンフォーマット
//
//領域の輪郭の線のデータが入っている
//上から見て行って、輪郭に含まれたら1、もう一度含まれたら0
//ということをやればビットマップデータに出来る。
//
//最初のwordで上からの行数を示す (飛ばした行は上の行と同じ)
//(繰り返し){
// 次のwordで輪郭部分の開始位置
// 次のwordで輪郭部分の終了位置
//}
//32767でライン終了
int scanline = 0;
for(int i=0; i<len-10;){
int lastscanline = scanline;
scanline = readOpcode(stream,2);
i+=2;
for(int yy=lastscanline+1; yy<scanline; yy++){
if(yy-rtop<size.height&&yy-rtop>=0){
for(int xx=0; xx<size.width; xx++){
db.setElem((yy-rtop)*size.width+xx, db.getElem((yy-rtop-1)*size.width+xx));
}
}
}
int x=rleft;
while(i<len-10){
int xstart = readOpcode(stream,2);
i+=2;
if(xstart==32767) {
if(scanline-rtop<size.height && scanline-rtop>0){
for(; x-rleft<size.width; x++){
db.setElem((scanline-rtop)*size.width+(x-rleft), db.getElem((scanline-rtop-1)*size.width+(x-rleft)));
}
}
break;
}
int xend = readOpcode(stream,2);
i+=2;
if(scanline-rtop<size.height&&scanline-rtop>=0){
for(; x<xstart; x++){
if(scanline-rtop>0&&x>0&&x-rleft<size.width){
db.setElem((scanline-rtop)*size.width+(x-rleft), db.getElem((scanline-rtop-1)*size.width+(x-rleft)));
}
}
}
if(scanline-rtop<size.height&&scanline-rtop>=0){
for(; x<xend; x++){
if(scanline-rtop==0){
db.setElem((scanline-rtop)*size.width+(x-rleft), 0xFFFFFFFF);
}else{
if(db.getElem((scanline-rtop-1)*size.width+(x-rleft))==0x00000000){
db.setElem((scanline-rtop)*size.width+(x-rleft), 0xFFFFFFFF);
}
}
}
}
}
}
}
if(bitmap_flag&&!packbits_flag){
//無圧縮BitMap
for(int v=0; v<bbottom-btop; v++){
byte[] data = new byte[rowBytes];
for(int i=0; i<rowBytes; i++){
try { data[i] = (byte)stream.read(); }
catch (IOException e) {}
}
for(int h=0; h<bright-bleft; h++){
int pix = (data[h/8]>>(7-(h%8)))&0x01;
if(pix!=0) {
if(trans_mode==1) pix=0xFF000000;//continue; //#srcOr
else pix=0xFFFFFFFF;
}
else if( pix==0 && trans_mode==3 ){ //#srcBic
pix=0xFFFFFFFF;
}
else if(pix==0 && trans_mode==1){
pix=0xFFFFFFFF;
}
if(v-btop>=size.height||h-bleft>=size.width||v-btop<0||h-bleft<0)continue;
if(rgnmask_flag){
if(db.getElem((v-btop)*size.width+(h-bleft))==0x00000000){
continue;
}
}
db.setElem(((v-btop)/*+btop*/)*size.width+((h-bleft)/*+bleft*/), pix);
}
}
}
else if(bitmap_flag&&packbits_flag){
//圧縮BitMap
int dlen = 1;
if(rowBytes>=251) dlen=2;
for(int v=0; v<bbottom-btop; v++){
if(v+dtop>=size.height) break;
byte[] data = new byte[rowBytes];
int offset = 0;
//packBitsを展開
int packsize = readOpcode(stream,dlen);
for(int i=0; i<packsize; i++){
int dsize = readOpcode(stream,1);
if(dsize>=128) {
//同じデータが連続する場合
dsize = 256-dsize+1;
int src = readOpcode(stream,1);
for(int j=0; j<dsize; j++){
data[j+offset] = (byte)src;
}
offset += dsize;
}
else {
//データそのまま
dsize++;
for(int j=0; j<dsize; j++){
try { data[j+offset] = (byte)stream.read(); }
catch (IOException e) {}
}
offset += dsize;
}
}
for(int h=0; h<bright-bleft; h++){
int pix = (data[h/8]>>(7-(h%8)))&0x01;
if(pix!=0){
if(trans_mode==1) continue; //#srcOr
else pix=0xFFFFFFFF;
}
else if( pix==0 && trans_mode==3 ){ //#srcBic
pix=0xFFFFFFFF;
}
if(rgnmask_flag){
if(db.getElem(v*size.width+h)==0x00000000){
continue;
}
}
db.setElem((v+dtop)*size.width+(h+dleft), pix);
}
}
}
else if(!bitmap_flag&&packbits_flag){
System.out.println("packbits-pixmap");
System.out.println("bpp:"+bpp);
System.out.println("ispalette:"+(palette!=null));
System.out.println("rowBytes:"+rowBytes);
//圧縮PixMap
int dlen = 1;
if(rowBytes>=251) dlen=2;
for(int v=0; v<bbottom-btop; v++){
//System.out.println(v+"<"+(bbottom-btop));
byte[] data = new byte[rowBytes];
int offset = 0;
//packBitsを展開
int packsize = readOpcode(stream,dlen);
//System.out.println("packsize:"+packsize);
if(bpp==16){ //16bitのpackbitsは違うらしい
for(int i=0; i<packsize; i++){
int dsize = readOpcode(stream,1);
if(dsize>=128) {
//同じデータが連続する場合
//System.out.println("renzoku dsize:"+dsize);
dsize = 256-dsize+1;
int src1 = readOpcode(stream,1);
int src2 = readOpcode(stream,1);
i+=2;
for(int j=0; j<dsize*2 && j+offset<data.length; j++){
data[j+offset] = (byte)src1;
j++;
data[j+offset] = (byte)src2;
}
offset += dsize*2;
}
else {
//データそのまま
//System.out.println("sonomama dsize:"+dsize);
dsize++;
for(int j=0; j<dsize*2; j++){
if(rowBytes<=j+offset){
//System.out.println("over rowBytes2!");
continue;
}
try { data[j+offset] = (byte)stream.read();i++; }
catch (IOException e) {}
//System.out.println("data["+(j+offset)+"]:"+data[j+offset]);
}
offset += dsize*2;
}
}
}
else{ //16bit以外のpackbits
for(int i=0; i<packsize; i++){
int dsize = readOpcode(stream,1);
if(dsize>=128) {
//System.out.println("renzoku dsize:"+dsize);
//同じデータが連続する場合
dsize = 256-dsize+1;
int src = readOpcode(stream,1);
//System.out.println("src:"+src);
i++;
if(rowBytes<dsize+offset){
//System.out.println("over rowBytes1!");
continue;
}
for(int j=0; j<dsize && j+offset<data.length; j++){ data[j+offset] = (byte)src; }
offset += dsize;
}
else {
//データそのまま
//System.out.println("sonomama dsize:"+dsize);
dsize++;
for(int j=0; j<dsize; j++){
if(rowBytes<=j+offset){
//System.out.println("over rowBytes2!");
continue;
}
try { data[j+offset] = (byte)stream.read();i++; }
catch (IOException e) {}
//System.out.println("data["+(j+offset)+"]:"+data[j+offset]);
}
offset += dsize;
}
}
} //packbits終了
if(v+dtop>=size.height) {
//System.out.println("v+dtop("+(v+dtop)+") > "+size.height);
break;
}
for(int h=0; h<bright-bleft; h++){
if(h+dleft>=size.width) break;
int pix = 0;
int idx = 0;
if(bpp==1){
idx = (data[h/8]>>(8-(h%8+1)))&0x01;
}
else if(bpp==2){
idx = (data[h/4]>>(8-(h%4*2+2)))&0x03;
}
else if(bpp==4){