📄 imgedit.c
字号:
#ifndef moy3
#define moy3( a, b, c ) ( a + b + c ) / 3
#endif
#ifndef is_b
#define is_b( a, b, c ) ( a < b && b < c )
#endif
#include<seal.h>
#include<grfx-f.h>
#include"imgedit.h"
#include<inputbox.h>
#include<allegro/aintern.h>
#include"grfx-c.c"
/*
(c) Copyright 2001 Julien Etelain. All rights reserved.
*/
BITMAP *img_bmps[11];
#define IMGEDIT_COLSEL_IMG img_bmps[0]
#define TOOL_ZOOM_CUR img_bmps[1]
#define TOOL_GET_CUR img_bmps[2]
#define TOOL_POINT_CUR img_bmps[3]
#define TOOL_FILL_CUR img_bmps[4]
#define TOOL_TEXT_CUR img_bmps[5]
#define TOOL_SELECT_CUR img_bmps[6]
#define FORME_RECT_CUR img_bmps[7]
#define FORME_LINE_CUR img_bmps[8]
#define FORME_OVAL_CUR img_bmps[9]
#define FORME_CIRCLE_CUR img_bmps[10]
p_list imgfilters = NULL;
p_colsel2 (*colsel2_init) ( p_colsel2 o, t_rect r ) = &_colsel2_init;
p_imgedit (*imgedit_init) ( p_imgedit o, t_rect r, p_colsel2 cc, p_stattext st ) = &_imgedit_init;
////////////////////////////////////////////////////////////////////////////////
void free_undoimgedit ( void *o ) {
if ( o ) {
DEBUG_printf ( "\nStart Free a Undo {\n");
byebyebmp(undoimgedit(o)->picture);
byebyebmp(undoimgedit(o)->selection);
byebyebmp(undoimgedit(o)->temp);
undoimgedit(o)->select = NULL;
undoimgedit(o)->actsel = rect_assign(NULL,NULL,NULL,NULL);
DEBUG_printf ( "}Free Ok;\n");
_free(o);
};
};
////////////////////////////////////////////////////////////////////////////////
void free_imgfilter ( void *o ) {
if ( o ) {
if (imgfilter(o)->copyright) _free(imgfilter(o)->copyright);
if (imgfilter(o)->name) _free(imgfilter(o)->name);
if (imgfilter(o)->description) _free(imgfilter(o)->description);
_free(o);
};
};
////////////////////////////////////////////////////////////////////////////////
p_imgfilter new_imgfilter( void (*smallpreview) ( BITMAP *bmp ),
void (*filterbox) ( BITMAP *bmp ),
l_text copyright,
l_text name,
l_text description ) {
p_imgfilter i = imgfilter(_malloc(sizeof(t_imgfilter)));
if ( i ) {
i->name = _strdup(name);
i->description = _strdup(description);
i->copyright = _strdup(copyright);
i->smallpreview = smallpreview;
i->filterbox = filterbox;
};
return i;
};
////////////////////////////////////////////////////////////////////////////////
void add_imgfilter( void (*smallpreview) ( BITMAP *bmp ),
void (*filterbox) ( BITMAP *bmp ),
l_text copyright,
l_text name,
l_text description ) {
imgfilters->insert( imgfilters , new_imgfilter ( smallpreview, filterbox, copyright, name, description ) );
};
////////////////////////////////////////////////////////////////////////////////
p_imgfilter getimgfilter ( l_int id ) {
return list_at(imgfilters, id) ;
};
////////////////////////////////////////////////////////////////////////////////
void greylevelit (BITMAP *bmp) {
l_uint x = 0;
l_uint y = 0;
while ( y < bmp->h ) {
x=0;
while ( x < bmp->w ) {
l_int col = getpixel(bmp, x, y);
l_int z = moy3( getr(col),getg(col),getb(col) );
if ( col != bmp->vtable->mask_color ) putpixel(bmp, x, y, makecol(z, z, z));
x++;
};
y++;
};
};
////////////////////////////////////////////////////////////////////////////////
void invertit (BITMAP *bmp) {
l_uint x = 0;
l_uint y = 0;
while ( y < bmp->h ) {
x=0;
while ( x < bmp->w ) {
l_int col = getpixel(bmp, x, y);
if ( col != bmp->vtable->mask_color ) putpixel(bmp, x, y, makecol(255-getr(col), 255-getg(col), 255-getb(col)));
x++;
};
y++;
};
};
////////////////////////////////////////////////////////////////////////////////
l_color adoucir ( BITMAP *bmp, l_int x, l_int y, l_int n, l_int max, l_int mode ) {
#define gocol(x) if ( x != bmp->vtable->mask_color ) { rs += getr(x); gs += getg(x); bs += getb(x); nb++; };
l_color col = getpixel(bmp, x, y);
l_int w = IMAGE_WIDTH ( bmp );
l_int h = IMAGE_HEIGHT ( bmp );
l_int rs = 0;
l_int gs = 0;
l_int bs = 0;
l_int nb = 0;
if ( x != 0 ) gocol(getpixel(bmp, x-1, y))
if ( x != w ) gocol(getpixel(bmp, x+1, y))
if ( y != 0 ) gocol(getpixel(bmp, x, y-1))
if ( y != h ) gocol(getpixel(bmp, x, y+1))
if ( x != w && y != h ) gocol(getpixel(bmp, x+1, y+1))
if ( x != w && y != 0 ) gocol(getpixel(bmp, x+1, y-1))
if ( x != 0 && y != h ) gocol(getpixel(bmp, x-1, y+1))
if ( x != 0 && y != 0 ) gocol(getpixel(bmp, x-1, y-1))
if ( col != bmp->vtable->mask_color ) {
if (mode == 1 )
return col2col(col,makecol(rs/nb,gs/nb,bs/nb),n,max);
else
return colNcol(col,makecol(rs/nb,gs/nb,bs/nb),n,max);
} else
return bmp->vtable->mask_color;
};
////////////////////////////////////////////////////////////////////////////////
void soft_it (BITMAP *bmp) {
BITMAP *dest = create_bitmap(bmp->w,bmp->h);
l_int x = 0;
l_int y = 0;
l_int n = 50;
l_int max = 100;
while ( y < bmp->h ) {
x = 0;
while ( x < bmp->w ) {
putpixel(dest, x, y, adoucir(bmp,x,y,n,max,1));
x++;
};
y++;
};
draw_sprite(bmp,dest ,0,0);
destroy_bitmap(dest);
};
////////////////////////////////////////////////////////////////////////////////
void hard_it (BITMAP *bmp) {
BITMAP *dest = create_bitmap(bmp->w,bmp->h);
l_int x = 0;
l_int y = 0;
l_int n = 50;
l_int max = 100;
while ( y < bmp->h ) {
x = 0;
while ( x < bmp->w ) {
putpixel(dest, x, y, adoucir(bmp,x,y,n,max,2));
x++;
};
y++;
};
draw_sprite(bmp,dest ,0,0);
destroy_bitmap(dest);
};
////////////////////////////////////////////////////////////////////////////////
void init_imgfilters ( ) {
DEBUG_printf("\nInit image's filters\n");
imgfilters = list_init(_malloc(sizeof(t_list)), &free_imgfilter, 0);
add_imgfilter ( greylevelit, greylevelit,"(c) Copyright 2001 Julien Etelain. All rights reserved","Greys levels","Change picture in greys levels ");
add_imgfilter ( soft_it, soft_it,"(c) Copyright 2001 Julien Etelain. All rights reserved","Softer","'Soft' the picture");
add_imgfilter ( hard_it, hard_it,"(c) Copyright 2001 Julien Etelain. All rights reserved","Harder","'Hard' the picture");
add_imgfilter ( invertit, invertit,"(c) Copyright 2001 Julien Etelain. All rights reserved","Invert","Invert the picture colors");
DEBUG_printf("\n - %d Filters loaded.\nImage's Filters init done ...\n\n",list_get_max_item(imgfilters) + 1);
};
////////////////////////////////////////////////////////////////////////////////
void colsel2_draw ( p_view o ) {
t_rect r = o->get_local_extent(o);
t_point p;
BITMAP *out = o->begin_paint(o, &p, r);
if ( out ) {
stretch_sprite(out, IMGEDIT_COLSEL_IMG, p.x, p.y, r.b.x, r.b.y - 10);
if ( colsel2(o)->imgedit ) {
rectfill(out, p.x, p.y + r.b.y - 10, p.x + (r.b.x / 2), p.y + r.b.y, colsel2(o)->imgedit->col1);
rectfill(out, p.x + (r.b.x / 2), p.y + r.b.y - 10, p.x + r.b.x, p.y + r.b.y, colsel2(o)->imgedit->col2);
if ( colsel2(o)->imgedit->picture ) {
if ( colsel2(o)->imgedit->col2 == colsel2(o)->imgedit->picture->vtable->mask_color )
textout_draw_rect(out, font_system, "(trans)", -1, p.x + (r.b.x / 2), p.y + r.b.y - 10, p.x + r.b.x, p.y + r.b.y, TX_ALIGN_CENTER, COLOR(CO_BLACK),TX_NOCOLOR, 0);
if ( colsel2(o)->imgedit->col1 == colsel2(o)->imgedit->picture->vtable->mask_color )
textout_draw_rect(out, font_system, "(trans)", -1, p.x, p.y + r.b.y - 10, p.x + (r.b.x / 2), p.y + r.b.y, TX_ALIGN_CENTER, COLOR(CO_BLACK), makecol(255,0,255), 0);
};
};
};
o->end_of_paint(o, r);
};
////////////////////////////////////////////////////////////////////////////////
void colsel2_translate_event ( p_object o, t_event *event )
{
RETVIEW(o, event); // Not yet !
if ( event->type & EV_MOUSE ) { // MO_SF_MOUSELDOWN
if ( colsel2(o)->imgedit ) {
p_view xo = VIEW(o);
l_int ok = 0;
t_point p2 = xo->get_local_point(xo, mouse->where);
if ( OBJECT(mouse)->state & MO_SF_MOUSELDOWN ) ok = 1;
if ( OBJECT(mouse)->state & MO_SF_MOUSERDOWN ) ok = 2;
if (ok != 0) {
if ( p2.x > 0 && p2.x < rect_sizex(xo->bounds) && p2.y > 0 && p2.y < rect_sizey(xo->bounds) - 10 ) {
t_rect r = xo->get_local_extent(xo);
t_point p;
t_point pe = VIEW(desktop)->get_local_point(VIEW(desktop), mouse->where);
BITMAP *out = xo->begin_paint(xo, &p, r);
l_color x = getpixel(out, pe.x, pe.y);
if ( ok == 1 ) colsel2(o)->imgedit->col1 = x;
if ( ok == 2 ) colsel2(o)->imgedit->col2 = x;
xo->end_of_paint(xo, r);
xo->draw_me(xo);
} else if ( p2.x > 0 && p2.x < rect_sizex(xo->bounds) / 2 && p2.y > rect_sizey(xo->bounds) - 10 && p2.y < rect_sizey(xo->bounds) ) {
colsel2(o)->imgedit->col1 = rgbcolorbox ( colsel2(o)->imgedit->col1 );
xo->draw_me(xo);
} else if ( p2.x > rect_sizex(xo->bounds) / 2 && p2.x < rect_sizex(xo->bounds) && p2.y > rect_sizey(xo->bounds) - 10 && p2.y < rect_sizey(xo->bounds) ) {
colsel2(o)->imgedit->col2 = rgbcolorbox ( colsel2(o)->imgedit->col2 );
xo->draw_me(xo);
};
};
};
};
};
////////////////////////////////////////////////////////////////////////////////
void imgedit_reset ( p_imgedit o ) {
o->zoom = 1;
o->editing = 0;
o->topx = 0;
o->topy = 0;
o->select = 0;
o->actsel = rect_assign(0,0,0,0);
byebyebmp(o->selection);
byebyebmp(o->temp);
byebyebmp(o->picture);
if ( o->filename ) _free (o->filename);
o->filename = _strdup("New Image");
if ( o->undolist )
o->undolist->free_all(o->undolist);
o->undolist = list_init(_malloc(sizeof(t_list)), &free_undoimgedit, 0);
o->pr = 0;
o->floodstyle = 0;
o->set1 = 50;
o->set2 = 50;
o->floodtol = 0;
};
////////////////////////////////////////////////////////////////////////////////
void statext_update ( p_stattext o, l_text text, ...) {
va_list arg;
va_start(arg, text);
vsprintf(o->text, text, arg);
va_end(arg);
VIEW(o)->draw_me(VIEW(o));
};
////////////////////////////////////////////////////////////////////////////////
BITMAP *make_bmp_with_text ( l_text txt, l_color color, l_font f ) {
l_int w = text_length ( (FONT*)f, txt ) + 4;
l_int h = text_height ( (FONT*)f );
BITMAP *bmp = create_bitmap ( w, h );
rectfill( bmp, 0, 0, w + 4, h + 2, makecol( 255, 0, 255 ) );
textout_draw_rect(bmp, f, txt, -1, 2, 0, w + 2, h, TX_ALIGN_DEFAULT, color, makecol( 255, 0, 255 ), 0);
return bmp;
};
////////////////////////////////////////////////////////////////////////////////
BITMAP *bmpdup ( BITMAP *bmp ) {
if ( bmp ) {
BITMAP *m = create_bitmap(bmp->w,bmp->h);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -