January 04, 2016
I think you are looking for something like this.

Context.getTarget will get you the surface the Context is drawing to, this most likely isn't a ImageSurface.
So you will need to create an pixbuf from the returned surface, with the Pixbuf you can then get the raw pixel data using getPixelsWithLength().

```
import gdk.Pixbuf;

bool drawCallback(Scoped!Context cr, Widget widget)
{
    GtkAllocation size;
    Pixbuf surafce;

    getAllocation(size);

    //Draw something;

    surface = getFromSurface(cr.getTarget(), 0, 0, size.width, size.height);

    ubyte[] data = cast(ubyte[])surface.getPixelsWithLength();

    //Do somthing with data.

    return true;
}
```

getPixelsWithLength has the wrong return type, which will probably be fixed some time.

-- 
Mike Wey
January 04, 2016
On Monday, 4 January 2016 at 19:27:48 UTC, Mike Wey wrote:
> I think you are looking for something like this.
>
> Context.getTarget will get you the surface the Context is drawing to, this most likely isn't a ImageSurface.
> So you will need to create an pixbuf from the returned surface, with the Pixbuf you can then get the raw pixel data using getPixelsWithLength().
>
> ```
> import gdk.Pixbuf;
>
> bool drawCallback(Scoped!Context cr, Widget widget)
> {
>     GtkAllocation size;
>     Pixbuf surafce;
>
>     getAllocation(size);
>
>     //Draw something;
>
>     surface = getFromSurface(cr.getTarget(), 0, 0, size.width, size.height);
>
>     ubyte[] data = cast(ubyte[])surface.getPixelsWithLength();
>
>     //Do somthing with data.
>
>     return true;
> }
> ```
>
> getPixelsWithLength has the wrong return type, which will probably be fixed some time.

Thank you very much! But surface.getPixelsWithLength() only gives me an array with 16 fields (with a 256x256 DrawingArea)?

I also tried to save the Pixbuf with:

string[] options = ["quality"];
string[] opval = ["100"];
surface.savev("C:\\Users\\Standardbenutzer\\Desktop\test.jpeg", "jpeg", options, opval);

