00001 
00011 #include "gio.h"
00012 #include "jbxl_state.h"
00013 
00014 
00015 IRBound  ExRBound;
00016 
00017 
00019 
00020 
00036 int  write_wsg_file(const char* fname, WSGraph gr)
00037 {
00038     CmnHead hd;
00039     unsigned int  i;
00040 
00041     hd.xsize = gr.xs;
00042     hd.ysize = gr.ys;
00043     hd.zsize = gr.zs;
00044     hd.depth = 16;
00045     hd.grptr = (uByte*)gr.gp;
00046     hd.kind  = CT_3DM;
00047 
00048     if (hd.zsize<=1){
00049         hd.zsize = 1;
00050         hd.kind  = CT_DATA;
00051     }
00052     hd.lsize = (hd.depth+7)/8*hd.zsize*hd.ysize*hd.zsize;
00053     hd.bsize = sizeof(CTHead);
00054     hd.buf   = (uByte*)malloc(hd.bsize);
00055     if (hd.buf==NULL) {
00056         
00057         return JBXL_GRAPH_MEMORY_ERROR;
00058     }
00059 
00060     for (i=0; i<hd.bsize; i++) *(hd.buf+i) = (sByte)0;
00061     int ret = write_ct_file(fname, &hd);
00062 
00063     free(hd.buf);
00064     return ret;
00065 }
00066 
00067 
00068 
00081 int  write_ras_file(const char* fname, WSGraph gr)
00082 {
00083     CmnHead hd;
00084 
00085     init_CmnHead(&hd);
00086     hd.xsize = gr.xs;
00087     hd.ysize = gr.ys;
00088     hd.zsize = gr.zs;
00089     hd.depth = 16;
00090     hd.grptr = (uByte*)gr.gp;
00091 
00092     if (hd.zsize>1) {
00093         
00094         return JBXL_GRAPH_HEADER_ERROR;
00095     }
00096     else {
00097         hd.zsize = Max(hd.zsize, 1);
00098         hd.kind  = CT_DATA;
00099         hd.lsize = (hd.depth+7)/8*hd.xsize*hd.ysize*hd.zsize;
00100         hd.bsize = sizeof(CTHead);
00101         hd.buf   = (uByte*)malloc(hd.bsize);
00102         if (hd.buf==NULL) {
00103             
00104             return JBXL_GRAPH_MEMORY_ERROR;
00105         }
00106         memset(hd.buf, 0, hd.bsize);
00107     }
00108 
00109     int ret = write_ras_file_obit(fname, &hd, 8);
00110 
00111     free(hd.buf);
00112     return ret;
00113 }
00114 
00115 
00116 
00133 int  write_file_rb(const char* fname, WSGraph gr, IRBound rb)
00134 {
00135     CmnHead hd;
00136     CTHead* chd;
00137     unsigned int  i;
00138 
00139     init_CmnHead(&hd);
00140     hd.xsize = gr.xs;
00141     hd.ysize = gr.ys;
00142     hd.zsize = gr.zs;
00143     hd.depth = 16;
00144     hd.grptr = (uByte*)gr.gp;
00145     hd.kind  = CT_3DM | HAS_RBOUND;
00146 
00147     if (hd.zsize<=0) {
00148         hd.zsize = 1;
00149         hd.kind  = CT_DATA | HAS_RBOUND;
00150     }
00151     hd.lsize = (hd.depth+7)/8*hd.zsize*hd.ysize*hd.zsize;
00152     hd.bsize = sizeof(CTHead);
00153     hd.buf   = (uByte*)malloc(hd.bsize);
00154     if (hd.buf==NULL) {
00155         
00156         return JBXL_GRAPH_MEMORY_ERROR;
00157     }
00158 
00159     for (i=0; i<hd.bsize; i++) *(hd.buf+i) = (sByte)0;
00160     chd = (CTHead*)hd.buf;
00161     chd->cutleft  = rb.xmin;
00162     chd->cutright = rb.xmax;
00163     chd->cutup    = rb.ymin;
00164     chd->cutdown  = rb.ymax;
00165     chd->ctmin    = rb.zmin;
00166     chd->ctmax    = rb.zmax;
00167 
00168     if (RZxy!=1.0 && RZxy<RZXY_RATE && chk_RZxy()) {
00169         chd->anydata[0] = (sWord)(RZxy*RZXY_RATE);
00170         chd->anydata[1] = (sWord)RZXY_RATE;
00171         hd.kind        |= HAS_RZXY;
00172     }       
00173             
00174     if (ZeroBase!=0) {
00175         chd->anydata[2] = (sWord)ZeroBase;
00176         hd.kind        |= HAS_BASE;
00177     }   
00178 
00179     int ret = write_ct_file(fname, &hd);
00180 
00181     free(hd.buf);
00182     return ret;
00183 }
00184 
00185 
00186 
00203 WSGraph  read_wsg_file(const char* fname)
00204 {
00205     WSGraph  gr;
00206     CmnHead  hd;
00207     int  i, xs, ys, zs;
00208     double rzm;
00209     sWord* rz;
00210 
00211     memset(&gr, 0, sizeof(WSGraph));
00212 
00213     hd = read_xxx_file(fname);
00214     if (hd.kind==HEADER_NONE) {
00215         
00216         gr.state = JBXL_GRAPH_HEADER_ERROR;
00217         return gr;
00218     }
00219 
00220     if (checkBit(hd.kind, HAS_RBOUND)) {
00221         
00222         
00223         
00224         gr = read_wsg_file_rb(fname, &ExRBound);
00225         return gr;
00226     }
00227     
00228     xs = hd.xsize;
00229     ys = hd.ysize;
00230     zs = Max(hd.zsize, 1);
00231 
00232     if (hd.depth==16){
00233         gr.gp = (sWord*)hd.grptr;
00234     }
00235     else if (hd.depth<16){
00236         gr.gp = (sWord*)malloc(xs*ys*zs*sizeof(sWord));
00237         if (gr.gp==NULL) {
00238             
00239             free_CmnHead(&hd);
00240             gr.state = JBXL_GRAPH_MEMORY_ERROR;
00241             return gr;
00242         }
00243         for (i=0; i<xs*ys*zs; i++) gr.gp[i] = (uByte)hd.grptr[i];
00244         free(hd.grptr);
00245     }
00246     else if (hd.depth>16){
00247         gr.gp = (sWord*)malloc(xs*ys*zs*sizeof(sWord));
00248         if (gr.gp==NULL) {
00249             
00250             free_CmnHead(&hd);
00251             gr.state = JBXL_GRAPH_MEMORY_ERROR;
00252             return gr;
00253         }
00254         for (i=0;i<xs*ys*zs;i++) gr.gp[i]=(sWord)((uByte)hd.grptr[i]>>(hd.depth-15));
00255         free(hd.grptr);
00256     }
00257 
00258     gr.xs = xs;
00259     gr.ys = ys;
00260     gr.zs = zs;
00261     gr.state = JBXL_NORMAL;
00262 
00263     if (hd.bsize>0 && checkBit(hd.kind, CT_DATA)) {
00264         rz  = (sWord*)hd.buf;
00265         if (rz[9]==RZXY_RATE || checkBit(hd.kind, HAS_RZXY)) {
00266             rzm = (double)(rz[8])/rz[9];
00267             if (rzm<1.0 && rzm>0.1) set_RZxy(rzm);
00268             
00269         }
00270         if (rz[10]!=0 && checkBit(hd.kind, HAS_BASE)) {
00271             ZeroBase = rz[10];
00272         }
00273     }
00274     free(hd.buf);
00275 
00276     return  gr;
00277 }
00278 
00279 
00280 
00294 WSGraph  read_ras_file(const char* fn)
00295 {
00296     int  i, sz;
00297     FILE *fp;
00298     CmnHead hd;
00299     WSGraph gd;
00300     size_t rs;
00301     UNUSED(rs);
00302 
00303     memset(&gd, 0, sizeof(WSGraph));
00304 
00305     if ((fp=fopen(fn,"rb"))==NULL) {
00306         
00307         gd.state = JBXL_GRAPH_OPFILE_ERROR;
00308         return gd;
00309     }
00310 
00311     rs = fread(&hd, sizeof(CmnHead), 1, fp);
00312     ntoh_st(&hd, 4);
00313     if (hd.kind!=RAS_MAGIC) {
00314         
00315         gd.state = JBXL_GRAPH_HEADER_ERROR;
00316         return gd;
00317     }
00318 
00319     hd = read_ras_data(fp); 
00320     fclose(fp);
00321     if (hd.kind==HEADER_NONE) {
00322         free_CmnHead(&hd);
00323         gd.state = JBXL_GRAPH_HEADER_ERROR;
00324         return gd;
00325     }
00326 
00327     gd.xs = hd.xsize;
00328     gd.ys = hd.ysize;
00329     gd.zs = 1;
00330 
00331     sz  = gd.xs*gd.ys*gd.zs;
00332     gd.gp = (sWord*)malloc(sz*sizeof(sWord));
00333     if (gd.gp==NULL) {
00334         
00335         memset(&gd, 0, sizeof(WSGraph));
00336         gd.state = JBXL_GRAPH_MEMORY_ERROR;
00337         return gd;
00338     }
00339 
00340     for (i=0; i<sz; i++) gd.gp[i] = (uByte)hd.grptr[i];
00341     free_CmnHead(&hd);
00342 
00343     gd.state = JBXL_NORMAL;
00344     return gd;
00345 }
00346 
00347 
00348 
00366 WSGraph  read_wsg_file_rb(const char* fname, IRBound* rb)
00367 {
00368     int  i, xs, ys, zs;
00369     double  rzm;
00370     WSGraph  gr;
00371     CmnHead  hd;
00372     CTHead*  chd;
00373     sWord* rz;
00374   
00375     memset(&gr, 0, sizeof(WSGraph));
00376 
00377     hd = read_xxx_file(fname);
00378     if (hd.kind==HEADER_NONE) {
00379         
00380         
00381         gr.state = JBXL_GRAPH_OPFILE_ERROR;
00382         return gr;
00383     }
00384     
00385     xs = hd.xsize;
00386     ys = hd.ysize;
00387     zs = Max(hd.zsize, 1);
00388     rb->xmin = rb->ymin = rb->zmin = 0;
00389     rb->xmax = xs - 1;
00390     rb->ymax = ys - 1;
00391     rb->zmax = zs - 1;
00392 
00393     if (hd.depth==16){
00394         gr.gp = (sWord*)hd.grptr;
00395     }
00396     else if (hd.depth<16){
00397         gr.gp = (sWord*)malloc(xs*ys*zs*sizeof(sWord));
00398         if (gr.gp==NULL) {
00399             
00400             gr.state = JBXL_GRAPH_MEMORY_ERROR;
00401             return gr;
00402         }
00403         for (i=0; i<xs*ys*zs; i++) gr.gp[i] = (uByte)hd.grptr[i];
00404         free(hd.grptr);
00405     }
00406     else if (hd.depth>16){
00407         gr.gp = (sWord*)malloc(xs*ys*zs*sizeof(sWord));
00408         if (gr.gp==NULL) { 
00409             
00410             gr.state = JBXL_GRAPH_MEMORY_ERROR;
00411             return gr;
00412         }
00413         for (i=0;i<xs*ys*zs;i++) gr.gp[i]=(sWord)((uByte)hd.grptr[i]>>(hd.depth-15));
00414         free(hd.grptr);
00415     }
00416 
00417     gr.xs = xs;
00418     gr.ys = ys;
00419     gr.zs = zs;
00420 
00421     if (hd.bsize>0 && checkBit(hd.kind, CT_DATA)) {
00422         if (checkBit(hd.kind, HAS_RBOUND)) {
00423             chd = (CTHead*)hd.buf;
00424             rb->xmin = chd->cutleft;
00425             rb->xmax = chd->cutright;
00426             rb->ymin = chd->cutup;
00427             rb->ymax = chd->cutdown;
00428             rb->zmin = chd->ctmin;
00429             rb->zmax = chd->ctmax;
00430         }
00431         rz  = (sWord*)hd.buf;
00432         if (rz[9]==RZXY_RATE || checkBit(hd.kind, HAS_RZXY)) {
00433             rzm = (double)(rz[8])/rz[9];
00434             if (rzm<1.0 && rzm>0.1) set_RZxy(rzm);
00435             
00436         }
00437         if (rz[10]!=0 && checkBit(hd.kind, HAS_BASE)) {
00438             ZeroBase = rz[10];
00439         }
00440     }
00441     free(hd.buf);
00442 
00443     gr.state = JBXL_NORMAL;
00444     return gr;
00445 }
00446 
00447 
00448 
00449 
00451 
00452 
00466 int  write_ras_file_obit(const char* fn, CmnHead* hd, int obit)
00467 {
00468     FILE *fp;
00469     RasHead shd;
00470     int  i, j, k, l, linebyte, databyte, depth, lsize;
00471     uByte null=0x00, *ptr, *buf;
00472 
00473     if ((fp=fopen(fn,"wb"))==NULL) return JBXL_GRAPH_OPFILE_ERROR;
00474     obit = Xabs(obit);
00475 
00476     if (hd->kind == RAS_DATA) {
00477         unsigned int u;
00478         memcpy((sByte*)&shd, hd->buf, hd->bsize);
00479         ptr = (uByte*)malloc(hd->lsize);
00480         if (ptr==NULL)  return JBXL_GRAPH_MEMORY_ERROR;
00481         lsize = hd->lsize;
00482         for (u=0; u<hd->lsize; u++) ptr[u] = hd->grptr[u];
00483     }
00484     else { 
00485         if (hd->depth==16) {
00486 
00487             if (obit==8) depth = 8;
00488             else         depth = 24;
00489 
00490             lsize = hd->xsize*hd->ysize*depth/8;
00491             buf = (uByte*)malloc(lsize);
00492             if (buf==NULL)  return JBXL_GRAPH_MEMORY_ERROR;
00493 
00494             if (obit==8) {
00495                 int  max = 255; 
00496                 uWord* wp = (uWord*)hd->grptr;
00497                 for (i=0; i<hd->xsize*hd->ysize; i++) {
00498                     max = Max(max, wp[i]);
00499                 }
00500                 for (i=0; i<hd->ysize*hd->xsize; i++) {
00501                     buf[i] = 255*wp[i]/max;
00502                 }
00503             }
00504             else {
00505                 k = l = 0;
00506                 for (i=0; i<hd->ysize*hd->xsize; i++) {
00507                     buf[k++] = hd->grptr[l++];
00508                     buf[k++] = hd->grptr[l++];
00509                     buf[k++] = null;
00510                 }
00511             }
00512         }
00513         else {
00514             depth = hd->depth;
00515             lsize = hd->lsize;
00516             buf   = (uByte*)hd->grptr;
00517         }
00518 
00519         databyte = hd->xsize*depth/8;
00520         linebyte = lsize/hd->ysize;
00521         if (linebyte%2==1) {
00522             linebyte++;
00523             lsize = linebyte*hd->ysize;
00524         }
00525 
00526         shd.ras_magic    = RAS_MAGIC;
00527         shd.ras_width    = hd->xsize;
00528         shd.ras_height   = hd->ysize;
00529         shd.ras_depth    = depth;
00530         shd.ras_length   = lsize;
00531         shd.ras_type     = RT_STANDARD;
00532         shd.ras_maptype  = RMT_NONE;
00533         shd.ras_maplength= 0;
00534 
00535         ptr = (uByte*)malloc(lsize);
00536         if (ptr==NULL) {
00537             if (hd->depth==16) free(buf);
00538             return JBXL_GRAPH_MEMORY_ERROR;
00539         }
00540 
00541         k = l = 0;
00542         for (i=0 ; i<hd->ysize; i++) {
00543             for (j=0; j<databyte; j++)        ptr[k++] = buf[l++];
00544             for (j=0; j<linebyte-databyte; j++) ptr[k++] = null;
00545         }
00546 
00547         if (hd->depth==16) free(buf);
00548     }
00549 
00550     hton_st(&shd, 4);
00551     fwrite(&shd, sizeof(RasHead), 1, fp);
00552     fwrite(ptr, lsize, 1, fp);
00553 
00554     fclose(fp);
00555     free(ptr);
00556     return  0;
00557 }
00558 
00559 
00560 
00584 int  write_ct_file(const char* fn, CmnHead* hd)
00585 {
00586     CTHead  chd;
00587     CmnHead cmd;
00588     FILE*  fp;
00589     sByte* ptr;
00590     sWord* wp;
00591     sByte  null=0x00;
00592     int i, j, k, l, lsize;
00593     int dbyte=2;   
00594 
00595     if ((fp=fopen(fn,"wb"))==NULL) return JBXL_GRAPH_OPFILE_ERROR;
00596 
00597     hd->zsize = Max(hd->zsize, 1); 
00598     lsize = hd->xsize*hd->ysize*hd->zsize*dbyte;
00599     hd->lsize = lsize;
00600     ptr = (sByte*)malloc(lsize);
00601     if (ptr==NULL) return JBXL_GRAPH_MEMORY_ERROR;
00602 
00603     if (checkBit(hd->kind, CT_DATA) || checkBit(hd->kind, CT_3DM)) {
00604         memcpy((sByte*)&chd, hd->buf, hd->bsize);
00605         hton_st(&chd, 2);
00606         chd.xsize = htons((sWord)hd->xsize);
00607         chd.ysize = htons((sWord)hd->ysize);
00608         for(i=0; i<lsize; i++) ptr[i] = hd->grptr[i];
00609     }
00610     else { 
00611         chd.xsize   = htons((sWord)hd->xsize);
00612         chd.ysize   = htons((sWord)hd->ysize);
00613         chd.ctmin   = 0;
00614         chd.ctmax   = 0;
00615         chd.cutup   = 0;
00616         chd.cutdown = 0;
00617         chd.cutleft = 0;
00618         chd.cutright= 0;
00619 
00620         k = l = 0;
00621         if (hd->depth < dbyte*8){   
00622             for (i=0; i<hd->xsize*hd->ysize; i++) {
00623                 for (j=0; j<hd->depth/8; j++) ptr[k++] = null;
00624                 for (j=hd->depth/8; j<dbyte; j++) 
00625                 ptr[k++] = hd->grptr[l++];
00626             }
00627         }
00628         else if (hd->depth == dbyte*8) {
00629             for (i=0; i<hd->xsize*hd->ysize; i++) {
00630                 for (j=0; j<dbyte; j++) ptr[k++] = hd->grptr[l++];
00631             }
00632         }
00633         else {
00634             for (i=0; i<hd->xsize*hd->ysize; i++) {
00635                 for (j=0; j<dbyte; j++) ptr[k++] = hd->grptr[l++];
00636                 l += (hd->depth)/8 - dbyte;
00637             }
00638         }
00639      
00640     }
00641 
00642     if (RZxy!=1.0 && RZxy<RZXY_RATE && chk_RZxy()) {
00643         chd.anydata[0] = htons((sWord)(RZxy*RZXY_RATE));
00644         chd.anydata[1] = htons((sWord)RZXY_RATE);
00645         hd->kind |= HAS_RZXY;
00646     }
00647 
00648     if (ZeroBase!=0) {
00649         chd.anydata[2] = htons((sWord)ZeroBase);
00650         hd->kind |= HAS_BASE;
00651     }
00652 
00653     fseek(fp, 0, 0);
00654     if (checkBit(hd->kind, CT_3DM)) {
00655         cmd = *hd;
00656         hton_st(&cmd, 4);
00657         fwrite(&cmd, sizeof(CmnHead), 1, fp);
00658     }
00659     fwrite(&chd, sizeof(CTHead), 1, fp);
00660 
00661     wp = (sWord*)ptr;
00662     hton_ar(wp, lsize);
00663     fwrite(ptr, lsize, 1, fp);
00664 
00665     fclose(fp);
00666     free(ptr);
00667     
00668     return  0;
00669 }
00670 
00671 
00672 
00685 int  write_cmn_file(const char* fn, CmnHead* hd)
00686 {
00687     FILE*   fp;
00688     CmnHead cd;
00689 
00690     if ((fp=fopen(fn,"wb"))==NULL) return JBXL_GRAPH_OPFILE_ERROR;
00691 
00692     cd = *hd;
00693     hton_st(&cd, 4);
00694     fwrite(&cd, sizeof(CmnHead), 1, fp);
00695     if (hd->bsize>0) fwrite(hd->buf, hd->bsize, 1, fp);
00696     if (hd->lsize>0) fwrite(hd->grptr, hd->lsize, 1, fp);
00697 
00698     return 0;
00699 }
00700 
00701 
00702 
00720 CmnHead  read_xxx_file(const char* fn)
00721 {
00722     FILE*   fp;
00723     int  i, fsz, hsz;
00724     sWord*  wptr;
00725     CmnHead hd;
00726     size_t rs;
00727     UNUSED(rs);
00728 
00729     init_CmnHead(&hd);
00730 
00731     fsz = file_size(fn);
00732     if ((fp=fopen(fn,"rb"))==NULL) {
00733         hd.xsize = JBXL_GRAPH_OPFILE_ERROR;
00734         return hd;
00735     }
00736 
00737     hsz = sizeof(CmnHead);
00738     fseek(fp,0,0);
00739     rs = fread(&hd, hsz, 1, fp);
00740     ntoh_st(&hd, 4);
00741 
00742     if (hd.kind>=0 && hd.kind<=NUM_KDATA) { 
00743         hd.zsize = Max(hd.zsize, 1);
00744         if (fsz == (int)(hsz+hd.bsize+hd.lsize)) {
00745             fseek(fp, hsz, 0);
00746             hd.buf   = (uByte*)malloc(hd.bsize);
00747             hd.grptr = (uByte*)malloc(hd.lsize);
00748             if ((hd.bsize>0&&hd.buf==NULL) || hd.grptr==NULL) {
00749                 init_CmnHead(&hd);
00750                 hd.xsize = JBXL_GRAPH_HEADER_ERROR;
00751                 return hd;
00752             }
00753 
00754             rs = fread(hd.buf, hd.bsize, 1, fp);
00755             wptr = (sWord*)hd.buf;
00756             ntoh_ar(wptr, hd.bsize);
00757 
00758             rs = fread(hd.grptr, hd.lsize, 1, fp);
00759             if (hd.depth==16) {
00760                 wptr = (sWord*)hd.grptr;
00761                 for(i=0; i<hd.xsize*hd.ysize*hd.zsize; i++) wptr[i] = ntohs(wptr[i]);
00762             }
00763             fclose(fp);
00764             return hd;
00765         }
00766     }
00767 
00768     if (hd.kind==RAS_MAGIC) {
00769         hd = read_ras_data(fp); 
00770         fclose(fp);
00771         return hd;
00772     }
00773     
00774     hd = read_ct_data(fp);
00775     if (fsz != (int)(hd.lsize+hd.bsize)) {
00776         hd.xsize = hd.ysize = hd.depth = 0;
00777         hd = read_user_data(fp, &hd);
00778     }
00779     fclose(fp);
00780 
00781     return hd;
00782 }
00783 
00784 
00785 
00798 CmnHead  read_cmn_header(const char* fn)
00799 {
00800     FILE*   fp;
00801     CmnHead hd;
00802     size_t rs;
00803     UNUSED(rs);
00804 
00805     init_CmnHead(&hd);
00806 
00807     if ((fp=fopen(fn,"rb"))==NULL) {
00808         hd.xsize = JBXL_GRAPH_OPFILE_ERROR;
00809         return hd;
00810     }
00811 
00812     rs = fread(&hd, sizeof(CmnHead), 1, fp);
00813     fclose(fp);
00814     ntoh_st(&hd, 4);
00815 
00816     if (hd.bsize>0) {
00817         hd.buf = (uByte*)malloc(hd.bsize);
00818         if (hd.buf==NULL) {
00819             init_CmnHead(&hd);
00820             hd.xsize = JBXL_GRAPH_MEMORY_ERROR;
00821             return hd;
00822         }
00823         rs = fread(hd.buf, hd.bsize, 1, fp);
00824     }
00825     else  {
00826         hd.bsize = 0;
00827         hd.buf = NULL;
00828     }
00829 
00830     return hd;
00831 }
00832 
00833 
00834 
00847 CmnHead  read_cmn_file(const char* fn)
00848 {
00849     FILE*   fp;
00850     CmnHead hd;
00851     size_t rs;
00852     UNUSED(rs);
00853 
00854     init_CmnHead(&hd);
00855 
00856     if ((fp=fopen(fn,"rb"))==NULL) {
00857         hd.xsize = JBXL_GRAPH_OPFILE_ERROR;
00858         return hd;
00859     }
00860     
00861     rs = fread(&hd, sizeof(CmnHead), 1, fp);
00862     fclose(fp);
00863     ntoh_st(&hd, 4);
00864 
00865     if (hd.bsize>0) {
00866         hd.buf = (uByte*)malloc(hd.bsize);
00867         if (hd.buf==NULL) {
00868             init_CmnHead(&hd);
00869             hd.xsize = JBXL_GRAPH_MEMORY_ERROR;
00870             return hd;
00871         }
00872         rs = fread(hd.buf, hd.bsize, 1, fp);
00873     }
00874     else  {
00875         hd.bsize = 0;
00876         hd.buf = NULL;
00877     }
00878 
00879     if (hd.lsize<=0) hd.lsize = file_size(fn)-sizeof(CmnHead)-hd.bsize;
00880     hd.grptr = (uByte*)malloc(hd.lsize);
00881     if (hd.grptr==NULL) {
00882         free_CmnHead(&hd);
00883         hd.xsize = JBXL_GRAPH_MEMORY_ERROR;
00884         return hd;
00885     }
00886     rs = fread(hd.grptr, hd.lsize, 1, fp);
00887 
00888     return hd;
00889 }
00890 
00891 
00892 
00893 
00895 
00896 
00917 CmnHead  read_user_data(FILE* fp, CmnHead* chd)
00918 {
00919     int  i;
00920     CmnHead hd;
00921     size_t rs;
00922     UNUSED(rs);
00923 
00924     init_CmnHead(&hd);
00925 
00926     if (chd==NULL) {
00927         hd.xsize = JBXL_GRAPH_HEADER_ERROR;
00928         return hd;
00929     }
00930     else if (!checkBit(chd->kind, USERSET_DATA)) {
00931         hd.xsize = JBXL_GRAPH_HEADER_ERROR;
00932         return hd;
00933     }
00934     hd = *chd;
00935 
00936     
00937     fseek(fp, 0, 0);
00938     hd.buf = (uByte*)malloc(hd.bsize);
00939     if (hd.buf==NULL) {
00940         init_CmnHead(&hd);
00941         hd.xsize = JBXL_GRAPH_MEMORY_ERROR;
00942         return hd;
00943     }
00944     rs = fread((void*)hd.buf, hd.bsize, 1, fp);
00945 
00946     hd.grptr = (uByte*)malloc(hd.lsize);
00947     if (hd.grptr==NULL) {
00948         free_CmnHead(&hd);
00949         hd.xsize = JBXL_GRAPH_MEMORY_ERROR;
00950         return hd;
00951     }
00952 
00953     fseek(fp, hd.bsize, 0);
00954     rs = fread(hd.grptr, hd.lsize, 1, fp);
00955     
00956     
00957     
00958     
00959 
00960     if (is_little_endian()) {
00961         if (hd.depth==16 && !checkBit(chd->kind, HAS_LENDIAN)) {
00962             sWord* wptr = (sWord*)hd.grptr;
00963             for (i=0; i<hd.xsize*hd.ysize*hd.zsize; i++) wptr[i] = ntohs(wptr[i]);
00964         }
00965     }
00966     else {
00967         if (hd.depth==16 && checkBit(chd->kind, HAS_LENDIAN)) {
00968             uWord* wptr = (uWord*)hd.grptr;
00969             for (i=0; i<hd.xsize*hd.ysize*hd.zsize; i++) wptr[i] = swaps(wptr[i]);
00970         }
00971     }
00972 
00973     return hd;
00974 }
00975 
00976 
00977 
00989 CmnHead  read_ras_data(FILE* fp)
00990 {
00991     RasHead rhd;
00992     CmnHead  hd;
00993     int  i, linebyte, databyte;
00994     uByte null[2], *buf;
00995     sWord* wptr;
00996     size_t rs;
00997     UNUSED(rs);
00998 
00999     init_CmnHead(&hd);
01000     hd.kind  = RAS_DATA;
01001     hd.bsize = 32;
01002 
01003     fseek(fp, 0, 0);
01004     rs = fread(&rhd, hd.bsize, 1, fp);
01005     ntoh_st(&rhd, 4);
01006 
01007     hd.xsize = rhd.ras_width;
01008     hd.ysize = rhd.ras_height;
01009     hd.zsize = 1;
01010     hd.depth = rhd.ras_depth;
01011     hd.lsize = hd.xsize*hd.ysize*((hd.depth+7)/8);
01012     hd.buf   = (uByte*)malloc(hd.bsize);
01013     if (hd.buf==NULL) {
01014         init_CmnHead(&hd);
01015         hd.xsize = JBXL_GRAPH_MEMORY_ERROR;
01016         return hd;
01017     }
01018     memcpy(hd.buf, (uByte*)&rhd, hd.bsize);
01019 
01020     hd.grptr = (uByte*)malloc(hd.lsize);
01021     if (hd.grptr==NULL) {
01022         free_CmnHead(&hd);
01023         hd.xsize = JBXL_GRAPH_MEMORY_ERROR;
01024         return hd;
01025     }
01026     buf = (uByte*)hd.grptr;
01027 
01028     fseek(fp, rhd.ras_maplength, 1); 
01029 
01030     databyte = hd.xsize*((hd.depth+7)/8);
01031     linebyte = rhd.ras_length/hd.ysize;
01032 
01033     if (databyte==linebyte) {
01034         rs = fread(buf, hd.lsize, 1, fp);
01035     }
01036     else {
01037         for (i=0; i<hd.ysize; i++) {
01038             rs = fread(buf, databyte, 1, fp);
01039             rs = fread(null, linebyte-databyte, 1, fp);
01040             buf += databyte;
01041         }
01042     }
01043 
01044     if (hd.depth==16){
01045         wptr = (sWord*)hd.grptr;
01046         for(i=0; i<hd.xsize*hd.ysize; i++) wptr[i] = ntohs(wptr[i]);
01047     }
01048 
01049     return hd;
01050 }
01051 
01052 
01053 
01065 CmnHead  read_ct_data(FILE* fp)
01066 {
01067     int i;
01068     CTHead chd;
01069     CmnHead hd;
01070     sWord *wptr;
01071     size_t rs;
01072     UNUSED(rs);
01073 
01074     init_CmnHead(&hd);
01075 
01076     
01077     fseek(fp, 0, 0);
01078     hd.bsize = 64;
01079     rs = fread((void*)&chd, hd.bsize, 1, fp);
01080     ntoh_st(&chd, 2);
01081 
01082     hd.xsize = chd.xsize - chd.cutleft - chd.cutright;
01083     hd.ysize = chd.ysize - chd.cutup   - chd.cutdown;
01084     hd.zsize = 1;
01085     hd.depth = 16;
01086     hd.lsize = hd.xsize*hd.ysize*(hd.depth/8);
01087 
01088     hd.buf   = (uByte*)malloc(hd.bsize);
01089     if (hd.buf==NULL) {
01090         init_CmnHead(&hd);
01091         hd.xsize = JBXL_GRAPH_MEMORY_ERROR;
01092         return hd;
01093     }
01094     hd.kind  = CT_DATA;
01095     memcpy(hd.buf, &chd, hd.bsize);
01096 
01097     hd.grptr = (uByte*)malloc(hd.lsize);
01098     if (hd.grptr==NULL) {
01099         free_CmnHead(&hd);
01100         hd.xsize = JBXL_GRAPH_MEMORY_ERROR;
01101         return hd;
01102     }
01103 
01104     fseek(fp, hd.bsize, 0);
01105     rs = fread(hd.grptr, hd.lsize, 1, fp);
01106 
01107     wptr = (sWord*)hd.grptr;
01108     for (i=0; i<hd.xsize*hd.ysize; i++) wptr[i] = ntohs(wptr[i]);
01109 
01110     return hd;
01111 }
01112 
01113 
01114 
01115 
01117 
01118 
01137 int  dicom_header(FILE* fp, int fsize, int* dsize, int* xsize, int* ysize, int* depth, double* rzxy)
01138 {
01139     int    i, j, sz, rp, ef;
01140     int    ln, hsize;
01141     int    xs = 0, ys = 0, dp = 0;
01142     double rz = 0.0;
01143 
01144     uWord  dt, cn[2];
01145     uWord* wp;
01146 
01147     if (fp==NULL || fsize<=0 || dsize==NULL) return JBXL_GRAPH_IVDARG_ERROR;
01148 
01149     *dsize = 0;
01150     if (rzxy!=NULL) *rzxy = 0.0;
01151     ef = OFF;
01152     fseek(fp, 0, 0);
01153 
01155     
01156     rp = (int)fread(&dt, sizeof(uWord), 1, fp);
01157     while(!feof(fp) && ef!=ON) {
01158         
01159         if (dt==DICOM_PIXEL_GROUP) {
01160             rp += (int)fread(&dt, sizeof(uWord), 1, fp);
01161             
01162             if (dt==DICOM_PIXEL_ELEMENT) {
01163                 rp += (int)fread(&cn, sizeof(uWord), 2, fp);
01164                 
01165                 if (cn[0]==DICOM_PIXCEL_VR && cn[1]==0x0000) {
01166                     rp += (int)fread(&cn, sizeof(uWord), 2, fp);
01167                     *dsize = *(int*)cn;
01168                     hsize = rp*sizeof(uWord);
01169                     if (fsize>=*dsize+hsize) {      
01170                         ef = ON;
01171                         break;
01172                     }
01173                 }
01174                 else {
01175                     *dsize = *(int*)cn;
01176                     hsize = rp*sizeof(uWord);
01177                     if (fsize>=*dsize+hsize) {      
01178                         ef = ON;
01179                         break;
01180                     }
01181                 }
01182             }
01183         }
01184         rp += (int)fread(&dt, sizeof(uWord), 1, fp);
01185     }
01186 
01187     if (!ef) return 0;
01188 
01189     sz = rp*sizeof(uWord);
01190     wp = (uWord*)malloc(sz);
01191     if (wp==NULL) return JBXL_GRAPH_MEMORY_ERROR;
01192 
01194     
01195     size_t rs;
01196     UNUSED(rs);
01197 
01198     fseek(fp, 0, 0);
01199     rs = fread(wp, sz, 1, fp);
01200 
01201     for (i=0; i<sz/2-3; i++) {
01202         
01203         if (wp[i]==DICOM_IMAGE_GROUP) {
01204 
01205             
01206             if (wp[i+1]==DICOM_PXLSPC_ELEMENT) {
01207                 char   rx[LNAME], ry[LNAME];
01208                 uByte* bp;
01209 
01210                 memset(rx, 0, LNAME);
01211                 memset(ry, 0, LNAME);
01212 
01213                 if (wp[i+2]==DICOM_STR_VR) ln = wp[i+3];
01214                 else                       ln = *(int*)&wp[i+2];
01215 
01216                 if (ln<LNAME-1) {
01217                     bp = (uByte*)&wp[i+4];
01218                     j  = 0;
01219                     while (j<ln-1 && bp[j]!='\\') {
01220                         rx[j] = bp[j];
01221                         j++;
01222                     }
01223                     ln -= j + 1;
01224                     bp += j + 1;
01225                     j   = 0;
01226                     while (j<ln-1 && bp[j]!=' ') {
01227                         ry[j] = bp[j];
01228                         j++;
01229                     }
01230                     if (!strcmp(rx, ry)) {
01231                         rz = atof(rx);
01232                     } 
01233                 }
01234             }
01235 
01236             
01237             else if (wp[i+1]==DICOM_XSIZE_ELEMENT) {
01238                 if (wp[i+2]==DICOM_INT_VR) ln = wp[i+3];
01239                 else                       ln = *(int*)&wp[i+2];
01240                 if (ln==2) {
01241                     xs = (int)*(sWord*)&wp[i+4];
01242                 }
01243                 else if (ln==4) {
01244                     xs = *(int*)&wp[i+4];
01245                 }
01246             }
01247 
01248             
01249             else if (wp[i+1]==DICOM_YSIZE_ELEMENT) {
01250                 if (wp[i+2]==DICOM_INT_VR) ln = wp[i+3];
01251                 else                       ln = *(int*)&wp[i+2];
01252                 if (ln==2) {
01253                     ys = (int)*(sWord*)&wp[i+4];
01254                 }
01255                 else if (ln==4) {
01256                     ys = *(int*)&wp[i+4];
01257                 }
01258             }
01259 
01260             
01261             else if (wp[i+1]==DICOM_DEPTH_ELEMENT) {
01262                 if (wp[i+2]==DICOM_INT_VR) ln = wp[i+3];
01263                 else                       ln = *(int*)&wp[i+2];
01264                 if (ln==2) {
01265                     dp = (int)*(sWord*)&wp[i+4];
01266                 }
01267                 else if (ln==4) {
01268                     dp = *(int*)&wp[i+4];
01269                 }
01270             }
01271         }
01272     }
01273     free(wp);
01274     
01275     if (rzxy!=NULL)  *rzxy  = rz;
01276     if (xsize!=NULL) *xsize = xs;
01277     if (ysize!=NULL) *ysize = ys;
01278     if (depth!=NULL) *depth = dp;
01279     
01280     return sz;
01281 }
01282 
01283 
01284 
01289 WSGraph  read_dicom_file(const char* fn)
01290 {
01291     int  sz, fsize, dsize, xsize, ysize, depth;
01292     double rzxy;
01293     FILE*  fp;
01294     size_t rs;
01295     UNUSED(rs);
01296 
01297     WSGraph vp;
01298 
01299     memset(&vp, 0, sizeof(WSGraph));
01300 
01301     fsize = file_size(fn);
01302     if (fsize<=0) {
01303         vp.state = JBXL_GRAPH_NOFILE_ERROR;
01304         return vp;
01305     }
01306 
01307     if ((fp=fopen(fn,"rb"))==NULL) {
01308         vp.state = JBXL_GRAPH_OPFILE_ERROR;
01309         return vp;
01310     }
01311 
01312     sz = dicom_header(fp, fsize, &dsize, &xsize, &ysize, &depth, &rzxy);
01313     if (sz<=0) {
01314         fclose(fp);
01315         vp.state = JBXL_GRAPH_HEADER_ERROR;
01316         return vp;
01317     }
01318 
01319     DEBUG_MODE PRINT_MESG("read_dicom_file: xsize = %d, ysize = %d, depth = %d, RZxy = %f\n", xsize, ysize, depth, rzxy);
01320 
01321     
01322     if (dsize!=xsize*ysize*((depth+7)/8)) {
01323         unset_RZxy();
01324         fclose(fp);
01325         vp.state = JBXL_GRAPH_HEADER_ERROR;
01326         return vp;
01327     }
01328 
01329     if ((depth+7)/8!=2) {
01330         unset_RZxy();
01331         fclose(fp);
01332         vp.state = JBXL_GRAPH_HEADER_ERROR;
01333         return vp;
01334     }
01335 
01336     vp = make_WSGraph(xsize, ysize, 1);
01337     if (vp.gp==NULL) {
01338         vp.state = JBXL_GRAPH_MEMORY_ERROR;
01339         return vp;
01340     }
01341 
01342     fseek(fp, sz, 0);
01343     rs = fread(vp.gp, dsize, 1, fp);
01344     fclose(fp);
01345 
01346     if (rzxy>0.0) set_RZxy(rzxy);   
01347     vp.state = JBXL_NORMAL;
01348     return vp;
01349 }
01350 
01351