home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_09_11 / 9n11076a < prev    next >
Text File  |  1991-04-12  |  13KB  |  498 lines

  1.  
  2.  
  3.        /************************************************
  4.        *
  5.        *       file d:\lsu\wtiff.c
  6.        *
  7.        *       Functions: This file contains
  8.        *           create_allocate_tiff_file
  9.        *           write_array_into_tiff_image
  10.        *           write_line
  11.        *           insert_short_into_buffer
  12.        *           insert_long_into_buffer
  13.        *           round_off_image_size
  14.        *           does_not_exist
  15.        *
  16.        *       Purpose:
  17.        *          These functions insert a 100x100 array into
  18.        *          a tiff image already stored on disk.
  19.        *
  20.        *       External Calls:
  21.        *          rtiff.c - seek_to_first_line
  22.        *                    seek_to_end_of_line
  23.        *          tiff.c - read_tiff_header
  24.        *          mrw.c - my_write
  25.        *
  26.        *       Modifications:
  27.        *          29 January 1991 - created
  28.        *
  29.        *************************************************/
  30.  
  31.  
  32.  
  33. #include "d:\cips\cips.h"
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.        /*******************************************************
  43.        *
  44.        *   create_alllocate_tiff_file(...
  45.        *
  46.        *   This function creates a file on disk that will be
  47.        *   large enough to hold a tiff image.  The input
  48.        *   tiff_header_struct describes the desired tiff file.
  49.        *   This function writes the tiff header and then
  50.        *   writes a blank image array out to disk the proper
  51.        *   number of times.  This has the effect of allocating
  52.        *   the correct number of bytes on the disk.
  53.        *
  54.        *******************************************************/
  55.  
  56.  
  57. create_allocate_tiff_file(file_name, image_header, image)
  58.    char   file_name[];
  59.    short  image[ROWS][COLS];
  60.    struct tiff_header_struct *image_header;
  61. {
  62.    char  buffer[12];
  63.  
  64.    int   bytes_written,
  65.          file_desc,
  66.          i,
  67.          j,
  68.          l,
  69.          w;
  70.  
  71.    long  k;
  72.  
  73.  
  74.  
  75.       /***************************************
  76.       *
  77.       *   Create the image file in binary mode
  78.       *   for both reading and writing.
  79.       *
  80.       ****************************************/
  81.  
  82.    file_desc = open(file_name,
  83.           O_CREAT | O_RDWR | O_BINARY,
  84.           S_IREAD | S_IWRITE);
  85.    printf("\n file desc=%d", file_desc);
  86.  
  87.       /***************************************
  88.       *
  89.       *   Write out the first 8 bytes of the
  90.       *   header.  The meaning of the
  91.       *   bytes (HEX) is:
  92.       *      0-1 = 49 49 - LSB first
  93.       *      2-3 = 2A 00 - version #
  94.       *      4-7 = 08 00 00 00 - go to offset
  95.       *           8 for the first
  96.       *           Image File
  97.       *           Directory
  98.       *
  99.       ****************************************/
  100.  
  101.    buffer[0] = 0x49;
  102.    buffer[1] = 0x49;
  103.    buffer[2] = 0x2A;
  104.    buffer[3] = 0x00;
  105.    buffer[4] = 0x08;
  106.    buffer[5] = 0x00;
  107.    buffer[6] = 0x00;
  108.    buffer[7] = 0x00;
  109.  
  110.    bytes_written = my_write(file_desc, buffer, 8);
  111.  
  112.    printf("\n wrote %d bytes", bytes_written);
  113.  
  114.       /***************************************
  115.       *
  116.       *   Write out the first 2 bytes of the
  117.       *   Image File Directory.  These tell
  118.       *   the number of entries in the IFD.
  119.       *
  120.       ****************************************/
  121.  
  122.    buffer[0] = 0x05;
  123.    buffer[1] = 0x00;
  124.    bytes_written = my_write(file_desc, buffer, 2);
  125.  
  126.    printf("\n wrote %d bytes", bytes_written);
  127.  
  128.       /***************************************
  129.       *
  130.       *   Write out the entries into the
  131.       *   Image File Directory.
  132.       *
  133.       ****************************************/
  134.  
  135.       /* Subfile Type */
  136.    buffer[0]  = 0xFF;
  137.    buffer[1]  = 0x00;
  138.    buffer[2]  = 0x03;
  139.    buffer[3]  = 0x00;
  140.    buffer[4]  = 0x01;
  141.    buffer[5]  = 0x00;
  142.    buffer[6]  = 0x00;
  143.    buffer[7]  = 0x00;
  144.    buffer[8]  = 0x01;
  145.    buffer[9]  = 0x00;
  146.    buffer[10] = 0x00;
  147.    buffer[11] = 0x00;
  148.  
  149.    bytes_written = my_write(file_desc, buffer, 12);
  150.  
  151.    printf("\n wrote %d bytes", bytes_written);
  152.  
  153.       /* Image Width */
  154.    insert_short_into_buffer(buffer, 0, 256);
  155.    insert_short_into_buffer(buffer, 2, 3);
  156.    insert_short_into_buffer(buffer, 4, 1);
  157.    insert_short_into_buffer(buffer, 8, image_header->image_width);
  158.  
  159.    bytes_written = my_write(file_desc, buffer, 12);
  160.    printf("\n wrote %d bytes", bytes_written);
  161.  
  162.       /* Image Length */
  163.    insert_short_into_buffer(buffer, 0, 257);
  164.    insert_short_into_buffer(buffer, 2, 3);
  165.    insert_short_into_buffer(buffer, 4, 1);
  166.    insert_short_into_buffer(buffer, 8, image_header->image_length);
  167.  
  168.    bytes_written = my_write(file_desc, buffer, 12);
  169.    printf("\n wrote %d bytes", bytes_written);
  170.  
  171.       /* Bits Per Pixel */
  172.    insert_short_into_buffer(buffer, 0, 258);
  173.    insert_short_into_buffer(buffer, 2, 3);
  174.    insert_short_into_buffer(buffer, 4, 1);
  175.    insert_short_into_buffer(buffer, 8, image_header->bits_per_pixel);
  176.  
  177.    bytes_written = my_write(file_desc, buffer, 12);
  178.    printf("\n wrote %d bytes", bytes_written);
  179.  
  180.       /* Strip Offset */
  181.    insert_short_into_buffer(buffer, 0, 273);
  182.    insert_short_into_buffer(buffer, 2, 3);
  183.    insert_short_into_buffer(buffer, 4, 1);
  184.    insert_short_into_buffer(buffer, 8, 74);
  185.    bytes_written = my_write(file_desc, buffer, 12);
  186.    printf("\n wrote %d bytes", bytes_written);
  187.  
  188.  
  189.       /* Offset to next IFD (0 means no more IFD's) */
  190.    buffer[0]  = 0x00;
  191.    buffer[1]  = 0x00;
  192.    buffer[2]  = 0x00;
  193.    buffer[3]  = 0x00;
  194.  
  195.    bytes_written = my_write(file_desc, buffer, 4);
  196.    printf("\n wrote %d bytes", bytes_written);
  197.    printf("\n length is %ld", image_header->image_length);
  198.    printf("\n width is %ld", image_header->image_width);
  199.  
  200.  
  201.    round_off_image_size(image_header, &l, &w);
  202.    k = l * w;
  203.  
  204.    if(image_header->bits_per_pixel == 8)
  205.       k = k/2;
  206.    else
  207.       k = k/4;
  208.    k++;
  209.  
  210.    for(i=0; i<ROWS; i++)
  211.       for(j=0; j<COLS; j++)
  212.         image[i][j] = 0;
  213.  
  214.    j = sizeof(short) * ROWS * COLS;
  215.  
  216.    for(i=0; i<k; i++){
  217.       bytes_written = my_write(file_desc, image, j);
  218.       printf("\n wrote %d bytes", bytes_written);
  219.    }
  220.  
  221.  
  222.    close(file_desc);
  223.  
  224. }  /* ends create_allocate_tiff_file */
  225.  
  226.  
  227.  
  228.  
  229.  
  230.        /*******************************************************
  231.        *
  232.        *   write_array_into_tiff_file(...
  233.        *
  234.        *   This function takes an array of shorts and writes
  235.        *   them into an existing tiff image file.
  236.        *
  237.        *******************************************************/
  238.  
  239.  
  240.  
  241. write_array_into_tiff_image(image_file_name, array,
  242.                             il, ie, ll, le)
  243.  
  244.         char    image_file_name[];
  245.         int     il, ie, ll, le;
  246.         short   array[ROWS][COLS];
  247. {
  248.  
  249.    char  buffer[COLS];
  250.  
  251.    int   bytes_written,
  252.          closed,
  253.          file_descriptor,
  254.          i,
  255.          written;
  256.  
  257.    float a;
  258.  
  259.    long  line_length,
  260.          offset,
  261.          position;
  262.  
  263.    struct tiff_header_struct image_header;
  264.  
  265.  
  266.  
  267.    read_tiff_header(image_file_name, &image_header);
  268.  
  269.  
  270.       /****************************************************
  271.       *
  272.       *   Procedure:
  273.       *   Seek to the strip offset where the data begins.
  274.       *   Seek to the first line you want.
  275.       *   Loop over the lines you want to write.
  276.       *      Seek to the first element of the line.
  277.       *      Write the line.
  278.       *      Seek to the end of the data in that line.
  279.       *
  280.       ****************************************************/
  281.  
  282.    file_descriptor = my_open(image_file_name);
  283.    position        = lseek(file_descriptor,
  284.                      image_header.strip_offset, 0);
  285.    position        = seek_to_first_line(file_descriptor,
  286.                                         &image_header, il);
  287.  
  288.    for(i=0; i<(ll-il); i++){
  289.       offset        = (ie-1)/(8/image_header.bits_per_pixel);
  290.       position      = lseek(file_descriptor, offset, 1);
  291.       bytes_written = write_line(file_descriptor, array,
  292.                                  i, &image_header, ie, le);
  293.       position      = seek_to_end_of_line(file_descriptor,
  294.                                           le, &image_header);
  295.       position      = lseek(file_descriptor, 1, 1); /*???*/
  296.    }  /* ends loop over i  */
  297.  
  298.    closed = close(file_descriptor);
  299.  
  300. }  /*  ends write_array_into_tiff_image */
  301.  
  302.  
  303.  
  304.  
  305.  
  306.  
  307.  
  308.  
  309.        /*******************************************************
  310.        *
  311.        *   write_line(...
  312.        *
  313.        *   This function takes an array of shorts, extracts the
  314.        *   numbers and puts them into a buffer, then writes
  315.        *   this buffer into a tiff file on disk.
  316.        *   The process depends on the number of bits per
  317.        *   pixel used in the file (4 or 8).
  318.        *
  319.        *******************************************************/
  320.  
  321. write_line(file_descriptor, array, line_number, image_header, ie, le)
  322.    int    file_descriptor, ie, le, line_number;
  323.    short  array[ROWS][COLS];
  324.    struct tiff_header_struct *image_header;
  325. {
  326.    char     buffer[100], first, second;
  327.    float    a, b;
  328.    int      bytes_written, i;
  329.    unsigned int bytes_to_write;
  330.    union    short_char_union scu;
  331.  
  332.    for(i=0; i<100; i++)
  333.       buffer[i] = '\0';
  334.  
  335.    bytes_to_write = (le-ie)/(8/image_header->bits_per_pixel);
  336.  
  337.    for(i=0; i<bytes_to_write; i++){
  338.  
  339.         /**********************************************
  340.         *
  341.         *   Use unions defined in cips.h to stuff shorts
  342.         *   into bytess.
  343.         *
  344.         *************************************************/
  345.  
  346.       if(image_header->bits_per_pixel == 8){
  347.        scu.s_num = 0;
  348.        scu.s_num = array[line_number][i];
  349.        buffer[i] = scu.s_alpha[0];
  350.       }  /* ends if bits_per_pixel == 8 */
  351.  
  352.  
  353.       if(image_header->bits_per_pixel == 4){
  354.  
  355.        scu.s_num = 0;
  356.        scu.s_num = array[line_number][i*2];
  357.        first     = scu.s_alpha[0] << 4;
  358.  
  359.        scu.s_num = 0;
  360.        scu.s_num = array[line_number][i*2];
  361.        second    = scu.s_alpha[0] & 0X000F;
  362.  
  363.        buffer[i] = first | second;
  364.       }  /* ends if bits_per_pixel == 4 */
  365.  
  366.    }  /*  ends loop over i  */
  367.  
  368.  
  369.    bytes_written  = write(file_descriptor, buffer, bytes_to_write);
  370.  
  371.    return(bytes_written);
  372.  
  373. }  /* ends write_line  */
  374.  
  375.  
  376.  
  377.  
  378.  
  379.  
  380.    /***************************************
  381.    *
  382.    *   insert_short_into_buffer(...
  383.    *
  384.    *   This inserts a two byte short into a
  385.    *   buffer of characters.  It does this
  386.    *   is LSB order.
  387.    *
  388.    ***************************************/
  389.  
  390.  
  391. insert_short_into_buffer(buffer, start, number)
  392.     char  buffer[];
  393.     int   start;
  394.     short number;
  395. {
  396.     union short_char_union lsu;
  397.  
  398.     lsu.s_num       = number;
  399.     buffer[start+0] = lsu.s_alpha[0];
  400.     buffer[start+1] = lsu.s_alpha[1];
  401.  
  402. }  /* ends insert_short_into_buffer */
  403.  
  404.  
  405.  
  406.  
  407.  
  408.  
  409.  
  410.  
  411.  
  412.  
  413.  
  414.  
  415.    /***************************************
  416.    *
  417.    *   insert_long_into_buffer(...
  418.    *
  419.    *   This inserts a four byte long into a
  420.    *   buffer of characters.  It does this
  421.    *   is LSB order.
  422.    *
  423.    ***************************************/
  424.  
  425.  
  426.  
  427. insert_long_into_buffer(buffer, start, number)
  428.     char buffer[];
  429.     int  start;
  430.     long number;
  431. {
  432.     union long_char_union lsu;
  433.  
  434.     lsu.l_num       = number;
  435.     buffer[start+0] = lsu.l_alpha[0];
  436.     buffer[start+1] = lsu.l_alpha[1];
  437.     buffer[start+2] = lsu.l_alpha[2];
  438.     buffer[start+3] = lsu.l_alpha[3];
  439.  
  440. }  /* ends insert_short_into_buffer */
  441.  
  442.  
  443.  
  444.  
  445.  
  446.  
  447.  
  448.    /***************************************
  449.    *
  450.    *   round_off_image_size(...
  451.    *
  452.    *   This takes the image header and rounds
  453.    *   it off to a multiple of ROWS and COLS.
  454.    *   e.g. if width=123 it returns 1.
  455.    *
  456.    ***************************************/
  457.  
  458.  
  459. round_off_image_size(image_header, length, width)
  460.     int    *length, *width;
  461.     struct tiff_header_struct *image_header;
  462. {
  463.    *length = (90 + image_header->image_length)/ROWS;
  464.    *width  = (90 + image_header->image_width)/COLS;
  465. } /* ends round_off_image_size */
  466.  
  467.  
  468.  
  469.  
  470.  
  471.    /***********************************************
  472.     *
  473.     *    does_not_exist(...
  474.     *
  475.     *    This function checks the disk to see if
  476.     *    a file exists.  If the file is there this
  477.     *    function returns a 0, if it does not exist
  478.     *    this function returns a 1.
  479.     *
  480.     ***********************************************/
  481.  
  482.  
  483. does_not_exist(file_name)
  484.     char file_name[];
  485. {
  486.    int file_desc, result;
  487.  
  488.    result = 1;
  489.    file_desc = open(file_name, O_BINARY | O_RDONLY);
  490.    if(file_desc > 0)
  491.       result = 0;
  492.    close(file_desc);
  493.    return(result);
  494. }  /* ends does_not_exist */
  495.  
  496.  
  497.  
  498.