📄 brwtiff.c
字号:
#include "cips.h"
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 */
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 */
/*********************************************
*
* 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.
* 21 April 1998 - modified to work with
* an entire image at one time.
*
*
*
**********************************************/
read_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 = read_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 read_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).
*
**********************************************/
read_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 read_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 */
/**********************************************
*
*
* 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.
* 21 April 1998 - modified to work with
* an entire image at one time.
*
*********************************************/
/**********************************************
*
* 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.
*
***********************************************/
create_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);
create_allocate_tiff_file(out_name, &image_header);
printf("\nBFIN> Created %s", out_name);
} /* ends if does_not_exist */
} /* ends create_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).
*
***************************************************/
create_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);
/***************************************
*
* Write out the entries into the
* Image File Directory.
*
****************************************/
/* New Subfile Type */
buffer[0] = 0xFE;
buffer[1] = 0x00;
buffer[2] = 0x03;
buffer[3] = 0x00;
buffer[4] = 0x01;
buffer[5] = 0x00;
buffer[6] = 0x00;
buffer[7] = 0x00;
buffer[8] = 0x00;
buffer[9] = 0x00;
buffer[10] = 0x00;
buffer[11] = 0x00;
bytes_written = fwrite(buffer, 1, 12, image_file);
printf("\n wrote %d bytes", bytes_written);
/* Subfile Type */
buffer[0] = 0xFF;
buffer[1] = 0x00;
buffer[2] = 0x03;
buffer[3] = 0x00;
buffer[4] = 0x01;
buffer[5] = 0x00;
buffer[6] = 0x00;
buffer[7] = 0x00;
buffer[8] = 0x01;
buffer[9] = 0x00;
buffer[10] = 0x00;
buffer[11] = 0x00;
bytes_written = fwrite(buffer, 1, 12, image_file);
printf("\n wrote %d bytes", bytes_written);
/* Image Width */
insert_short_into_buffer(buffer, 0, 256);
insert_short_into_buffer(buffer, 2, 3);
insert_short_into_buffer(buffer, 4, 1);
insert_short_into_buffer(buffer, 8,
image_header->image_width);
bytes_written = fwrite(buffer, 1, 12, image_file);
printf("\n wrote %d bytes", bytes_written);
/* Image Length */
insert_short_into_buffer(buffer, 0, 257);
insert_short_into_buffer(buffer, 2, 3);
insert_short_into_buffer(buffer, 4, 1);
insert_short_into_buffer(buffer, 8,
image_header->image_length);
bytes_written = fwrite(buffer, 1, 12, image_file);
printf("\n wrote %d bytes", bytes_written);
/* Bits Per Sample */
insert_short_into_buffer(buffer, 0, 258);
insert_short_into_buffer(buffer, 2, 3);
insert_short_into_buffer(buffer, 4, 1);
insert_short_into_buffer(buffer, 8,
image_header->bits_per_pixel);
bytes_written = fwrite(buffer, 1, 12, image_file);
printf("\n wrote %d bytes", bytes_written);
/* Compression - None */
insert_short_into_buffer(buffer, 0, 259);
insert_short_into_buffer(buffer, 2, 3);
insert_short_into_buffer(buffer, 4, 1);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -