📄 imageio.c
字号:
fwrite(buffer , 1, 1, image_file);
buffer[0] = rgb[i].green;
fwrite(buffer , 1, 1, image_file);
buffer[0] = rgb[i].red;
fwrite(buffer , 1, 1, image_file);
buffer[0] = 0x00;
fwrite(buffer , 1, 1, image_file);
} /* ends loop over i */
position = fseek(image_file,
file_header.bitmapoffset,
SEEK_SET);
pad = calculate_pad(width);
for(i=0; i<height; i++){
for(j=0; j<width; j++){
if(bmheader.bitsperpixel == 8){
scu.s_num = 0;
if(bmheader.height > 0)
scu.s_num = array[height-1-i][j];
else
scu.s_num = array[i][j];
buffer[j] = scu.s_alpha[0];
} /* ends if bits_per_pixel == 8 */
else{
printf("\nERROR bitsperpixel is not 8");
exit(1);
}
} /* ends loop over j */
bytes = fwrite(buffer, 1, width, image_file);
if(pad != 0){
for(j=0; j<pad; j++)
buffer[j] = 0x00;
fwrite(buffer, 1, pad, image_file);
} /* ends if pad != 0 */
} /* ends loop over i */
fclose(image_file);
free(buffer);
} /* ends write_bmp_image */
/******************************************
*
* calculate_pad(...
*
* This function calculates the pad needed
* at the end of each row of pixels in a
* bmp image.
*
******************************************/
int calculate_pad(width)
long width;
{
int pad = 0;
pad = ( (width%4) == 0) ? 0 : (4-(width%4));
return(pad);
} /* ends calculate_pad */
/**********************************************
*
* get_image_size(...
*
* This function reads the rows and cols
* from the header of either a tiff or bmp
* image file.
*
* IF IT CANNOT FIND THIS INFORMATION,
* it returns a ZERO.
*
***********************************************/
int get_image_size(file_name, rows, cols)
char *file_name;
long *cols, *rows;
{
int is_bmp = 0,
is_tiff = 0,
result = 0;
struct bitmapheader bmph;
struct tiff_header_struct tiffh;
if(is_a_bmp(file_name)){
is_bmp = 1;
read_bm_header(file_name, &bmph);
*rows = bmph.height;
*cols = bmph.width;
} /* ends if is_a_bmp */
if(is_a_tiff(file_name)){
is_tiff = 1;
read_tiff_header(file_name, &tiffh);
*rows = tiffh.image_length;
*cols = tiffh.image_width;
} /* ends if is_a_bmp */
if(is_bmp == 1 || is_tiff == 1)
result = 1;
return(result);
} /* ends get_image_size */
/******************************************
*
* get_bitsperpixel(...
*
* This function reads the bits per pixel
* from either a tiff or bmp image file.
*
******************************************/
int get_bitsperpixel(file_name, bitsperpixel)
char *file_name;
long *bitsperpixel;
{
int is_bmp = 0,
is_tiff = 0,
result = 0;
long temp;
struct bitmapheader bmph;
struct tiff_header_struct tiffh;
if(is_a_bmp(file_name)){
is_bmp = 1;
read_bm_header(file_name, &bmph);
temp = (long)bmph.bitsperpixel;
*bitsperpixel = temp;
} /* ends if is_a_bmp */
if(is_a_tiff(file_name)){
is_tiff = 1;
read_tiff_header(file_name, &tiffh);
*bitsperpixel = tiffh.bits_per_pixel;
} /* ends if is_a_bmp */
if(is_bmp == 1 || is_tiff == 1)
result = 1;
return(result);
} /* ends get_image_size */
/******************************************
*
* get_lsb(...
*
* This function reads the lsb flag
* from a tiff image file.
*
******************************************/
int get_lsb(name)
char *name;
{
int result = 0;
struct tiff_header_struct tiff_header;
if(is_a_bmp(name))
result = 1;
if(is_a_tiff(name)){
read_tiff_header(name, &tiff_header);
if(tiff_header.lsb == 1)
result = 1;
} /* ends if is a tiff */
return(result);
} /* ends get_lsb */
/**********************************************
*
* is_a_bmp(...
*
* This function looks at a file to see if it
* is a bmp file. First look at the file
* extension. Next look at the filetype to
* ensure it is 0x4d42.
*
***********************************************/
int is_a_bmp(file_name)
char *file_name;
{
char *cc;
int result = 0;
struct bmpfileheader file_header;
cc = strstr(file_name, ".bmp");
if(cc == NULL)
return(result);
read_bmp_file_header(file_name, &file_header);
if(file_header.filetype != 0x4d42)
return(result);
result = 1;
return(result);
} /* ends is_a_bmp */
/**********************************************
*
* is_a_tiff(...
*
* This function looks at a file to see if it
* is a tiff file. First look at the file
* extension. Next look at the first four
* bytes of the header.
*
***********************************************/
int is_a_tiff(file_name)
char *file_name;
{
char *cc;
char buffer[4];
FILE *fp;
int ok = 0,
result = 0;
cc = strstr(file_name, ".tif");
if(cc == NULL)
return(result);
fp = fopen(file_name, "rb");
fread(buffer, 1, 4, fp);
fclose(fp);
if(buffer[0] == 0x49 &&
buffer[1] == 0x49 &&
buffer[2] == 0x2a &&
buffer[3] == 0x00)
ok = 1;
if(buffer[0] == 0x4d &&
buffer[1] == 0x4d &&
buffer[2] == 0x00 &&
buffer[3] == 0x2a)
ok = 1;
if(ok == 0)
return(result);
result = 1;
return(result);
} /* ends is_a_tiff */
/*******************************************
*
* read_image_array(...
*
* This routine reads the image data from
* either a tiff or bmp image.
*
********************************************/
read_image_array(file_name, array)
char *file_name;
short **array;
{
int ok = 0;
if(is_a_tiff(file_name)){
read_tiff_image(file_name, array);
ok = 1;
}
if(is_a_bmp(file_name)){
read_bmp_image(file_name, array);
ok = 1;
}
if(ok == 0){
printf("\nERROR could not read file %s",
file_name);
exit(1);
}
} /* ends read_image_array */
/*******************************************
*
* write_image_array(...
*
* This routine writes the image data to
* either a tiff or bmp image.
*
********************************************/
write_image_array(file_name, array)
char *file_name;
short **array;
{
int ok = 0;
if(is_a_tiff(file_name)){
write_tiff_image(file_name, array);
ok = 1;
}
if(is_a_bmp(file_name)){
write_bmp_image(file_name, array);
ok = 1;
}
if(ok == 0){
printf("\nERROR could not write file %s",
file_name);
exit(1);
}
} /* ends write_image_array */
/*********************************************
*
* equate_tiff_headers(...
*
* This function sets the elements of the
* destination header to the values of the
* source header.
*
*********************************************/
equate_tiff_headers(src, dest)
struct tiff_header_struct *src, *dest;
{
dest->lsb = src->lsb;
dest->bits_per_pixel = src->bits_per_pixel;
dest->image_length = src->image_length;
dest->image_width = src->image_width;
dest->strip_offset = src->strip_offset;
} /* ends equate_tiff_headers */
/*********************************************
*
* equate_bmpfileheaders(...
*
* This function sets the elements of the
* destination header to the values of the
* source header.
*
*********************************************/
equate_bmpfileheaders(src, dest)
struct bmpfileheader *src, *dest;
{
dest->filetype = src->filetype;
dest->filesize = src->filesize;
dest->reserved1 = src->reserved1;
dest->reserved2 = src->reserved2;
dest->bitmapoffset = src->bitmapoffset;
} /* ends equate_bmpfileheaders */
/*********************************************
*
* equate_bitmapheaders(...
*
* This function sets the elements of the
* destination header to the values of the
* source header.
*
*********************************************/
equate_bitmapheaders(src, dest)
struct bitmapheader *src, *dest;
{
dest->size = src->size;
dest->width = src->width;
dest->height = src->width;
dest->planes = src->planes;
dest->bitsperpixel = src->bitsperpixel;
dest->compression = src->compression;
dest->sizeofbitmap = src->sizeofbitmap;
dest->horzres = src->horzres;
dest->vertres = src->vertres;
dest->colorsused = src->colorsused;
dest->colorsimp = src->colorsimp;
} /* ends equate_bitmapheader */
/*********************************************
*
* are_not_same_size(...
*
* This function checks the rows and cols
* of two images whose names are passed.
* It returns a 1 if the images are not
* the same size.
*
*********************************************/
int are_not_same_size(file1, file2)
char *file1, *file2;
{
int result = 0;
long cols1 = 1,
cols2 = 2,
rows1 = 3,
rows2 = 4;
get_image_size(file1, &rows1, &cols1);
get_image_size(file2, &rows2, &cols2);
if(rows1 != rows2 || cols1 != cols2)
result = 1;
return(result);
} /* ends are_not_same_size */
/*********************************************
*
* create_file_if_needed(...
*
* This function creates an output file
* if it does not exist. It can create
* the output file as a tiff or a bmp
* based on the input file type.
*
*********************************************/
create_file_if_needed(in_name, out_name, array)
char *in_name, *out_name;
short **array;
{
if(is_a_tiff(in_name)){
create_tiff_file_if_needed(in_name,
out_name,
array);
} /* ends if is a tiff */
if(is_a_bmp(in_name)){
create_bmp_file_if_needed(in_name,
out_name,
array);
} /* ends if is a tiff */
} /* ends create_file_if_needed */
/*********************************************
*
* create_image_file(...
*
* This function creates an output
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -