forked from old/legacy-imlib2
539 lines
14 KiB
C
539 lines
14 KiB
C
#include <X11/Xlib.h>
|
|
#include <X11/extensions/XShm.h>
|
|
#include <X11/Xutil.h>
|
|
#include <X11/extensions/shape.h>
|
|
#include <X11/Xatom.h>
|
|
#include <X11/Xos.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <stdlib.h>
|
|
#include <unistd.h>
|
|
/*
|
|
#include <sys/time.h>
|
|
#include "common.h"
|
|
#include "image.h"
|
|
#include "rend.h"
|
|
#include "rgba.h"
|
|
#include "ximage.h"
|
|
#include "color.h"
|
|
*/
|
|
#include "Imlib2.h"
|
|
|
|
Display *disp;
|
|
Window win;
|
|
Visual *vis;
|
|
Colormap cm;
|
|
int depth;
|
|
|
|
void progress(Imlib_Image *im, char percent, int update_x, int update_y,
|
|
int update_w, int update_h);
|
|
|
|
void
|
|
progress(Imlib_Image *im, char percent,
|
|
int update_x, int update_y,
|
|
int update_w, int update_h)
|
|
{
|
|
imlib_context_set_display(disp);
|
|
imlib_context_set_visual(vis);
|
|
imlib_context_set_colormap(cm);
|
|
imlib_context_set_drawable(win);
|
|
imlib_context_set_dither(0);
|
|
imlib_context_set_blend(0);
|
|
imlib_context_set_color_modifier(NULL);
|
|
imlib_context_set_operation(IMLIB_OP_COPY);
|
|
imlib_context_set_image(im);
|
|
imlib_render_image_part_on_drawable_at_size(update_x, update_y,
|
|
update_w, update_h,
|
|
update_x, update_y,
|
|
update_w, update_h);
|
|
}
|
|
|
|
int main (int argc, char **argv)
|
|
{
|
|
int i, j;
|
|
Imlib_Image *im = NULL;
|
|
int sec1, usec1, sec2, usec2;
|
|
int pixels = 0;
|
|
struct timeval timev;
|
|
double sec;
|
|
char *file = NULL;
|
|
char *fon = NULL, *str = NULL;
|
|
|
|
int root = 0;
|
|
int scale = 0;
|
|
int w = 20;
|
|
int h = 20;
|
|
int aa = 0;
|
|
int dith = 0;
|
|
int loop = 0;
|
|
int blend = 1;
|
|
int interactive = 1;
|
|
int blendtest = 0;
|
|
int rotate = 0;
|
|
int rottest = 0;
|
|
|
|
for (i = 1; i < argc; i++)
|
|
{
|
|
if (!strcmp(argv[i], "-root"))
|
|
root = 1;
|
|
else if (!strcmp(argv[i], "-smooth"))
|
|
aa = 1;
|
|
else if (!strcmp(argv[i], "-blast"))
|
|
interactive = 0;
|
|
else if (!strcmp(argv[i], "-loop"))
|
|
{
|
|
interactive = 0;
|
|
loop = 1;
|
|
}
|
|
else if (!strcmp(argv[i], "-blend"))
|
|
blend = 1;
|
|
else if (!strcmp(argv[i], "-blendtest"))
|
|
{
|
|
blendtest = 1;
|
|
interactive = 0;
|
|
}
|
|
else if (!strcmp(argv[i], "-dither"))
|
|
dith = 1;
|
|
else if (!strcmp(argv[i], "-scale"))
|
|
scale = 1;
|
|
else if (!strcmp(argv[i], "-noloop"))
|
|
loop = 0;
|
|
else if (!strcmp(argv[i], "-size"))
|
|
{
|
|
i++;
|
|
w = atoi(argv[i++]);
|
|
h = atoi(argv[i]);
|
|
}
|
|
else if (!strcmp(argv[i], "-maxcolors"))
|
|
{
|
|
i++;
|
|
imlib_set_color_usage(atoi(argv[i]));
|
|
}
|
|
else if (!strcmp(argv[i], "-font"))
|
|
{
|
|
i++;
|
|
fon = argv[i];
|
|
}
|
|
else if (!strcmp(argv[i], "-text"))
|
|
{
|
|
i++;
|
|
str = argv[i];
|
|
}
|
|
else if (!strcmp(argv[i], "-rotate"))
|
|
rotate = 1;
|
|
else if (!strcmp(argv[i], "-rotatetest"))
|
|
{
|
|
rottest = 1;
|
|
interactive = 0;
|
|
}
|
|
else if (!strcmp(argv[i], "-rotatetest2"))
|
|
{
|
|
rottest = 2;
|
|
interactive = 0;
|
|
}
|
|
else
|
|
file = argv[i];
|
|
}
|
|
printf("init\n");
|
|
disp = XOpenDisplay(NULL);
|
|
vis = DefaultVisual(disp, DefaultScreen(disp));
|
|
depth = DefaultDepth(disp, DefaultScreen(disp));
|
|
cm = DefaultColormap(disp, DefaultScreen(disp));
|
|
/* nasty - using imlib internal function.. but it makes benchmarks fair */
|
|
if (!interactive)
|
|
__imlib_SetMaxXImageCount(disp, 3);
|
|
if (root)
|
|
win = DefaultRootWindow(disp);
|
|
else
|
|
{
|
|
win = XCreateSimpleWindow(disp, DefaultRootWindow(disp), 0, 0, 10, 10, 0, 0, 0);
|
|
XSelectInput(disp, win, ButtonPressMask | ButtonReleaseMask |
|
|
ButtonMotionMask | PointerMotionMask | ExposureMask);
|
|
}
|
|
if (!interactive)
|
|
{
|
|
im = imlib_load_image_immediately(file);
|
|
if (!im)
|
|
{
|
|
printf("load fialed\n");
|
|
exit(0);
|
|
}
|
|
imlib_context_set_image(im);
|
|
w = imlib_image_get_width();
|
|
h = imlib_image_get_height();
|
|
}
|
|
if (!root)
|
|
{
|
|
XResizeWindow(disp, win, w, h);
|
|
XMapWindow(disp, win);
|
|
}
|
|
if (scale)
|
|
{
|
|
Window d;
|
|
int dd;
|
|
|
|
XGetGeometry(disp, win, &d, &dd, &dd, &w, &h, &dd, &dd);
|
|
}
|
|
XSync(disp, False);
|
|
printf("rend\n");
|
|
gettimeofday(&timev,NULL);
|
|
sec1=(int)timev.tv_sec; /* and stores it so we can time outselves */
|
|
usec1=(int)timev.tv_usec; /* we will use this to vary speed of rot */
|
|
|
|
imlib_context_set_display(disp);
|
|
imlib_context_set_visual(vis);
|
|
imlib_context_set_colormap(cm);
|
|
imlib_context_set_drawable(win);
|
|
imlib_context_set_anti_alias(aa);
|
|
imlib_context_set_dither(dith);
|
|
imlib_context_set_blend(blend);
|
|
imlib_context_set_color_modifier(NULL);
|
|
imlib_context_set_operation(IMLIB_OP_COPY);
|
|
imlib_context_set_image(im);
|
|
|
|
if (loop)
|
|
{
|
|
for (i = 0; i < w; i++)
|
|
{
|
|
imlib_render_image_on_drawable_at_size(0, 0,
|
|
w - i, (((w - i) * h) / w));
|
|
pixels += (w - i) * (((w - i) * h) / w);
|
|
}
|
|
}
|
|
else if (blendtest)
|
|
{
|
|
Imlib_Image im2;
|
|
|
|
im2 = imlib_create_image(w, h);
|
|
imlib_context_set_image(im2);
|
|
w = imlib_image_get_width();
|
|
h = imlib_image_get_height();
|
|
imlib_context_set_image(im);
|
|
for (i = 0; i < 1024; i++)
|
|
{
|
|
imlib_blend_image_onto_image(im2, 0, 0, 0, w, h, 0, 0, w, h);
|
|
pixels += (w * h);
|
|
}
|
|
}
|
|
else if (rottest == 1)
|
|
{
|
|
Imlib_Image rotim;
|
|
double ang;
|
|
int x, y;
|
|
|
|
rotim = imlib_create_rotated_image_test(ang);
|
|
imlib_context_set_image(rotim);
|
|
x = imlib_image_get_width();
|
|
y = imlib_image_get_height();
|
|
printf("rotating %dx%d cutout\n", x, y);
|
|
x = (w - x) / 2;
|
|
y = (h - y) / 2;
|
|
imlib_free_image_and_decache();
|
|
imlib_context_set_image(im);
|
|
imlib_render_image_on_drawable(0, 0);
|
|
|
|
for (ang = 0.0; ang < 6.2831853; ang += 0.031415927) {
|
|
imlib_context_set_image(im);
|
|
rotim = imlib_create_rotated_image_test(ang);
|
|
imlib_context_set_image(rotim);
|
|
imlib_render_image_on_drawable(x, y);
|
|
pixels += imlib_image_get_width() * imlib_image_get_height();
|
|
imlib_free_image_and_decache();
|
|
}
|
|
}
|
|
else if (rottest == 2)
|
|
{
|
|
Imlib_Image rotim, im2;
|
|
double ang;
|
|
int x, y;
|
|
|
|
rotim = imlib_create_rotated_image_test2(ang);
|
|
imlib_context_set_image(rotim);
|
|
x = imlib_image_get_width();
|
|
y = imlib_image_get_height();
|
|
printf("rotating inside %dx%d frame\n", x, y);
|
|
if (!root)
|
|
XResizeWindow(disp, win, x, y);
|
|
imlib_free_image_and_decache();
|
|
imlib_context_set_image(im);
|
|
|
|
for (ang = 0.0; ang < 6.2831853; ang += 0.031415927) {
|
|
imlib_context_set_image(im);
|
|
im2 = imlib_create_cropped_scaled_image(0, 0, w, h, x, y);
|
|
rotim = imlib_create_rotated_image_test2(ang);
|
|
imlib_context_set_blend(1);
|
|
imlib_context_set_image(im2);
|
|
imlib_blend_image_onto_image(rotim, 0, 0, 0,
|
|
x, y, 0, 0, x, y);
|
|
imlib_render_image_on_drawable(0, 0);
|
|
imlib_free_image_and_decache();
|
|
imlib_context_set_image(rotim);
|
|
pixels += imlib_image_get_width() * imlib_image_get_height();
|
|
imlib_free_image_and_decache();
|
|
}
|
|
}
|
|
else if (interactive)
|
|
{
|
|
int wo, ho, px, py, first = 1;
|
|
Imlib_Image im_bg, im_sh1, im_sh2, im_sh3, im_ic[13], im_tmp;
|
|
/* Imlib_Border border; */
|
|
Imlib_Updates up = NULL;
|
|
int x, y, i, j;
|
|
XEvent ev;
|
|
Imlib_Font fn=NULL;
|
|
|
|
/* "ARIAL/30" "COMIC/30" "IMPACT/30" "Prole/30" "Proteron/30" */
|
|
/* "TIMES/30" "badacid/30" "bajoran/30" "bigfish/30" */
|
|
imlib_add_path_to_font_path("./ttfonts");
|
|
if (fon)
|
|
{
|
|
fn = imlib_load_font(fon);
|
|
imlib_context_set_font(fn);
|
|
imlib_context_set_direction(IMLIB_TEXT_TO_RIGHT);
|
|
if (!fn)
|
|
fon = NULL;
|
|
}
|
|
|
|
imlib_context_set_progress_function(NULL);
|
|
imlib_context_set_progress_granularity(0);
|
|
if (file)
|
|
im_bg = imlib_load_image(file);
|
|
else
|
|
im_bg = imlib_load_image("test_images/bg.png");
|
|
imlib_context_set_image(im_bg);
|
|
im_tmp = imlib_clone_image();
|
|
w = imlib_image_get_width();
|
|
h = imlib_image_get_height();
|
|
wo = w;
|
|
ho = h;
|
|
w *= 1;
|
|
h *= 1;
|
|
XResizeWindow(disp, win, w, h);
|
|
XSync(disp, False);
|
|
im = imlib_create_image(w, h);
|
|
imlib_set_cache_size(4 * 1024 * 1024);
|
|
i = 0;
|
|
up = imlib_update_append_rect(up, 0, 0, w, h);
|
|
x = -9999;
|
|
y = -9999;
|
|
while (1)
|
|
{
|
|
px = x;
|
|
py = y;
|
|
do
|
|
{
|
|
XNextEvent(disp, &ev);
|
|
switch (ev.type)
|
|
{
|
|
case Expose:
|
|
up = imlib_update_append_rect(up,
|
|
ev.xexpose.x, ev.xexpose.y,
|
|
ev.xexpose.width, ev.xexpose.height);
|
|
break;
|
|
case ButtonRelease:
|
|
exit(0);
|
|
break;
|
|
case MotionNotify:
|
|
x = ev.xmotion.x;
|
|
y = ev.xmotion.y;
|
|
default:
|
|
break;
|
|
|
|
}
|
|
}
|
|
while (XPending(disp));
|
|
|
|
im_sh1 = imlib_load_image("test_images/sh1.png");
|
|
im_sh2 = imlib_load_image("test_images/sh2.png");
|
|
im_sh3 = imlib_load_image("test_images/sh3.png");
|
|
im_ic[0] = imlib_load_image("test_images/audio.png");
|
|
im_ic[1] = imlib_load_image("test_images/folder.png");
|
|
im_ic[2] = imlib_load_image("test_images/mush.png");
|
|
im_ic[3] = imlib_load_image("test_images/paper.png");
|
|
im_ic[4] = imlib_load_image("test_images/mail.png");
|
|
im_ic[5] = imlib_load_image("test_images/calc.png");
|
|
im_ic[6] = imlib_load_image("test_images/cal.png");
|
|
im_ic[7] = imlib_load_image("test_images/stop.png");
|
|
im_ic[8] = imlib_load_image("test_images/globe.png");
|
|
im_ic[9] = imlib_load_image("test_images/menu.png");
|
|
im_ic[10] = imlib_load_image("test_images/tnt.png");
|
|
im_ic[11] = imlib_load_image("test_images/bulb.png");
|
|
im_ic[12] = imlib_load_image("test_images/lock.png");
|
|
|
|
imlib_context_set_image(im);
|
|
if (first)
|
|
{
|
|
imlib_blend_image_onto_image(im_bg, 0,
|
|
0, 0, w, h,
|
|
0, 0, w, h);
|
|
first = 0;
|
|
}
|
|
else if (rotate)
|
|
{
|
|
Imlib_Image rotim;
|
|
double ang = (double)x / 100.0;
|
|
int rw, rh, rx, ry;
|
|
|
|
imlib_context_set_blend(1);
|
|
imlib_context_set_image(im_bg);
|
|
rotim = imlib_create_rotated_image_test2(ang);
|
|
imlib_context_set_image(rotim);
|
|
rw = imlib_image_get_width();
|
|
rh = imlib_image_get_height();
|
|
rx = (w - rw) / 2;
|
|
ry = (h - rh) / 2;
|
|
imlib_context_set_image(im);
|
|
imlib_blend_image_onto_image(rotim, 0,
|
|
0, 0, rw, rh,
|
|
rx, ry, rw, rh);
|
|
imlib_context_set_image(rotim);
|
|
imlib_free_image_and_decache();
|
|
imlib_context_set_image(im);
|
|
up = imlib_update_append_rect(up, rx, ry, rw, rh);
|
|
}
|
|
{
|
|
Imlib_Updates uu;
|
|
|
|
imlib_context_set_color(255, 255, 255, 255);
|
|
uu = imlib_image_draw_line(200, 200, x, y, 1);
|
|
up = imlib_updates_append_updates(up, uu);
|
|
}
|
|
{
|
|
static Imlib_Color_Range rg = NULL;
|
|
|
|
if (!rg)
|
|
{
|
|
rg = imlib_create_color_range();
|
|
imlib_context_set_color_range(rg);
|
|
imlib_context_set_color(255, 255, 255, 255);
|
|
imlib_add_color_to_color_range(0);
|
|
imlib_context_set_color(255, 255,160, 255);
|
|
imlib_add_color_to_color_range(1);
|
|
imlib_context_set_color(255, 160, 120, 255);
|
|
imlib_add_color_to_color_range(1);
|
|
imlib_context_set_color(255, 80, 100, 128);
|
|
imlib_add_color_to_color_range(1);
|
|
imlib_context_set_color(32, 48, 80, 0);
|
|
imlib_add_color_to_color_range(1);
|
|
}
|
|
imlib_context_set_operation(IMLIB_OP_RESHADE);
|
|
imlib_image_fill_color_range_rectangle(60, 60, 256, 256, (double)x);
|
|
up = imlib_update_append_rect(up, 60, 60, 256, 256);
|
|
imlib_context_set_operation(IMLIB_OP_COPY);
|
|
}
|
|
if (fon)
|
|
{
|
|
int retw, reth, ty, nx, ny, cx, cy, cw, ch, cp;
|
|
|
|
if (!str)
|
|
str = "This is a test string";
|
|
ty = 50;
|
|
for (i = 0; i < 16; i++)
|
|
{
|
|
int al;
|
|
|
|
al = (15 - i) * 16;
|
|
if (al > 255)
|
|
al = 255;
|
|
imlib_context_set_color(255, 255, 255, al);
|
|
imlib_text_draw_with_return_metrics(50, ty, str,
|
|
&retw, &reth,
|
|
&nx, &ny);
|
|
up = imlib_update_append_rect(up, 50, ty, retw, reth);
|
|
ty += ny;
|
|
}
|
|
cp = imlib_text_get_index_and_location(str, x - 50, y - 50,
|
|
&cx, &cy, &cw, &ch);
|
|
if (cp >= 0)
|
|
printf("over char %c\n", str[cp]);
|
|
}
|
|
imlib_context_set_blend(1);
|
|
if ((px != x) || (py != y))
|
|
{
|
|
for (j = 0; j < 32; j++)
|
|
{
|
|
for (i = 0; i < 32; i++)
|
|
{
|
|
int ic, iw, ih, ww, hh;
|
|
|
|
ic = ((j * 32) + i) % 13;
|
|
imlib_context_set_image(im_ic[ic]);
|
|
iw = imlib_image_get_width();
|
|
ih = imlib_image_get_height();
|
|
ww = iw;
|
|
hh = ih;
|
|
up = imlib_update_append_rect(up, x + (i * iw * 2),
|
|
y + (j * ih * 2), ww, hh);
|
|
up = imlib_update_append_rect(up, px + (i * iw * 2),
|
|
py + (j * ih * 2), ww, hh);
|
|
imlib_context_set_image(im);
|
|
imlib_blend_image_onto_image(im_ic[ic], 0,
|
|
0, 0, iw, ih,
|
|
x + (i * iw * 2),
|
|
y + (j * ih * 2),
|
|
ww, hh);
|
|
}
|
|
}
|
|
}
|
|
imlib_blend_image_onto_image(im_sh1, 0, 0, 0, 50, 50, 0, 0, 50, 50);
|
|
up = imlib_update_append_rect(up, 0, 0, 50, 50);
|
|
imlib_blend_image_onto_image(im_sh2, 0, 0, 0, 50, 50, 50, 0, w - 50, 50);
|
|
up = imlib_update_append_rect(up, 50, 0, w - 50, 50);
|
|
imlib_blend_image_onto_image(im_sh3, 0, 0, 0, 50, 50, 0, 50, 50, h - 50);
|
|
up = imlib_update_append_rect(up, 0, 50, 50, h - 50);
|
|
up = imlib_updates_merge_for_rendering(up, w, h);
|
|
imlib_context_set_blend(0);
|
|
imlib_render_image_updates_on_drawable(up, 0, 0);
|
|
if ((px != x) || (py != y))
|
|
{
|
|
Imlib_Updates u;
|
|
|
|
u = up;
|
|
while (u)
|
|
{
|
|
int ux, uy, uw, uh;
|
|
|
|
imlib_updates_get_coordinates(u, &ux, &uy, &uw, &uh);
|
|
imlib_blend_image_onto_image(im_bg, 0,
|
|
ux, uy, uw, uh,
|
|
ux, uy, uw, uh);
|
|
u = imlib_updates_get_next(u);
|
|
}
|
|
}
|
|
imlib_updates_free(up);
|
|
up = NULL;
|
|
imlib_context_set_image(im_sh1);imlib_free_image();
|
|
imlib_context_set_image(im_sh1);imlib_free_image();
|
|
imlib_context_set_image(im_sh1);imlib_free_image();
|
|
imlib_context_set_image(im_ic[0]);imlib_free_image();
|
|
imlib_context_set_image(im_ic[1]);imlib_free_image();
|
|
imlib_context_set_image(im_ic[2]);imlib_free_image();
|
|
imlib_context_set_image(im_ic[3]);imlib_free_image();
|
|
|
|
}
|
|
}
|
|
else
|
|
{
|
|
for (i = 0; i < w; i++)
|
|
{
|
|
imlib_render_image_on_drawable_at_size(0, 0, w, h);
|
|
pixels += w * h;
|
|
}
|
|
}
|
|
gettimeofday(&timev,NULL);
|
|
sec2=(int)timev.tv_sec; /* and stores it so we can time outselves */
|
|
usec2=(int)timev.tv_usec; /* we will use this to vary speed of rot */
|
|
printf("done\n");
|
|
i = sec2 - sec1;
|
|
j = usec2 - usec1;
|
|
while (j < 0)
|
|
{
|
|
i++;
|
|
j += 1000000;
|
|
}
|
|
sec = (double)i + ((double)j / 1000000);
|
|
printf("%3.3f sec\n", sec);
|
|
printf("%3.3f Mpixels / sec\n", (double)(pixels) / (sec * 1000000));
|
|
return 0;
|
|
}
|