Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create new .png Image in D

Tags:

image

d

I am trying to create a .png image that is X pixels tall and Y pixels short. I am not finding what I am looking for on dlang.org, and am struggling to find any other resources via google.

Can you please provide an example of how to create a .png image in D?

For example, BufferedImage off_Image = new BufferedImage(100, 50, BufferedImage.TYPE_INT_ARGB); from http://docs.oracle.com/javase/tutorial/2d/images/drawonimage.html is what I am looking for (I think), except in the D programming language.

like image 751
Evorlor Avatar asked Dec 02 '22 20:12

Evorlor


2 Answers

I wrote a little lib that can do this too. Grab png.d and color.d from here:

https://github.com/adamdruppe/misc-stuff-including-D-programming-language-web-stuff

import arsd.png;

void main() {
    // width * height
    TrueColorImage image = new TrueColorImage(100, 50);

    // fill it in with a gradient
    auto colorData = image.imageData.colors; // get a ref to the color array
    foreach(y; 0 .. image.height)
        foreach(x; 0 .. image.width)
        colorData[y * image.width + x] = Color(x * 2, 0, 0); // fill in (r,g,b,a=255)
    writePng("test.png", image); // save it to a file
}
like image 123
Adam D. Ruppe Avatar answered Dec 23 '22 02:12

Adam D. Ruppe


There is nothing in standard library for image work but you should be able to use DevIL or FreeImage to do what you want. Both of them have Derelict bindings.

  • DevIL (derelict-il)
  • FreeImage (derelict-fi)

Just use the C API documentation for either of them.

like image 33
eco Avatar answered Dec 23 '22 01:12

eco