Set a wxImage to a specific wxColor - wxwidgets

Is there a way to set every Pixel of a wxImage to a specific wxColour?
I am looking for a simpler way then to iterate over the pixels.
Maybe there's some built in fucntionality in wxImage already?

You can use method wxImage::SetRGB:
wxImage::setRGB(const wxRect &rect, unsigned char red, unsigned char green, unsigned char blue)
which sets the colour of the pixels within the given rectangle. But this method does not affect pixels alpha value (if the image has alpha channel).
On the other hand, you can convert your image to wxBitmap and fill it with desired colour using wxDC (or just create empty bitmap, fill with the colour and then convert to wxImage). Here is the example:
wxBitmap bitmap(my_wxImage_instance);
wxMemoryDC memdc;
memdc.SelectObject(b);
memdc.SetBackground(*wxGREEN_BRUSH);
memdc.Clear(); //fills the entire bitmap with green colour
memdc.SelectObject(wxNullBitmap);
my_wxImage_instance = wxBitmap(bitmap); //optionally
wxMemoryDC description.

In addition to the (good) answer by #nnatarr, you can also use wxImage::Clear() to set the image to any grey colour, including black or white.

Related

Reading CMYK colors for graphic vectors from PDF

I am trying to read CMYK colors from a PDF file for graphic vectors, I am using PDFBOX 2 to read the color space, The color space being returned is of type PDSeparation with alternative color space of PDDeviceCMYK, I didn't know how to proceed with PDDeviceCMYK, so I extracted the RGB colors and will convert them back to CMYK, but I didn't even find a function to convert them back to CMYK, so is there a way to extract the CMYK colors directly from PDDeviceCMYK ?
PDColor color = getGraphicsState().getNonStrokingColor();
PDSeparation colorSpace = (PDSeparation) color.getColorSpace();
float[] rgb = colorSpace.toRGB(color.getComponents());
There are no CMYK colours in a Separation space, its a spot colour, for example a Pantone colour or something like Silver or Gold. You print it using the specific required ink.
In order to print (and display) the content on devices which don't have the required ink, Separation spaces have an Alternate colour space and a method for converting the input ink percentage into that colour space.
In your case the Alternate is DeviceCMYK and there will be a PDF Function which takes 1 input and returns 4 outputs. Given a colour between 0 and 1 of the Separation ink, it will return the equivalent CMYK values.
There are no RGB components for you to recover from the file either, I presume that colorSpace.toRGB() is retrieving the ink value, running the function to convert that to CMYK and then converting the CMYK to RGB. Assuming that pdfbox has a colorSpace.toCMYK() function I would use that instead.
In Addition to what #KenS said in his first comment, and with the help of #Tilman, you can extract the CMYK colors by overriding protected / private code inside PDSeperation.java, you can do it like this, I am not posting the entire code, but the section to read the colors are posted below
private static final int TINT_TRANSFORM = 3;
PDColor color = getGraphicsState().getNonStrokingColor();
COSArray array = (COSArray) color.getColorSpace().getCOSObject();
PDFunction tintTransform = PDFunction.create(array.getObject(TINT_TRANSFORM));
cmykColor = tintTransform.eval(color.getComponents());

Lesscss: Why spin(#000,180) is returning #000?

I need to know how to get the inverse color by lesscss.
Example: I have #000, i need #FFF.
And i need the detail explanation of spin(). And necessary links where i can see a color wheel where i can understand how spin() works.
Thanks.
Why it is not working as you expect
The spin() function only deals with hue (color), not value (grey scale changes are a value change). Take a look at Figures 9 and 10 on this page from North Carolina State University's site. Those figures help show the difference. The spin() function is rotating only in the two dimensional space of the hue circle of color, not along the axis of the third dimensional space dealing with saturation; i.e. the gray scale itself, which is what differentiates white from black, both of which have no color saturation).
This is why on the LESS site we read of spin() (emphasis added):
Note that colors are passed through an RGB conversion, which doesn't
retain hue value for greys (because hue has no meaning when there is
no saturation)
And
Colors are always returned as RGB values, so applying spin to a grey
value will do nothing.
Getting what you want (Color Inversion)
See #seven-phases-max's answer.
The spin function changes the Hue property of a colour. Shades of grey (incl. white and black) are achromatic colours (i.e. they have the same "undefined" hue value).
To simply invert a colour use either difference function:
difference(white, #colour)
or the simple colour arithmetic:
(#fff - #colour)

How to convert 32 bit PNG to RGB565?

How can I accomplish this? A programmatic solution (Objective-c) is great, but even a non-progarmmatic one is good.
I have pixelmator -> But that doesn't give you the option. I can't seem to do it with Preview either.
I have tried googling, but haven't been able to find a solution so far. The only tool I have been able to use to do this is TexturePacker, but that creates a sprite sheet.
You can use libpng to convert the PNG image to three-byte (8:8:8) RGB. Then you can downsample to the 5:6:5 16-bit color values of RGB565. If r, g, and b are the respective 8-bit colors (stored in an unsigned char type), then the 16-bit RGB565 value is:
((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3)
You can improve a tad on this by rounding instead of chopping, being careful to not overflow the values. You can also force the green value to be equal to the blue and red values when they are all equal in the original 8-bit values. Otherwise it is possible to have colors that were originally gray inadvertently take on color after conversion.
Create Bitmap Context with color RGB565 using Quartz, paint your PNG on this context, save this bitmap context to file.
PNG does not support a RGB565 packing. You can always apply a posterize to the image (programatically or with ImageMagick or with any image editor), which amounts to discard the lower significant bits in each channel. When saving to PNG, you will still be saving 8 bits per channel (unless you use a palette), but even then you will get an appreciable reduction in size, because of the PNG compression.
A quick example: original:
after a simple posterize with 32 levels (equivalent to a RGB555) applied with XnView
The size goes from 89KB to 47KB, with a small quality loss.
In case of synthetic images with gradients, the quality loss could be much more noticiable (banding).
I received this answer from the creator of texture packer:
you can do it from command line - see
http://www.texturepacker.com/uncategorized/batch-converting-images-to-pvr-or-pvr-ccz/
Just adjust the opt and set output to .png instead of pvr.ccz
Make sure that you do not overwrite your source images.
According to Wikipedia, which is always right, the only 16-bit PNG is a greyscale PNG. http://en.wikipedia.org/wiki/Portable_Network_Graphics
If you just add your 32-bit (alpha) or 24-bit (no alpha) PNG to your project as normal, and then set the texture format in Cocos2D, all should be fine. The code for that is:
[CCTexture2D setDefaultAlphaPixelFormat:kCCTexture2DPixelFormat_RGB565];

create a new image with only masked part (without transparent area) with new size

I have a mask and an image on which mask is applied to get a portion of that image.
The problem is when I apply that mask on the image ,the resultant image from masking is of same size as the original image .Though the unmasked part is transparent. What I need is an image which only has the masked part of the original image ,I dont want transparent part to be in the image. so that the resultant image will be of smaller size an contain only the masked part.
Thanks
You can:
Draw the image to a new CGBitmapContext at actual size, providing a buffer for the bitmap. CGBitmapContextCreate
Read alpha values from the bitmap to determine the transparent boundaries. You will have to determine how to read this based on the pixel data you have specified.
Create a new CGBitmapContext providing the external buffer, using some variation or combination of: a) a pixel offset, b) offset bytes per row, or c) manually move the bitmap's data (in place to reduce memory usage, if possible). CGBitmapContextCreate
Create a CGImage from the second bitmap context. CGBitmapContextCreateImage

How to replace all pixels of some color in a bitmap in Rebol?

Let's say I have a picture, I want to create some variations by changing a color. How to do this ?
I don't want to apply color filter to a picture, I want to change pixels color pixel by pixel by testing a color pixel if it is let's say red, i want to turn it to blue.
In Rebol images are also series, so you can use most of the series functions to change/find rgb colors etc.
i: load %test.png
type? i
image!
first i
255.255.255.0 (the last value is alpha)
change i 255.0.0.0 ;change the first rgba value to red
view layout [image i] ;you can see the upper-left pixel is now red
you can dump all rgba values in an image:
forall i [print first i]
you can also change a continues part:
change/dup head i blue 100 ;change first 100 pixels to blue
you can also work on i/rgb and i/alpha, these are binary values (bytes)
and you can use copy to get a part of an image:
j: copy/part at i 100x100 50x50 ;copy from 100x100 to 150x150 to a new image.
Use some of the image processing capabilities as documented here:
http://www.rebol.com/docs/view-guide.html
Demo program showing some of them in action here:
http://www.rebol.com/view/demos/gel.r