⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bigc.c

📁 This is code tutorial for image processing include:histogram,sketon....
💻 C
📖 第 1 页 / 共 2 页
字号:
#include "cips.h"




main(argc, argv)
   int argc;
   char *argv[];
{
   char  image_name[MAX_NAME_LENGTH];
   char  image_name2[MAX_NAME_LENGTH];
   char  response[MAX_NAME_LENGTH];
   int   i, j;
   short **the_image, **out_image;
   short filter[3][3];
   struct   tiff_header_struct image_header;
   
      /******************************************
      *
      *   Ensure the command line is correct.
      *
      ******************************************/

   if(argc < 2){
    printf("\n\nNot enough parameters:");
    printf("\n");
    printf("\n   usage: bigc image-name");
    exit(0);
   }

   strcpy(image_name, argv[1]);
   strcpy(image_name2, argv[2]);

   if(does_not_exist(image_name)){
      printf("\n%s does not exist, quitting", 
          image_name);
      exit(1);
   }  /* ends if does_not_exist */
     


   read_tiff_header(image_name, &image_header);
   
      /******************************************
      *
      *   This is how to malloc a 2D array.
      *   I tried to get this into a subroutine
      *   but not having any luck with that
      *   19 April 98
      *
      ******************************************/

   the_image = malloc(image_header.image_length * sizeof(short  *));
   for(i=0; i<image_header.image_length; i++){
      the_image[i] = malloc(image_header.image_width * sizeof(short ));
      if(the_image[i] == '\0'){
         printf("\n\tmalloc of the_image[%d] failed", i);
         exit(0);
      }  /* ends if */
   }  /* ends loop over i */

   out_image = malloc(image_header.image_length * sizeof(short  *));
   for(i=0; i<image_header.image_length; i++){
      out_image[i] = malloc(image_header.image_width * sizeof(short ));
      if(out_image[i] == '\0'){
         printf("\n\tmalloc of out_image[%d] failed", i);
         exit(0);
      }  /* ends if */
   }  /* ends loop over i */




   
      /******************************************
      *
      *   I can read an image correctly
      *
      ******************************************/

   bread_tiff_image(image_name, the_image);
   
   for(i=0; i<15; i++){
      printf("\n");
      for(j=0; j<15; j++){
         printf("%4d", the_image[i][j]);
      }
   }


      /******************************************
      *
      *   I can create an image file
      *
      ******************************************/
   bcreate_file_if_needed(image_name, "stupid.tif", the_image);
   
   for(i=0; i<15; i++){
      printf("\n");
      for(j=0; j<15; j++){
         printf("%4d", the_image[i][j]);
      }
   }
   bwrite_tiff_image("stupid.tif", the_image);
printf("\n1Holding");
printf("\n2Holding");
printf("\n3Holding");

      /******************************************
      *
      *   This is how to free a 2D array.
      *
      ******************************************/
   
   free_image_array(the_image, 
      image_header.image_length);
#ifdef NEVER
   for(i=0; i<image_header.image_length; i++)
      free(the_image[i]);
#endif

}  /* ends main */


#ifdef NEVER
short *allocate_image_array(length, width)
   long  length, width;
{
   int i;
   short **the_array;

   the_array = malloc(length * sizeof(short  *));
   for(i=0; i<length; i++){
      the_array[i] = malloc(width * sizeof(short ));
      if(the_array[i] == '\0'){
         printf("\n\tmalloc of the_image[%d] failed", i);
      }  /* ends if */
   }  /* ends loop over i */
   return(the_array);

}  /* ends allocate_image_array */
#endif

int free_image_array(the_array, length)
   short **the_array;
   long  length;
{
   int i;
   for(i=0; i<length; i++)
      free(the_array[i]);
   return(1);
}  /* ends free_image_array */


       /*********************************************
       *
       *  file d:\cips\rtiff.c
       *
       *  Functions: This file contains
       *          read_tiff_image
       *          read_line
       *          seek_to_first_line
       *          seek_to_end_of_line
       *
       *  Purpose:
       *  These functions read a TIFF image and insert
       *  the data into a ROWSxCOLS array of short.
       *
       *       NOTE: The fseek constants are
       *             0=SEEK_SET = beginning of file
       *             1=SEEK_CUR = current offset
       *             2=SEEK_END = end of file
       *
       *  External Calls:
       *  tiff.c - read_tiff_header
       *
       *  Modifications:
       *       25 June 1990 - created
       *       27 March 1993 - use fopen, fread, fseek
       *           instead of the earlier open, read,
       *           seek, etc.
       *
       **********************************************/



