java & SWT - how to use masking on images - image

i'm trying to use masking on an image in order to set a part of the image to be transparent.
from what i've read , it's supported by the SWT library , but i can't find a way to do it . i can't find even an example for that .
i've therefore created my own way to do it , by implementing the masking part :
first i find an RGB value that isn't taken anywhere on the image , and then i set it to be the transparency color. then i take the masking image , and use it to put the correct pixel (either transparent or taken from the source image) in order to create a new image.
it works , but i wish i had a simpler method .
can anyone please help?

This constructor on the Image class looks to do the job:
public Image(Device device,
ImageData source,
ImageData mask)
from the javadoc...
Constructs an instance of this class, whose type is SWT.ICON, from the two given ImageData objects. The two images must be the same size. Pixel transparency in either image will be ignored.
The mask image should contain white wherever the icon is to be visible, and black wherever the icon is to be transparent. In addition, the source image should contain black wherever the icon is to be transparent.
Parameters:
device - the device on which to create the icon
source - the color data for the icon
mask - the mask data for the icon

Related

Delphi PNGImage - Image transparency is not pure

I am using PNG images as main image resource in my application. Since im using Delphi 7, i downloaded PNGImage lib and included it in project. I load images like this:
Form.image.Picture.LoadFromFile(PAnsiChar('\background.png'));
Image has transparent and semi transparent pixels on its border. The problem i get is that transparent pixels are filled with random zoomed part of my desktop with currently opened windows, while i expected to see what is actually located beneath form.
Additionally, form has this properties:
BorderStyle: bsNone;
TransparentColor: true;
Visible: false;
Here is a picture of current state (above black line) and desired:
Can this be fixed somehow or it is how delphi deals with transparency?
To have the form "shade" what's beneath it, use the forms AlphaBlend and AlphaBlendValue properties. The .png image doesn't have to be partially (alpha blended) transparent, but it can be.
If you want the form to be semi-transparent you use Alphablending, that's a limitation of Windows. In addition you can have a certain color fully transparent. In the following sample the forms color is clGray, which is also defined as the Transparent color property in addition to the Transparent property set to True. The image, aligned alClient, is 50% transparent, placed on a TImage which is set as transparent, but even so, it doesn't show up as semi-transparent unless you have AlphaBlending on. Again, this is a limitation of Windows. The best you can do is try with a fairly high value for AlphaBlendingValue (240..250) and a rather light image to find the right compromise.

read image but show strange colour

I have read a image like that
a = imread('test.jpg');
image(a)
what the test.jpg is:
but after the image function
the result is:
and I don't know why it show that?
Because I want to crop some part, so I have to see the image shown.
How to fix it by showing the human face by image?
You are using image command to display an image. From here: "image creates an image graphics object by interpreting each element in a matrix as an index into the figure's colormap or directly as RGB values". Since you are providing a 2-D matrix, each element is interpreted as the index to the figures colormap. You can see the figure's colormap by using
c_map=colormap;
Also, the axis is set to square, therefore you see a circle instead of an ellipse. Use imshow(a,[]) to display the grayscale image as desired.
You image data might be in a color map instead. try [a,cmap] = imread(...). If cmap is not empty, a is indices into cmap, and cmap contains the actual colors.
Use img = cat(3,cmap(a,1),cmap(a,2),cmap(a,3)) to get your image, and show it with image(img).
Note that using imagesc might be misleading in this case as it will still show something that looks like your image when simply doing imagesc(a). This as different pixels colors are associated with different index-values in a.

Convert StdPicture Transparency Colour to White

I am using a CodeJock ImageManager component to hold a variety of images. I want to put one of these images into a FlexGrid Cell.
The Images I have are Png format and have transparent backgrounds so when I load the image into the grid like so:
Grid.Cell(flexcpPicture, 123, 4) = _
ImageManagerControl.Icons.GetImage(ImageNum, 16).CreatePicture(xtpImageNormal)
the background which in the original image was transparent is now black:
(the same happens if I load the image into a PictureBox using the above method)
According to the documentation CreatePicture returns an StdPicture object, is there some way I can convert this image (using BitBlt perhaps) so that the black is white? I'm not sure if this even possible?
I only need to do this with about three images so if I have to load them into an ImageList or something else first that would be ok.
I'm not sure if this helps at all, but I've been playing around with the PictureBox. I put two picture boxes on a form, put a bmp file (this only works with bmp files, so it might not be helpful to you), and did this:
Picture2.PaintPicture Picture1.Picture, 0, 0, opcode:=vbNotSrcCopy
Which successfully inverted the bitmap. Here are the RasterOp constants: http://msdn.microsoft.com/en-us/library/aa243035(v=vs.60).aspx

How to Crop image to the maximum image rect?

In Mac OSX,
I have an image with black pixel in all 4 directions.
I want to programmatically crop the image to the maximum image rect.
Should i check for the black pixel and then create the crop rect or is there any supported API is there?
Create an NSImage of the desired size, lock focus on it, draw the desired crop rectangle of the source image into the whole bounds of the destination image, and unlock focus. The image you created now contains the crop from the source image.
Note that this will lose information like resolution (DPI), color profile, and EXIF tags. If you want to preserve those things (probably a good idea), use CGImage:
Use CGImageSource to load the image. Be sure to recover the properties of each image from the file, as well as the images themselves. And note that I used the plural: TIFF files can contain multiple images.
Use the CGImageCreateWithImageInRect function to crop out the desired section of each image. Don't forget to release each original image as appropriate.
If you want to write the cropped-out images to a file, do so using CGImageDestination. Pass both the images and the attributes dictionaries you obtained in step 1.

How do you programatically change the source of an image whose name is ambiguous?

How do you say or achieve container.child.source = image.png?
I have a hexagon map built by specifying width and height. I draw a wireframe and place a base image for each hex into a canvas. Next, the canvas listens for a mouse click. I then run a calculation to determine which hex the mouse click was closest to. My intent is to change the source of the image that the user clicked on.
I know that mapSlate.getChildByName(mapProperties[closestHex]['baseName']) is the intended hex but I can't quite get to the point of doing a .source as Flex doesn't know that the selected object is an image.
If u are sure that mapSlate.getChildByName(mapProperties[closestHex]['baseName']) is the intended hex and that it is in fact an Image, can't you cast it into Image and change the source like:
Image(mapSlate.getChildByName(
mapProperties[closestHex]['baseName'])).source = "image.png";
or
(mapSlate.getChildByName(
mapProperties[closestHex]['baseName']) as Image).source = "image.png";

Resources