Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
//
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <io.h>
#include <fcntl.h>
#include <dos.h>
#include <math.h>
#include <malloc.h>
#include <string.h>
#include <conio.h>
#include <sys\types.h>
#include <sys\stat.h>
#define MAX_NAME_LENGTH 80
#define ROWS 100
#define COLS 100
#define GRAY_LEVELS 255
#define PREWITT 1
#define PEAK_SPACE 50
#define PEAKS 30
#define KIRSCH 2
#define SOBEL 3
#define STACK_SIZE 40000
#define STACK_FILE_LENGTH 500
#define FORGET_IT -50
#define STACK_FILE "c:stack"
struct bmpfileheader{
unsigned short filetype;
unsigned long filesize;
short reserved1;
short reserved2;
unsigned long bitmapoffset;
};
struct bitmapheader{
unsigned long size;
long width;
long height;
unsigned short planes;
unsigned short bitsperpixel;
unsigned long compression;
unsigned long sizeofbitmap;
unsigned long horzres;
unsigned long vertres;
unsigned long colorsused;
unsigned long colorsimp;
};
struct ctstruct{
unsigned char blue;
unsigned char green;
unsigned char red;
};
union short_char_union{
short s_num;
char s_alpha[2];
};
union int_char_union{
int i_num;
char i_alpha[2];
};
union long_char_union{
long l_num;
char l_alpha[4];
};
union float_char_union{
float f_num;
char f_alpha[4];
};
union ushort_char_union{
short s_num;
char s_alpha[2];
};
union uint_char_union{
int i_num;
char i_alpha[2];
};
union ulong_char_union{
long l_num;
char l_alpha[4];
};
void extract_ushort_from_buffer(char buffer[], int lsb, int start, unsigned shor
t *number)
{
union ushort_char_union lcu;
if(lsb == 1){
lcu.s_alpha[0] = buffer[start+0];
lcu.s_alpha[1] = buffer[start+1];
} /* ket thuc lsb = 1 */
if(lsb == 0){
lcu.s_alpha[0] = buffer[start+1];
lcu.s_alpha[1] = buffer[start+0];
} /* ket thuc lsb = 0 */
*number = lcu.s_num;
} /* ket thuc extract_ushort_from_buffer */
void extract_ulong_from_buffer(char buffer[], int lsb, int start, unsigned long
*number)
{
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];
} /* ket thuc 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];
} /* ket thuc lsb = 0 */
*number = lcu.l_num;
} /* ket thuc extract_ulong_from_buffer */
void extract_short_from_buffer(char buffer[], int lsb, int start, short *number)
{
union short_char_union lcu;
if(lsb == 1){
lcu.s_alpha[0] = buffer[start+0];
lcu.s_alpha[1] = buffer[start+1];
} /* ket thuc lsb = 1 */
if(lsb == 0){
lcu.s_alpha[0] = buffer[start+1];
lcu.s_alpha[1] = buffer[start+0];
} /* ket thuc lsb = 0 */
*number = lcu.s_num;
/*********************************************
*
* ghi 40 byte info vao anh.
*
*********************************************/
insert_ulong_into_buffer(buffer, 0, bmheader->size);
fwrite(buffer, 1, 4, fp);
insert_long_into_buffer(buffer, 0, bmheader->width);
fwrite(buffer, 1, 4, fp);
insert_long_into_buffer(buffer, 0, bmheader->height);
fwrite(buffer, 1, 4, fp);
insert_ushort_into_buffer(buffer, 0, bmheader->planes);
fwrite(buffer, 1, 2, fp);
insert_ushort_into_buffer(buffer, 0, bmheader->bitsperpixel);
fwrite(buffer, 1, 2, fp);
insert_ulong_into_buffer(buffer, 0, bmheader->compression);
fwrite(buffer, 1, 4, fp);
insert_ulong_into_buffer(buffer, 0, bmheader->sizeofbitmap);
fwrite(buffer, 1, 4, fp);
insert_ulong_into_buffer(buffer, 0, bmheader->horzres);
fwrite(buffer, 1, 4, fp);
insert_ulong_into_buffer(buffer, 0, bmheader->vertres);
fwrite(buffer, 1, 4, fp);
insert_ulong_into_buffer(buffer, 0, bmheader->colorsused);
fwrite(buffer, 1, 4, fp);
insert_ulong_into_buffer(buffer, 0, bmheader->colorsimp);
fwrite(buffer, 1, 4, fp);
/*********************************************
*
* Voi anh co bitcount=8, ghi 256 thong tin mau vao anh
* moi thong tin mau co 4 byte (thong tin rong)
*
*********************************************/
buffer[0] = 0x00;
for(i=0; i<(256*4); i++)
fwrite(buffer, 1, 1, fp);
/*********************************************
*
* tao anh rong
*
*********************************************/
buffer[0] = 0x00;
for(i=0; i<bmheader->sizeofbitmap; i++)
fwrite(buffer, 1, 1, fp);
fclose(fp);
} /* ket thuc create_allocate_bmp_file */
/*********************************************
*
* create_image_file(...
*
* tao file bmp moi
*
*********************************************/
void create_image_file(char *in_name, char *out_name)
{
struct bmpfileheader bmp_file_header;
struct bitmapheader bmheader;
read_bmp_file_header(in_name, &bmp_file_header);
read_bm_header(in_name, &bmheader);
create_allocate_bmp_file(out_name, &bmp_file_header, &bmheader);
} /* ket thuc create_image_file */
void create_resized_image_file(char *in_name, char *out_name, long length, long
width)
{
struct bmpfileheader bmp_file_header;
struct bitmapheader bmheader;
read_bmp_file_header(in_name, &bmp_file_header);
read_bm_header(in_name, &bmheader);
bmheader.height = length;
bmheader.width = width;
create_allocate_bmp_file(out_name, &bmp_file_header, &bmheader);
} /* ket thuc create_resided_image_file */
void print_array_image(short **array, long rows, long cols)
{
int i,j;
for(i=0;i<rows;i++)
for(j=0;j<cols;j++)
{
printf("%4d ",array[i][j]);
if(j==cols-1) printf("\n");
}
}
short **grey_level_image(short **red, short **green, short **blue, long rows, lo
ng cols)
{
short **g_level;
int i,j;
g_level=allocate_image_array(rows,cols);
for(i=0;i<rows;i++)
for(j=0;j<cols;j++)
{
g_level[i][j]=(red[i][j]+green[i][j]+blue[i][j])/3;
}
return g_level;
}
void main()
{
// clrscr();
char duongdan1[30];
char duongdan2[30];
short **the_image_red;
short **the_image_green;
short **the_image_blue;
long l, w;
struct bmpfileheader bmp_file_header;
struct bitmapheader bmheader;
printf("Nhap duong dan thu 1: "); scanf("%s",duongdan1);
printf("Nhap duong dan thu 2: "); scanf("%s",duongdan2);
get_image_size(duongdan1, &l, &w);
the_image_red= allocate_image_array(l, w);
the_image_green= allocate_image_array(l, w);
the_image_blue= allocate_image_array(l, w);
bmheader.height = l;
bmheader.width = w;
create_allocate_bmp_file(duongdan2,&bmp_file_header,&bmheader);
read_bmp_image(duongdan1, the_image_red, the_image_green, the_image_blue);
printf("Mau RED cua anh\n");
print_array_image(the_image_red,l,w);
printf("Mau GREEN cua anh\n");
print_array_image(the_image_green,l,w);
printf("Mau BLUE cua anh\n");
print_array_image(the_image_blue,l,w);
printf("Anh xam cua anh goc\n");
print_array_image(grey_level_image(the_image_red, the_image_green, the_image_blu
e,l,w),l,w);
write_bmp_image(duongdan2, the_image_red);
free_image_array(the_image_red, l);
free_image_array(the_image_green, l);
free_image_array(the_image_blue, l);
getch();
}
//===================================//
//load file bmp
/* Function to open and display a 16-color bitmap file
* Written by Vineeth Kaimal
* For Turbo C++ 3.0
* http://writeka.com/emage/bmp16_format.html
* Please retain these comments if you use the code.
*/
int loadbitmap(char *filename)
{
FILE *ptr=NULL; //file handle to open bitmap file
int width,height; //width and height of the bitmap
unsigned long temp=0,i=0,j=0; //some variables i need
unsigned long ww;
ptr=fopen(filename,"rb"); //open the bitmap file
if(!ptr) return 0; //if its not there return
width=0;height=0; //initialise wisth and height to zero
fseek(ptr,18,SEEK_SET); //got to offset 18 in file
for(i=0x1;i<=0x10000;i*=0x100) //read the width
{
temp=fgetc(ptr);
width+=(temp*i);
}
fseek(ptr,22,SEEK_SET); //go to offset 22 in file
for(i=0x1;i<=0x10000;i*=0x100) //read the height
{
temp=fgetc(ptr);
height+=(temp*i);
}
ww=width; //ww is the number of reads to make for each horizontal lin
e
if(ww%2) //ww has to be half of width - since each pixel is only 4 b
its of information
ww++;ww/=2; //just getting the correct value
if(ww%4) //now - ww is stored as sets of 4 pixels each - i think !?!
:so this is the adjustment made
ww=(ww/4)*4+4; //if width is less than ww*2 we ignore what we read
fseek(ptr,119,SEEK_SET); //Ok! offset 119 - lets read the pixels -
//remember the bitmap is stroed up - side - down
int ch,ch1,ch2;
for(i=0;i<height;i++)
for(j=0;j<ww;j++)
{
ch=fgetc(ptr); //each character read is 2 pixels - yes 4 bits per
pixel - so 16 colors
ch1=ch;ch2=ch; //find those colors usi
ng bitwise ops
ch1=ch1&(0xf0);ch1=ch1>>4; // ~~
ch2=ch2&(0x0f); // ~~
if(j*2<width) //ok so put the first p
ixel read on screen
putpixel((j)*2,(height-1-i),getcol(ch1));
if(j*2+1<width) //put the second pixel
read on screen
putpixel((j)*2+1,(height-1-i),getcol(ch2));
}
fclose(ptr); //close the file handle
return 1;
}