bread_tiff_image(image_file_name, the_image)
      char   image_file_name[];
      short   **the_image;
{
   char  *buffer,  /* CHANGED */
         rep[80];
   int   bytes_read,
         closed,
         position,
         i,
         j;
   FILE  *image_file;
   float a;
   long  line_length, offset;

   struct tiff_header_struct image_header;

   read_tiff_header(image_file_name, &image_header);

      /***********************************************
      *
      *   Procedure:
      *   Seek to the strip offset where the data begins.
      *   Seek to the first line you want.
      *   Loop over the lines you want to read:
      *      Seek to the first element of the line.
      *      Read the line.
      *      Seek to the end of the data in that line.
      *
      ************************************************/

   image_file = fopen(image_file_name, "rb");
   if(image_file != NULL){
      position = fseek(image_file, 
                       image_header.strip_offset, 
                       SEEK_SET);

      for(i=0; i<image_header.image_length; i++){

         bytes_read   = bread_line(image_file, the_image, 
                                  i, &image_header, 
                                  0, image_header.image_width);
      }  /* ends loop over i  */

      closed = fclose(image_file);
   }  /* ends if file opened ok */
   else{
      printf("\nRTIFF.C> ERROR - cannot open "
             "tiff file");
   }

}  /*  ends bread_tiff_image */




       /**********************************************
       *
       *   read_line(...
       *
       *   This function reads bytes from the TIFF 
       *   file into a buffer, extracts the numbers 
       *   from that buffer, and puts them into a 
       *   ROWSxCOLS array of shorts. The process 
       *   depends on the number of bits per pixel used 
       *   in the file (4 or 8).
       *
       **********************************************/

bread_line(image_file, the_image, line_number, 
          image_header, ie, le)
   FILE   *image_file;
   int    ie, le, line_number;
   short  **the_image;
   struct tiff_header_struct *image_header;
{
   char  *buffer, first, second;
   float a, b;
   int bytes_read, i;
   unsigned int bytes_to_read;
   union short_char_union scu;


   buffer = (char  *) malloc(image_header->image_width * sizeof(char ));
   for(i=0; i<image_header->image_width; i++)
      buffer[i] = '\0';

        /********************************************
        *
        *   Use the number of bits per pixel to 
        *   calculate how many bytes to read.
        *
        ********************************************/

   bytes_to_read = (le-ie)/
                   (8/image_header->bits_per_pixel);
   bytes_read    = fread(buffer, 1, bytes_to_read, 
                         image_file);

   for(i=0; i<bytes_read; i++){

        /*********************************************
        *
        *   Use unions defined in cips.h to stuff bytes
        *   into shorts.
        *
        **********************************************/

      if(image_header->bits_per_pixel == 8){
       scu.s_num          = 0;
       scu.s_alpha[0]        = buffer[i];
       the_image[line_number][i] = scu.s_num;
      }  /* ends if bits_per_pixel == 8 */

      if(image_header->bits_per_pixel == 4){

       scu.s_num             = 0;
       second                = buffer[i] & 0X000F;
       scu.s_alpha[0]        = second;
       the_image[line_number][i*2+1] = scu.s_num;

       scu.s_num             = 0;
       first                 = buffer[i] >> 4;
       first                 = first & 0x000F;
       scu.s_alpha[0]        = first;
       the_image[line_number][i*2] = scu.s_num;

      }  /* ends if bits_per_pixel == 4 */

   }  /*  ends loop over i  */

   free(buffer);
   return(bytes_read);

}  /* ends bread_line  */





       /*********************************************
       *
       *   seek_to_first_line(...
       *
       **********************************************/

seek_to_first_line(image_file, image_header, il)
   FILE   *image_file;
   int    il;
   struct tiff_header_struct *image_header;
{
   long offset;
   int  position;

   offset   = (il-1)*image_header->image_width/
             (8/image_header->bits_per_pixel);
      /* seek from current position */
   position = fseek(image_file, offset, SEEK_CUR);
   return(position);
}  /* ends seek_to_first_line */





       /**********************************************
       *
       *   seek_to_end_of_line(...
       *
       ***********************************************/

seek_to_end_of_line(image_file, le, image_header)
   FILE   *image_file;
   int    le;
   struct tiff_header_struct *image_header;
{
   long  offset;
   int   position;

