Category : Printer + Display Graphics
Archive   : TIFGIF.ZIP
Filename : READTIF.C
/* Copyright (c) 1988-1989 */
/* by CompuServe Inc., Tucson, AZ. All Rights Reserved */
/* READTIF.C can be copied and distributed freely for any */
/* non-commercial purposes. READTIF.C can only be incorporated */
/* into commercial software with the permission of CompuServe Inc. */
/*----------------------------------------------------------------------*/
/* READTIF.C */
/*
* ABSTRACT:
* This file contains procedures and variables to read page data
* out of TIF file
*
* AUTHOR: Doug Chinnock
*
* REVISION HISTORY:
*
*/
#include
#include
#include
#include "cnvhuf.h"
#include "readtif.h"
static unsigned char last_bytes[8];
static unsigned char extra_bits; /* bits in last_bytes[ 1 ] */
static FILE *tif_file;
extern unsigned char next_bit_from_stream()
{
unsigned char Fretval;
if (extra_bits == 0) /* If don't have bits in last_bytes[ 1 ] */
{ /* get 8 more bits */
if ( ! feof( tif_file ) )
{
last_bytes[1] = (unsigned char)fgetc( tif_file );
extra_bits = 8;
}
else
{ /* Fake EOLs */
last_bytes[0] = 0;
last_bytes[1] = 0x8;
extra_bits = 12;
}
}
Fretval = last_bytes[0] & 0x01;
extra_bits --;
if ((last_bytes[1] & 0x01) != 0)
last_bytes[0] = ( last_bytes[0] >> 1 ) + 0x80;
else
last_bytes[0] = ( last_bytes[0] >> 1 );
last_bytes[1] >>= 1;
return(Fretval);
} /*next_bit_from_stream*/
extern void refill_byte( unsigned char *byte_to_fill,
unsigned char bits_to_add )
{
static unsigned char
bit_counter;
bit_counter = bits_to_add;
while (bit_counter > 0)
{
if (extra_bits == 0)
{
if ( ! feof( tif_file ) )
{
last_bytes[1] = (unsigned char)fgetc( tif_file );
extra_bits = 8;
}
else
{ /* Fake EOLs */
last_bytes[0] = 0;
last_bytes[1] = 0x8;
extra_bits = 12;
}
} /* if (extra_bits == 0) */
if ((last_bytes[0] & 0x01) != 0)
(*byte_to_fill) = ((*byte_to_fill) << 1) + 1;
else
(*byte_to_fill) = ((*byte_to_fill) << 1);
bit_counter --;
extra_bits --;
if ((last_bytes[1] & 0x01) != 0)
last_bytes[0] = ( last_bytes[0] >> 1 ) + 0x80;
else
last_bytes[0] = ( last_bytes[0] >> 1 );
last_bytes[1] >>= 1;
} /* while (bit_counter > 0) */
} /* refill_byte */
extern void close_stream()
{
fclose( tif_file );
}/*close_stream */
extern short int initialize_stream( file_name_string filename )
{
static union
{ /* Buffer to hold whole TIF header */
TIF_header TIF_member;
char TIF_buffer[sizeof(TIF_header)+sizeof(IFD_header)];
} TIF_read_header;
int
IFD_cur_element,
file_sts,
file_byte_number,
temp_code;
long int
image_start,
IFD_element_count;
IFD_header
* IFD_body;
tif_file = fopen( filename, "rb");
if ( tif_file == NULL )
{
printf( "TIF file not found\n" );
return -1;
};
/* Read in TIF header and its IFD */
for ( file_byte_number = 0;
file_byte_number < sizeof( TIF_header ) + sizeof( IFD_header );
file_byte_number ++ )
{ TIF_read_header.TIF_buffer[ file_byte_number ] =
(char)(fgetc( tif_file ));
if ( ferror( tif_file ) )
{
printf( "Error scanning TIF header\n" );
return -1;
};
};
/* Setup to get useful info out of header */
IFD_body = (IFD_header *)(&TIF_read_header);
FP_OFF(IFD_body) += (unsigned int)(TIF_read_header.TIF_member.IFD_offset);
IFD_element_count = (*IFD_body).IFD_ele_cnt;
for ( IFD_cur_element = 0;
( IFD_cur_element < (short int)(IFD_element_count) )
& ( (*IFD_body).IFD_tag_element[IFD_cur_element].IFD_tag != 0 )
& ( (*IFD_body).IFD_tag_element[IFD_cur_element].IFD_tag != TT_Image );
IFD_cur_element ++
)
;
if ( (*IFD_body).IFD_tag_element[IFD_cur_element].IFD_tag != TT_Image )
{
printf( "No image pointer found in TIF file\n" );
return -1;
};
image_start = (*IFD_body).IFD_tag_element[IFD_cur_element].params.long_one;
if (file_sts = fseek( tif_file, image_start, 0 ) != 0)
return file_sts;
/* Scan for first EOL which serves as sync for image ( = 000000000001 ) */
last_bytes[0] = 0xFF; /* Flush out zeros */
extra_bits = 0; /* Only 1st byte is current */
temp_code = 0xFFF; /* Start with all ones in here */
do
{ /* Find initial sync (EOL) */
temp_code = (temp_code << 1) & 0xFFF;
if (next_bit_from_stream( ) == 1)
temp_code ++;
}
while (temp_code != 1); /* Is this 11 zeros then a 1? */
return 0;
} /*initialize_stream */
Very nice! Thank you for this wonderful archive. I wonder why I found it only now. Long live the BBS file archives!
This is so awesome! 😀 I’d be cool if you could download an entire archive of this at once, though.
But one thing that puzzles me is the “mtswslnkmcjklsdlsbdmMICROSOFT” string. There is an article about it here. It is definitely worth a read: http://www.os2museum.com/wp/mtswslnk/