📄 cut.c
字号:
/********************************************************************
*
* image cut tool
*
********************************************************************/#include <tina/all_tina.h>#include "bmp.h"static Tv *tv = NULL;static Tv *tv1=NULL;#define MAXPATHLEN 64static char directory_name[MAXPATHLEN];static char file_name[MAXPATHLEN];static char pathname[MAXPATHLEN];
static void *pdir=0, *pfile=0;static int cut_method = 0;static char filename[MAXPATHLEN];static Imrect *orig_im =NULL;static Imrect *dest_im = NULL;//#define CUTPIX 254.56789#define CUTPIX 255static int PERCENT = 50;static void *pper=NULL;static int step=5;static void *pstep=NULL;static void inc_per_proc(void){ PERCENT+=step; tw_iglobal_reset(pper);}static void dec_per_proc(void){ PERCENT-=step; tw_iglobal_reset(pper);}/*static void cut_choice_proc(int val)
{
cut_method = val;
}*/static Imrect *read_image(void){ Imrect *srcIm;/*----------------Read the image---------------------------*/ (void) strip_spaces(file_name); (void) strip_spaces(directory_name); (void) string_append(pathname, directory_name, "/", file_name, NULL); srcIm = ReadGIF(pathname, 1);/*------------------------------------------------*/ return(srcIm);}static void mypush_proc(void){ Imrect *srcIm,*srcIm0; int width, height;
Imregion *roi; int i,j; //BYTE pix; float pix; srcIm = read_image(); width = srcIm->width;
height = srcIm->height;
roi = srcIm->region;
if (roi == NULL) return ; srcIm0 = im_alloc( height, width, roi, float_v ); for(i = 0;i < height; i ++) for(j = 0;j < width; j++) { IM_PIX_GET(srcIm, i, j, pix);
IM_PIX_SET(srcIm0, i, j, pix); } mono_image_set(srcIm0);
stack_push(srcIm0, IMRECT, im_free);}static Imrect* mypop_proc(void){ Imrect *srcIm; //BYTE pix; float pix; int type; if (stack_check_types(IMRECT, NULL) == false)
{
error("cut : wrong types on stack", warning);
return;
} srcIm = (Imrect *) stack_pop(&type); return srcIm;}static void myshow_proc(void){ int type; int width, height;
Imregion *roi; Imrect *srcIm,*srcIm0; int i,j; //BYTE pix; float pix; if (stack_check_types(IMRECT, NULL) == false)
{
error("cut : wrong types on stack", warning);
return;
} srcIm = (Imrect *) stack_pop(&type); width = srcIm->width;
height = srcIm->height;
roi = srcIm->region;
if (roi == NULL) return ; srcIm0 = im_alloc( height, width, roi, float_v ); for(i = 0;i < height; i ++) for(j = 0;j < width; j++) { IM_PIX_GET(srcIm, i, j, pix);
IM_PIX_SET(srcIm0, i, j, pix); } orig_im = im_copy(srcIm); tv_imrect2(tv, orig_im); mono_image_set(srcIm0);
stack_push(srcIm0, IMRECT, im_free);}static void mysave_proc(void){ int type; int width, height;
Imregion *roi; Imrect *srcIm,*srcIm0; int i,j; //BYTE pix; float pix; if (stack_check_types(IMRECT, NULL) == false)
{
error("cut : wrong types on stack", warning);
return;
} srcIm = (Imrect *) stack_pop(&type); width = srcIm->width;
height = srcIm->height;
roi = srcIm->region;
if (roi == NULL) return ; srcIm0 = im_alloc( height, width, roi, float_v ); for(i = 0;i < height; i ++) for(j = 0;j < width; j++) { IM_PIX_GET(srcIm, i, j, pix);
IM_PIX_SET(srcIm0, i, j, pix); } strcat(filename,".bmp"); printf("saved the image :%s\n",filename); ImrectToBMP8(srcIm0,filename); mono_image_set(srcIm0);
stack_push(srcIm0, IMRECT, im_free);}static void cut_proc(void){ Imrect *srcIm,*destIm;
int width, height;
Imregion *roi; int i,j; int type;
//BYTE pix; float pix; if (stack_check_types(IMRECT, NULL) == false)
{
error("cut : wrong types on stack", warning);
return;
} srcIm = (Imrect *) stack_pop(&type);
width = srcIm->width;
height = srcIm->height;
roi = srcIm->region;
if (roi == NULL) return ; orig_im = im_copy(srcIm); tv_imrect2(tv, orig_im); destIm = im_alloc( height, width, roi, float_v ); for(i = 0;i < height; i ++) for(j = 0;j < width; j++) { IM_PIX_GET(srcIm, i, j, pix);
IM_PIX_SET(destIm, i, j, pix); } strcpy(filename,"result/cut/"); char filename0[64]; strcpy(filename0,"cut");/*------------------------------------------------ int hmid,wmid,h4,w4; hmid = height/2; wmid = width/2; h4 = height/4; w4 = width/4; switch( cut_method )
{
case 0:
for(i = 0;i < height;i ++) for(j = 0;j < w4;j ++) IM_PIX_SET(destIm, i, j, CUTPIX); strcat(filename0, "25%_");
break;
case 1:
for(i = 0;i < hmid;i ++) for(j = 0;j < width;j ++) IM_PIX_SET(destIm, i, j, CUTPIX); strcat(filename0, "50%_");
break;
case 2:
for(i = 0;i < hmid;i ++) for(j = wmid;j < width;j ++) IM_PIX_SET(destIm, i, j, CUTPIX); for(i = hmid;i < height;i ++) for(j = 0;j < width;j ++) IM_PIX_SET(destIm, i, j, CUTPIX); strcat(filename0, "75%-L_up_");
break;
case 3:
for(i = 0;i < hmid;i ++) for(j = 0;j < wmid;j ++) IM_PIX_SET(destIm, i, j, CUTPIX); for(i = hmid;i < height;i ++) for(j = 0;j < width;j ++) IM_PIX_SET(destIm, i, j, CUTPIX); strcat(filename0, "75%-R_up_");
break;
case 4:
for(i = 0;i < hmid;i ++) for(j = 0;j < width;j ++) IM_PIX_SET(destIm, i, j, CUTPIX); for(i = hmid;i < height;i ++) for(j = wmid;j < width;j ++) IM_PIX_SET(destIm, i, j, CUTPIX); strcat(filename0, "75%-L_down_");
break;
case 5:
for(i = 0;i < hmid;i ++) for(j = 0;j < width;j ++) IM_PIX_SET(destIm, i, j, CUTPIX); for(i = hmid;i < height;i ++) for(j = 0;j < wmid;j ++) IM_PIX_SET(destIm, i, j, CUTPIX); strcat(filename0, "75%-R_down_");
break;
case 6:
for(i = 0;i < height;i ++) for(j = 0;j < width*0.1;j ++) IM_PIX_SET(destIm, i, j, CUTPIX); strcat(filename0, "10%");
break;
case 7:
strcat(filename0, "_");
break;
case 8:
strcat(filename0, "_");
break;
case 9:
strcat(filename0, "_");
break;
default:
break;
}/*------------------------------------------------
//printf("\n=== %s begin: ===\n",filename0);/*----------------------------------------------*/ for(i = 0;i < height;i ++) for(j = 0;j < width*PERCENT/100;j ++) IM_PIX_SET(destIm, i, j, CUTPIX);/*----------------------------------------------*/ char string[25]; gcvt(PERCENT,5,string); strcat(filename0, string); strcat(filename0,"%"); strcat(filename, filename0);/*------------------------------------------------*/ dest_im = im_copy(destIm); tv_imrect2(tv1, dest_im); mono_image_set(destIm);
stack_push(destIm, IMRECT, im_free); printf("\n=== %s complete! ===\n",filename0); }static void fulldrawo(Tv *tv){ tv_imrect2(tv, orig_im);}static void fulldrawd(Tv *tv){ tv_imrect2(tv, dest_im);}static void tv_choice_proc(choice)
int choice;
{
switch (choice)
{
case 0: tv_set_next(tv);
break;
case 1: tv_set_next(tv1);
break;
default:error("tv_choice_proc: unknown choice\n", warning);
break;
}}static void scan_proc(void){ scan_files(directory_name,file_name); tw_sglobal_reset(pdir); tw_sglobal_reset(pfile);}static void tool_proc(void)
{
static int x = 100, y = 200;
static int xr = 0, yr = 0;
tv_tool(x+xr, y+yr);
xr += 50;
yr += 10;
if (x+xr>700)
{
xr = yr = 0;
y += 50;
}
if (y>400) y = 200;
}/********** Tool creation **********/
void cut_tool(int x, int y)
{
static void *tool = NULL; if (tool)
{
tw_show_tool(tool);
return;
}
tool = (void *)tw_tool("Image cut tool", x, y);
/* Initialise pathname from environment variable (or #define) */ (void) environ_pathname_get(directory_name, file_name, "TINA_IMAGE_DEFAULT", "/home/wzz/newimscramdegree/result/cut/Lena.gif");
tw_choice("Tv choice", tv_choice_proc, 0, "original","cut", NULL); tv = tv_create("cut original");
tv1 = tv_create("cut"); tv_set_fulldraw(tv, fulldrawo); tv_set_fulldraw(tv1,fulldrawd); /*---------------------------*/ tw_newrow();
tw_label("Display");
tw_button("New Tvtool", tool_proc, NULL); /*---------------------------*/ tw_newrow(); pdir = (void *) tw_sglobal("Directory:", directory_name, 32); tw_button("scan", scan_proc, NULL); tw_newrow(); pfile = (void*) tw_sglobal("File:", file_name, 32); tw_newrow(); /*---------------------------*/ tw_button("push",mypush_proc,NULL); tw_button("pop",mypop_proc,NULL); tw_button("show",myshow_proc,NULL);
tw_button("save",mysave_proc,NULL); tw_newrow(); /*tw_choice("Cut %", cut_choice_proc, 0,\
"25%", "50%", "75%-L_up", "75%-R_up", "75%-L_down", "75%-R_down", "10%", "--", "--", "--", NULL);*/ //tw_iglobal("cut percent: ", &PERCENT, 5); //tw_label("% "); pper=tw_iglobal("cut percent:(%)", &PERCENT, 10); tw_button("<", dec_per_proc, NULL); tw_button(">", inc_per_proc, NULL); pstep=tw_iglobal("", &step, 5);
tw_newrow(); tw_button("cut",cut_proc,NULL); tw_newrow();
tw_end_tool();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -