home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / procssng / ccs / ccs-11tl.lha / lbl / xview / xhist / load_i.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-09-17  |  3.4 KB  |  183 lines

  1.  
  2. #include <stdio.h>
  3. #include <hipl_format.h>
  4.  
  5. int       types[] = {PFBYTE, PFSHORT, PFINT, PFFLOAT, LASTTYPE};
  6.  
  7. extern char *cur_fname;        /* current file name */
  8.  
  9. extern byte *barray;
  10. extern short *sarray;
  11. extern int *iarray;
  12. extern float *farray;
  13.  
  14. extern int pix_format;        /* byte, short, int, float     */
  15.  
  16. extern int size;        /* size of image         */
  17.  
  18. #define Calloc(x,y) (y *)calloc((unsigned)(x), sizeof(y))
  19.  
  20. int       nrow;
  21. int       ncol;
  22. int       i_ocol;
  23. int       nfr;
  24.  
  25. byte     *qb;
  26. short    *qs;
  27. int      *qi;
  28. float    *qf;
  29.  
  30. /**********************************************************/
  31. int
  32. load_image()
  33. {
  34.     struct header hd, hdp;
  35.     int       method, f;
  36.     FILE     *fp = NULL;
  37.     int       start, finish;    /* for report time     */
  38.  
  39.     fprintf(stderr, " Loading image... \n");
  40.  
  41.     if((fp = hfopenr(cur_fname)) == (FILE *)HIPS_ERROR) 
  42.     return(-1);
  43.     if (fread_hdr_a(fp, &hd, cur_fname) == HIPS_ERROR) {
  44.     fclose(fp);
  45.     free_image(&hd);
  46.     return (-1);
  47.     }
  48.     method = fset_conversion(&hd, &hdp, types, cur_fname);
  49.  
  50.     nrow = hdp.rows;
  51.     ncol = hdp.cols;
  52.     i_ocol = hdp.ocols;
  53.  
  54.     nfr = hdp.num_frame;
  55.  
  56.     size = nrow * ncol * nfr;
  57.  
  58.     pix_format = hdp.pixel_format;
  59.  
  60.     switch (pix_format) {
  61.     case PFBYTE:
  62.     if ((barray = Calloc(size, byte)) == NULL) {
  63.         perror("calloc error: barray ");
  64.         goto f_error;
  65.     }
  66.     qb = barray;
  67.     break;
  68.     case PFSHORT:
  69.     if ((sarray = Calloc(size, short)) == NULL) {
  70.         perror("calloc error: sarray ");
  71.         goto f_error;
  72.     }
  73.     qs = sarray;
  74.     break;
  75.     case PFINT:
  76.     if ((iarray = Calloc(size, int)) == NULL) {
  77.         perror("calloc error: iarray ");
  78.         goto f_error;
  79.     }
  80.     qi = iarray;
  81.     break;
  82.     case PFFLOAT:
  83.     if ((farray = Calloc(size, float)) == NULL) {
  84.         perror("calloc error: farray ");
  85.         goto f_error;
  86.     }
  87.     qf = farray;
  88.     break;
  89.     }                /* end of switch( pixel_format )     */
  90.  
  91.     for (f = 0; f < nfr; f++) {
  92.     fprintf(stderr, "%s: starting frame #%d\n", cur_fname, f);
  93.     if (fread_imagec(fp, &hd, &hdp, method, f, cur_fname) == HIPS_ERROR) {
  94.         fprintf(stderr,"%s\n",hipserr);
  95.         return (-1);
  96.     }
  97.     build_array(&hdp);
  98.     }
  99.  
  100.     start = time(NULL);
  101.     comp_hist();        /* to compute histogram     */
  102.     finish = time(NULL);
  103.     printf("\n time spent computing histogram = %d sec\n\n", finish - start);
  104.  
  105.     return 0;
  106.  
  107. f_error:
  108.     size = 0;            /* no image         */
  109.     return (-1);
  110.  
  111. }                /* end of load_image ()             */
  112.  
  113. build_array(hdi)
  114.     struct header *hdi;
  115. {
  116.     switch (pix_format) {
  117.     case PFBYTE:
  118.     bd_barray(hdi);
  119.     break;
  120.     case PFSHORT:
  121.     bd_sarray(hdi);
  122.     break;
  123.     case PFINT:
  124.     bd_iarray(hdi);
  125.     break;
  126.     case PFFLOAT:
  127.     bd_farray(hdi);
  128.     break;
  129.     }
  130. }                /* end of build_array (hdi)      */
  131.  
  132. bd_barray(hdi)
  133.     struct header *hdi;
  134. {
  135.     int       x, y;
  136.     byte     *p;
  137.  
  138.     p = hdi->firstpix;
  139.  
  140.     for (y = 0; y < nrow; y++, p = p + i_ocol)
  141.     for (x = 0; x < ncol; x++, qb++)
  142.         *qb = p[x];
  143. }
  144.  
  145. bd_sarray(hdi)
  146.     struct header *hdi;
  147. {
  148.     int       x, y;
  149.     short    *p;
  150.  
  151.     p = (short *) hdi->firstpix;
  152.  
  153.     for (y = 0; y < nrow; y++, p = p + i_ocol)
  154.     for (x = 0; x < ncol; x++, qs++)
  155.         *qs = p[x];
  156. }
  157.  
  158. bd_iarray(hdi)
  159.     struct header *hdi;
  160. {
  161.     int       x, y;
  162.     int      *p;
  163.  
  164.     p = (int *) hdi->firstpix;
  165.  
  166.     for (y = 0; y < nrow; y++, p = p + i_ocol)
  167.     for (x = 0; x < ncol; x++, qi++)
  168.         *qi = p[x];
  169. }
  170.  
  171. bd_farray(hdi)
  172.     struct header *hdi;
  173. {
  174.     int       x, y;
  175.     float    *p;
  176.  
  177.     p = (float *) hdi->firstpix;
  178.  
  179.     for (y = 0; y < nrow; y++, p = p + i_ocol)
  180.     for (x = 0; x < ncol; x++, qf++)
  181.         *qf = p[x];
  182. }
  183.