home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / TIFF / TACS40.ZIP / TIFWRITE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1988-04-27  |  8.6 KB  |  446 lines

  1. /* ------------------------------------------------------------------ */
  2. /*                                      */
  3. /*        TIFF IMAGE DATA WRITE AND UPDATE FUNCTION          */
  4. /*                                      */
  5. /*        DEVELOPED BY:    VIJAY RANGARAJAN              */
  6. /*        DATE:        8/26/86                   */
  7. /*                                      */
  8. /*        DEST CORPORATION                      */
  9. /*                                      */
  10. /* ------------------------------------------------------------------ */
  11.  
  12. /* this version is backedup as tifwrite.bak prior to new changes */
  13. /* this version stores compressed data, but the image looks distorted */
  14. /* changes - 9/15   10.00am */
  15.  
  16.  
  17. #define  T_273    273
  18. #define  T_279    279
  19. #include "apptag.h"
  20.  
  21. #define NULL    0L
  22.  
  23. extern int tlpWrite();
  24. extern int write();
  25.  
  26. extern short field_mem_tab[];
  27. extern long    field_data_endpos;
  28. extern long    field_data_pos;
  29. extern long    ifdsize;
  30. extern long    get_cpos();
  31. extern int    field_data_write();
  32. extern short fields_exist;
  33.  
  34. short    image_begin;
  35.  
  36. static long    strpofst_pos;
  37. static long    strpbytcnt_pos;
  38. static long    sav_fil_pos;
  39. static long    *strpofst_mem_ptr;
  40. static long    *strpbytcnt_mem_ptr;
  41.  
  42. static long    curnt_pos;
  43. static long    strperimg;
  44. static int    filhandle;
  45.  
  46.    /********* should be an array indexed by fhandle ***********************/
  47. static short  status = 0;
  48.  
  49. static long offset_strip_mem[500];
  50. static long count_strip_mem[500];
  51.  
  52.    /* included to handled compress data  9/11 */
  53.  
  54. extern char    far *bigalloc();
  55. extern char    far *bigfree();
  56. extern short compress_block();
  57. static char    far *comp_buffer = 0L;
  58. static char    far *comp_ptr = 0L;
  59. static char    far *rawbuf_ptr;
  60. static long    comp_size = 0L;
  61. static long    lines_alloc = 0L;
  62. static long    line_count;
  63. static short compress_type;
  64. static short comp_count;
  65. static long    tot_comp_count;
  66. static long    tot_byte_count;
  67.  
  68.   /* ---------------------------------------- */
  69.  
  70.  
  71.  
  72. short byte_width;
  73.  
  74. int set_position(), write_img_data(), write_strptrs(), rewrite_field_data();
  75. void strpofst_update_ptr(), strpbytcnt_update_ptr(), write_tagval();
  76. void update_strp_ifd();
  77.  
  78. long  write_image(fhandle,fdtype,plane,startpos,numlines,buffer_ptr)
  79.  
  80. int     fhandle;
  81. short fdtype;
  82. short plane;
  83. long  startpos;
  84. long  numlines;
  85. char  far *buffer_ptr;
  86. {
  87.  
  88.     long   image_length;
  89.     long   image_width;
  90.     static long     l_bytewidth;        /* 9/16 */
  91.     static long     rowsprstrp;
  92.     long   get_tagval();
  93.     long   num_bytes;
  94.     long   i;
  95.     int      j;
  96.     long bitssamp;
  97.     long samppix;
  98.  
  99.     if (status == 0) {
  100.         status = 1;
  101.         image_begin = 1;
  102.  
  103.         image_length = get_tagval(T_IMG_LENGTH);
  104.         image_width =  get_tagval(T_IMG_WIDTH);
  105.  
  106.         if((bitssamp = get_tagval(T_BITS_SAMPLE)) == NULL) {
  107.             bitssamp = BITSSAMP;
  108.         }
  109.  
  110.         if((samppix = get_tagval(T_SAMP_PIX)) == NULL) {
  111.             samppix = SAMPPIXEL;
  112.         }
  113.  
  114.         compress_type = (short) get_tagval(T_COMPRESS);  /* comp 9/11 */
  115.  
  116.         /* keep in mind here that data may be lost if the number */
  117.         /* of bytes per line exceeds 65535 */
  118.  
  119.         byte_width = (short)(image_width * samppix * bitssamp + 7 ) / 8;
  120.         l_bytewidth = (image_width * samppix * bitssamp + 7 ) / 8;
  121.         tot_byte_count = 0L;
  122.  
  123.         rowsprstrp = get_tagval(T_RWPRSTRP);
  124.         if (rowsprstrp == 0)
  125.             rowsprstrp = image_length;
  126.  
  127.         filhandle = fhandle;
  128.         strperimg = (image_length + rowsprstrp -1)/rowsprstrp;
  129.  
  130.  
  131.         if (set_position(fhandle, field_data_endpos) < 0)
  132.             return (0L);
  133.  
  134.         strpofst_mem_ptr = (long *) &(offset_strip_mem[0]);
  135.  
  136.         strpbytcnt_mem_ptr = (long *) &(count_strip_mem[0]);
  137.  
  138.         sav_fil_pos = get_cpos(fhandle);
  139.  
  140.         if (strperimg == 1) {
  141.             *strpofst_mem_ptr =(long) sav_fil_pos;
  142.             *strpbytcnt_mem_ptr =(long)(l_bytewidth * image_length);
  143.         }
  144.  
  145.     }
  146.  
  147. /*  Get current file position */
  148.  
  149.     num_bytes = (long) (l_bytewidth * numlines);
  150.     line_count = numlines;
  151.     lines_alloc = line_count;
  152.  
  153.     if (strperimg != 1) {
  154.         sav_fil_pos = get_cpos(fhandle);
  155.         strpofst_update_ptr();
  156.     }
  157.  
  158. /*  Check if data is to be compressed 9/11 */
  159.  
  160.     switch(compress_type) {
  161.  
  162.         case ONE_D_MOD:
  163.  
  164.      /* Compress image data using 1D modified Huffman code technique
  165.       * and allocate necessary buffer size for compressed data    */
  166.  
  167.             j = 1;         /* one line compression */
  168.             comp_size = (l_bytewidth * (j * 6));
  169.             if ((comp_buffer = bigalloc(comp_size)) == 
  170.                     (char far *) 0x0000) {
  171.                 return(0L);
  172.             }
  173.             comp_ptr = comp_buffer;
  174.             rawbuf_ptr = buffer_ptr;
  175.             while (line_count > 0) {
  176.  
  177.                 /* compress 1 line 9/15 */
  178.  
  179.                 comp_count = compress_block(rawbuf_ptr,
  180.                          comp_ptr, l_bytewidth, j);
  181.  
  182.  
  183.                 rawbuf_ptr += (l_bytewidth * j);
  184.                 tot_comp_count = (long) comp_count;
  185.  
  186.  
  187.                 if (write_img_data (fhandle, comp_buffer,
  188.                          tot_comp_count) < 0) {
  189.                     bigfree(comp_buffer);
  190.                     return(0L);
  191.                 }
  192.  
  193.                 tot_byte_count += tot_comp_count; /* 9/12 */
  194.  
  195.                 line_count -= j;
  196.  
  197.             }  /* end of outer while loop */
  198.  
  199. /*  new addition 9/12 */
  200.  
  201.             if (strperimg ==1) {
  202.                 *strpbytcnt_mem_ptr = tot_byte_count;
  203.             }
  204.             else {
  205.                 strpbytcnt_update_ptr(tot_byte_count);
  206.                 tot_byte_count = 0L;    /* 10/9 vj */
  207.             }
  208.  
  209.             bigfree(comp_buffer);
  210.             break;
  211.                               
  212.         /* ----- end of case ---------- */
  213.  
  214.         default:     /*  Write image data onto the file */
  215.  
  216.             if (write_img_data (fhandle, buffer_ptr, num_bytes)<0){
  217.                 return (0L);
  218.             }
  219.  
  220.             if (strperimg !=1)
  221.                 strpbytcnt_update_ptr(num_bytes); /* 9/12 */
  222.  
  223.     }    /* close braces for switch loop */
  224.  
  225.  
  226.     return(numlines);
  227. }
  228.  
  229.  
  230. /*  Write image data function */
  231.  
  232.  
  233. int write_img_data(fhndle, bufptr, bytesize)
  234.    int    fhndle;
  235.    char far *bufptr;
  236.    long bytesize;
  237.  
  238.    {
  239.    int smallbytesize;
  240.  
  241.    smallbytesize = (int) bytesize;
  242.    if ((tlpWrite (fhndle, bufptr, smallbytesize)) != smallbytesize)
  243.       return (-1);
  244.  
  245.    return (0);
  246.    }
  247.  
  248.  
  249. /*  Update stripoffset table */
  250.  
  251. void strpofst_update_ptr()
  252.    {
  253.    *strpofst_mem_ptr = (long) sav_fil_pos;
  254.    ++strpofst_mem_ptr;
  255.    }
  256.  
  257. /*  Update stripbytecount table */
  258.  
  259. void strpbytcnt_update_ptr(no_bytes)
  260.    long no_bytes;
  261.    {
  262.  
  263.    *strpbytcnt_mem_ptr = no_bytes;
  264.    strpbytcnt_mem_ptr++;
  265.    }
  266.  
  267.  
  268.  
  269.  
  270.  
  271.  
  272.  
  273.  
  274. /*  Get the value of the specfied tag */
  275.  
  276.    long get_tagval(tag_num)
  277.    short  tag_num;
  278.    {
  279.  
  280.    short *field_ptr;
  281.    short tag_index;
  282.    long  tag_val, *ptr;
  283.    short i;
  284.  
  285.    field_ptr = field_mem_tab+1;
  286.  
  287.    i = field_mem_tab[0];
  288.    while (i-- > 0)
  289.        {
  290.        if (tag_num != *field_ptr)
  291.       {
  292.       field_ptr += 6;
  293.       }
  294.  
  295.        else
  296.       {
  297.       field_ptr += 4;
  298.       ptr = (long *) field_ptr;
  299.       tag_val = *ptr;
  300.       return(tag_val);
  301.       }
  302.  
  303.        }
  304.  
  305.    return(0L);
  306.    }
  307.  
  308.  
  309. /*  Position the file */
  310.  
  311. int set_position(fhandl, fil_pos)
  312.  
  313.   int    fhandl;
  314.   long    fil_pos;
  315.  
  316.   {
  317.      long    lseek();
  318.      int    mode = 0;
  319.  
  320.      if (lseek(fhandl, fil_pos, mode) < 0)
  321.     return (-1);
  322.  
  323.      return (0);
  324.   }
  325.  
  326.  
  327.  
  328.  
  329. /* Close TIFF creating subsystem */
  330.  
  331.    int close_create(fhndl)
  332.  
  333.    int    fhndl;
  334.  
  335.    {
  336.  
  337.    image_begin = 0;
  338.    fields_exist = 0;
  339.    status = 0;        /* reset for next file */
  340.  
  341.     if(strperimg != 1) {
  342.         strpofst_pos = get_cpos(fhndl);
  343.         if(write_strptrs(fhndl,&(offset_strip_mem[0]),(strperimg*4))< 0)
  344.             return (0);
  345.  
  346.         strpbytcnt_pos = get_cpos(fhndl);
  347.         if(write_strptrs(fhndl,&(count_strip_mem[0]),(strperimg*4)) < 0)
  348.             return (0);
  349.  
  350.     }
  351.     else {
  352.         strpofst_pos = offset_strip_mem[0];
  353.         strpbytcnt_pos = count_strip_mem[0];
  354.     }
  355.  
  356.     update_strp_ifd(fhndl);
  357.  
  358.     if (rewrite_field_data(fhndl) < 0)
  359.         return (0);
  360.  
  361.    return(1);
  362.    }
  363.  
  364. /*  Write strip offset table onto the file */
  365.  
  366. int write_strptrs(handle, strp_mem, length)
  367.  
  368.    int    handle;
  369.    long *strp_mem;
  370.    long length;
  371.  
  372.    {
  373.    int smalllength;
  374.  
  375.    smalllength = (int) length;
  376.    if ((write (handle, strp_mem, smalllength)) != smalllength)
  377.       return (-1);
  378.    return (0);
  379.    }
  380.  
  381.  
  382. /*  Update strip tags data in ifd */
  383.  
  384. void update_strp_ifd(fl_handl)
  385.  
  386.    int fl_handl;
  387.    {
  388.    short tagnum = T_273;
  389.    write_tagval(fl_handl, tagnum, strperimg, strpofst_pos);
  390.  
  391.    tagnum = T_279;
  392.    write_tagval(fl_handl, tagnum, strperimg, strpbytcnt_pos);
  393.  
  394.    }
  395.  
  396.  
  397.  
  398. /*  Write tag length and tag value into the tag */
  399.  
  400. void write_tagval(handle, tagnum, tagln, tagval)
  401.  
  402.    int    handle;
  403.    short tagnum;
  404.    long  tagln;
  405.    long  tagval;
  406.  
  407.    {
  408.    short *field_ptr;
  409.    long  tag_val, *ptr;
  410.    short i;
  411.  
  412.    field_ptr = field_mem_tab+1;
  413.    i = field_mem_tab[0];
  414.    while (i-- > 0)
  415.        {
  416.        if (tagnum != *field_ptr)
  417.       {
  418.       field_ptr += 6;
  419.       }
  420.  
  421.        else
  422.       {
  423.       field_ptr += 2;
  424.       ptr = (long *) field_ptr;
  425.       *ptr++ = tagln;
  426.       *ptr++ = tagval;
  427.       break;
  428.       }
  429.        }
  430.    }
  431.  
  432.  
  433. /*  Rewrite the field data */
  434.  
  435. int rewrite_field_data(handle)
  436.  
  437.    int handle;
  438.  
  439.    {
  440.  
  441.    if (set_position(handle, field_data_pos) < 0)
  442.       return (-1);
  443.    field_data_write (handle, ifdsize);
  444.    return (0);
  445.    }
  446.