JunkBox_Lib++ (for Windows) 1.10.1
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Gio.cpp
Go to the documentation of this file.
1
7#include "tools++.h"
8#include "Gio.h"
9#include "JpegTool.h"
10
11
12using namespace jbxl;
13
14
16// ファイル識別子と共通ヘッダによるI/Oサポート
17
31{
32 RasHead rhd;
33 CmnHead hd;
34 int i, linebyte, databyte;
35 uByte null[2], *buf;
36 sWord* wptr;
37
38 hd.kind = RAS_DATA;
39 hd.bsize = 32;
40 hd.buf = NULL;
41 hd.grptr = NULL;
42
43 fseek(fp, 0, 0);
44 size_t ret = fread(&rhd, hd.bsize, 1, fp);
45 ntoh_st(&rhd, 4);
46 if (ret==0) {
48 hd.kind = HEADER_NONE;
49 return hd;
50 }
51
52 hd.xsize = rhd.ras_width;
53 hd.ysize = rhd.ras_height;
54 hd.zsize = 1;
55 hd.depth = rhd.ras_depth;
56 hd.lsize = hd.xsize*hd.ysize*((hd.depth+7)/8);
57 hd.buf = (uByte*)malloc(hd.bsize);
58 if (hd.buf==NULL) {
60 hd.kind = HEADER_NONE;
61 return hd;
62 }
63 memcpy(hd.buf, (uByte*)&rhd, hd.bsize);
64
65 hd.grptr = (uByte*)malloc(hd.lsize);
66 if (hd.grptr==NULL) {
67 freeNull(hd.buf);
69 hd.kind = HEADER_NONE;
70 return hd;
71 }
72 memset(hd.grptr, 0, hd.lsize);
73 buf = (uByte*)hd.grptr;
74
75 fseek(fp, rhd.ras_maplength, 1);
76 databyte = hd.xsize*((hd.depth+7)/8);
77 linebyte = rhd.ras_length/hd.ysize;
78
79 if (databyte==linebyte) {
80 ret = fread(buf, hd.lsize, 1, fp);
81 if (ret==0) {
83 hd.kind = HEADER_NONE;
84 return hd;
85 }
86 }
87 else {
88 for (i=0; i<hd.ysize; i++) {
89 ret = fread(buf, databyte, 1, fp);
90 if (ret>0) ret = fread(null, linebyte - databyte, 1, fp);
91 if (ret==0) {
93 hd.kind = HEADER_NONE;
94 return hd;
95 }
96 buf += databyte;
97 }
98 }
99
100 if (hd.depth==16){
101 wptr = (sWord*)hd.grptr;
102 for(i=0; i<hd.xsize*hd.ysize; i++) wptr[i] = ntohs(wptr[i]);
103 }
104
105 return hd;
106}
107
108
121int jbxl::writeRasData(FILE* fp, CmnHead* hd, int obit)
122{
123 RasHead shd;
124 int i, j, k, l, linebyte, databyte, depth, lsize;
125 uByte null=0x00, *ptr, *buf;
126
127 obit = Xabs(obit);
128 if (hd->kind == RAS_DATA) {
129 unsigned int u;
130 memcpy((sByte*)&shd, hd->buf, hd->bsize);
131 ptr = (uByte*)malloc(hd->lsize);
132 if (ptr==NULL) return JBXL_GRAPH_MEMORY_ERROR;
133 for (u=0; u<hd->lsize; u++) ptr[u] = hd->grptr[u];
134 lsize = hd->lsize;
135 }
136 else { // CT -> RAS
137 if (hd->depth==16) {
138
139 if (obit==8) depth = 8;
140 else depth = 24;
141
142 lsize = hd->xsize*hd->ysize*depth/8;
143 buf = (uByte*)malloc(lsize);
144 if (buf==NULL) return JBXL_GRAPH_MEMORY_ERROR;
145 memset(buf, 0, lsize);
146
147 if (obit==8) {
148 int max = 255; // 8bit mode での最大値
149 uWord* wp = (uWord*)hd->grptr;
150 for (i=0; i<hd->xsize*hd->ysize; i++) {
151 max = Max(max, wp[i]);
152 }
153 for (i=0; i<hd->ysize*hd->xsize; i++) {
154 buf[i] = 255*wp[i]/max;
155 }
156 }
157 else {
158 k = l = 0;
159 for (i=0; i<hd->ysize*hd->xsize; i++) {
160 buf[k++] = hd->grptr[l++];
161 buf[k++] = hd->grptr[l++];
162 buf[k++] = null;
163 }
164 }
165 }
166 else {
167 depth = hd->depth;
168 lsize = hd->lsize;
169 buf = (uByte*)hd->grptr;
170 }
171
172 databyte = hd->xsize*depth/8;
173 linebyte = lsize/hd->ysize;
174 if (linebyte%2==1) {
175 linebyte++;
176 lsize = linebyte*hd->ysize;
177 }
178
179 shd.ras_magic = RAS_MAGIC;
180 shd.ras_width = hd->xsize;
181 shd.ras_height = hd->ysize;
182 shd.ras_depth = depth;
183 shd.ras_length = lsize;
184 shd.ras_type = RT_STANDARD;
185 shd.ras_maptype = RMT_NONE;
186 shd.ras_maplength= 0;
187
188 ptr = (uByte*)malloc(lsize);
189 if (ptr==NULL) {
190 if (hd->depth==16) free(buf);
191 return -2;
192 }
193 memset(ptr, 0, lsize);
194
195 k = l = 0;
196 for (i=0 ; i<hd->ysize; i++) {
197 for (j=0; j<databyte; j++) ptr[k++] = buf[l++];
198 for (j=0; j<linebyte-databyte; j++) ptr[k++] = null;
199 }
200
201 if (hd->depth==16) free(buf);
202 }
203
204 hton_st(&shd, 4);
205 fwrite(&shd, sizeof(RasHead), 1, fp);
206 fwrite(ptr, lsize, 1, fp);
207
208 free(ptr);
209 return sizeof(RasHead)+lsize;
210}
211
212
235CmnHead jbxl::readUserSetData(FILE* fp, CmnHead* chd, bool cnt)
236{
237 CmnHead hd;
238 init_CmnHead(&hd);
239 CVCounter* counter = NULL;
240
241 if (chd==NULL) {
243 return hd;
244 }
245 else if ((chd->kind&0x0ff)!=USERSET_DATA) {
247 return hd;
248 }
249 hd = *chd;
250
251 // ヘッダ読み込み
252 fseek(fp, 0, 0);
253 hd.buf = (uByte*)malloc(hd.bsize);
254 if (hd.buf==NULL) {
256 hd.kind = HEADER_NONE;
257 return hd;
258 }
259 memset(hd.buf, 0, hd.bsize);
260 size_t ret = fread((void*)hd.buf, hd.bsize, 1, fp);
261 if (ret==0) {
263 hd.kind = HEADER_NONE;
264 return hd;
265 }
266
267 // カウンタ
268 if (hd.zsize>=10 && cnt) {
269 counter = GetUsableGlobalCounter();
270 if (counter!=NULL) {
271 //counter->SetTitle("ユーザ指定ファイル読み込み中");
272 counter->SetMax((hd.zsize+1)/10);
273 counter->SetPos(1);
274 }
275 }
276
277 hd.grptr = (uByte*)malloc(hd.lsize);
278 if (hd.grptr==NULL) {
279 freeNull(hd.buf);
281 hd.kind = HEADER_NONE;
282 return hd;
283 }
284 memset(hd.grptr, 0, hd.lsize);
285
286 fseek(fp, hd.bsize, 0);
287
288 if (counter==NULL) {
289 ret = fread(hd.grptr, hd.lsize, 1, fp);
290 if (ret==0) {
292 hd.kind = HEADER_NONE;
293 return hd;
294 }
295 }
296 else {
297 int psize = hd.xsize*hd.ysize*((hd.depth+7)/8);
298 for (int i=0; i<hd.zsize; i++) {
299 ret = fread(hd.grptr+i*psize, psize, 1, fp);
300 if (ret==0) {
302 hd.kind = HEADER_NONE;
303 return hd;
304 }
305 if (i%10==0) {
306 counter->StepIt();
307 if (counter->isCanceled()) { // キャンセル
308 free_CmnHead(&hd);
310 hd.kind = HEADER_NONE;
311 return hd;
312 }
313 }
314 }
315 }
316
317 if (hd.depth==16) {
318 if (checkBit(chd->kind, HAS_LENDIAN) && is_little_endian()) {
319 // CPU とファイルが同じ Little Endianなので,何もしない.
320 }
321 else {
322 sWord* wptr = (sWord*)hd.grptr;
323 for (int i=0; i<hd.xsize*hd.ysize*hd.zsize; i++) wptr[i] = ntohs(wptr[i]);
324 }
325 }
326
327 if (counter!=NULL) counter->PutFill();
328 return hd;
329}
330
331
332
334// CTファイルサポート
335//
336
350CmnHead jbxl::readMoonFile(const char* fn, bool no_ntoh)
351{
352 CmnHead hd;
353 FILE* fp;
354 unsigned int fsz;
355
356 memset(&hd, 0, sizeof(CmnHead));
357 hd.kind = HEADER_NONE;
358
359 fsz = file_size(fn);
360 if (fsz==0) {
362 return hd;
363 }
364 if ((fp=fopen(fn,"rb"))==NULL) {
366 return hd;
367 }
368
369 hd = readMoonData(fp, fsz, no_ntoh);
370 fclose(fp);
371 return hd;
372}
373
374
388CmnHead jbxl::readMoonData(FILE* fp, unsigned int fsz, bool no_ntoh)
389{
390 int i;
391 size_t ret;
392 CTHead chd;
393 CmnHead hd;
394
395 hd.kind = HEADER_NONE;
396 hd.bsize = 64; // Moon形式のヘッダサイズ
397
398 // ヘッダ読み込み
399 fseek(fp, 0, 0);
400 ret = fread((void*)&chd, hd.bsize, 1, fp);
401 ntoh_st(&chd, 2);
402 if (ret==0) {
404 hd.kind = HEADER_NONE;
405 return hd;
406 }
407
408 hd.xsize = chd.xsize - chd.cutleft - chd.cutright;
409 hd.ysize = chd.ysize - chd.cutup - chd.cutdown;
410 hd.zsize = 1;
411 hd.depth = 16;
412 hd.lsize = hd.xsize*hd.ysize*(hd.depth/8);
413 hd.buf = NULL;
414 hd.grptr = NULL;
415 if (fsz>0 && fsz!=hd.bsize+hd.lsize) return hd;
416
417 hd.buf = (uByte*)malloc(hd.bsize);
418 if (hd.buf==NULL) {
420 return hd;
421 }
422 memcpy(hd.buf, &chd, hd.bsize);
423 hd.kind = MOON_DATA;
424
425 hd.grptr = (uByte*)malloc(hd.lsize);
426 if (hd.grptr==NULL) {
427 freeNull(hd.buf);
429 hd.kind = HEADER_NONE;
430 return hd;
431 }
432 memset(hd.grptr, 0, hd.lsize);
433
434 fseek(fp, hd.bsize, 0);
435 ret = fread(hd.grptr, hd.lsize, 1, fp);
436 if (ret==0) {
438 hd.kind = HEADER_NONE;
439 return hd;
440 }
441 if (!no_ntoh) {
442 sWord* wptr = (sWord*)hd.grptr;
443 for (i=0; i<hd.xsize*hd.ysize; i++) wptr[i] = ntohs(wptr[i]);
444 }
445 return hd;
446}
447
448
449
451// DICOM
452
471int jbxl::dicomHeader(FILE* fp, int fsize, int* dsize, int* xsize, int* ysize, int* depth, double* rzxy)
472{
473 int i, j, sz, rp, ef;
474 int ln, hsize;
475 int xs = 0, ys = 0, dp = 0;
476 double rz = 0.0;
477
478#ifdef WIN32
479 uWord cn[2];
480#else
481 uWord cn[2] __attribute__((may_alias));
482#endif
483 uWord dt;
484 uWord* wp;
485
486 if (fp==NULL || fsize<=0 || dsize==NULL) return JBXL_GRAPH_IVDARG_ERROR;
487
488 *dsize = 0;
489 if (rzxy!=NULL) *rzxy = 0.0;
490 ef = OFF;
491 fseek(fp, 0, 0);
492
494 // ヘッダの区切りを検出
495 rp = (int)fread(&dt, sizeof(uWord), 1, fp);
496 while(!feof(fp) && ef!=ON) {
497 //if ((uWord)ntohs(dt)==0xe07f) {
498 if (dt==DICOM_PIXEL_GROUP) {
499 rp += (int)fread(&dt, sizeof(uWord), 1, fp);
500 //if ((uWord)ntohs(dt)==0x1000) {
501 if (dt==DICOM_PIXEL_ELEMENT) {
502 rp += (int)fread(&cn, sizeof(uWord), 2, fp);
503 //if ((uWord)ntohs(cn[0])==0x4f57 && cn[1]==0x0000) {
504 if (cn[0]==DICOM_PIXCEL_VR && cn[1]==0x0000) {
505 rp += (int)fread(&cn, sizeof(uWord), 2, fp);
506 }
507 //*dsize = *((int*)cn);
508 int* sizep = (int*)cn;
509 *dsize = *sizep;
510 hsize = rp*sizeof(uWord);
511 if (fsize>=*dsize+hsize) { // footer
512 ef = ON;
513 break;
514 }
515 }
516 }
517 rp += (int)fread(&dt, sizeof(uWord), 1, fp);
518 }
519
520 if (!ef) return 0;
521
522 sz = rp*sizeof(uWord);
523 wp = (uWord*)malloc(sz);
524 if (wp==NULL) return JBXL_GRAPH_MEMORY_ERROR;
525 memset(wp, 0, sz);
526
528 // ヘッダ読み込み
529 fseek(fp, 0, 0);
530 size_t ret = fread(wp, sz, 1, fp);
531 if (ret==0) {
533 }
534
535 for (i=0; i<sz/2-3; i++) {
536 //if ((uWord)ntohs(wp[i])==0x2800 && (uWord)ntohs(wp[i+1])==0x3000) {
537 if (wp[i]==DICOM_IMAGE_GROUP) {
538
539 // Z方向の歪
540 if (wp[i+1]==DICOM_PXLSPC_ELEMENT) {
541 char rx[LNAME], ry[LNAME];
542 uByte* bp;
543
544 memset(rx, 0, LNAME);
545 memset(ry, 0, LNAME);
546
547 if (wp[i+2]==DICOM_STR_VR) ln = wp[i+3];
548 else ln = *(int*)&wp[i+2];
549
550 if (ln<LNAME-1) {
551 bp = (uByte*)&wp[i+4];
552 j = 0;
553 while (j<ln-1 && bp[j]!='\\') {
554 rx[j] = bp[j];
555 j++;
556 }
557 ln -= j + 1;
558 bp += j + 1;
559 j = 0;
560 while (j<ln-1 && bp[j]!=' ') {
561 ry[j] = bp[j];
562 j++;
563 }
564 if (!strcmp(rx, ry)) {
565 rz = atof(rx);
566 }
567 }
568 }
569
570 // X サイズ
571 else if (wp[i+1]==DICOM_XSIZE_ELEMENT) {
572 if (wp[i+2]==DICOM_INT_VR) ln = wp[i+3];
573 else ln = *(int*)&wp[i+2];
574 if (ln==2) {
575 xs = (int)*(sWord*)&wp[i+4];
576 }
577 else if (ln==4) {
578 xs = *(int*)&wp[i+4];
579 }
580 }
581
582 // Y サイズ
583 else if (wp[i+1]==DICOM_YSIZE_ELEMENT) {
584 if (wp[i+2]==DICOM_INT_VR) ln = wp[i+3];
585 else ln = *(int*)&wp[i+2];
586 if (ln==2) {
587 ys = (int)*(sWord*)&wp[i+4];
588 }
589 else if (ln==4) {
590 ys = *(int*)&wp[i+4];
591 }
592 }
593
594 // Depth
595 else if (wp[i+1]==DICOM_DEPTH_ELEMENT) {
596 if (wp[i+2]==DICOM_INT_VR) ln = wp[i+3];
597 else ln = *(int*)&wp[i+2];
598 if (ln==2) {
599 dp = (int)*(sWord*)&wp[i+4];
600 }
601 else if (ln==4) {
602 dp = *(int*)&wp[i+4];
603 }
604 }
605 }
606 }
607 free(wp);
608
609 if (rzxy!=NULL) *rzxy = rz;
610 if (xsize!=NULL) *xsize = xs;
611 if (ysize!=NULL) *ysize = ys;
612 if (depth!=NULL) *depth = dp;
613
614 return sz;
615}
616
617
629{
630 int fsize;
631 FILE* fp;
633
634 memset(&vp, 0, sizeof(MSGraph<sWord>));
635
636 fsize = file_size(fn);
637 if (fsize<=0) {
639 return vp;
640 }
641
642 if ((fp=fopen(fn,"r"))==NULL) {
644 return vp;
645 }
646
647 vp = readDicomData(fp, fsize);
648
649 fclose(fp);
650 return vp;
651}
652
653
655{
656 int sz, dsize;
657 int xsize, ysize, depth;
658 double rzxy;
660
661 memset(&vp, 0, sizeof(MSGraph<sWord>));
662
663 sz = dicomHeader(fp, fsize, &dsize, &xsize, &ysize, &depth, &rzxy);
664 if (sz<=0) {
666 return vp;
667 }
668
669 if (dsize!=xsize*ysize*((depth+7)/8)) {
671 return vp;
672 }
673
674 if ((depth+7)/8!=2) {
676 return vp;
677 }
678
679 vp.set(xsize, ysize, 1, 0, 0, rzxy);
680
681 fseek(fp, sz, 0);
682 size_t ret = fread(vp.gp, dsize, 1, fp);
683 if (ret==0) vp.xs = JBXL_FILE_READ_ERROR;
684
685 return vp;
686}
687
688
689
691// ファイル名と共通ヘッダによるI/O統合
692
694// ファイル名と共通ヘッダ
695
708CmnHead jbxl::readXHead(const char* fn, CmnHead* chd)
709{
710 FILE *fp;
711 int fsz, csz;
712 CmnHead hd;
713
714 memset(&hd, 0, sizeof(CmnHead));
715 hd.kind = HEADER_NONE;
716
717 fsz = (int)file_size(fn);
718 if (fsz<=0) {
720 return hd;
721 }
722
724 // オペレータ指定のデータ形式を確認(予めヘッダに情報が設定されている)
725 //
726 if (chd!=NULL && (chd->kind&0x00ff)==USERSET_DATA) {
727 if (chd->zsize<=0) chd->zsize = 1;
728 chd->lsize = chd->xsize*chd->ysize*chd->zsize*((chd->depth+7)/8);
729 if (fsz==(int)(chd->bsize+chd->lsize)) { // ファイルサイズのチェック
730 return *chd;
731 }
732 }
733
734 if ((fp=fopen(fn,"rb"))==NULL) {
736 return hd;
737 }
738
740 // 共通ヘッダの読み込み
741 //
742
743 int hsz = sizeof(CmnHead_Entry);
744 fseek(fp, 0, 0);
745 size_t ret = fread(&hd.entry, hsz, 1, fp);
746 hd.buf = NULL;
747 hd.grptr = NULL;
748 ntoh_st(&hd, 4);
749 if (ret==0) {
751 hd.kind = HEADER_NONE;
752 fclose(fp);
753 return hd;
754 }
755
756 // Sun Raster
757 if (hd.kind==RAS_MAGIC) {
758 hd.kind = RAS_DATA;
759 DEBUG_MODE PRINT_MESG("JBXL::readXHead: Sun Raster File\n");
760 hd.depth = hd.zsize;
761 hd.zsize = 1;
762 hd.lsize = file_size(fn) - sizeof(RasHead);
763 hd.bsize = 0;
764 fclose(fp);
765 return hd;
766 }
767
768 // Common ヘッダ
769 if (hd.kind > 0 && hd.kind <= NUM_KDATA) {
770 if (fsz == (int)(hsz + hd.bsize + hd.lsize) + 4 ) hsz = 36; // x86 file
771 if (fsz == (int)(hsz + hd.bsize + hd.lsize) + 16) hsz = 48; // x64 file
772 //
773 if (fsz == (int)(hsz + hd.bsize + hd.lsize)) {
774 if (hd.zsize<=0) hd.zsize = 1;
775 if (hd.bsize>0) {
776 hd.buf = (uByte*)malloc(hd.bsize);
777 if (hd.buf==NULL) {
778 free_CmnHead(&hd);
780 hd.kind = HEADER_NONE;
781 fclose(fp);
782 return hd;
783 }
784 memset(hd.buf, 0, hd.bsize);
785
786 fseek(fp, hsz, 0);
787 ret = fread(hd.buf, hd.bsize, 1, fp);
788 ntoh_ar((sWord*)hd.buf, hd.bsize);
789 hd.grptr = NULL;
790 if (ret==0) {
792 hd.kind = HEADER_NONE;
793 fclose(fp);
794 return hd;
795 }
796 }
797
798 DEBUG_MODE {
799 PRINT_MESG("JBXL::readXHead: Common ヘッダ\n");
800 PRINT_MESG("JBXL::readXHead: ヘッダ種別 kind = %d\n", hd.kind);
801 PRINT_MESG("JBXL::readXHead: ファイルサイズ fsz = %d\n", fsz);
802 PRINT_MESG("JBXL::readXHead: ヘッダサイズ hsz = %d\n", sizeof(CmnHead_Entry));
803 PRINT_MESG("JBXL::readXHead: ヘッダバッファ bsize = %d\n", hd.bsize);
804 PRINT_MESG("JBXL::readXHead: データサイズ lsize = %d\n", hd.lsize);
805 PRINT_MESG("JBXL::readXHead: サイズ %dx%dx%d %d\n", hd.xsize, hd.ysize, hd.zsize, hd.depth);
806 }
807 fclose(fp);
808 return hd;
809 }
810 }
811 memset(&hd, 0, sizeof(CmnHead));
812
814 // CT File (Moon)
815 //
816 CTHead cthd;
817 csz = sizeof(CTHead);
818 fseek(fp, 0, 0);
819 ret = fread(&cthd, csz, 1, fp);
820 ntoh_st(&cthd, 2);
821 if (ret==0) {
823 hd.kind = HEADER_NONE;
824 fclose(fp);
825 return hd;
826 }
827 hd.xsize = cthd.xsize - cthd.cutleft - cthd.cutright;
828 hd.ysize = cthd.ysize - cthd.cutup - cthd.cutdown;
829 hd.zsize = 1;
830 hd.depth = 16;
831 hd.bsize = csz;
832 hd.lsize = hd.xsize*hd.ysize*hd.zsize*((hd.depth+7)/8);
833
834 if (fsz==(int)(hd.bsize+hd.lsize)) {
835 hd.kind = MOON_DATA;
836 hd.buf = (uByte*)malloc(csz);
837 if (hd.buf==NULL) {
838 free_CmnHead(&hd);
840 hd.kind = HEADER_NONE;
841 fclose(fp);
842 return hd;
843 }
844 memcpy(hd.buf, &cthd, csz);
845
846 DEBUG_MODE {
847 PRINT_MESG("JBXL::readXHead: CT ファイル\n");
848 PRINT_MESG("JBXL::readXHead: ヘッダ種別 kind = %d\n", hd.kind);
849 PRINT_MESG("JBXL::readXHead: ファイルサイズ fsz = %d\n", fsz);
850 PRINT_MESG("JBXL::readXHead: ヘッダサイズ hsz = %d\n", sizeof(CmnHead_Entry));
851 PRINT_MESG("JBXL::readXHead: ヘッダバッファ bsize = %d\n", hd.bsize);
852 PRINT_MESG("JBXL::readXHead: データサイズ lsize = %d\n", hd.lsize);
853 PRINT_MESG("JBXL::readXHead: サイズ %dx%dx%d %d\n", hd.xsize, hd.ysize, hd.zsize, hd.depth);
854 }
855 fclose(fp);
856 return hd;
857 }
858 memset(&hd, 0, sizeof(CmnHead));
859
861 // Dicom
862 //
863 int ls, sz, dsize, xsize, ysize, depth;
864 double rzxy;
865
866 sz = dicomHeader(fp, fsz, &dsize, &xsize, &ysize, &depth, &rzxy);
867 ls = xsize*ysize*((depth+7)/8);
868 if (sz>0 && dsize==ls) {
869 hd.kind = DICOM_DATA;
870 hd.xsize = xsize;
871 hd.ysize = ysize;
872 hd.zsize = 1;
873 hd.depth = depth;
874 hd.bsize = sizeof(CTHead);
875 hd.lsize = ls;
876 hd.buf = (uByte*)malloc(hd.bsize);
877 hd.grptr = NULL;
878
879 CTHead* pcthd = (CTHead*)hd.buf;
880 memset(pcthd, 0, hd.bsize);
881 pcthd->xsize = hd.xsize;
882 pcthd->ysize = hd.ysize;
883
884 if (rzxy>0.0) {
885 pcthd->anydata[0] = (sWord)(rzxy*RZXY_RATE);
886 pcthd->anydata[1] = RZXY_RATE;
887 hd.kind |= HAS_RZXY;
888 }
889
890 fclose(fp);
891 return hd;
892 }
893
895 // Well Known ファイル
896 //
897 fseek(fp, 0, 0);
898 Buffer buf = read_Buffer_data(fp, 32);
899
900 // TIFF
901 /*
902 if (head[0]=='I' && head[1]=='I') {
903 if (head[2]==0x2a && head[3]==0x00) {
904 DEBUG_MODE PRINT_MESG("JBXL::readXHead: TIFFデータ形式\n");
905 hd.kind = TIFF_DATA;
906 fclose(fp);
907 return hd;
908 }
909 }
910 if (head[0]=='M' && head[1]=='M') {
911 if (head[2]==0x00 && head[3]==0x2a) {
912 DEBUG_MODE PRINT_MESG("JBXL::readXHead: TIFFデータ形式\n");
913 hd.kind = TIFF_DATA;
914 fclose(fp);
915 return hd;
916 }
917 }*/
918
919#ifdef ENABLE_JPEGLIB
920 // JPEG
921 if (isJPEGHeader(buf)) {
922 DEBUG_MODE PRINT_MESG("JBXL::readXHead: JPEGデータ形式\n");
923 hd.kind = JPEG_RGB_DATA; // JPEG_RGB_DATA or JPEG_MONO_DATA
924 free_Buffer(&buf);
925 fclose(fp);
926 return hd;
927 }
928#endif
929
930 free_Buffer(&buf);
931
933 // Another Unknown File Format
934 //
935 DEBUG_MODE PRINT_MESG("JBXL::readXHead: 未知のデータ形式\n");
936 memset(&hd, 0, sizeof(CmnHead));
937 hd.kind = UN_KNOWN_DATA;
938 hd.lsize = fsz;
939
940 fclose(fp);
941 return hd;
942}
943
944
975CmnHead jbxl::readXHeadFile(const char* fn, CmnHead* chd, bool cnt)
976{
977 FILE* fp;
978 int fsz;
979 bool no_ntoh = false;
980 CmnHead hd;
981 CVCounter* counter = NULL;
982
983 memset(&hd, 0, sizeof(CmnHead));
984 hd.kind = HEADER_NONE;
985
986 fsz = (int)file_size(fn);
987 if (fsz==0) {
989 return hd;
990 }
991
992 if ((fp=fopen(fn,"rb"))==NULL) {
994 return hd;
995 }
996
998 // オペレータ指定のデータ形式を確認(予めヘッダに情報が設定されている)
999 //
1000 if (chd!=NULL) {
1001 // ファイルとCPUのエンディアンが同じ
1002 if (checkBit(chd->kind, HAS_LENDIAN) && is_little_endian()) {
1003 no_ntoh = true;
1004 }
1005
1006 // オペレータ指定のデータ形式.カウンタ未サポート
1007 if ((chd->kind&0x00ff)==USERSET_DATA) {
1008 if (chd->zsize<=0) chd->zsize = 1;
1009 chd->lsize = chd->xsize*chd->ysize*chd->zsize*((chd->depth+7)/8);
1010 if (fsz==(int)(chd->bsize+chd->lsize)) { // ファイルサイズのチェック
1011 DEBUG_MODE PRINT_MESG("JBXL::readXHeadFile: オペレータ指定のデータ形式\n");
1012 hd = readUserSetData(fp, chd, true); // カウンタ有効
1013
1014 // 24, 32bit 未対応
1015 if (hd.depth==16 && !no_ntoh) {
1016 sWord* wptr = (sWord*)hd.grptr;
1017 for (int i=0; i<hd.xsize*hd.ysize*hd.zsize; i++) {
1018 wptr[i] = ntohs(wptr[i]);
1019 }
1020 }
1021
1022 fclose(fp);
1023 return hd;
1024 }
1025 }
1026 }
1027
1029 // 共通ヘッダの読み込み
1030 //
1031 int hsz = sizeof(CmnHead_Entry);
1032 fseek(fp, 0, 0);
1033 size_t ret = fread(&hd.entry, hsz, 1, fp);
1034 hd.buf = NULL;
1035 hd.grptr = NULL;
1036 ntoh_st(&hd, 4);
1037 if (ret==0) {
1039 hd.kind = HEADER_NONE;
1040 fclose(fp);
1041 return hd;
1042 }
1043
1044 // Sun Raster
1045 if (hd.kind==RAS_MAGIC) {
1046 DEBUG_MODE PRINT_MESG("JBXL::readXHeadFile: Sun Rasterデータ形式\n");
1047 hd = readRasData(fp);
1048 fclose(fp);
1049 return hd;
1050 }
1051
1052 // Common形式
1053 // データ読み取りでは hd.lsize==0 のファイルサイズ無効(CT_RGN_SL)はまだサポートされていない
1054 if (hd.kind>0 && hd.kind<=NUM_KDATA) {
1055 if (fsz == (int)(hsz + hd.bsize + hd.lsize) + 4 ) hsz = 36; // x86 file
1056 if (fsz == (int)(hsz + hd.bsize + hd.lsize) + 16) hsz = 48; // x64 file
1057 //
1058 if (fsz == (int)(hsz + hd.bsize + hd.lsize)) {
1059 DEBUG_MODE PRINT_MESG("JBXL::readXHeadFile: Commonデータ形式\n");
1060 if (hd.zsize <= 0) hd.zsize = 1;
1061
1062 // カウンタ
1063 if (hd.zsize >= 10 && cnt) {
1064 counter = GetUsableGlobalCounter();
1065 if (counter != NULL) {
1066 //counter->SetTitle("Commonファイル読み込み中");
1067 counter->SetMax(hd.zsize / 10);
1068 counter->SetPos(0);
1069 }
1070 }
1071
1072 if (hd.bsize > 0) {
1073 hd.buf = (uByte*)malloc(hd.bsize);
1074 if (hd.buf!=NULL) memset(hd.buf, 0, hd.bsize);
1075 }
1076 hd.grptr = (uByte*)malloc(hd.lsize);
1077 if ((hd.bsize > 0 && hd.buf == NULL) || hd.grptr == NULL) {
1078 free_CmnHead(&hd);
1080 hd.kind = HEADER_NONE;
1081 fclose(fp);
1082 return hd;
1083 }
1084 memset(hd.grptr, 0, hd.lsize);
1085 fseek(fp, hsz, 0);
1086 if (hd.bsize > 0) {
1087 size_t ret = fread(hd.buf, hd.bsize, 1, fp);
1088 ntoh_ar((sWord*)hd.buf, hd.bsize);
1089 if (ret==0) {
1091 hd.kind = HEADER_NONE;
1092 fclose(fp);
1093 return hd;
1094 }
1095 }
1096 }
1097
1098 // ヘッダのみ
1099 if (hd.lsize==0) {
1100 if (counter!=NULL) counter->PutFill();
1101 fclose(fp);
1102 return hd;
1103 }
1104
1105 // データ本体
1106 if (counter==NULL) {
1107 size_t ret = fread(hd.grptr, hd.lsize, 1, fp);
1108 if (ret==0) {
1110 hd.kind = HEADER_NONE;
1111 fclose(fp);
1112 return hd;
1113 }
1114 }
1115 else {
1116 int psize = hd.xsize*hd.ysize*((hd.depth+7)/8);
1117 for (int i=0; i<hd.zsize; i++) {
1118 size_t ret = fread(hd.grptr+i*psize, psize, 1, fp);
1119 if (ret==0) {
1121 hd.kind = HEADER_NONE;
1122 fclose(fp);
1123 return hd;
1124 }
1125 if (i%10==0) {
1126 counter->StepIt();
1127 if (counter->isCanceled()) { // キャンセル
1128 //counter->Stop();
1129 free_CmnHead(&hd);
1131 hd.kind = HEADER_NONE;
1132 fclose(fp);
1133 return hd;
1134 }
1135 }
1136 }
1137 }
1138
1139 // 24, 32bit 未対応
1140 if (hd.depth==16 && !no_ntoh) {
1141 sWord* wptr = (sWord*)hd.grptr;
1142 for (int i=0; i<hd.xsize*hd.ysize*hd.zsize; i++) {
1143 wptr[i] = ntohs(wptr[i]);
1144 }
1145 }
1146
1147 if (counter!=NULL) counter->PutFill();
1148 fclose(fp);
1149 return hd;
1150 }
1151 memset(&hd, 0, sizeof(CmnHead));
1152
1154 // Moon形式 16bit
1155 //
1156 hd = readMoonData(fp, fsz, no_ntoh);
1157 if ((hd.kind & 0x00ff)==MOON_DATA) {
1158 DEBUG_MODE PRINT_MESG("JBXL::readXHeadFile: Moonデータ形式\n");
1159 fclose(fp);
1160 return hd;
1161 }
1162 memset(&hd, 0, sizeof(CmnHead));
1163
1165 // DICOMファイル
1166 //
1167 MSGraph<sWord> vp = readDicomData(fp, fsz);
1168 if (vp.gp!=NULL) {
1169 DEBUG_MODE PRINT_MESG("JBXL::readXHeadFile: DICOMデータ形式\n");
1170 hd.kind = DICOM_DATA;
1171 hd.xsize = vp.xs;
1172 hd.ysize = vp.ys;
1173 hd.zsize = vp.zs;
1174 hd.depth = 16;
1175 hd.bsize = sizeof(CTHead);
1176 hd.lsize = vp.xs*vp.ys*vp.zs*2;
1177 hd.buf = (uByte*)malloc(hd.bsize);
1178 hd.grptr = (uByte*)vp.gp;
1179
1180 CTHead* pcthd = (CTHead*)hd.buf;
1181 memset(pcthd, 0, hd.bsize);
1182 pcthd->xsize = (sWord)hd.xsize;
1183 pcthd->ysize = (sWord)hd.ysize;
1184 //pcthd->ctmin = 0;
1185 //pcthd->ctmax = 0;
1186 //pcthd->cutup = 0;
1187 //pcthd->cutdown = 0;
1188 //pcthd->cutleft = 0;
1189 //pcthd->cutright = 0;
1190
1191 if (vp.RZxy>0.0) {
1192 pcthd->anydata[0] = (sWord)(vp.RZxy*RZXY_RATE);
1193 pcthd->anydata[1] = RZXY_RATE;
1194 hd.kind |= HAS_RZXY;
1195 }
1196
1197 if (!no_ntoh) {
1198 sWord* wptr = (sWord*)hd.grptr;
1199 for (int i=0; i<hd.xsize*hd.ysize*hd.zsize; i++) {
1200 wptr[i] = ntohs(wptr[i]);
1201 }
1202 }
1203
1204 fclose(fp);
1205 return hd;
1206 }
1207
1209 // Well Known ファイル
1210 //
1211 fseek(fp, 0, 0);
1212 Buffer buf = read_Buffer_data(fp, 32);
1213
1215 // TIFF
1216 /*
1217 size_t ret = fread(head, 32, 1, fp);
1218 if (ret==0) {
1219 hd.xsize = JBXL_FILE_READ_ERROR;
1220 hd.kind = HEADER_NONE;
1221 fclose(fp);
1222 return hd;
1223 }
1224 if (head[0]=='I' && head[1]=='I') {
1225 if (head[2]==0x2a && head[3]==0x00) {
1226 DEBUG_MODE PRINT_MESG("JBXL::readXHeadFile: TIFFデータ形式\n");
1227 hd.kind = TIFF_DATA;
1228 //fclose(fp);
1229 //return hd;
1230 }
1231 }
1232 if (head[0]=='M' && head[1]=='M') {
1233 if (head[2]==0x00 && head[3]==0x2a) {
1234 DEBUG_MODE PRINT_MESG("JBXL::readXHeadFile: TIFFデータ形式\n");
1235 hd.kind = TIFF_DATA;
1236 //fclose(fp);
1237 //return hd;
1238 }
1239 }*/
1240
1241#ifdef ENABLE_JPEGLIB
1242 // JPEG
1243 if (isJPEGHeader(buf)) {
1244 DEBUG_MODE PRINT_MESG("JBXL::readXHeadFile: JPEGデータ形式\n");
1245 JPEGImage jpg = readJPEGData(fp);
1246 hd = JPEGImage2CmnHead(jpg);
1247
1248 if (hd.zsize==1) hd.kind = JPEG_MONO_DATA;
1249 jpg.free();
1250 free_Buffer(&buf);
1251 fclose(fp);
1252 return hd;
1253 }
1254#endif
1255
1256 free_Buffer(&buf);
1257
1259 // 解析不能.データのまま読み込み UN_KNOWN_DATA
1260 //
1261 DEBUG_MODE PRINT_MESG("JBXL::readXHeadFile: 未知のデータ形式\n");
1262 memset(&hd, 0, sizeof(CmnHead));
1263
1264 hd.grptr = (uByte*)malloc(fsz);
1265 if (hd.grptr==NULL) {
1267 hd.kind = HEADER_NONE;
1268 fclose(fp);
1269 return hd;
1270 }
1271 memset(hd.grptr, 0, fsz);
1272
1273 fseek(fp, 0, 0);
1274 ret = fread(hd.grptr, fsz, 1, fp);
1275 if (ret>0) {
1276 hd.kind = UN_KNOWN_DATA;
1277 hd.lsize = fsz;
1278 }
1279 else {
1281 hd.kind = HEADER_NONE;
1282 }
1283
1284 fclose(fp);
1285 return hd;
1286}
1287
1288
1314CmnHead jbxl::readCmnHeadFile(const char* fn, CmnHead* chd, bool cnt)
1315{
1316 FILE* fp;
1317 int kind, fsz;
1318 bool no_ntoh = false;
1319 CmnHead hd;
1320 CVCounter* counter = NULL;
1321
1322 memset(&hd, 0, sizeof(CmnHead));
1323 hd.kind = HEADER_NONE;
1325
1326 if (chd==NULL) return hd;
1327
1328 fsz = (int)file_size(fn);
1329 if (fsz==0) {
1331 return hd;
1332 }
1333
1334 if ((fp=fopen(fn,"rb"))==NULL) {
1336 return hd;
1337 }
1338
1339 // ファイルとCPUのエンディアンが同じ
1340 if (checkBit(chd->kind, HAS_LENDIAN) && is_little_endian()) {
1341 no_ntoh = true;
1342 }
1343 kind = chd->kind & 0x00ff;
1344
1345 // オペレータ指定のデータ形式.カウンタ未サポート
1346 if (kind == USERSET_DATA) {
1347 if (chd->zsize<=0) chd->zsize = 1;
1348 chd->lsize = chd->xsize*chd->ysize*chd->zsize*((chd->depth+7)/8);
1349 if (fsz==(int)(chd->bsize+chd->lsize)) { // ファイルサイズのチェック
1350 DEBUG_MODE PRINT_MESG("JBXL::readCmnHeadFile: オペレータ指定のデータ形式\n");
1351 hd = readUserSetData(fp, chd, true); // カウンタ有効
1352
1353 // 24, 32bit 未対応
1354 if (hd.depth==16 && !no_ntoh) {
1355 sWord* wptr = (sWord*)hd.grptr;
1356 for (int i=0; i<hd.xsize*hd.ysize*hd.zsize; i++) {
1357 wptr[i] = ntohs(wptr[i]);
1358 }
1359 }
1360 }
1361 }
1362
1363 // DICOMファイル.
1364 else if (kind==DICOM_DATA) {
1365 MSGraph<sWord> vp = readDicomData(fp, fsz);
1366 if (vp.gp!=NULL) {
1367 DEBUG_MODE PRINT_MESG("JBXL::readCmnHeadFile: DICOMデータ形式\n");
1368 hd.kind = DICOM_DATA;
1369 hd.xsize = vp.xs;
1370 hd.ysize = vp.ys;
1371 hd.zsize = vp.zs;
1372 hd.depth = 16;
1373 hd.bsize = sizeof(CTHead);
1374 hd.lsize = vp.xs*vp.ys*vp.zs*2;
1375 hd.buf = (uByte*)malloc(hd.bsize);
1376 hd.grptr = (uByte*)vp.gp;
1377
1378 CTHead* pcthd = (CTHead*)hd.buf;
1379 memset(pcthd, 0, hd.bsize);
1380 pcthd->xsize = (sWord)hd.xsize;
1381 pcthd->ysize = (sWord)hd.ysize;
1382 //pcthd->ctmin = 0;
1383 //pcthd->ctmax = 0;
1384 //pcthd->cutup = 0;
1385 //pcthd->cutdown = 0;
1386 //pcthd->cutleft = 0;
1387 //pcthd->cutright = 0;
1388
1389 if (vp.RZxy>0.0) {
1390 pcthd->anydata[0] = (sWord)(vp.RZxy*RZXY_RATE);
1391 pcthd->anydata[1] = RZXY_RATE;
1392 hd.kind |= HAS_RZXY;
1393 }
1394
1395 if (!no_ntoh) {
1396 sWord* wptr = (sWord*)hd.grptr;
1397 for (int i=0; i<hd.xsize*hd.ysize*hd.zsize; i++) {
1398 wptr[i] = ntohs(wptr[i]);
1399 }
1400 }
1401 }
1402 }
1403
1404 // Sun Raster
1405 else if (kind==RAS_DATA) {
1406 DEBUG_MODE PRINT_MESG("JBXL::readCmnHeadFile: Sun Rasterデータ形式\n");
1407 hd = readRasData(fp);
1408 }
1409
1410 // Moon形式 16bit
1411 else if (kind==MOON_DATA) {
1412 DEBUG_MODE PRINT_MESG("JBXL::readCmnHeadFile: Moonデータ形式\n");
1413 hd = readMoonData(fp, fsz, no_ntoh);
1414 }
1415
1416#ifdef ENABLE_JPEGLIB
1417 // JPEG
1418 else if (kind==JPEG_RGB_DATA || kind==JPEG_MONO_DATA) {
1419 DEBUG_MODE PRINT_MESG("JBXL::readCmnHeadFile: JPEGデータ形式\n");
1420 JPEGImage jpg = readJPEGData(fp);
1421 hd = JPEGImage2CmnHead(jpg);
1422 jpg.free();
1423 }
1424#endif
1425
1426 // TIFF
1427 /*else if (kind == TIFF_DATA) {
1428
1429 }*/
1430
1431 // Common形式
1432 else if (kind!=UN_KNOWN_DATA) {
1433 // データ読み取りでは hd.lsize==0 のファイルサイズ無効(CT_RGN_SL)はまだサポートされていない
1434 DEBUG_MODE PRINT_MESG("JBXL::readCmnHeadFile: Commonデータ形式\n");
1435
1436 int hsz = sizeof(CmnHead_Entry);
1437 fseek(fp, 0, 0);
1438 size_t ret = fread(&hd.entry, hsz, 1, fp);
1439 hd.buf = NULL;
1440 hd.grptr = NULL;
1441 ntoh_st(&hd, 4);
1442 if (ret==0) {
1444 hd.kind = HEADER_NONE;
1445 fclose(fp);
1446 return hd;
1447 }
1448 if (hd.zsize<=0) hd.zsize = 1;
1449
1450 if (fsz == (int)(hsz + hd.bsize + hd.lsize) + 4 ) hsz = 36; // x86 file
1451 if (fsz == (int)(hsz + hd.bsize + hd.lsize) + 16) hsz = 48; // x64 file
1452
1453 // カウンタ
1454 if (hd.zsize>=10 && cnt) {
1455 counter = GetUsableGlobalCounter();
1456 if (counter!=NULL) {
1457 //counter->SetTitle("Commonファイル読み込み中");
1458 counter->SetMax(hd.zsize/10);
1459 counter->SetPos(0);
1460 }
1461 }
1462
1463 if (hd.bsize>0) {
1464 hd.buf = (uByte*)malloc(hd.bsize);
1465 if (hd.buf!=NULL) memset(hd.buf, 0, hd.bsize);
1466 }
1467 hd.grptr = (uByte*)malloc(hd.lsize);
1468 if ((hd.bsize>0&&hd.buf==NULL) || hd.grptr==NULL) {
1469 free_CmnHead(&hd);
1471 hd.kind = HEADER_NONE;
1472 fclose(fp);
1473 return hd;
1474 }
1475 memset(hd.grptr, 0, hd.lsize);
1476
1477 fseek(fp, hsz, 0);
1478 if (hd.bsize>0) {
1479 ret = fread(hd.buf, hd.bsize, 1, fp);
1480 ntoh_ar((sWord*)hd.buf, hd.bsize);
1481 if (ret==0) {
1483 hd.kind = HEADER_NONE;
1484 fclose(fp);
1485 return hd;
1486 }
1487 }
1488
1489 // ヘッダのみ
1490 if (hd.lsize==0) {
1491 if (counter!=NULL) counter->PutFill();
1492 }
1493 else {
1494 // データ本体
1495 if (counter==NULL) {
1496 ret = fread(hd.grptr, hd.lsize, 1, fp);
1497 if (ret==0) {
1499 hd.kind = HEADER_NONE;
1500 fclose(fp);
1501 return hd;
1502 }
1503 }
1504 else {
1505 int psize = hd.xsize*hd.ysize*((hd.depth+7)/8);
1506 for (int i=0; i<hd.zsize; i++) {
1507 ret = fread(hd.grptr+i*psize, psize, 1, fp);
1508 if (ret==0) {
1510 hd.kind = HEADER_NONE;
1511 fclose(fp);
1512 return hd;
1513 }
1514 if (i%10==0) {
1515 counter->StepIt();
1516 if (counter->isCanceled()) { // キャンセル
1517 //counter->Stop();
1518 free_CmnHead(&hd);
1520 hd.kind = HEADER_NONE;
1521 fclose(fp);
1522 return hd;
1523 }
1524 }
1525 }
1526 }
1527
1528 // 24, 32bit 未対応
1529 if (hd.depth==16 && !no_ntoh) {
1530 sWord* wptr = (sWord*)hd.grptr;
1531 for (int i=0; i<hd.xsize*hd.ysize*hd.zsize; i++) {
1532 wptr[i] = ntohs(wptr[i]);
1533 }
1534 }
1535
1536 if (counter!=NULL) counter->PutFill();
1537 }
1538 }
1539
1540 else {
1541 PRINT_MESG("JBXL::readCmnHeadFile: 未知のデータ形式が指定された.(%04x, %04x)\n", chd->kind, (uWord)kind);
1542 }
1543
1544 fclose(fp);
1545 return hd;
1546}
1547
1548
1576int jbxl::writeCmnHeadFile(const char* fn, CmnHead* hd, bool cnt)
1577{
1578 CmnHead cmd;
1579 FILE* fp;
1580 int csize, psize;
1581 int kind = hd->kind & 0x0ff;
1582
1583 if (kind==UN_KNOWN_DATA) return JBXL_GRAPH_HEADER_ERROR;
1584 if (hd==NULL || hd->lsize==0) return JBXL_GRAPH_IVDARG_ERROR;
1585
1586 if ((fp=fopen(fn,"wb"))==NULL) {
1587 PRINT_MESG("JBXL::writeCmnHeadFile: ERROR:ファイルオープン失敗\n");
1589 }
1590 DEBUG_MODE PRINT_MESG("JBXL::writeCmnHeadFile: ファイル種別 = %d で書き込み中.%dx%dx%d\n", hd->kind, hd->xsize, hd->ysize, hd->zsize);
1591
1592 // Write File
1593 csize = writeCmnHeadData(fp, hd, cnt);
1594 fclose(fp);
1595 if (csize<0) return (int)csize;
1596
1597 // SUN RASTER
1598 if (kind==RAS_DATA) return csize;
1599
1600 // JPEG
1601 if (kind==JPEG_RGB_DATA || kind==JPEG_MONO_DATA) return csize;
1602 if (kind==JPEG_ARGB_DATA || kind==JPEG_RGBA_DATA) return csize;
1603 if (kind==JPEG16_RGB_DATA || kind==JPEG16_ARGB_DATA || kind==JPEG16_RGBA_DATA) return csize;
1604
1605 // TIFF
1606 //if (kind==TIFF_DATA) return csize;
1607
1608 // 書き込みチェック
1609 int fsz = (int)file_size(fn);
1610 if (kind==MOON_DATA) psize = hd->bsize + hd->lsize;
1611 else psize = sizeof(CmnHead_Entry) + hd->bsize + hd->lsize;
1612
1613 if (fsz!=psize) {
1614 PRINT_MESG("JBXL::writeCmnHeadFile: ERROR:書き込みファイルのサイズが合わない %d != %d\n", psize, fsz);
1616 }
1617 if (kind==MOON_DATA) return psize;
1618
1619 if ((fp=fopen(fn,"rb"))==NULL) {
1620 PRINT_MESG("JBXL::writeCmnHeadFile: ERROR:ファイル検査:再オープン失敗\n");
1622 }
1623
1624 size_t ret = fread((sByte*)&cmd.entry, sizeof(CmnHead_Entry), 1, fp);
1625 fclose(fp);
1626 ntoh_st(&cmd.entry, 4);
1627 if (ret==0) {
1628 PRINT_MESG("JBXL::writeCmnHeadFile: ERROR:ファイルヘッダ検査:ヘッダ異常\n");
1629 return JBXL_FILE_READ_ERROR;
1630 }
1631 if (cmd.xsize!=hd->xsize || cmd.ysize!=hd->ysize || cmd.zsize!=hd->zsize ||
1632 cmd.bsize!=hd->bsize || cmd.lsize!=hd->lsize || cmd.depth!=hd->depth || cmd.kind!=hd->kind) {
1633 PRINT_MESG("JBXL::writeCmnHeadFile: ERROR:ファイルヘッダ検査:ヘッダ異常\n");
1635 }
1636
1637 return csize;
1638}
1639
1640
1666int jbxl::writeCmnHeadData(FILE* fp, CmnHead* hd, bool cnt)
1667{
1668 CTHead chd;
1669 CmnHead cmd;
1670 CVCounter* counter = NULL;
1671 sByte* ptr;
1672 int i, j, k, l;
1673 int csize, psize;
1674 int kind = hd->kind & 0x00ff;
1675
1676 if (kind==UN_KNOWN_DATA) return JBXL_GRAPH_HEADER_ERROR;
1677
1678 // SUN RASTER
1679 if (kind==RAS_DATA) {
1680 //csize = writeRasData(fp, hd, hd->depth);
1681 csize = writeRasData(fp, hd, 8);
1682 return csize;
1683 }
1684
1685#ifdef ENABLE_JPEGLIB
1686 // JPEG
1687 if (kind==JPEG_RGB_DATA || kind==JPEG_MONO_DATA || kind==JPEG_ARGB_DATA || kind==JPEG_RGBA_DATA ||
1688 kind==JPEG16_RGB_DATA || kind==JPEG16_ARGB_DATA || kind==JPEG16_RGBA_DATA) {
1689 JPEGImage jpg = CmnHead2JPEGImage(*hd);
1690 csize = writeJPEGData(fp, &jpg, 100);
1691 jpg.free();
1692 return csize;
1693 }
1694#endif
1695
1696// Not Implement yet
1697#ifdef ENABLE_PGN
1698 // PNG
1699#endif
1700
1701 // TIFF
1702 /*if (kind==TIFF_DATA) {
1703 csize =
1704 return csize;
1705 }*/
1706
1707 // TGA
1708
1709
1711 // CT : ヘッダの準備
1712 if (hd->zsize<=0) hd->zsize = 1;
1713 if (hd->depth<=0) hd->depth = 16;
1714
1715 psize = hd->xsize*hd->ysize*((hd->depth+7)/8);
1716 hd->lsize = psize*hd->zsize;
1717 ptr = (sByte*)malloc(hd->lsize);
1718 if (ptr==NULL) return JBXL_GRAPH_MEMORY_ERROR;
1719 memset(ptr, 0, hd->lsize);
1720
1721 // CTHead chd を作る.
1722 if (kind==CT_DATA || kind==CT_3DM || kind==CT_3D_VOL) {
1723 DEBUG_MODE {
1724 PRINT_MESG("JBXL::writeCmnHeadData: CTデータ\n");
1725 PRINT_MESG("JBXL::writeCmnHeadData: ヘッダバッファ bsize = %d\n", hd->bsize);
1726 PRINT_MESG("JBXL::writeCmnHeadData: データサイズ lsize = %d\n", hd->lsize);
1727 PRINT_MESG("JBXL::writeCmnHeadData: サイズ %dx%dx%d %d\n", hd->xsize, hd->ysize, hd->zsize, hd->depth);
1728 }
1729 memcpy(&chd, hd->buf, hd->bsize);
1730 chd.anydata[2] += TempBase;
1731 hton_st(&chd, 2);
1732
1733 memcpy(ptr, hd->grptr, hd->lsize);
1734 }
1735 else if (kind==MOON_DATA || kind==USERSET_DATA) {
1736 chd.xsize = htons((sWord)hd->xsize);
1737 chd.ysize = htons((sWord)hd->ysize);
1738 chd.ctmin = 0;
1739 chd.ctmax = 0;
1740 chd.cutup = 0;
1741 chd.cutdown = 0;
1742 chd.cutleft = 0;
1743 chd.cutright= 0;
1744
1745 k = l = 0;
1746 if (hd->depth<16){
1747 for (i=0; i<hd->xsize*hd->ysize; i++) {
1748 for (j=0; j<hd->depth/8; j++) ptr[k++] = 0x00;
1749 for (j=hd->depth/8; j<2; j++) ptr[k++] = hd->grptr[l++];
1750 }
1751 }
1752 else if (hd->depth==16) {
1753 for (i=0; i<hd->xsize*hd->ysize; i++) {
1754 for (j=0; j<2; j++) ptr[k++] = hd->grptr[l++];
1755 }
1756 }
1757 else {
1758 for (i=0; i<hd->xsize*hd->ysize; i++) {
1759 for (j=0; j<2; j++) ptr[k++] = hd->grptr[l++];
1760 l += (hd->depth)/8 - 2;
1761 }
1762 }
1763 }
1764 else {
1765 PRINT_MESG("JBXL::writeCmnHeadData: サポートしていないヘッダタイプ %d\n", hd->kind);
1766 free(ptr);
1767 return 0;
1768 }
1769
1771 // データ書き込み
1772 csize = 0;
1773 // 共通ヘッダ書き込み
1774 if (kind==CT_DATA || kind==CT_3DM || kind==CT_3D_VOL) {
1775 // カウンタ
1776 if (hd->zsize>=10 && cnt) {
1777 counter = GetUsableGlobalCounter();
1778 if (counter!=NULL) {
1779 //counter->SetTitle("Commonファイル書き込み中");
1780 counter->SetMax((hd->zsize+1)/10);
1781 counter->SetPos(1);
1782 }
1783 }
1784
1785 cmd = *hd;
1786 cmd.grptr = NULL;
1787 hton_st(&cmd, 4);
1788 csize = sizeof(CmnHead_Entry);
1789 fwrite(&cmd.entry, csize, 1, fp);
1790 }
1791
1792 // CTヘッダ書き込み
1793 fwrite(&chd, sizeof(CTHead), 1, fp);
1794 csize += sizeof(CTHead);
1795
1796 // 本体データ書き込み
1797 hton_ar((sWord*)ptr, hd->lsize);
1798
1799 if (counter==NULL) {
1800 fwrite(ptr, hd->lsize, 1, fp);
1801 csize += hd->lsize;
1802 }
1803 else {
1804 for (i=0; i<hd->zsize; i++) {
1805 fseek(fp, csize, 0);
1806 fwrite(ptr+i*psize, psize, 1, fp);
1807 csize += psize;
1808 if (i%10==0) {
1809 counter->StepIt();
1810 if (counter->isCanceled()) { // キャンセル
1811 free(ptr);
1812 return JBXL_GRAPH_CANCEL;
1813 }
1814 }
1815 }
1816 }
1817
1818 free(ptr);
1819 if (counter!=NULL) counter->PutFill();
1820
1821 return csize;
1822}
1823
グラフィック用ファイル入出力関数ヘッダ
JPEGグラフィックデータ定義用ヘッダ
void free_Buffer(Buffer *buf)
Buffer型変数のバッファ部を解放する
Definition buffer.cpp:128
Buffer read_Buffer_data(FILE *fp, int sz)
ファイルポインタ fp から szバイトをBuffer型変数に読み込む.
Definition buffer.cpp:1497
virtual void StepIt(int n=1)
カウンタのメモリを増やす
Definition ClassBox.h:171
virtual void SetMax(int m)
カウンタの最大値(最終目標)を設定
Definition ClassBox.h:161
virtual void PutFill()
取り敢えずの目標(最短目標)までカウンタを進める.
Definition ClassBox.h:164
virtual bool isCanceled()
カウンタがオペレータにより,キャンセルされたか
Definition ClassBox.h:173
virtual void SetPos(int p)
カウンタの現在位置を設定
Definition ClassBox.h:163
T * gp
グラフィックデータへのポインタ.
Definition Gdata.h:81
int zs
zサイズ. 4Byte. 2Dの場合は 1.
Definition Gdata.h:80
int xs
xサイズ. 4Byte.
Definition Gdata.h:78
double RZxy
Z軸の歪.Z軸の間隔を 1とした XY軸の間隔.(X or Y)/Z.
Definition Gdata.h:92
void set(RBound< int > rb, T v=(T) 0, T b=(T) 0, double rz=1.0)
Definition Gdata.h:207
int ys
yサイズ. 4Byte.
Definition Gdata.h:79
#define LNAME
Definition common.h:153
char sByte
1Byte
Definition common.h:333
#define OFF
Definition common.h:231
unsigned short uWord
2Byte
Definition common.h:334
#define Max(x, y)
Definition common.h:247
#define Xabs(x)
Definition common.h:257
short sWord
2Byte
Definition common.h:335
unsigned char uByte
1Byte
Definition common.h:332
#define ON
Definition common.h:230
#define checkBit(dat, bit)
Definition common.h:260
#define JPEG16_RGB_DATA
0x0022 // 書き込み用(16bit->32bit変換)
Definition gheader.h:186
#define RAS_MAGIC
Definition gheader.h:94
#define JPEG_ARGB_DATA
0x0025 // 書き込み用(アルファチャンネルは削除される)
Definition gheader.h:189
#define DICOM_STR_VR
Z歪
Definition gheader.h:319
#define HEADER_NONE
0x8000 // ヘッダ種別の指定なし
Definition gheader.h:212
#define DICOM_PIXEL_GROUP
Definition gheader.h:309
#define MOON_DATA
0x0003 // CT DATA
Definition gheader.h:172
#define RT_STANDARD
Definition gheader.h:97
#define CT_3DM
0x0012 // 3D CT DATA(マルチスライス)
Definition gheader.h:178
#define DICOM_XSIZE_ELEMENT
Definition gheader.h:312
#define JPEG_RGBA_DATA
0x0026 // 書き込み用(アルファチャンネルは削除される)
Definition gheader.h:190
#define USERSET_DATA
0x0005 // ユーザ指定のデータ形式(ヘッダ形式指定の場合,使用する)
Definition gheader.h:174
#define DICOM_INT_VR
X, Y.
Definition gheader.h:320
#define RZXY_RATE
RZxy をファイルのヘッダに埋め込む際の比率(倍率).
Definition gheader.h:215
#define RAS_DATA
0x0070 // SUN RASTER 8bit
Definition gheader.h:198
#define JPEG_RGB_DATA
0x0020 // JPEG RGB
Definition gheader.h:184
#define DICOM_PXLSPC_ELEMENT
Definition gheader.h:314
#define DICOM_YSIZE_ELEMENT
Definition gheader.h:313
#define DICOM_DATA
0x0004 // DICOM Header
Definition gheader.h:173
#define CT_DATA
0x0010 // CT DATA (Moon形式)
Definition gheader.h:177
#define HAS_LENDIAN
0x4000 // リトルエンディアン
Definition gheader.h:210
#define JPEG16_ARGB_DATA
0x0023 // 書き込み用(16bit->32bit変換)
Definition gheader.h:187
#define DICOM_IMAGE_GROUP
Definition gheader.h:308
#define DICOM_DEPTH_ELEMENT
Definition gheader.h:315
#define DICOM_PIXEL_ELEMENT
Definition gheader.h:311
#define RMT_NONE
Definition gheader.h:100
#define DICOM_PIXCEL_VR
Not.
Definition gheader.h:318
#define UN_KNOWN_DATA
0x0000 // 知らないデータ形式(システムにお任せ)
Definition gheader.h:169
#define JPEG_MONO_DATA
0x0021 // JPEG MONO
Definition gheader.h:185
#define JPEG16_RGBA_DATA
0x0024 // 書き込み用(16bit->32bit変換)
Definition gheader.h:188
#define NUM_KDATA
0x7fff // Max Numver of Data Format
Definition gheader.h:213
#define HAS_RZXY
0x1000 // with RZxy data
Definition gheader.h:208
#define CT_3D_VOL
0x0013 // CT ボリュームデータ
Definition gheader.h:179
#define JBXL_GRAPH_IVDARG_ERROR
無効な引数
Definition jbxl_state.h:178
#define JBXL_FILE_READ_ERROR
ファイル読み込みエラー
Definition jbxl_state.h:45
#define JBXL_GRAPH_OPFILE_ERROR
ファイルのオープンエラー
Definition jbxl_state.h:173
#define JBXL_GRAPH_RDFILE_ERROR
ファイルの読み込みエラー
Definition jbxl_state.h:174
#define JBXL_GRAPH_CANCEL
処理がキャンセルされた
Definition jbxl_state.h:168
#define JBXL_GRAPH_NOFILE_ERROR
ファイルが存在しない
Definition jbxl_state.h:172
#define JBXL_GRAPH_WRFILE_ERROR
ファイルの書き込みエラー
Definition jbxl_state.h:175
#define JBXL_GRAPH_HEADER_ERROR
画像ヘッダーのエラー
Definition jbxl_state.h:169
#define JBXL_GRAPH_MEMORY_ERROR
メモリエラー
Definition jbxl_state.h:170
Definition Brep.h:29
CVCounter * GetUsableGlobalCounter()
現在有効なグローバルカウンタを得る.(子カウンタを得るかもしれない)
Definition ClassBox.h:185
MSGraph< sWord > readDicomFile(const char *fn)
Definition Gio.cpp:628
int writeCmnHeadData(FILE *fp, CmnHead *hd, bool cnt=false)
Definition Gio.cpp:1666
int TempBase
Definition Gdata.cpp:14
MSGraph< sWord > readDicomData(FILE *fp, int fsz)
Definition Gio.cpp:654
void init_CmnHead(CmnHead *hd)
Definition Gdata.cpp:42
void free_CmnHead(CmnHead *hd)
Definition Gdata.cpp:25
CmnHead readRasData(FILE *fp)
read_ras_data() for C // SUN RASTER形式のファイルを読み込む
Definition Gio.cpp:30
int writeRasData(FILE *fp, CmnHead *ch, int obit=8)
write_ras_data() for C
Definition Gio.cpp:121
CmnHead readMoonData(FILE *fp, unsigned int fsz=0, bool no_ntoh=false)
Definition Gio.cpp:388
CmnHead readUserSetData(FILE *fp, CmnHead *ch, bool cnt=false)
read_user_data() for C // ユーザ指定(ch)のデータ形式でファイルを読み込む
Definition Gio.cpp:235
CmnHead readMoonFile(const char *fn, bool no_ntoh=false)
Definition Gio.cpp:350
CmnHead readCmnHeadFile(const char *fn, CmnHead *ch, bool cnt=false)
拡張read関数.ファイル種別を指定して読み込む.
Definition Gio.cpp:1314
CmnHead readXHeadFile(const char *fn, CmnHead *ch=NULL, bool cnt=false)
拡張read関数.ファイルを自動判別して読み込む.
Definition Gio.cpp:975
void freeNull(T &p)
Definition common++.h:37
int writeCmnHeadFile(const char *fn, CmnHead *hd, bool cnt=false)
Definition Gio.cpp:1576
int dicomHeader(FILE *fp, int fsize, int *dsize, int *xsize, int *ysize, int *depth, double *rzxy)
Definition Gio.cpp:471
CmnHead readXHead(const char *fn, CmnHead *ch=NULL)
ヘッダ部分のみ読み込み
Definition Gio.cpp:708
sWord xsize
Definition gheader.h:65
sWord cutdown
Definition gheader.h:70
sWord ctmax
Definition gheader.h:68
sWord ysize
Definition gheader.h:66
sWord cutright
Definition gheader.h:72
sWord anydata[23]
Definition gheader.h:73
sWord cutup
Definition gheader.h:69
sWord ctmin
Definition gheader.h:67
sWord cutleft
Definition gheader.h:71
Definition gheader.h:110
CmnHead_Entry entry
Definition gheader.h:125
unsigned int lsize
Size of Graphics Data (byte unit)
Definition gheader.h:133
uByte * buf
Ture Header buffer
Definition gheader.h:137
int zsize
For 3D Data (or Color)
Definition gheader.h:130
int kind
Kind of Graphics Format.
Definition gheader.h:127
unsigned int bsize
Fllowing buf size or Any Data (byte unit)
Definition gheader.h:132
uByte * grptr
Pointer to Data.
Definition gheader.h:138
int ysize
Height of Graphics.
Definition gheader.h:129
int depth
Color Depth of Graphics (bit unit)
Definition gheader.h:131
int xsize
Width of Graphics.
Definition gheader.h:128
int ras_length
Definition gheader.h:87
int ras_maptype
Definition gheader.h:89
int ras_magic
Definition gheader.h:83
int ras_height
Definition gheader.h:85
int ras_depth
Definition gheader.h:86
int ras_type
Definition gheader.h:88
int ras_maplength
Definition gheader.h:90
int ras_width
Definition gheader.h:84
ツールライブラリ ヘッダ for C++
unsigned long int file_size(const char *fn)
ファイルの大きさを返す.
Definition tools.cpp:2309
int is_little_endian(void)
エンディアンの動的チェック
Definition tools.cpp:80
#define hton_ar(p, s)
host形式から network形式へ.長さsバイトの配列pに対して変換.
Definition tools.h:428
#define hton_st(p, s)
host形式から network形式へ.構造体pに対して sバイトづつ変換.
Definition tools.h:426
#define ntoh_ar(p, s)
network形式から host形式へ.長さsバイトの配列pに対して変換
Definition tools.h:427
#define PRINT_MESG(...)
環境依存用の出力関数.MS Windows用は未実装
Definition tools.h:469
#define ntoh_st(p, s)
network形式から host形式へ.構造体pに対して sバイトづつ変換.
Definition tools.h:425
#define DEBUG_MODE
Definition tools.h:502