📄 imageio.c
字号:
/*********************************************
*
* file imageio.c
*
* Functions: This file contains
* calculate_pad
*
* create_tiff_file_if_needed
* create_allocate_tiff_file
* create_allocate_bmp_file
* create_bmp_file_if_needed
* create_file_if_needed
* create_image_file
* create_resized_image_file
*
* does_not_exist
* are_not_same_size
*
* extract_long_from_buffer
* extract_short_from_buffer
* extract_ulong_from_buffer
* extract_ushort_from_buffer
*
* equate_tiff_headers
* equate_bmpfileheaders
* equate_bitmapheaders
*
* insert_short_into_buffer
* insert_ushort_into_buffer
* insert_long_into_buffer
* insert_ulong_into_buffer
*
* allocate_image_array
* free_image_array
* flip_image_array
*
* get_image_size
* get_bitsperpixel
* get_lsb
*
* is_a_bmp
* is_a_tiff
*
* print_bmp_file_header
* print_bm_file_header
* print_color_table
*
* read_tiff_header
* read_tiff_image
* read_line
* read_bmp_file_header
* read_bm_header
* read_color_table
* read_bmp_image
* read_image_array
*
* round_off_image_size
*
* seek_to_first_line
* seek_to_end_of_line
*
* write_array_into_tiff_file
* write_tiff_image
* write_bmp_image
* write_image_array
* write_line
*
* Purpose:
* This file contains the subroutines that
* read and write tiff and bmp image files.
*
* External Calls:
* none
*
* Modifications:
* 23 June 1990 - created
* 28 March 1993 - using fopen, fread, fseek
* instead of my_open, my_read, lseek.
* 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.
* 18 September 1998 - imagio.c was created
* by combining tiff.c brwtiff.c and
* functions written for bmp file io.
*
************************************************/
#include "cips.h"
/***********************************************
*
* read_tiff_header(...
*
* This function reads the header of a TIFF
* file and places the needed information into
* the struct tiff_header_struct.
*
***********************************************/
read_tiff_header(file_name, image_header)
char file_name[];
struct tiff_header_struct *image_header;
{
char buffer[12], response[80];
FILE *image_file;
int bytes_read,
closed,
i,
j,
lsb,
not_finished,
position;
long bits_per_pixel,
image_length,
image_width,
length_of_field,
offset_to_ifd,
strip_offset,
subfile,
value;
short entry_count,
field_type,
s_bits_per_pixel,
s_image_length,
s_image_width,
s_strip_offset,
tag_type;
image_file = fopen(file_name, "rb");
if(image_file != NULL){
/*************************************
*
* Determine if the file uses MSB
* first or LSB first
*
*************************************/
bytes_read = fread(buffer, 1, 8, image_file);
if(buffer[0] == 0x49)
lsb = 1;
else
lsb = 0;
/*************************************
*
* Read the offset to the IFD
*
*************************************/
extract_long_from_buffer(buffer, lsb, 4,
&offset_to_ifd);
not_finished = 1;
while(not_finished){
/*************************************
*
* Seek to the IFD and read the
* entry_count, i.e. the number of
* entries in the IFD.
*
*************************************/
position = fseek(image_file, offset_to_ifd,
SEEK_SET);
bytes_read = fread(buffer, 1, 2, image_file);
extract_short_from_buffer(buffer, lsb, 0,
&entry_count);
/***************************************
*
* Now loop over the directory entries.
* Look only for the tags we need. These
* are:
* ImageLength
* ImageWidth
* BitsPerPixel(BitsPerSample)
* StripOffset
*
*****************************************/
for(i=0; i<entry_count; i++){
bytes_read = fread(buffer, 1, 12, image_file);
extract_short_from_buffer(buffer, lsb, 0,
&tag_type);
switch(tag_type){
case 255: /* Subfile Type */
extract_short_from_buffer(buffer, lsb, 2,
&field_type);
extract_short_from_buffer(buffer, lsb, 4,
&length_of_field);
extract_long_from_buffer(buffer, lsb, 8,
&subfile);
break;
case 256: /* ImageWidth */
extract_short_from_buffer(buffer, lsb, 2,
&field_type);
extract_short_from_buffer(buffer, lsb, 4,
&length_of_field);
if(field_type == 3){
extract_short_from_buffer(buffer, lsb, 8,
&s_image_width);
image_width = s_image_width;
}
else
extract_long_from_buffer(buffer, lsb, 8,
&image_width);
break;
case 257: /* ImageLength */
extract_short_from_buffer(buffer, lsb, 2,
&field_type);
extract_short_from_buffer(buffer, lsb, 4,
&length_of_field);
if(field_type == 3){
extract_short_from_buffer(buffer, lsb, 8,
&s_image_length);
image_length = s_image_length;
}
else
extract_long_from_buffer(buffer, lsb, 8,
&image_length);
break;
case 258: /* BitsPerSample */
extract_short_from_buffer(buffer, lsb, 2,
&field_type);
extract_short_from_buffer(buffer, lsb, 4,
&length_of_field);
if(field_type == 3){
extract_short_from_buffer(buffer, lsb, 8,
&s_bits_per_pixel);
bits_per_pixel = s_bits_per_pixel;
}
else
extract_long_from_buffer(buffer, lsb, 8,
&bits_per_pixel);
break;
case 273: /* StripOffset */
extract_short_from_buffer(buffer, lsb, 2,
&field_type);
extract_short_from_buffer(buffer, lsb, 4,
&length_of_field);
if(field_type == 3){
extract_short_from_buffer(buffer, lsb, 8,
&s_strip_offset);
strip_offset = s_strip_offset;
}
else
extract_long_from_buffer(buffer, lsb, 8,
&strip_offset);
break;
default:
break;
} /* ends switch tag_type */
} /* ends loop over i directory entries */
bytes_read = fread(buffer, 1, 4, image_file);
extract_long_from_buffer(buffer, lsb, 0,
&offset_to_ifd);
if(offset_to_ifd == 0) not_finished = 0;
} /* ends while not_finished */
image_header->lsb = lsb;
image_header->bits_per_pixel = bits_per_pixel;
image_header->image_length = image_length;
image_header->image_width = image_width;
image_header->strip_offset = strip_offset;
closed = fclose(image_file);
} /* ends if file opened ok */
else{
printf("\n\nTIFF.C> ERROR - could not open "
"tiff file");
}
} /* ends read_tiff_header */
/****************************************
*
* extract_long_from_buffer(...
*
* This takes a four byte long out of a
* buffer of characters.
*
* It is important to know the byte order
* LSB or MSB.
*
****************************************/
extract_long_from_buffer(buffer, lsb, start, number)
char buffer[];
int lsb, start;
long *number;
{
int i;
union long_char_union lcu;
if(lsb == 1){
lcu.l_alpha[0] = buffer[start+0];
lcu.l_alpha[1] = buffer[start+1];
lcu.l_alpha[2] = buffer[start+2];
lcu.l_alpha[3] = buffer[start+3];
} /* ends if lsb = 1 */
if(lsb == 0){
lcu.l_alpha[0] = buffer[start+3];
lcu.l_alpha[1] = buffer[start+2];
lcu.l_alpha[2] = buffer[start+1];
lcu.l_alpha[3] = buffer[start+0];
} /* ends if lsb = 0 */
*number = lcu.l_num;
} /* ends extract_long_from_buffer */
/****************************************
*
* extract_ulong_from_buffer(...
*
* This takes a four byte unsigned long
* out of a buffer of characters.
*
* It is important to know the byte order
* LSB or MSB.
*
****************************************/
extract_ulong_from_buffer(buffer, lsb, start, number)
char buffer[];
int lsb, start;
unsigned long *number;
{
int i;
union ulong_char_union lcu;
if(lsb == 1){
lcu.l_alpha[0] = buffer[start+0];
lcu.l_alpha[1] = buffer[start+1];
lcu.l_alpha[2] = buffer[start+2];
lcu.l_alpha[3] = buffer[start+3];
} /* ends if lsb = 1 */
if(lsb == 0){
lcu.l_alpha[0] = buffer[start+3];
lcu.l_alpha[1] = buffer[start+2];
lcu.l_alpha[2] = buffer[start+1];
lcu.l_alpha[3] = buffer[start+0];
} /* ends if lsb = 0 */
*number = lcu.l_num;
} /* ends extract_ulong_from_buffer */
/****************************************
*
* extract_short_from_buffer(...
*
* This takes a two byte short out of a
* buffer of characters.
*
* It is important to know the byte order
* LSB or MSB.
*
****************************************/
extract_short_from_buffer(buffer, lsb, start, number)
char buffer[];
int lsb, start;
short *number;
{
int i;
union short_char_union lcu;
if(lsb == 1){
lcu.s_alpha[0] = buffer[start+0];
lcu.s_alpha[1] = buffer[start+1];
} /* ends if lsb = 1 */
if(lsb == 0){
lcu.s_alpha[0] = buffer[start+1];
lcu.s_alpha[1] = buffer[start+0];
} /* ends if lsb = 0 */
*number = lcu.s_num;
} /* ends extract_short_from_buffer */
/****************************************
*
* extract_ushort_from_buffer(...
*
* This takes a two byte unsiged short
* out of a buffer of characters.
*
* It is important to know the byte order
* LSB or MSB.
*
****************************************/
extract_ushort_from_buffer(buffer, lsb, start, number)
char buffer[];
int lsb, start;
unsigned short *number;
{
int i;
union ushort_char_union lcu;
if(lsb == 1){
lcu.s_alpha[0] = buffer[start+0];
lcu.s_alpha[1] = buffer[start+1];
} /* ends if lsb = 1 */
if(lsb == 0){
lcu.s_alpha[0] = buffer[start+1];
lcu.s_alpha[1] = buffer[start+0];
} /* ends if lsb = 0 */
*number = lcu.s_num;
} /* ends extract_ushort_from_buffer */
/****************************************
*
* allocate_image_array(...
*
* This function allocates memory for
* a two-dimensional image array.
*
****************************************/
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 */
/****************************************
*
* free_image_array(...
*
* This function frees up the memory
* used by a two-dimensional imaage array.
*
****************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -