1 2 module example; 3 4 import 5 std.stdio, 6 std.datetime, 7 std.file, 8 std.stream; 9 10 import 11 imaged.image; 12 13 import 14 sdc = arsd.color, 15 sd = simpledisplay; /// Adam Ruppe's simpledisplay.d 16 17 int main() 18 { 19 /** 20 * For simple usage, loading from a file can be achieved by: 21 * Image pic = load(string filename); 22 */ 23 24 /** 25 * The example below shows how to decode using a stream. 26 * Note that it is quite slow, since we resize and re-paint 27 * the image after every streamParse. This is just designed 28 * to show usage, it is not efficient. 29 */ 30 31 // Grab a directory listing 32 auto dFiles = dirEntries("testimages/","*.png", SpanMode.depth); 33 34 // Make a window and simpledisplay image, with fixed width to keep things simple 35 sd.SimpleWindow wnd = new sd.SimpleWindow(512, 512, "Press space to change image, ESC to close"); 36 auto sd_image = new sd.Image(512, 512); 37 38 39 // This is the simpledisplay event loop 40 wnd.eventLoop(0, 41 42 // Character presses are handled here 43 (dchar c) 44 { 45 46 // Output the filename we are loading 47 writeln(dFiles.front.name); 48 49 // Get a decoder for this file 50 Decoder dcd = getDecoder(dFiles.front.name); 51 52 // Create a stream around the file 53 Stream stream = new BufferedFile(dFiles.front.name); 54 55 // Parse the stream until empty 56 while(dcd.parseStream(stream)) 57 { 58 // Get a handle to the image being created by the decoder 59 auto orig_pic = dcd.image; 60 if (orig_pic is null) continue; 61 62 // Make a copy so we can resize to fit the window 63 auto pic = orig_pic.copy(); 64 65 // Resize using nearest neighbour (alternatives are CROP and BILINEAR) 66 pic.resize(512, 512, Image.ResizeAlgo.NEAREST); 67 68 /* 69 * Paint to the simpledisplay image, using the resized copy of the decoded 70 * part of the current image 71 */ 72 foreach(x; 0..512) 73 { 74 foreach(y; 0..512) 75 { 76 77 // get the pixel at location (x,y) 78 Pixel pix = pic[x,y]; 79 80 // Use the alpha channel (if any) to blend the image to a white background 81 int r = cast(int)((pix.a/255.)*pix.r + (1 - pix.a/255.)*255); 82 int g = cast(int)((pix.a/255.)*pix.g + (1 - pix.a/255.)*255); 83 int b = cast(int)((pix.a/255.)*pix.b + (1 - pix.a/255.)*255); 84 85 // Paint in the pixel in simpledisplay image 86 sd_image.putPixel(x, y, sdc.Color(r, g, b)); 87 } 88 } 89 // Draw the current image 90 wnd.draw().drawImage(sd.Point(0,0), sd_image); 91 } 92 // Move on to the next filename 93 dFiles.popFront(); 94 95 }, 96 97 // Key presses are handled here 98 (int key) 99 { 100 writeln("Got a keydown event: ", key); 101 if(key == sd.KEY_ESCAPE) 102 { 103 wnd.close(); 104 } 105 }); 106 107 return 0; 108 }