but i found absolutely NOTHING about the options or the option values i have to set, therefore i get an invalid argument exception :(
January 04, 2016
On 01/04/2016 09:13 PM, TheDGuy wrote:
> On Monday, 4 January 2016 at 19:27:48 UTC, Mike Wey wrote:
>> I think you are looking for something like this.
>>
>> Context.getTarget will get you the surface the Context is drawing to,
>> this most likely isn't a ImageSurface.
>> So you will need to create an pixbuf from the returned surface, with
>> the Pixbuf you can then get the raw pixel data using
>> getPixelsWithLength().
>>
>> ```
>> import gdk.Pixbuf;
>>
>> bool drawCallback(Scoped!Context cr, Widget widget)
>> {
>>     GtkAllocation size;
>>     Pixbuf surafce;
>>
>>     getAllocation(size);
>>
>>     //Draw something;
>>
>>     surface = getFromSurface(cr.getTarget(), 0, 0, size.width,
>> size.height);
>>
>>     ubyte[] data = cast(ubyte[])surface.getPixelsWithLength();
>>
>>     //Do somthing with data.
>>
>>     return true;
>> }
>> ```
>>
>> getPixelsWithLength has the wrong return type, which will probably be
>> fixed some time.
>
> Thank you very much! But surface.getPixelsWithLength() only gives me an
> array with 16 fields (with a 256x256 DrawingArea)?
>
> I also tried to save the Pixbuf with:
>
> string[] options = ["quality"];
> string[] opval = ["100"];
> surface.savev("C:\\Users\\Standardbenutzer\\Desktop\test.jpeg", "jpeg",
> options, opval);
>
> but i found absolutely NOTHING about the options or the option values i
> have to set, therefore i get an invalid argument exception :(

I don't have any issues with either getPixelsWithLength and savev.
for the savev call there is an missing \ just before test.jpg, but that might be a copy and paste error?

For the options that are available for savev the documentation of GDK-PixBuff lists the few available options.
https://developer.gnome.org/gdk-pixbuf/unstable/gdk-pixbuf-File-saving.html#gdk-pixbuf-save

Although i'm on Linux so that might make an difference.

-- 
Mike Wey
January 04, 2016
On Monday, 4 January 2016 at 21:42:16 UTC, Mike Wey wrote:
> On 01/04/2016 09:13 PM, TheDGuy wrote:
>> [...]
>
> I don't have any issues with either getPixelsWithLength and savev.
> for the savev call there is an missing \ just before test.jpg, but that might be a copy and paste error?
>
> For the options that are available for savev the documentation of GDK-PixBuff lists the few available options.
> https://developer.gnome.org/gdk-pixbuf/unstable/gdk-pixbuf-File-saving.html#gdk-pixbuf-save
>
> Although i'm on Linux so that might make an difference.

Ups, that was my fault, sry :(

But how do i get now the color for each pixel out of the ubyte[]?
January 04, 2016
On Friday, 1 January 2016 at 22:00:04 UTC, TheDGuy wrote:
> On Friday, 1 January 2016 at 19:32:40 UTC, Basile B. wrote:
>> On Wednesday, 30 December 2015 at 23:20:23 UTC, Basile B. wrote:
>>> On Wednesday, 30 December 2015 at 20:44:44 UTC, TheDGuy wrote:
>>>> Hello,
>>>>
>>>> is there any way to get the pixel color of a single pixel by x and y coordinates of a context?
>>>
>>> render to a png back buffer.
>>>
>>> see cairo_image_surface_create_for_data
>>>
>>> then you'll be able to access the data and, at the same time, to blit your buffer to screen.
>>
>>
>> Actually I was thinking to a user defined buffer type:
>>
>> struct SurfaceBuffer
>> {
>>     void* data; // used as param to create the surface
>>     Rgba[] opIndex(size_t index);
>>     Rgba[][] scanline();
>> }
>>
>> that you would pass as data in cairo_image_surface_create_for_data().
>>
>> But gtk certainly has pitcure classes with the typical scanline method and that you could use in cairo_image_surface_create_for_data.
>
> Ahm, i am not quite sure if you and [Mike Wey] talk about the same thing. And i posted the error message in my last post when i try to call "cairo_image_surface_create_for_data". I still don't know where i am able to call the function?

I've not followed the conversation since last time, but you can have a look at this:

https://github.com/BBasile/kheops/blob/master/src/kheops/bitmap.d#L143

this is how I do with Cairo only (even x11 is not implied since it's just a bitmap).
Then I can access pixels (for example to make shadows or blurs etc.) and do vectorial drawings as well with a context for the bitmap surface.
January 05, 2016
On Monday, 4 January 2016 at 23:47:05 UTC, Basile B. wrote:
> On Friday, 1 January 2016 at 22:00:04 UTC, TheDGuy wrote:
>> On Friday, 1 January 2016 at 19:32:40 UTC, Basile B. wrote:
>>> On Wednesday, 30 December 2015 at 23:20:23 UTC, Basile B. wrote:
>>>> On Wednesday, 30 December 2015 at 20:44:44 UTC, TheDGuy wrote:
>>>>> Hello,
>>>>>
>>>>> is there any way to get the pixel color of a single pixel by x and y coordinates of a context?
>>>>
>>>> render to a png back buffer.
>>>>
>>>> see cairo_image_surface_create_for_data
>>>>
>>>> then you'll be able to access the data and, at the same time, to blit your buffer to screen.
>>>
>>>
>>> Actually I was thinking to a user defined buffer type:
>>>
>>> struct SurfaceBuffer
>>> {
>>>     void* data; // used as param to create the surface
>>>     Rgba[] opIndex(size_t index);
>>>     Rgba[][] scanline();
>>> }
>>>
>>> that you would pass as data in cairo_image_surface_create_for_data().
>>>
>>> But gtk certainly has pitcure classes with the typical scanline method and that you could use in cairo_image_surface_create_for_data.
>>
>> Ahm, i am not quite sure if you and [Mike Wey] talk about the same thing. And i posted the error message in my last post when i try to call "cairo_image_surface_create_for_data". I still don't know where i am able to call the function?
>
> I've not followed the conversation since last time, but you can have a look at this:
>
> https://github.com/BBasile/kheops/blob/master/src/kheops/bitmap.d#L143
>
> this is how I do with Cairo only (even x11 is not implied since it's just a bitmap).
> Then I can access pixels (for example to make shadows or blurs etc.) and do vectorial drawings as well with a context for the bitmap surface.

I tried it like in this C++ example:

http://stackoverflow.com/questions/16785886/get-pixel-value-on-gdkpixbuf-set-pixel-value-with-gdkcairo

surface = getFromSurface(cr.getTarget(),0,0,size.width,size.height);
int offset = 12*surface.getRowstride() + 12*surface.getNChannels();
auto px = surface.getPixels();
px[offset][0] = 0;

But i get "only one index allowed to index char". So it looks like there is no 2D array but just a char.
If i try like this:

auto px = surface.getPixels()[offset];

the value of 'px' looks like this in the debugger:

http://www.pic-upload.de/view-29334489/d_value.png.html


January 05, 2016
On Tuesday, 5 January 2016 at 15:04:57 UTC, TheDGuy wrote:
> But i get "only one index allowed to index char". So it looks like there is no 2D array but just a char.
> If i try like this:

The data is just a contiguous memory area. You have to implement your own opIndexAssign()/opIndex() to write/read a pixel at [line, column].

This is basically `dataPtr + (line * width + column) * 4`.

---
auto opIndex(size_t line, size_t column)
{
    auto ptr = basePtr + (line * width + column) * 4;
    // return something at "ptr".
}

void opIndexAssign(T)(auto ref T t, size_t line, size_t column)
{
    auto ptr = basePtr + (line * width + column) * 4;
    // assign t at "ptr".
}
---

(assuming format is ARGB, so 32bit, so "*4").
January 05, 2016
On Tuesday, 5 January 2016 at 16:16:39 UTC, Basile B. wrote:
> On Tuesday, 5 January 2016 at 15:04:57 UTC, TheDGuy wrote:
>> But i get "only one index allowed to index char". So it looks like there is no 2D array but just a char.
>> If i try like this:
>
> The data is just a contiguous memory area. You have to implement your own opIndexAssign()/opIndex() to write/read a pixel at [line, column].
>
> This is basically `dataPtr + (line * width + column) * 4`.
>
> ---
> auto opIndex(size_t line, size_t column)
> {
>     auto ptr = basePtr + (line * width + column) * 4;
>     // return something at "ptr".
> }
>
> void opIndexAssign(T)(auto ref T t, size_t line, size_t column)
> {
>     auto ptr = basePtr + (line * width + column) * 4;
>     // assign t at "ptr".
> }
> ---
>
> (assuming format is ARGB, so 32bit, so "*4").

But how do i know which line or column my pixel is in? And what is 't' in 'opIndexAssign'?
January 05, 2016
On Tuesday, 5 January 2016 at 16:25:01 UTC, TheDGuy wrote:
> But how do i know which line or column my pixel is in?
- study D operator overloading, I've given you the solution.

> And what is 't' in 'opIndexAssign'?
- t is what you want to assign. It can be an uint or maybe a float[4]. Look at my bitmap class.

January 05, 2016
On Tuesday, 5 January 2016 at 16:43:00 UTC, Basile B. wrote:
> On Tuesday, 5 January 2016 at 16:25:01 UTC, TheDGuy wrote:
>> But how do i know which line or column my pixel is in?
> - study D operator overloading, I've given you the solution.
>
>> And what is 't' in 'opIndexAssign'?
> - t is what you want to assign. It can be an uint or maybe a float[4]. Look at my bitmap class.

Okay, but what is this?

"import iz.memory, iz.streams, iz.properties;"

I dont' understand what "MemoryStream" is?