home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_300 / 343_01 / cips3.c < prev    next >
C/C++ Source or Header  |  1992-04-10  |  15KB  |  513 lines

  1.  
  2.        /***********************************************
  3.        *
  4.        *       file d:\cips\addsub.c
  5.        *
  6.        *       Functions: This file contains
  7.        *          add_image_array
  8.        *          subtract_image_array
  9.        *
  10.        *       Purpose:
  11.        *          These functions implement
  12.        *          image addition and subtraction.
  13.        *
  14.        *       External Calls:
  15.        *          wtiff.c - does_not_exist
  16.        *                    round_off_image_size
  17.        *                    create_allocate_tiff_file
  18.        *                    write_array_into_tiff_image
  19.        *          tiff.c - read_tiff_header
  20.        *          rtiff.c - read_tiff_image
  21.        *
  22.        *
  23.        *       Modifications:
  24.        *          1 April 1992 - created
  25.        *
  26.        *************************************************/
  27.  
  28. #include "d:\cips\cips.h"
  29.  
  30.      /*******************************************
  31.      *
  32.      *   add_image_array(...
  33.      *
  34.      *   This function adds two ROWSxCOLS image
  35.      *   sections.  The image file named out_name
  36.      *   will receive the sum of the image file
  37.      *   named in1_name and the image file
  38.      *   named in2_name.
  39.      *
  40.      *******************************************/
  41.  
  42.  
  43. add_image_array(in1_name, in2_name, out_name, the_image, out_image,
  44.           il1, ie1, ll1, le1,
  45.           il2, ie2, ll2, le2,
  46.           il3, ie3, ll3, le3)
  47.    char   in1_name[], in2_name[], out_name[];
  48.    int    il1, ie1, ll1, le1,
  49.           il2, ie2, ll2, le2,
  50.           il3, ie3, ll3, le3;
  51.    short  the_image[ROWS][COLS],
  52.           out_image[ROWS][COLS];
  53.  
  54. {
  55.    int    i, j, length, max, width;
  56.    struct tiff_header_struct image_header;
  57.  
  58.  
  59.    if(does_not_exist(out_name)){
  60.       printf("\n\n output file does not exist %s", out_name);
  61.       read_tiff_header(in1_name, &image_header);
  62.       round_off_image_size(&image_header,
  63.                            &length, &width);
  64.       image_header.image_length = length*ROWS;
  65.       image_header.image_width  = width*COLS;
  66.       create_allocate_tiff_file(out_name, &image_header,
  67.                                 out_image);
  68.    }  /* ends if does_not_exist */
  69.  
  70.    read_tiff_header(in1_name, &image_header);
  71.  
  72.    max = 255;
  73.    if(image_header.bits_per_pixel == 4)
  74.       max = 16;
  75.  
  76.    read_tiff_image(in1_name, the_image, il1, ie1, ll1, le1);
  77.    read_tiff_image(in2_name, out_image, il2, ie2, ll2, le2);
  78.  
  79.    for(i=0; i<ROWS; i++){
  80.       for(j=0; j<COLS; j++){
  81.          out_image[i][j] = the_image[i][j] + out_image[i][j];
  82.          if(out_image[i][j] > max)
  83.             out_image[i][j] = max;
  84.       }  /* ends loop over j */
  85.    }  /* ends loop over i */
  86.  
  87.    write_array_into_tiff_image(out_name, out_image,
  88.                                il3, ie3, ll3, le3);
  89.  
  90. }  /* ends add_image_array */
  91.  
  92.  
  93.  
  94.  
  95.  
  96.      /*******************************************
  97.      *
  98.      *   subtract_image_array(...
  99.      *
  100.      *   This function subtracts two ROWSxCOLS image
  101.      *   sections.  The image file named out_name
  102.      *   will receive the difference of the image file
  103.      *   named in1_name and the image file
  104.      *   named in2_name.
  105.      *
  106.      *   out_name = in1_name - in2_name
  107.      *
  108.      *******************************************/
  109.  
  110.  
  111. subtract_image_array(in1_name, in2_name, out_name, the_image, out_image,
  112.           il1, ie1, ll1, le1,
  113.           il2, ie2, ll2, le2,
  114.           il3, ie3, ll3, le3)
  115.    char   in1_name[], in2_name[], out_name[];
  116.    int    il1, ie1, ll1, le1,
  117.           il2, ie2, ll2, le2,
  118.           il3, ie3, ll3, le3;
  119.    short  the_image[ROWS][COLS],
  120.           out_image[ROWS][COLS];
  121.  
  122. {
  123.    int    i, j, length, width;
  124.    struct tiff_header_struct image_header;
  125.  
  126.  
  127.    if(does_not_exist(out_name)){
  128.       printf("\n\n output file does not exist %s", out_name);
  129.       read_tiff_header(in1_name, &image_header);
  130.       round_off_image_size(&image_header,
  131.                            &length, &width);
  132.       image_header.image_length = length*ROWS;
  133.       image_header.image_width  = width*COLS;
  134.       create_allocate_tiff_file(out_name, &image_header,
  135.                                 out_image);
  136.    }  /* ends if does_not_exist */
  137.  
  138.    read_tiff_header(in1_name, &image_header);
  139.  
  140.    read_tiff_image(in1_name, the_image, il1, ie1, ll1, le1);
  141.    read_tiff_image(in2_name, out_image, il2, ie2, ll2, le2);
  142.  
  143.    for(i=0; i<ROWS; i++){
  144.       for(j=0; j<COLS; j++){
  145.          out_image[i][j] = the_image[i][j] - out_image[i][j];
  146.          if(out_image[i][j] < 0)
  147.             out_image[i][j] = 0;
  148.       }  /* ends loop over j */
  149.    }  /* ends loop over i */
  150.  
  151.    write_array_into_tiff_image(out_name, out_image,
  152.                                il3, ie3, ll3, le3);
  153.  
  154. }  /* ends subtract_image_array */
  155.  
  156.        /***********************************************
  157.        *
  158.        *       file d:\cips\cutp.c
  159.        *
  160.        *       Functions: This file contains
  161.        *          cut_image_piece
  162.        *          paste_image_piece
  163.        *          check_cut_and_paste_limits
  164.        *
  165.        *       Purpose:
  166.        *          These functions cut pieces out
  167.        *          of images and paste them back into
  168.        *          images.
  169.        *
  170.        *       External Calls:
  171.        *          wtiff.c - does_not_exist
  172.        *                    round_off_image_size
  173.        *                    create_allocate_tiff_file
  174.        *                    write_array_into_tiff_image
  175.        *          tiff.c - read_tiff_header
  176.        *          rtiff.c - read_tiff_image
  177.        *
  178.        *
  179.        *       Modifications:
  180.        *          3 April 1992 - created
  181.        *
  182.        *************************************************/
  183.  
  184.  
  185.      /*******************************************
  186.      *
  187.      *   cut_image_piece(...
  188.      *
  189.      *   This function cuts out a rectangular
  190.      *   piece of an image.  This rectangle can
  191.      *   be any shape so long as no dimension
  192.      *   is greater than ROWS or COLS.
  193.      *
  194.      *******************************************/
  195.  
  196.  
  197. cut_image_piece(name, the_image, il, ie, ll, le)
  198.    char   name[];
  199.    int    il, ie, ll, le;
  200.    short  the_image[ROWS][COLS];
  201.  
  202. {
  203.    if(does_not_exist(name)){
  204.       printf("\n\ncut_image_piece>> ERROR "
  205.              "image file does not exist %s", name);
  206.       return(-1);
  207.    }  /* ends if does_not_exist */
  208.  
  209.    read_tiff_image(name, the_image, il, ie, ll, le);
  210.  
  211. }  /* ends cut_image_piece */
  212.  
  213.  
  214.  
  215.  
  216.      /*******************************************
  217.      *
  218.      *   paste_image_piece(...
  219.      *
  220.      *   This function pastes a rectangular
  221.      *   piece of an image into another image.
  222.      *   This rectangle can be any shape so long
  223.      *   as no dimension is greater than ROWS or COLS.
  224.      *   The rectangle to be pasted into the image
  225.      *   is described by the il, ie, ll, le
  226.      *   parameters.
  227.      *
  228.      *   You pass is the out_image array just in
  229.      *   case you need to allocate the destination
  230.      *   image.
  231.      *
  232.      *******************************************/
  233.  
  234.  
  235. paste_image_piece(dest_name, source_name, the_image,
  236.                   out_image, il, ie, ll, le)
  237.    char   dest_name[], source_name[];
  238.    int    il, ie, ll, le;
  239.    short  the_image[ROWS][COLS],
  240.           out_image[ROWS][COLS];
  241.  
  242. {
  243.    struct tiff_header_struct image_header;
  244.  
  245.    if(does_not_exist(dest_name)){
  246.       printf("\n\ncut_image_piece>> "
  247.              "image file does not exist %s", dest_name);
  248.       read_tiff_header(source_name, &image_header);
  249.       create_allocate_tiff_file(dest_name, &image_header,
  250.                                 out_image);
  251.    }  /* ends if does_not_exist */
  252.  
  253.    write_array_into_tiff_image(dest_name, the_image,
  254.                                il, ie, ll, le);
  255.  
  256. }  /* ends paste_image_piece */
  257.  
  258.  
  259.  
  260.      /*******************************************
  261.      *
  262.      *   check_cut_and_paste_limits(...
  263.      *
  264.      *   This function looks at the line and
  265.      *   element parameters and ensures that they
  266.      *   are not bigger than ROWS and COLS.  If
  267.      *   they are bigger, the last element or
  268.      *   last line parameters are reduced.
  269.      *
  270.      *******************************************/
  271.  
  272. check_cut_and_paste_limits(il, ie, ll, le)
  273.    int *il, *ie, *ll, *le;
  274. {
  275.    if((*ll - *il) > ROWS)
  276.       *ll = *il + ROWS;
  277.    if((*le - *ie) > COLS)
  278.       *le = *ie + COLS;
  279. }  /* ends check_cut_and_paste_limits */
  280.  
  281.  
  282.  
  283.        /***********************************************
  284.        *
  285.        *       file d:\cips\rotate.c
  286.        *
  287.        *       Functions: This file contains
  288.        *          rotate_flip_image_array
  289.        *
  290.        *       Purpose:
  291.        *          This function rotates or flips an image
  292.        *          array in one of five ways.
  293.        *
  294.        *       External Calls:
  295.        *          wtiff.c - does_not_exist
  296.        *                    round_off_image_size
  297.        *                    create_allocate_tiff_file
  298.        *                    write_array_into_tiff_image
  299.        *          tiff.c - read_tiff_header
  300.        *          rtiff.c - read_tiff_image
  301.        *
  302.        *
  303.        *       Modifications:
  304.        *          1 April 1992 - created
  305.        *
  306.        *************************************************/
  307.  
  308.  
  309.      /*******************************************
  310.      *
  311.      *   rotate_flip_image_array(...
  312.      *
  313.      *   This function rotates an image array
  314.      *   in one of three ways or flips an image
  315.      *   array either vertically or horizontally.
  316.      *   The rotation_type parameter specifies
  317.      *   the operation.  When rotation_type is
  318.      *   1, 2, or 3 you rotate.  When it is
  319.      *   4 or 5 you flip.
  320.      *
  321.      *   I define rotation as this:  Pin down the
  322.      *   lower left hand corner of the image array
  323.      *   and rotate the image 90 degrees clockwise.
  324.      *   1 rotation is 90 degrees, 2 rotations are
  325.      *   180 degrees, and 3 rotations are 270 degrees.
  326.      *   4 rotations bring you back to where you
  327.      *   started.
  328.      *
  329.      *   The cases are:
  330.      *
  331.      *   If the input image array is:
  332.      *        1 2 3
  333.      *        4 5 6
  334.      *        7 8 9
  335.      *
  336.      *   Rotate # 1 - the result is:
  337.      *        7 4 1
  338.      *        8 5 2
  339.      *        9 6 3
  340.      *
  341.      *   Rotate # 2 - the result is:
  342.      *        9 8 7
  343.      *        6 5 4
  344.      *        3 2 1
  345.      *
  346.      *   Rotate # 3 - the result is:
  347.      *        3 6 9
  348.      *        2 5 8
  349.      *        1 4 7
  350.      *
  351.      *   Flip # 4 - horizontal the result is:
  352.      *        3 2 1
  353.      *        6 5 4
  354.      *        9 8 7
  355.      *
  356.      *   Flip # 5 - vertical the result is:
  357.      *        7 8 9
  358.      *        4 5 6
  359.      *        1 2 3
  360.      *
  361.      *
  362.      *   The in_file is the source image with
  363.      *   parameters given by il1, ie1, ll1, le1.
  364.      *
  365.      *   The out_file is the destination image with
  366.      *   parameters given by il2, ie2, ll2, le2.
  367.      *
  368.      *******************************************/
  369.  
  370.  
  371. rotate_flip_image_array(in_name, out_name, the_image,
  372.                    out_image, il1, ie1, ll1, le1,
  373.                    il2, ie2, ll2, le2, rotation_type)
  374.    char  in_name[], out_name[];
  375.    int   il1, ie1, ll1, le1,
  376.          il2, ie2, ll2, le2, rotation_type;
  377.    short the_image[ROWS][COLS],
  378.          out_image[ROWS][COLS];
  379. {
  380.    int    cd2, i, j, length, rd2, type, width;
  381.    struct tiff_header_struct image_header;
  382.  
  383.    if(does_not_exist(out_name)){
  384.       printf("\n\n output file does not exist %s", out_name);
  385.       read_tiff_header(in_name, &image_header);
  386.       round_off_image_size(&image_header,
  387.                            &length, &width);
  388.       image_header.image_length = length*ROWS;
  389.       image_header.image_width  = width*COLS;
  390.       create_allocate_tiff_file(out_name, &image_header,
  391.                                 out_image);
  392.    }  /* ends if does_not_exist */
  393.  
  394.      /*******************************************
  395.      *
  396.      *   Check the rotation_type.  If it is not
  397.      *   a valid value, set it to 1.
  398.      *
  399.      *******************************************/
  400.  
  401.    type = rotation_type;
  402.    if(type != 1  &&
  403.       type != 2  &&
  404.       type != 3  &&
  405.       type != 4  &&
  406.       type != 5) type = 1;
  407.  
  408.    read_tiff_image(in_name, the_image, il1, ie1, ll1, le1);
  409.  
  410.      /*******************************************
  411.      *
  412.      *   Rotate the image array as desired.
  413.      *
  414.      *******************************************/
  415.  
  416.      /*******************************************
  417.      *
  418.      *   1 90 degree rotation
  419.      *
  420.      *******************************************/
  421.  
  422.    if(type == 1  ||  type == 2  ||  type == 3){
  423.       for(i=0; i<ROWS; i++){
  424.          for(j=0; j<COLS; j++)
  425.             out_image[j][COLS-1-i] = the_image[i][j];
  426.       }  /* ends loop over i */
  427.    }  /* ends if type == 1 or 2 or 3 */
  428.  
  429.  
  430.      /*******************************************
  431.      *
  432.      *   a second 90 degree rotation
  433.      *
  434.      *******************************************/
  435.  
  436.    if(type == 2  ||  type == 3){
  437.       for(i=0; i<ROWS; i++)
  438.          for(j=0; j<COLS; j++)
  439.             the_image[i][j] = out_image[i][j];
  440.       for(i=0; i<ROWS; i++){
  441.          for(j=0; j<COLS; j++)
  442.             out_image[j][COLS-1-i] = the_image[i][j];
  443.       }  /* ends loop over i */
  444.    }  /* ends if type == 2 or 3 */
  445.  
  446.  
  447.      /*******************************************
  448.      *
  449.      *   a third 90 degree rotation
  450.      *
  451.      *******************************************/
  452.  
  453.    if(type == 3){
  454.       for(i=0; i<ROWS; i++)
  455.          for(j=0; j<COLS; j++)
  456.             the_image[i][j] = out_image[i][j];
  457.       for(i=0; i<ROWS; i++){
  458.          for(j=0; j<COLS; j++)
  459.             out_image[j][COLS-1-i] = the_image[i][j];
  460.       }  /* ends loop over i */
  461.    }  /* ends if type == 3 */
  462.  
  463.  
  464.      /*******************************************
  465.      *
  466.      *   Flip the image array horizontally
  467.      *   about the center vertical axis.
  468.      *
  469.      *******************************************/
  470.  
  471.    if(type == 4){
  472.       cd2 = COLS/2;
  473.       for(j=0; j<cd2; j++){
  474.          for(i=0; i<ROWS; i++){
  475.             out_image[i][COLS-1-j] = the_image[i][j];
  476.          }  /* ends loop over i */
  477.       }  /* ends loop over j */
  478.  
  479.       for(j=cd2; j<COLS; j++){
  480.          for(i=0; i<ROWS; i++){
  481.             out_image[i][COLS-1-j] = the_image[i][j];
  482.          }  /* ends loop over i */
  483.       }  /* ends loop over j */
  484.    }  /* ends if type == 4 */
  485.  
  486.  
  487.      /*******************************************
  488.      *
  489.      *   Flip the image array vertically
  490.      *   about the center horizontal axis.
  491.      *
  492.      *******************************************/
  493.  
  494.    if(type == 5){
  495.       rd2 = ROWS/2;
  496.       for(i=0; i<rd2; i++){
  497.          for(j=0; j<COLS; j++){
  498.             out_image[ROWS-1-i][j] = the_image[i][j];
  499.          }  /* ends loop over j */
  500.       }  /* ends loop over i */
  501.  
  502.       for(i=rd2; i<ROWS; i++){
  503.          for(j=0; j<COLS; j++){
  504.             out_image[ROWS-1-i][j] = the_image[i][j];
  505.          }  /* ends loop over j */
  506.       }  /* ends loop over i */
  507.    }  /* ends if type == 5 */
  508.  
  509.    write_array_into_tiff_image(out_name, out_image,
  510.                                il2, ie2, ll2, le2);
  511.  
  512. }  /* ends rotate_flip_image_array */
  513.