root/trunk/qd/test3.d

Revision 676, 1.7 kB (checked in by FeepingCreature, 2 years ago)
  • Faster blit
Line 
1 module test3;
2
3 import qd, SDL_image, std.file, std.stdio, std.math;
4
5 void main(string[] args) {
6   screen(640, 480);
7   int width = screen.width, height = screen.height;
8   foreach (idx, arg; args[1..$]) {
9     writefln("> ", arg);
10     Image img, scaled;
11     try {
12       img=new Image(read(arg));
13       scaled=img.nearscale(width, height);
14     } catch (Exception e) continue;
15     SDL_SetAlpha(scaled.surface, SDL_SRCALPHA, 255/(idx+1));
16     float limit=1f / pow(1f + idx, /*0.707*/0.6); //(idx/sqrt(args.length*1f)+1f);
17     writefln(limit, " > ", 0.05);
18     if (limit < 0.05) break;
19     for (int x=0; x<width; ++x) {
20       for (int y=0; y<height; ++y) {
21         ubyte[4] current;
22         getpixel32(display.surface, x, y, &current);
23         ubyte[4] newOne;
24         switch (scaled.surface.format.BytesPerPixel) {
25           case 4: getpixel32(scaled.surface, (x*scaled.width)/width, (y*scaled.height)/height, &newOne); break;
26           case 3: getpixel24(scaled.surface, (x*scaled.width)/width, (y*scaled.height)/height, &newOne); break;
27           default: writefln("Agh!"); goto agh;
28         }
29         float[3] dists=[current[0]*1f-newOne[0]*1f, current[1]*1f-newOne[1]*1f, current[2]*1f-newOne[2]*1f];
30         float dist=sqrt(dists[0]*dists[0]+dists[1]*dists[1]+dists[2]*dists[2]);
31         dist/=sqrt(3f*255*255);
32         void blend(ubyte[4] *what, ubyte[4] to, float f) {
33           foreach (id, ref e; *what) e=cast(ubyte)(e*(1f-f)+to[id]*f);
34         }
35         if (dist<=limit) {
36           blend(&current, newOne, dist);
37           pset(pt(x, y), rgb(current[0], current[1], current[2]));
38         }
39       }
40     }
41     agh:
42     events; flip;
43   }
44   writefln("Save as result.bmp");
45   SaveBMP(display, "result.bmp");
46   writefln("Done, looping");
47   while (true) { flip; events; }
48 }
Note: See TracBrowser for help on using the browser.