   offset   = (image_header->image_width-le)/
             (8/image_header->bits_per_pixel);
   position = fseek(image_file, offset, SEEK_CUR);
   return(position);
}  /* ends seek_to_end_of_line         */






       /**********************************************
       *
       *  file d:\cips\wtiff.c
       *
       *  Functions: This file contains
       *      create_file_if_needed
       *      create_allocate_tiff_file
       *      write_array_into_tiff_image
       *      write_line
       *      insert_short_into_buffer
       *      insert_long_into_buffer
       *      round_off_image_size
       *      does_not_exist
       *
       *  Purpose:
       *     These functions create TIFF image files 
       *     on disk and insert a ROWSxCOLS array
       *     into a tiff image already stored on disk.
       *
       *  External Calls:
       *     rtiff.c - seek_to_first_line
       *               seek_to_end_of_line
       *     tiff.c - read_tiff_header
       *
       *  Modifications:
       *     29 January 1991 - created
       *     28 March 1993 - replaced open, lseek
       *         etc. with fopen, fseek, etc.
       *     10 May 1993 - added a number of tags
       *         to make the TIFF files I create
       *         TIFF 6.0 Gray Scale image compliant.
       *
       *********************************************/






       /**********************************************
       *
       *   create_file_if_needed(...
       *
       *   This function creates a file on disk
       *   if it does not exist.  The out file is
       *   patterned after the in file.
       *
       ***********************************************/

bcreate_file_if_needed(in_name, out_name, out_image)
   char in_name[], out_name[];
   short **out_image;
{
   int    length, width;
   struct tiff_header_struct image_header;

   if(does_not_exist(out_name)){
      printf("\n\n output file does not exist %s",
               out_name);
      read_tiff_header(in_name, &image_header);
      bcreate_allocate_tiff_file(out_name, &image_header,
                                 out_image);
   }  /* ends if does_not_exist */
printf("\nBFIN> Created %s", out_name);
}  /* ends bcreate_file_if_needed */





   /**********************************************
   *
   *   create_alllocate_tiff_file(...
   *
   *   This function creates a file on disk that will be
   *   large enough to hold a tiff image.  The input
   *   tiff_header_struct describes the desired tiff file.
   *   This function writes the tiff header and then
   *   writes a blank image array out to disk the proper
   *   number of times.  This has the effect of allocating
   *   the correct number of bytes on the disk.
   *
   *   There will be 18 entries in the IFD.
   *   The image data will begin at byte 296.
   *   I will use LSB first data.
   *   I will have one strip for the entire image.
   *   Black is zero.
   *   The component values for the image are CHUNKY
   *      (Planer configuration = 1).
   *
   ***************************************************/


bcreate_allocate_tiff_file(file_name, 
                          image_header)
   char   file_name[];
   struct tiff_header_struct *image_header;
{
   char  buffer[12], *image_buffer, long_buffer[50];
   FILE  *image_file;
   int   bytes_written,
         i,
         j,
         l,
         w;

   long  k;



      /***************************************
      *
      *   Create the image file in binary mode
      *   for both reading and writing.
      *
      ****************************************/

   image_file = fopen(file_name, "wb");

      /***************************************
      *
      *   Write out the first 8 bytes of the
      *   header.  The meaning of the
      *   bytes (HEX) is:
      *      0-1 = 49 49 - LSB first
      *      2-3 = 2A 00 - version #
      *      4-7 = 08 00 00 00 - go to offset
      *           8 for the first
      *           Image File
      *           Directory
      *
      ****************************************/

   buffer[0] = 0x49;
   buffer[1] = 0x49;
   buffer[2] = 0x2A;
   buffer[3] = 0x00;
   buffer[4] = 0x08;
   buffer[5] = 0x00;
   buffer[6] = 0x00;
   buffer[7] = 0x00;

   bytes_written = fwrite(buffer, 1, 8, image_file);

   printf("\n wrote %d bytes", bytes_written);

      /***************************************
      *
      *   Write out the first 2 bytes of the
      *   Image File Directory.  These tell
      *   the number of entries in the IFD.
      *
      ****************************************/

   buffer[0] = 0x12;
   buffer[1] = 0x00;
   bytes_written = fwrite(buffer, 1, 2, image_file);

   printf("\n wrote %d bytes", bytes_written);

      /***************************************

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -