00001 
00009 #include "tools++.h"
00010 #include "Gio.h"
00011 #include "JpegTool.h"
00012 
00013 
00014 using namespace jbxl;
00015 
00016 
00018 
00019 
00032 CmnHead  jbxl::readRasData(FILE* fp)
00033 {
00034     RasHead rhd;
00035     CmnHead  hd;
00036     int  i, linebyte, databyte;
00037     uByte null[2], *buf;
00038     sWord* wptr;
00039 
00040     hd.kind  = RAS_DATA;
00041     hd.bsize = 32;
00042     hd.buf   = NULL;
00043     hd.grptr = NULL;
00044 
00045     fseek(fp, 0, 0);
00046     fread(&rhd, hd.bsize, 1, fp);
00047     ntoh_st(&rhd, 4);
00048 
00049     hd.xsize = rhd.ras_width;
00050     hd.ysize = rhd.ras_height;
00051     hd.zsize = 1;
00052     hd.depth = rhd.ras_depth;
00053     hd.lsize = hd.xsize*hd.ysize*((hd.depth+7)/8);
00054     hd.buf   = (uByte*)malloc(hd.bsize);
00055     if (hd.buf==NULL) {
00056         hd.xsize = JBXL_GRAPH_MEMORY_ERROR;
00057         hd.kind  = HEADER_NONE;
00058         return hd;
00059     }
00060     memcpy(hd.buf, (uByte*)&rhd, hd.bsize);
00061 
00062     hd.grptr = (uByte*)malloc(hd.lsize);
00063     if (hd.grptr==NULL) {
00064         freeNull(hd.buf);
00065         hd.xsize = JBXL_GRAPH_MEMORY_ERROR;
00066         hd.kind  = HEADER_NONE;
00067         return hd;
00068     }
00069     buf = (uByte*)hd.grptr;
00070 
00071     fseek(fp, rhd.ras_maplength, 1); 
00072 
00073     databyte = hd.xsize*((hd.depth+7)/8);
00074     linebyte = rhd.ras_length/hd.ysize;
00075 
00076     if (databyte==linebyte) {
00077         fread(buf, hd.lsize, 1, fp);
00078     }
00079     else {
00080         for (i=0; i<hd.ysize; i++) {
00081             fread(buf, databyte, 1, fp);
00082             fread(null, linebyte-databyte, 1, fp);
00083             buf += databyte;
00084         }
00085     }
00086 
00087     if (hd.depth==16){
00088         wptr = (sWord*)hd.grptr;
00089         for(i=0; i<hd.xsize*hd.ysize; i++) wptr[i] = ntohs(wptr[i]);
00090     }
00091 
00092     return hd;
00093 }
00094 
00095 
00096 
00109 int  jbxl::writeRasData(FILE* fp, CmnHead* hd, int obit)
00110 {
00111     RasHead shd;
00112     int  i, j, k, l, linebyte, databyte, depth, lsize;
00113     uByte null=0x00, *ptr, *buf;
00114 
00115     obit = Xabs(obit);
00116     if (hd->kind == RAS_DATA) {
00117         unsigned int u;
00118         memcpy((sByte*)&shd, hd->buf, hd->bsize);
00119         ptr = (uByte*)malloc(hd->lsize);
00120         if (ptr==NULL)  return JBXL_GRAPH_MEMORY_ERROR;
00121         lsize = hd->lsize;
00122         for (u=0; u<hd->lsize; u++) ptr[u] = hd->grptr[u];
00123     }
00124     else { 
00125         if (hd->depth==16) {
00126 
00127             if (obit==8) depth = 8;
00128             else         depth = 24;
00129 
00130             lsize = hd->xsize*hd->ysize*depth/8;
00131             buf = (uByte*)malloc(lsize);
00132             if (buf==NULL)  return JBXL_GRAPH_MEMORY_ERROR;
00133 
00134             if (obit==8) {
00135                 int  max = 255; 
00136                 uWord* wp = (uWord*)hd->grptr;
00137                 for (i=0; i<hd->xsize*hd->ysize; i++) {
00138                     max = Max(max, wp[i]);
00139                 }
00140                 for (i=0; i<hd->ysize*hd->xsize; i++) {
00141                     buf[i] = 255*wp[i]/max;
00142                 }
00143             }
00144             else {
00145                 k = l = 0;
00146                 for (i=0; i<hd->ysize*hd->xsize; i++) {
00147                     buf[k++] = hd->grptr[l++];
00148                     buf[k++] = hd->grptr[l++];
00149                     buf[k++] = null;
00150                 }
00151             }
00152         }
00153         else {
00154             depth = hd->depth;
00155             lsize = hd->lsize;
00156             buf   = (uByte*)hd->grptr;
00157         }
00158 
00159         databyte = hd->xsize*depth/8;
00160         linebyte = lsize/hd->ysize;
00161         if (linebyte%2==1) {
00162             linebyte++;
00163             lsize = linebyte*hd->ysize;
00164         }
00165 
00166         shd.ras_magic    = RAS_MAGIC;
00167         shd.ras_width    = hd->xsize;
00168         shd.ras_height   = hd->ysize;
00169         shd.ras_depth    = depth;
00170         shd.ras_length   = lsize;
00171         shd.ras_type     = RT_STANDARD;
00172         shd.ras_maptype  = RMT_NONE;
00173         shd.ras_maplength= 0;
00174 
00175         ptr = (uByte*)malloc(lsize);
00176         if (ptr==NULL) {
00177             if (hd->depth==16) free(buf);
00178             return -2;
00179         }
00180 
00181         k = l = 0;
00182         for (i=0 ; i<hd->ysize; i++) {
00183             for (j=0; j<databyte; j++)          ptr[k++] = buf[l++];
00184             for (j=0; j<linebyte-databyte; j++) ptr[k++] = null;
00185         }
00186 
00187         if (hd->depth==16) free(buf);
00188     }
00189 
00190     hton_st(&shd, 4);
00191     fwrite(&shd, sizeof(RasHead), 1, fp);
00192     fwrite(ptr, lsize, 1, fp);
00193 
00194     free(ptr);
00195     return  sizeof(RasHead)+lsize;
00196 }
00197 
00198 
00199 
00222 CmnHead  jbxl::readUserSetData(FILE* fp, CmnHead* chd, bool cnt)
00223 {
00224     CmnHead hd;
00225     init_CmnHead(&hd);
00226     CVCounter* counter = NULL;
00227 
00228     if (chd==NULL) {
00229         hd.xsize = JBXL_GRAPH_MEMORY_ERROR;
00230         return hd;
00231     }
00232     else if ((chd->kind&0x0ff)!=USERSET_DATA) {
00233         hd.xsize = JBXL_GRAPH_MEMORY_ERROR;
00234         return hd;
00235     }
00236     hd = *chd;
00237 
00238     
00239     fseek(fp, 0, 0);
00240     hd.buf = (uByte*)malloc(hd.bsize);
00241     if (hd.buf==NULL) {
00242         hd.xsize = JBXL_GRAPH_MEMORY_ERROR;
00243         hd.kind  = HEADER_NONE;
00244         return hd;
00245     }
00246     fread((void*)hd.buf, hd.bsize, 1, fp);
00247 
00248     
00249     if (hd.zsize>=10 && cnt) {
00250         counter = GetUsableGlobalCounter();
00251         if (counter!=NULL) {
00252             
00253             counter->SetMax((hd.zsize+1)/10);
00254             counter->SetPos(1);
00255         }
00256     }
00257 
00258     hd.grptr = (uByte*)malloc(hd.lsize);
00259     if (hd.grptr==NULL) {
00260         freeNull(hd.buf);
00261         hd.xsize = JBXL_GRAPH_MEMORY_ERROR;
00262         hd.kind  = HEADER_NONE;
00263         return hd;
00264     }
00265 
00266     fseek(fp, hd.bsize, 0);
00267 
00268     if (counter==NULL) {
00269         fread(hd.grptr, hd.lsize, 1, fp);
00270     }
00271     else {
00272         int psize = hd.xsize*hd.ysize*((hd.depth+7)/8);
00273         for (int i=0; i<hd.zsize; i++) {
00274             fread(hd.grptr+i*psize, psize, 1, fp);
00275             if (i%10==0) {
00276                 counter->StepIt();
00277                 if (counter->isCanceled()) {    
00278                     free_CmnHead(&hd);
00279                     hd.xsize = JBXL_GRAPH_CANCEL;
00280                     hd.kind  = HEADER_NONE;
00281                     return hd;
00282                 }
00283             }
00284         }
00285     }
00286 
00287     if (hd.depth==16) {
00288         if (checkBit(chd->kind, HAS_LENDIAN) && is_little_endian()) {
00289             
00290         }
00291         else {
00292             sWord* wptr = (sWord*)hd.grptr;
00293             for (int i=0; i<hd.xsize*hd.ysize*hd.zsize; i++) wptr[i] = ntohs(wptr[i]);
00294         }
00295     }
00296 
00297     if (counter!=NULL) counter->PutFill();
00298     return hd;
00299 }
00300 
00301 
00302 
00303 
00305 
00306 
00307 
00320 CmnHead  jbxl::readMoonFile(const char* fn, bool no_ntoh)
00321 {
00322     CmnHead hd;
00323     FILE* fp;
00324     unsigned int fsz;
00325 
00326     memset(&hd, 0, sizeof(CmnHead));
00327     hd.kind  = HEADER_NONE;
00328 
00329     fsz = file_size(fn);
00330     if (fsz==0) {
00331         hd.xsize = JBXL_GRAPH_NOFILE_ERROR;
00332         return hd;
00333     }
00334     if ((fp=fopen(fn,"rb"))==NULL) {
00335         hd.xsize = JBXL_GRAPH_OPFILE_ERROR;
00336         return hd;
00337     }
00338 
00339     hd = readMoonData(fp, fsz, no_ntoh);
00340     return hd;
00341 }
00342 
00343 
00344 
00358 CmnHead  jbxl::readMoonData(FILE* fp, unsigned int fsz, bool no_ntoh)
00359 {
00360     int i;
00361     CTHead chd;
00362     CmnHead hd;
00363 
00364     hd.kind  = HEADER_NONE;
00365     hd.bsize = 64;      
00366 
00367     
00368     fseek(fp, 0, 0);
00369     fread((void*)&chd, hd.bsize, 1, fp);
00370     ntoh_st(&chd, 2);
00371 
00372     hd.xsize = chd.xsize - chd.cutleft - chd.cutright;
00373     hd.ysize = chd.ysize - chd.cutup   - chd.cutdown;
00374     hd.zsize = 1;
00375     hd.depth = 16;
00376     hd.lsize = hd.xsize*hd.ysize*(hd.depth/8);
00377     hd.buf   = NULL;
00378     hd.grptr = NULL;
00379     if (fsz>0 && fsz!=hd.bsize+hd.lsize) return hd;
00380     
00381     hd.buf   = (uByte*)malloc(hd.bsize);
00382     if (hd.buf==NULL) {
00383         hd.xsize = JBXL_GRAPH_MEMORY_ERROR;
00384         return hd;
00385     }
00386     hd.kind  = MOON_DATA;
00387     memcpy(hd.buf, &chd, hd.bsize);
00388 
00389     hd.grptr = (uByte*)malloc(hd.lsize);
00390     if (hd.grptr==NULL) {
00391         freeNull(hd.buf);
00392         hd.xsize = JBXL_GRAPH_MEMORY_ERROR;
00393         hd.kind  = HEADER_NONE;
00394         return hd;
00395     }
00396 
00397     fseek(fp, hd.bsize, 0);
00398     fread(hd.grptr, hd.lsize, 1, fp);
00399 
00400     if (!no_ntoh) {
00401         sWord* wptr = (sWord*)hd.grptr;
00402         for (i=0; i<hd.xsize*hd.ysize; i++) wptr[i] = ntohs(wptr[i]);
00403     }
00404     return hd;
00405 }
00406 
00407 
00408 
00409 
00411 
00412 
00431 int  jbxl::dicomHeader(FILE* fp, int fsize, int* dsize, int* xsize, int* ysize, int* depth, double* rzxy)
00432 {
00433     int   i, j, sz, rp, ef;
00434     int   ln, hsize;
00435     int   xs = 0, ys = 0, dp = 0;
00436     double rz = 0.0;
00437 
00438 #ifdef WIN32
00439     uWord  cn[2];
00440 #else
00441     uWord  cn[2] __attribute__((may_alias));
00442 #endif
00443     uWord  dt;
00444     uWord* wp;
00445 
00446     if (fp==NULL || fsize<=0 || dsize==NULL) return JBXL_GRAPH_IVDARG_ERROR;
00447 
00448     *dsize = 0;
00449     if (rzxy!=NULL) *rzxy = 0.0;
00450     ef = OFF;
00451     fseek(fp, 0, 0);
00452 
00454     
00455     rp = (int)fread(&dt, sizeof(uWord), 1, fp);
00456     while(!feof(fp) && ef!=ON) {
00457         
00458         if (dt==DICOM_PIXEL_GROUP) {
00459             rp += (int)fread(&dt, sizeof(uWord), 1, fp);
00460             
00461             if (dt==DICOM_PIXEL_ELEMENT) {
00462                 rp += (int)fread(&cn, sizeof(uWord), 2, fp);
00463                 
00464                 if (cn[0]==DICOM_PIXCEL_VR && cn[1]==0x0000) {
00465                     rp += (int)fread(&cn, sizeof(uWord), 2, fp);
00466                 }
00467                 
00468                 int* sizep = (int*)cn;
00469                 *dsize = *sizep;
00470                 hsize = rp*sizeof(uWord);
00471                 if (fsize>=*dsize+hsize) {      
00472                     ef = ON;
00473                     break;
00474                 }
00475             }
00476         }
00477         rp += (int)fread(&dt, sizeof(uWord), 1, fp);
00478     }
00479 
00480     if (!ef) return 0;
00481 
00482     sz = rp*sizeof(uWord);
00483     wp = (uWord*)malloc(sz);
00484     if (wp==NULL) return JBXL_GRAPH_MEMORY_ERROR;
00485 
00487     
00488     fseek(fp, 0, 0);
00489     fread(wp, sz, 1, fp);
00490 
00491     for (i=0; i<sz/2-3; i++) {
00492         
00493         if (wp[i]==DICOM_IMAGE_GROUP) {
00494 
00495             
00496             if (wp[i+1]==DICOM_PXLSPC_ELEMENT) {
00497                 char   rx[LNAME], ry[LNAME];
00498                 uByte* bp;
00499 
00500                 memset(rx, 0, LNAME);
00501                 memset(ry, 0, LNAME);
00502 
00503                 if (wp[i+2]==DICOM_STR_VR) ln = wp[i+3];
00504                 else                       ln = *(int*)&wp[i+2];
00505 
00506                 if (ln<LNAME-1) {
00507                     bp = (uByte*)&wp[i+4];
00508                     j  = 0;
00509                     while (j<ln-1 && bp[j]!='\\') {
00510                         rx[j] = bp[j];
00511                         j++;
00512                     }
00513                     ln -= j + 1;
00514                     bp += j + 1;
00515                     j   = 0;
00516                     while (j<ln-1 && bp[j]!=' ') {
00517                         ry[j] = bp[j];
00518                         j++;
00519                     }
00520                     if (!strcmp(rx, ry)) {
00521                         rz = atof(rx);
00522                     } 
00523                 }
00524             }
00525 
00526             
00527             else if (wp[i+1]==DICOM_XSIZE_ELEMENT) {
00528                 if (wp[i+2]==DICOM_INT_VR) ln = wp[i+3];
00529                 else                       ln = *(int*)&wp[i+2];
00530                 if (ln==2) {
00531                     xs = (int)*(sWord*)&wp[i+4];
00532                 }
00533                 else if (ln==4) {
00534                     xs = *(int*)&wp[i+4];
00535                 }
00536             }
00537 
00538             
00539             else if (wp[i+1]==DICOM_YSIZE_ELEMENT) {
00540                 if (wp[i+2]==DICOM_INT_VR) ln = wp[i+3];
00541                 else                       ln = *(int*)&wp[i+2];
00542                 if (ln==2) {
00543                     ys = (int)*(sWord*)&wp[i+4];
00544                 }
00545                 else if (ln==4) {
00546                     ys = *(int*)&wp[i+4];
00547                 }
00548             }
00549 
00550             
00551             else if (wp[i+1]==DICOM_DEPTH_ELEMENT) {
00552                 if (wp[i+2]==DICOM_INT_VR) ln = wp[i+3];
00553                 else                       ln = *(int*)&wp[i+2];
00554                 if (ln==2) {
00555                     dp = (int)*(sWord*)&wp[i+4];
00556                 }
00557                 else if (ln==4) {
00558                     dp = *(int*)&wp[i+4];
00559                 }
00560             }
00561         }
00562     }
00563     free(wp);
00564     
00565     if (rzxy!=NULL)  *rzxy  = rz;
00566     if (xsize!=NULL) *xsize = xs;
00567     if (ysize!=NULL) *ysize = ys;
00568     if (depth!=NULL) *depth = dp;
00569         
00570     return sz;
00571 }
00572 
00573 
00574 
00585 MSGraph<sWord>   jbxl::readDicomFile(const char* fn)
00586 {
00587     int fsize;
00588     FILE*  fp;
00589     MSGraph<sWord> vp;
00590 
00591     memset(&vp, 0, sizeof(MSGraph<sWord>));
00592 
00593     fsize = file_size(fn);
00594     if (fsize<=0) {
00595         vp.xs = JBXL_GRAPH_NOFILE_ERROR;
00596         return vp;
00597     }
00598 
00599     if ((fp=fopen(fn,"r"))==NULL) {
00600         vp.xs = JBXL_GRAPH_OPFILE_ERROR;
00601         return vp;
00602     }
00603 
00604     vp = readDicomData(fp, fsize);
00605     fclose(fp);
00606 
00607     return vp;
00608 }
00609 
00610 
00611 
00612 MSGraph<sWord>   jbxl::readDicomData(FILE* fp, int fsize)
00613 {
00614     int   sz, dsize;
00615     int   xsize, ysize, depth;
00616     double rzxy;
00617     MSGraph<sWord> vp;
00618 
00619     memset(&vp, 0, sizeof(MSGraph<sWord>));
00620 
00621     sz = dicomHeader(fp, fsize, &dsize, &xsize, &ysize, &depth, &rzxy);
00622     if (sz<=0) {
00623         vp.xs = JBXL_GRAPH_HEADER_ERROR;
00624         return vp;
00625     }
00626 
00627     if (dsize!=xsize*ysize*((depth+7)/8)) {
00628         vp.xs = JBXL_GRAPH_HEADER_ERROR;
00629         return vp;
00630     }
00631 
00632     if ((depth+7)/8!=2) {
00633         vp.xs = JBXL_GRAPH_HEADER_ERROR;
00634         return vp;
00635     }
00636 
00637     vp.set(xsize, ysize, 1, 0, 0, rzxy);
00638 
00639     fseek(fp, sz, 0);
00640     fread(vp.gp, dsize, 1, fp);
00641 
00642     return vp;
00643 }
00644 
00645 
00646 
00647 
00649 
00650 
00652 
00653 
00666 CmnHead  jbxl::readXHead(const char* fn, CmnHead* chd)
00667 {
00668     FILE    *fp;
00669     int    fsz, hsz, csz;
00670     CmnHead hd;
00671 
00672     hsz = sizeof(CmnHead);
00673     memset(&hd, 0, hsz);
00674     hd.kind = HEADER_NONE;
00675 
00676     fsz = (int)file_size(fn);
00677     if (fsz<=0) {
00678         hd.xsize = JBXL_GRAPH_NOFILE_ERROR;
00679         return hd;
00680     }
00681 
00683     
00684     
00685     if (chd!=NULL && (chd->kind&0x00ff)==USERSET_DATA) {
00686         if (chd->zsize<=0) chd->zsize = 1;
00687         chd->lsize = chd->xsize*chd->ysize*chd->zsize*((chd->depth+7)/8);
00688         if (fsz==(int)(chd->bsize+chd->lsize)) {    
00689             return *chd;
00690         }
00691     }
00692 
00693     if ((fp=fopen(fn,"rb"))==NULL) {
00694         hd.xsize = JBXL_GRAPH_OPFILE_ERROR;
00695         return hd;
00696     }
00697 
00699     
00700     
00701     fseek(fp, 0, 0);
00702     fread(&hd, hsz, 1, fp);
00703     hd.grptr = NULL;
00704     ntoh_st(&hd, 4);
00705 
00706     
00707     if (hd.kind==RAS_MAGIC) {
00708         hd.kind = RAS_DATA;
00709         PRINT_MESG("readXHead: Sun Raster File\n");
00710         fclose(fp);
00711         hd.depth = hd.zsize;
00712         hd.zsize = 1;
00713         hd.lsize = file_size(fn) - sizeof(RasHead);
00714         hd.bsize = 0;
00715         hd.buf = NULL;
00716         hd.grptr = NULL;
00717         return hd;
00718     }
00719 
00720     
00721     if (hd.kind>0 && hd.kind<=NUM_KDATA && fsz==(int)(hsz+hd.bsize+hd.lsize)) {
00722         if (hd.zsize<=0) hd.zsize = 1;
00723         if (hd.bsize>0) {
00724             hd.buf = (uByte*)malloc(hd.bsize);
00725             if (hd.buf==NULL) {
00726                 free_CmnHead(&hd);
00727                 hd.xsize = JBXL_GRAPH_MEMORY_ERROR;
00728                 hd.kind  = HEADER_NONE;
00729                 fclose(fp);
00730                 return hd;
00731             }
00732 
00733             fseek(fp, hsz, 0);
00734             fread(hd.buf, hd.bsize, 1, fp);
00735             ntoh_ar((sWord*)hd.buf, hd.bsize);      
00736         }
00737 
00738         PRINT_MESG("readXHead: Common ヘッダ\n");
00739         PRINT_MESG("readXHead: ヘッダ種別     kind  = %d\n", hd.kind);
00740         PRINT_MESG("readXHead: ファイルサイズ fsz   = %d\n", fsz);
00741         PRINT_MESG("readXHead: ヘッダサイズ   hsz   = %d\n", sizeof(CmnHead));
00742         PRINT_MESG("readXHead: ヘッダバッファ bsize = %d\n", hd.bsize);
00743         PRINT_MESG("readXHead: データサイズ   lsize = %d\n", hd.lsize);
00744         PRINT_MESG("readXHead: サイズ         %dx%dx%d %d\n", hd.xsize, hd.ysize, hd.zsize, hd.depth);
00745         
00746         fclose(fp);
00747         return hd;
00748     }
00749     memset(&hd, 0, hsz);
00750 
00752     
00753     
00754     CTHead cthd;
00755     csz = sizeof(CTHead);
00756     fseek(fp, 0, 0);
00757     fread(&cthd, csz, 1, fp);
00758     ntoh_st(&cthd, 2);
00759 
00760     hd.xsize = cthd.xsize - cthd.cutleft - cthd.cutright;
00761     hd.ysize = cthd.ysize - cthd.cutup   - cthd.cutdown;
00762     hd.zsize = 1;
00763     hd.depth = 16;
00764     hd.bsize = csz;
00765     hd.lsize = hd.xsize*hd.ysize*hd.zsize*((hd.depth+7)/8);
00766 
00767     if (fsz==(int)(hd.bsize+hd.lsize)) {
00768         hd.kind = MOON_DATA;
00769         hd.buf  = (uByte*)malloc(csz);
00770         if (hd.buf==NULL) {
00771             free_CmnHead(&hd);
00772             hd.xsize = JBXL_GRAPH_MEMORY_ERROR;
00773             hd.kind  = HEADER_NONE;
00774             fclose(fp);
00775             return hd;
00776         }
00777         memcpy(hd.buf, &cthd, csz);
00778 
00779         PRINT_MESG("readXHead: CT ファイル\n");
00780         PRINT_MESG("readXHead: ヘッダ種別     kind  = %d\n", hd.kind);
00781         PRINT_MESG("readXHead: ファイルサイズ fsz   = %d\n", fsz);
00782         PRINT_MESG("readXHead: ヘッダサイズ   hsz   = %d\n", sizeof(CmnHead));
00783         PRINT_MESG("readXHead: ヘッダバッファ bsize = %d\n", hd.bsize);
00784         PRINT_MESG("readXHead: データサイズ   lsize = %d\n", hd.lsize);
00785         PRINT_MESG("readXHead: サイズ         %dx%dx%d %d\n", hd.xsize, hd.ysize, hd.zsize, hd.depth);
00786 
00787         fclose(fp);
00788         return hd;
00789     }
00790     memset(&hd, 0, hsz);
00791 
00793     
00794     
00795     int   ls, sz, dsize, xsize, ysize, depth;
00796     double rzxy;
00797 
00798     sz = dicomHeader(fp, fsz, &dsize, &xsize, &ysize, &depth, &rzxy);
00799     ls = xsize*ysize*((depth+7)/8);
00800     if (sz>0 && dsize==ls) {
00801         hd.kind  = DICOM_DATA;
00802         hd.xsize = xsize;
00803         hd.ysize = ysize;
00804         hd.zsize = 1;
00805         hd.depth = depth;
00806         hd.bsize = sizeof(CTHead);
00807         hd.lsize = ls;
00808         hd.buf   = (uByte*)malloc(hd.bsize);
00809         hd.grptr = NULL; 
00810                                  
00811         CTHead* pcthd = (CTHead*)hd.buf;
00812         memset(pcthd, 0, hd.bsize);
00813         pcthd->xsize  = hd.xsize;
00814         pcthd->ysize  = hd.ysize;
00815 
00816         if (rzxy>0.0) {
00817             pcthd->anydata[0] = (sWord)(rzxy*RZXY_RATE);
00818             pcthd->anydata[1] = RZXY_RATE;
00819             hd.kind |= HAS_RZXY;
00820         }
00821 
00822         fclose(fp);
00823         return hd;
00824     }
00825 
00827     
00828     
00829     fseek(fp, 0, 0);
00830     Buffer buf = read_Buffer_data(fp, 32);
00831 
00832 
00833     
00834     
00835 
00836 
00837 
00838 
00839 
00840 
00841 
00842 
00843 
00844 
00845 
00846 
00847 
00848 
00849 
00850 
00851 
00852 #ifdef  ENABLE_JPEGLIB
00853     
00854     if (isJPEGHeader(buf)) {
00855         PRINT_MESG("readXHead: JPEGデータ形式\n");
00856         hd.kind = JPEG_RGB_DATA;    
00857         fclose(fp);
00858         free_Buffer(&buf);
00859         return hd;
00860     }
00861 #endif
00862 
00863     free_Buffer(&buf);
00864 
00866     
00867     
00868     PRINT_MESG("readXHead: 未知のデータ形式\n");
00869     memset(&hd, 0, hsz);
00870     hd.kind  = UN_KNOWN_DATA;
00871     hd.lsize = fsz;
00872 
00873     fclose(fp);
00874     return hd;
00875 }
00876 
00877 
00878 
00909 CmnHead  jbxl::readXHeadFile(const char* fn, CmnHead* chd, bool cnt)
00910 {
00911     FILE* fp;
00912     int  fsz, hsz;
00913     bool  no_ntoh = false;
00914     CmnHead hd;
00915     CVCounter* counter = NULL;
00916 
00917     memset(&hd, 0, sizeof(CmnHead));
00918     hd.kind = HEADER_NONE;
00919 
00920     fsz = (int)file_size(fn);
00921     if (fsz==0) {
00922         hd.xsize = JBXL_GRAPH_NOFILE_ERROR;
00923         return hd;
00924     }
00925 
00926     if ((fp=fopen(fn,"rb"))==NULL) {
00927         hd.xsize = JBXL_GRAPH_OPFILE_ERROR;
00928         return hd;
00929     }
00930 
00932     
00933     
00934     if (chd!=NULL) {
00935         
00936         if (checkBit(chd->kind, HAS_LENDIAN) && is_little_endian()) {
00937             no_ntoh = true;
00938         }
00939 
00940         
00941         if ((chd->kind&0x00ff)==USERSET_DATA) {
00942             if (chd->zsize<=0) chd->zsize = 1;
00943             chd->lsize = chd->xsize*chd->ysize*chd->zsize*((chd->depth+7)/8);
00944             if (fsz==(int)(chd->bsize+chd->lsize)) {        
00945                 PRINT_MESG("readXHeadFile: オペレータ指定のデータ形式\n");
00946                 hd = readUserSetData(fp, chd, true);        
00947 
00948                 
00949                 if (hd.depth==16 && !no_ntoh) {
00950                     sWord* wptr = (sWord*)hd.grptr;
00951                     for (int i=0; i<hd.xsize*hd.ysize*hd.zsize; i++) {
00952                         wptr[i] = ntohs(wptr[i]);
00953                     }
00954                 }
00955 
00956                 fclose(fp);
00957                 return hd;
00958             }
00959         }
00960     }
00961 
00963     
00964     
00965     hsz = sizeof(CmnHead);
00966     fseek(fp, 0, 0);
00967     fread(&hd, hsz, 1, fp);
00968     ntoh_st(&hd, 4);
00969 
00970     
00971     if (hd.kind==RAS_MAGIC) {
00972         PRINT_MESG("readXHeadFile: Sun Rasterデータ形式\n");
00973         hd = readRasData(fp); 
00974         fclose(fp);
00975         return hd;
00976     }
00977 
00978     
00979     
00980     if (hd.kind>0 && hd.kind<=NUM_KDATA && fsz==(int)(hsz+hd.bsize+hd.lsize)) {
00981         PRINT_MESG("readXHeadFile: Commonデータ形式\n");
00982         if (hd.zsize<=0) hd.zsize = 1;
00983 
00984         
00985         if (hd.zsize>=10 && cnt) {
00986             counter = GetUsableGlobalCounter();
00987             if (counter!=NULL) {
00988                 
00989                 counter->SetMax(hd.zsize/10);
00990                 counter->SetPos(0);
00991             }
00992         }
00993         
00994         if (hd.bsize>0) hd.buf = (uByte*)malloc(hd.bsize);
00995         hd.grptr = (uByte*)malloc(hd.lsize);
00996         if ((hd.bsize>0&&hd.buf==NULL) || hd.grptr==NULL) {
00997             free_CmnHead(&hd);
00998             hd.xsize = JBXL_GRAPH_MEMORY_ERROR;
00999             hd.kind  = HEADER_NONE;
01000             fclose(fp);
01001             return hd;
01002         }
01003         fseek(fp, hsz, 0);
01004         if (hd.bsize>0) {
01005             fread(hd.buf, hd.bsize, 1, fp);
01006             ntoh_ar((sWord*)hd.buf, hd.bsize);
01007         }
01008         
01009         
01010         if (hd.lsize==0) {
01011             fclose(fp);
01012             if (counter!=NULL) counter->PutFill();
01013             return hd;
01014         }
01015 
01016         
01017         if (counter==NULL) {
01018             fread(hd.grptr, hd.lsize, 1, fp);
01019         }
01020         else {
01021             int psize = hd.xsize*hd.ysize*((hd.depth+7)/8);
01022             for (int i=0; i<hd.zsize; i++) {
01023                 fread(hd.grptr+i*psize, psize, 1, fp);
01024                 if (i%10==0) {
01025                     counter->StepIt();
01026                     if (counter->isCanceled()) {    
01027                         
01028                         free_CmnHead(&hd);
01029                         hd.xsize = JBXL_GRAPH_CANCEL;
01030                         hd.kind  = HEADER_NONE;
01031                         fclose(fp);
01032                         return hd;
01033                     }
01034                 }
01035             }
01036         }
01037 
01038         
01039         if (hd.depth==16 && !no_ntoh) {
01040             sWord* wptr = (sWord*)hd.grptr;
01041             for (int i=0; i<hd.xsize*hd.ysize*hd.zsize; i++) {
01042                 wptr[i] = ntohs(wptr[i]);
01043             }
01044         }
01045 
01046         fclose(fp);
01047         if (counter!=NULL) counter->PutFill();
01048         return hd;
01049     }
01050     memset(&hd, 0, hsz);
01051 
01053     
01054     
01055     hd = readMoonData(fp, fsz, no_ntoh);
01056     if ((hd.kind & 0x00ff)==MOON_DATA) {
01057         PRINT_MESG("readXHeadFile: Moonデータ形式\n");
01058         fclose(fp);
01059         return hd;
01060     }
01061     memset(&hd, 0, hsz);
01062 
01064     
01065     
01066     MSGraph<sWord> vp = readDicomData(fp, fsz);
01067     if (vp.gp!=NULL) {
01068         PRINT_MESG("readXHeadFile: DICOMデータ形式\n");
01069         hd.kind  = DICOM_DATA;
01070         hd.xsize = vp.xs;
01071         hd.ysize = vp.ys;
01072         hd.zsize = vp.zs;
01073         hd.depth = 16;
01074         hd.bsize = sizeof(CTHead);
01075         hd.lsize = vp.xs*vp.ys*vp.zs*2;
01076         hd.buf   = (uByte*)malloc(hd.bsize);
01077         hd.grptr = (uByte*)vp.gp;
01078                 
01079         CTHead* pcthd = (CTHead*)hd.buf;
01080         memset(pcthd, 0, hd.bsize);
01081         pcthd->xsize  = (sWord)hd.xsize;
01082         pcthd->ysize  = (sWord)hd.ysize;
01083         
01084         
01085         
01086         
01087         
01088         
01089         
01090         if (vp.RZxy>0.0) {
01091             pcthd->anydata[0] = (sWord)(vp.RZxy*RZXY_RATE);
01092             pcthd->anydata[1] = RZXY_RATE;
01093             hd.kind |= HAS_RZXY;
01094         }
01095 
01096         if (!no_ntoh) {
01097             sWord* wptr = (sWord*)hd.grptr;
01098             for (int i=0; i<hd.xsize*hd.ysize*hd.zsize; i++) {
01099                 wptr[i] = ntohs(wptr[i]);
01100             }
01101         }
01102 
01103         fclose(fp);
01104         return hd;
01105     }
01106 
01108     
01109     
01110     fseek(fp, 0, 0);
01111     Buffer buf = read_Buffer_data(fp, 32);
01112 
01114     
01115     
01116 
01117 
01118 
01119 
01120 
01121 
01122 
01123 
01124 
01125 
01126 
01127 
01128 
01129 
01130 
01131 
01132 
01133 
01134 #ifdef  ENABLE_JPEGLIB
01135     
01136     if (isJPEGHeader(buf)) {
01137         PRINT_MESG("readXHeadFile: JPEGデータ形式\n");
01138         JPEGImage jpg = readJPEGData(fp);
01139         hd = JPEGImage2CmnHead(jpg);
01140 
01141         if (hd.zsize==1) hd.kind = JPEG_MONO_DATA;
01142         jpg.free();
01143         free_Buffer(&buf);
01144         fclose(fp);
01145         return hd;
01146     }
01147 #endif
01148 
01149     free_Buffer(&buf);
01150 
01152     
01153     
01154     PRINT_MESG("readXHeadFile: 未知のデータ形式\n");
01155     memset(&hd, 0, hsz);
01156 
01157     hd.grptr = (uByte*)malloc(fsz);
01158     if (hd.grptr==NULL) {
01159         hd.xsize = JBXL_GRAPH_MEMORY_ERROR;
01160         hd.kind  = HEADER_NONE;
01161         fclose(fp);
01162         return hd;
01163     }
01164     fseek(fp, 0, 0);
01165     fread(hd.grptr, fsz, 1, fp);
01166 
01167     hd.kind  = UN_KNOWN_DATA;
01168     hd.lsize = fsz;
01169 
01170     fclose(fp);
01171     return hd;
01172 }
01173 
01174 
01175 
01202 CmnHead  jbxl::readCmnHeadFile(const char* fn, CmnHead* chd, bool cnt)
01203 {
01204     FILE* fp;
01205     int  kind, fsz, hsz;
01206     bool  no_ntoh = false;
01207     CmnHead hd;
01208     CVCounter* counter = NULL;
01209 
01210     memset(&hd, 0, sizeof(CmnHead));
01211     hd.kind  = HEADER_NONE;
01212     hd.xsize = JBXL_GRAPH_HEADER_ERROR;
01213 
01214     if (chd==NULL) return hd;
01215 
01216     fsz = (int)file_size(fn);
01217     if (fsz==0) {
01218         hd.xsize = JBXL_GRAPH_NOFILE_ERROR;
01219         return hd;
01220     }
01221 
01222     if ((fp=fopen(fn,"rb"))==NULL) {
01223         hd.xsize = JBXL_GRAPH_OPFILE_ERROR;
01224         return hd;
01225     }
01226 
01227     
01228     if (checkBit(chd->kind, HAS_LENDIAN) && is_little_endian()) {
01229         no_ntoh = true;
01230     }
01231     kind = chd->kind & 0x00ff;
01232 
01233     
01234     if (kind == USERSET_DATA) {
01235         if (chd->zsize<=0) chd->zsize = 1;
01236         chd->lsize = chd->xsize*chd->ysize*chd->zsize*((chd->depth+7)/8);
01237         if (fsz==(int)(chd->bsize+chd->lsize)) {        
01238             PRINT_MESG("readCmnHeadFile: オペレータ指定のデータ形式\n");
01239             hd = readUserSetData(fp, chd, true);        
01240 
01241             
01242             if (hd.depth==16 && !no_ntoh) {
01243                 sWord* wptr = (sWord*)hd.grptr;
01244                 for (int i=0; i<hd.xsize*hd.ysize*hd.zsize; i++) {
01245                     wptr[i] = ntohs(wptr[i]);
01246                 }
01247             }
01248         }
01249     }
01250 
01251     
01252     else if (kind==DICOM_DATA) {
01253         MSGraph<sWord> vp = readDicomData(fp, fsz);
01254         if (vp.gp!=NULL) {
01255             PRINT_MESG("readCmnHeadFile: DICOMデータ形式\n");
01256             hd.kind  = DICOM_DATA;
01257             hd.xsize = vp.xs;
01258             hd.ysize = vp.ys;
01259             hd.zsize = vp.zs;
01260             hd.depth = 16;
01261             hd.bsize = sizeof(CTHead);
01262             hd.lsize = vp.xs*vp.ys*vp.zs*2;
01263             hd.buf   = (uByte*)malloc(hd.bsize);
01264             hd.grptr = (uByte*)vp.gp;
01265                 
01266             CTHead* pcthd = (CTHead*)hd.buf;
01267             memset(pcthd, 0, hd.bsize);
01268             pcthd->xsize  = (sWord)hd.xsize;
01269             pcthd->ysize  = (sWord)hd.ysize;
01270             
01271             
01272             
01273             
01274             
01275             
01276 
01277             if (vp.RZxy>0.0) {
01278                 pcthd->anydata[0] = (sWord)(vp.RZxy*RZXY_RATE);
01279                 pcthd->anydata[1] = RZXY_RATE;
01280                 hd.kind |= HAS_RZXY;
01281             }
01282 
01283             if (!no_ntoh) {
01284                 sWord* wptr = (sWord*)hd.grptr;
01285                 for (int i=0; i<hd.xsize*hd.ysize*hd.zsize; i++) {
01286                     wptr[i] = ntohs(wptr[i]);
01287                 }
01288             }
01289         }
01290     }
01291 
01292     
01293     else if (kind==RAS_DATA) {
01294         PRINT_MESG("readCmnHeadFile: Sun Rasterデータ形式\n");
01295         hd = readRasData(fp); 
01296     }
01297     
01298     
01299     else if (kind==MOON_DATA) {
01300         PRINT_MESG("readCmnHeadFile: Moonデータ形式\n");
01301         hd = readMoonData(fp, fsz, no_ntoh);
01302     }
01303 
01304 #ifdef  ENABLE_JPEGLIB
01305     
01306     else if (kind==JPEG_RGB_DATA || kind==JPEG_MONO_DATA) {
01307         PRINT_MESG("readCmnHeadFile: JPEGデータ形式\n");
01308         JPEGImage jpg = readJPEGData(fp);
01309         hd = JPEGImage2CmnHead(jpg);
01310         jpg.free();
01311     }
01312 #endif
01313 
01314     
01315     
01316 
01317 
01318 
01319     
01320     else if (kind!=UN_KNOWN_DATA) {
01321         
01322         PRINT_MESG("readCmnHeadFile: Commonデータ形式\n");
01323 
01324         hsz = sizeof(CmnHead);
01325         fseek(fp, 0, 0);
01326         fread(&hd, hsz, 1, fp);
01327         ntoh_st(&hd, 4);
01328         if (hd.zsize<=0) hd.zsize = 1;
01329 
01330         
01331         if (hd.zsize>=10 && cnt) {
01332             counter = GetUsableGlobalCounter();
01333             if (counter!=NULL) {
01334                 
01335                 counter->SetMax(hd.zsize/10);
01336                 counter->SetPos(0);
01337             }
01338         }
01339 
01340         if (hd.bsize>0) hd.buf = (uByte*)malloc(hd.bsize);
01341         hd.grptr = (uByte*)malloc(hd.lsize);
01342         if ((hd.bsize>0&&hd.buf==NULL) || hd.grptr==NULL) {
01343             free_CmnHead(&hd);
01344             hd.xsize = JBXL_GRAPH_MEMORY_ERROR;
01345             hd.kind  = HEADER_NONE;
01346             fclose(fp);
01347             return hd;
01348         }
01349         fseek(fp, hsz, 0);
01350         if (hd.bsize>0) {
01351             fread(hd.buf, hd.bsize, 1, fp);
01352             ntoh_ar((sWord*)hd.buf, hd.bsize);
01353         }
01354         
01355         
01356         if (hd.lsize==0) {
01357             if (counter!=NULL) counter->PutFill();
01358         }
01359         else {
01360             
01361             if (counter==NULL) {
01362                 fread(hd.grptr, hd.lsize, 1, fp);
01363             }
01364             else {
01365                 int psize = hd.xsize*hd.ysize*((hd.depth+7)/8);
01366                 for (int i=0; i<hd.zsize; i++) {
01367                     fread(hd.grptr+i*psize, psize, 1, fp);
01368                     if (i%10==0) {
01369                         counter->StepIt();
01370                         if (counter->isCanceled()) {    
01371                             
01372                             free_CmnHead(&hd);
01373                             hd.xsize = JBXL_GRAPH_CANCEL;
01374                             hd.kind  = HEADER_NONE;
01375                             fclose(fp);
01376                             return hd;
01377                         }
01378                     }
01379                 }
01380             }
01381 
01382             
01383             if (hd.depth==16 && !no_ntoh) {
01384                 sWord* wptr = (sWord*)hd.grptr;
01385                 for (int i=0; i<hd.xsize*hd.ysize*hd.zsize; i++) {
01386                     wptr[i] = ntohs(wptr[i]);
01387                 }
01388             }
01389 
01390             if (counter!=NULL) counter->PutFill();
01391         }
01392     }
01393 
01394     else {
01395         PRINT_MESG("readCmnHeadFile: 未知のデータ形式が指定された.(%04x, %04x)\n", chd->kind, (uWord)kind);
01396     }
01397 
01398     fclose(fp);
01399     return hd;
01400 }
01401 
01402 
01403 
01431 int  jbxl::writeCmnHeadFile(const char* fn, CmnHead* hd, bool cnt)
01432 {
01433     CmnHead cmd;
01434     FILE* fp;
01435     int  csize, psize;
01436     int  kind = hd->kind & 0x0ff;
01437 
01438     if (kind==UN_KNOWN_DATA) return JBXL_GRAPH_HEADER_ERROR;
01439     if (hd==NULL || hd->lsize==0) return JBXL_GRAPH_IVDARG_ERROR;
01440 
01441     if ((fp=fopen(fn,"wb"))==NULL) {
01442         PRINT_MESG("WRITECMNHEADFILE: エラー:ファイルオープン失敗\n");
01443         return JBXL_GRAPH_OPFILE_ERROR;
01444     }
01445     PRINT_MESG("writeCmnHeadFile: ファイル種別 = %d で書き込み中.%dx%dx%d\n", hd->kind, hd->xsize, hd->ysize, hd->zsize);
01446 
01447     
01448     csize = writeCmnHeadData(fp, hd, cnt);
01449     fclose(fp);
01450     if (csize<0) return (int)csize;
01451     
01452     
01453     if (kind==RAS_DATA) return csize;
01454 
01455     
01456     if (kind==JPEG_RGB_DATA   || kind==JPEG_MONO_DATA) return csize;
01457     if (kind==JPEG_ARGB_DATA  || kind==JPEG_RGBA_DATA) return csize;
01458     if (kind==JPEG16_RGB_DATA || kind==JPEG16_ARGB_DATA || kind==JPEG16_RGBA_DATA) return csize;
01459 
01460     
01461     
01462 
01463     
01464     int fsz = (int)file_size(fn);
01465     if (kind==MOON_DATA) psize = hd->bsize+hd->lsize;
01466     else psize = sizeof(CmnHead)+hd->bsize+hd->lsize;
01467 
01468     if (fsz!=psize) {
01469         PRINT_MESG("WRITECMNHEADFILE: エラー:書き込みファイルのサイズが合わない %d != %d\n", psize, fsz);
01470         return JBXL_GRAPH_WRFILE_ERROR;
01471     }
01472     if (kind==MOON_DATA) return psize;
01473 
01474     if ((fp=fopen(fn,"rb"))==NULL) {
01475         PRINT_MESG("WRITECMNHEADFILE: エラー:ファイル検査:再オープン失敗\n");
01476         return JBXL_GRAPH_RDFILE_ERROR;
01477     }
01478  
01479     fread((sByte*)&cmd, sizeof(CmnHead), 1, fp);
01480     ntoh_st(&cmd, 4);
01481     if (cmd.xsize!=hd->xsize || cmd.ysize!=hd->ysize || cmd.zsize!=hd->zsize ||
01482         cmd.bsize!=hd->bsize || cmd.lsize!=hd->lsize || cmd.depth!=hd->depth || cmd.kind!=hd->kind) {
01483         PRINT_MESG("WRITECMNHEADFILE: エラー:ファイルヘッダ検査:ヘッダ異常\n");
01484         fclose(fp);
01485         return JBXL_GRAPH_HEADER_ERROR;
01486     }
01487 
01488     fclose(fp);
01489     return  csize;
01490 }
01491 
01492 
01493 
01519 int  jbxl::writeCmnHeadData(FILE* fp, CmnHead* hd, bool cnt)
01520 {
01521     CTHead  chd;
01522     CmnHead cmd;
01523     CVCounter* counter = NULL;
01524     sByte* ptr;
01525     int i, j, k, l;
01526     int csize, psize;
01527     int kind = hd->kind & 0x00ff;
01528 
01529     if (kind==UN_KNOWN_DATA) return JBXL_GRAPH_HEADER_ERROR;
01530 
01531     
01532     if (kind==RAS_DATA) {
01533         
01534         csize = writeRasData(fp, hd, 8);
01535         return csize;
01536     }
01537 
01538 #ifdef  ENABLE_JPEGLIB
01539     
01540     if (kind==JPEG_RGB_DATA   || kind==JPEG_MONO_DATA   || kind==JPEG_ARGB_DATA || kind==JPEG_RGBA_DATA ||
01541         kind==JPEG16_RGB_DATA || kind==JPEG16_ARGB_DATA || kind==JPEG16_RGBA_DATA) {
01542         JPEGImage jpg = CmnHead2JPEGImage(*hd);
01543         csize = writeJPEGData(fp, jpg, 100);
01544         jpg.free();
01545         return csize;
01546     }
01547 #endif
01548 
01549     
01550     
01551 
01552 
01553 
01554 
01556     
01557     if (hd->zsize<=0) hd->zsize = 1;
01558     if (hd->depth<=0) hd->depth = 16;
01559 
01560     psize = hd->xsize*hd->ysize*((hd->depth+7)/8); 
01561     hd->lsize = psize*hd->zsize;
01562     ptr = (sByte*)malloc(hd->lsize);
01563     if (ptr==NULL) return JBXL_GRAPH_MEMORY_ERROR;
01564 
01565     
01566     if (kind==CT_DATA || kind==CT_3DM || kind==CT_3D_VOL) {
01567         PRINT_MESG("writeCmnHeadData: CTデータ\n");
01568         PRINT_MESG("writeCmnHeadData: ヘッダバッファ bsize = %d\n", hd->bsize);
01569         PRINT_MESG("writeCmnHeadData: データサイズ   lsize = %d\n", hd->lsize);
01570         PRINT_MESG("writeCmnHeadData: サイズ         %dx%dx%d %d\n", hd->xsize, hd->ysize, hd->zsize, hd->depth);
01571 
01572         memcpy(&chd, hd->buf, hd->bsize);
01573         chd.anydata[2] += TempBase;
01574         hton_st(&chd, 2);
01575 
01576         memcpy(ptr, hd->grptr, hd->lsize);
01577     }
01578     else if (kind==MOON_DATA || kind==USERSET_DATA) {   
01579         chd.xsize   = htons((sWord)hd->xsize);
01580         chd.ysize   = htons((sWord)hd->ysize);
01581         chd.ctmin   = 0;
01582         chd.ctmax   = 0;
01583         chd.cutup   = 0;
01584         chd.cutdown = 0;
01585         chd.cutleft = 0;
01586         chd.cutright= 0;
01587 
01588         k = l = 0;
01589         if (hd->depth<16){   
01590             for (i=0; i<hd->xsize*hd->ysize; i++) {
01591                 for (j=0; j<hd->depth/8; j++) ptr[k++] = 0x00;
01592                 for (j=hd->depth/8; j<2; j++) ptr[k++] = hd->grptr[l++];
01593             }
01594         }
01595         else if (hd->depth==16) {
01596             for (i=0; i<hd->xsize*hd->ysize; i++) {
01597                 for (j=0; j<2; j++) ptr[k++] = hd->grptr[l++];
01598             }
01599         }
01600         else {
01601             for (i=0; i<hd->xsize*hd->ysize; i++) {
01602                 for (j=0; j<2; j++) ptr[k++] = hd->grptr[l++];
01603                 l += (hd->depth)/8 - 2;
01604             }
01605         }
01606     }
01607     else {
01608         PRINT_MESG("writeCmnHeadData: サポートしていないヘッダタイプ %d\n", hd->kind);
01609         free(ptr);
01610         return 0;
01611     }
01612 
01614     
01615     csize = 0;
01616     
01617     if (kind==CT_DATA || kind==CT_3DM || kind==CT_3D_VOL) {
01618         
01619         if (hd->zsize>=10 && cnt) {
01620             counter = GetUsableGlobalCounter();
01621             if (counter!=NULL) {
01622                 
01623                 counter->SetMax((hd->zsize+1)/10);
01624                 counter->SetPos(1);
01625             }
01626         }
01627 
01628         cmd = *hd;
01629         cmd.grptr = NULL;
01630         hton_st(&cmd, 4);
01631         csize = sizeof(CmnHead);
01632         fwrite(&cmd, csize, 1, fp);
01633     }
01634 
01635     
01636     fwrite(&chd, sizeof(CTHead), 1, fp);
01637     csize += sizeof(CTHead);
01638 
01639     
01640     hton_ar((sWord*)ptr, hd->lsize);
01641 
01642     if (counter==NULL) {
01643         fwrite(ptr, hd->lsize, 1, fp);
01644         csize += hd->lsize;
01645     }
01646     else {
01647         for (i=0; i<hd->zsize; i++) {
01648             fseek(fp, csize, 0);
01649             fwrite(ptr+i*psize, psize, 1, fp);
01650             csize += psize;
01651             if (i%10==0) {
01652                 counter->StepIt();
01653                 if (counter->isCanceled()) {    
01654                     free(ptr);
01655                     return JBXL_GRAPH_CANCEL;
01656                 }
01657             }
01658         }
01659     }
01660 
01661     free(ptr);
01662     if (counter!=NULL) counter->PutFill();
01663 
01664     return  csize;
01665 }
01666 
01667