Welcome Guest! You need to login or register to make posts.

Notification

Icon
Error

Options
Go to last post Go to first unread
flluidmedia  
#1 Posted : Monday, May 10, 2004 4:40:00 AM(UTC)
flluidmedia

Rank: Member

Groups: Member
Joined: 4/27/2004(UTC)
Posts: 40



I have 2 images that I need to paste toghether. I am currently using a second component to carry out the alpha blending, but I would like to replace this with the graphics mill if possible to cut donw on complexity.

How I do it right now is I have my target image which is a perspective room scene. I also have a black and white image, which is the mask. The floor is black and the rest of the image is white. I use this replace the floor in the image with another image that I create on the fly.

I couldnt find anything in the docs or the forums that explained how to do this although I did find enough to make me thinks that it would be at least possible.

Thanks,
Brian
Fluid Media
Andrew  
#2 Posted : Monday, May 10, 2004 2:23:00 PM(UTC)
Andrew

Rank: Advanced Member

Groups: Member, Administration
Joined: 8/2/2003(UTC)
Posts: 876

Thanks: 2 times
Was thanked: 27 time(s) in 27 post(s)
Current version of Graphics Mill does not allow directly work with masks. However you can solve your problem with the following way:

1. Convert your mask image to 8-bit grayscale image and resize it to your bitmap size.
2. Invert it if necessary (so that white become opaque area, black - transparent one).
3. Append it as alpha channel to the bitmap.
4. Perform alpha blending.

See the comments to this code, and let me know if something is unclear.

Code:
<!-- METADATA TYPE="typelib" UUID="{3CE48541-DE7A-4909-9314-9D0ED0D1CA5A}"--> 
<%

'Declarations
Dim objBackground	' The background image. It will contain the result of merge.
Dim objBitmap		' The image you put on the background.
Dim objMask		' This bitmap will contain alpha channel

Dim x, y			' Offset coordinates of the objBitmap on objBackground

Dim strBackgroundFilename
Dim strBitmapFilename
Dim strMergedFilename
Dim strMaskFilename

' Specify file names here
strBitmapFilename = Server.MapPath("FrontImage.jpg")
strBackgroundFilename = Server.MapPath("BackgroundImage.jpg")
strMergedFilename = Server.MapPath("Result.jpg")
strMaskFilename = Server.MapPath("Mask.tif")

' Create bitmaps
Set objBitmap = Server.CreateObject("GraphicsMill.Bitmap")
Set objBackground = Server.CreateObject("GraphicsMill.Bitmap")
Set objMask = Server.CreateObject("GraphicsMill.Bitmap")

' Load bitmaps
objBackground.LoadFromFile strBackgroundFilename
objBitmap.LoadFromFile strBitmapFilename
objMask.LoadFromFile strMaskFilename

' Check if alpha channel exists in the image. If not, 
' we must add it.
If Not objBitmap.Data.HasAlphaChannel Then
	objBitmap.Channels.AddAlpha
End If

' In this sample we will put the image into the center. 
' To centrize the coordinates, make these calculations:
x = (objBackground.Data.Width - objBitmap.Data.Width) / 2
y = (objBackground.Data.Height - objBitmap.Data.Height) / 2

' Alpha channel is always 8-bit grayscale image. That's why we need 
' to convert our 1-bit mask to grayscale.
objMask.Data.ConvertTo8bppGrayscale

' If your mask contains black as opaque area, and white as transparent,
' you must invert it. By the way, if you always use 1-bit images, the 
' optimal way to invert the image is to swap palette entries before converting
' to grayscale instead of using Invert method.
objMask.Effects.Invert

' To be able to retrieve objMask to alpha channel of the image, we 
' need make sure that it has the same dimensions as target image. 
' You can also use Resize instead of Crop if necessary.
objMask.Transforms.Crop 0, 0, objBitmap.Data.Width, objBitmap.Data.Height

' This method replaces channel with given number (zero-based) to the given
' grayscale image. Number 3 means alpha in ARGB pixel format. 
objBitmap.Channels.Replace objMask, 3

' Apply alpha blending
objBitmap.DrawOnBitmap objBackground, x, y, , , , , , , CombineModeAlpha

' Save the result
objBackground.SaveToFile strMergedFilename
objBackground.SaveToStream Response

' Cleanup
Set objMask = Nothing
Set objBitmap = Nothing
Set objBackground = Nothing
%>

Edited by user Monday, December 24, 2007 5:49:47 PM(UTC)  | Reason: Not specified

flluidmedia  
#3 Posted : Monday, May 10, 2004 11:43:00 PM(UTC)
flluidmedia

Rank: Member

Groups: Member
Joined: 4/27/2004(UTC)
Posts: 40

Thanks Andrew - that explains it really well.

Do you think you could put up some samples for c#? The reason I ask is that there are a lot of optional parameters in VB that are not optional for c# and I don't what the values should be. The docuemtation doesnt do a very good job of explaiing what the effects of various arguments should be.

Are the any c# docs or examples available for Graphics Mill?

Thanks,
Brian
Fluid Media
Andrew  
#4 Posted : Tuesday, May 11, 2004 1:27:00 PM(UTC)
Andrew

Rank: Advanced Member

Groups: Member, Administration
Joined: 8/2/2003(UTC)
Posts: 876

Thanks: 2 times
Was thanked: 27 time(s) in 27 post(s)
Unfortunately we have not included C# samples to the documentation. We tried to make the documentation as comprehensive as possible, but if you find something to be unclear, please feel free to post here or submit case).

Meanwhile, the same code sample ported to C#:

Code:
<%@ Page Language="C#" %>
<%@ import Namespace="GraphicsMill" %>
<%

// Declarations
GraphicsMill.Bitmap bitmap = new GraphicsMill.Bitmap();
GraphicsMill.Bitmap background = new GraphicsMill.Bitmap();
GraphicsMill.Bitmap mask = new GraphicsMill.Bitmap();

// Offset coordinates of the bitmap on background
int x, y; 

// Filenames
string bitmapFilename = Server.MapPath("FrontImage.jpg");
string backgroundFilename = Server.MapPath("BackgroundImage.jpg");
string mergedFilename = Server.MapPath("Result.jpg");
string maskFilename = Server.MapPath("Mask.tif");

// Load bitmaps
background.LoadFromFile(backgroundFilename);
bitmap.LoadFromFile(bitmapFilename);
mask.LoadFromFile(maskFilename);


// Check if alpha channel exists in the image. If not, 
// we must add it

if (!bitmap.Data.HasAlphaChannel)
{
	bitmap.Channels.AddAlpha();
}

// In this sample we will put the image into the center. 
// To centrize the coordinates, make these calculations:
x = (background.Data.Width - bitmap.Data.Width) / 2;
y = (background.Data.Height - bitmap.Data.Height) / 2;

// Alpha channel is always 8-bit grayscale image. That's why we need 
// to convert our 1-bit mask to grayscale.
mask.Data.ConvertTo8bppGrayscale(false, GraphicsMillConstants.ColorWhite);

// If your mask contains black as opaque area, and white as transparent,
// you must invert it. By the way, if you always use 1-bit images, the 
// optimal way to invert the image is to swap palette entries before converting
// to grayscale instead of using Invert method.
mask.ColorAdjustment.Invert();

// To be able to retrieve mask to alpha channel of the image, we 
// need make sure that it has the same dimensions as target image. 
// You can also use Resize instead of Crop if necessary.
mask.Transforms.Crop (0, 0, bitmap.Data.Width, bitmap.Data.Height, GraphicsMillConstants.ColorWhite);

// This method replaces channel with given number (zero-based) to the given
// grayscale image. Number 3 means alpha in ARGB pixel format. 
bitmap.Channels.Replace(mask, 3);

// Apply alpha blending
bitmap.DrawOnBitmap(background, x, y, -1, -1, 0, 0, -1, -1, CombineMode.CombineModeAlpha, 255, InterpolationMode.InterpolationModeMediumQuality, true, true, true);

// Save the result
background.SaveToFile(mergedFilename);
Response.ContentType = "image/jpeg";
Response.BinaryWrite((byte[])background.SaveToMemory());
%>

Edited by user Friday, May 23, 2008 4:11:14 PM(UTC)  | Reason: Not specified

flluidmedia  
#5 Posted : Wednesday, May 12, 2004 12:18:00 AM(UTC)
flluidmedia

Rank: Member

Groups: Member
Joined: 4/27/2004(UTC)
Posts: 40

Thanks Andrew!


I came up with something similar to get it working. My comment about the documentation was to do with the explanation of the parameters for different calls

For example, from the DrawOnBitmap call, for the CombineMode parameter:


CombineModeTexturize 12 Specifies texturize combine mode.
CombineModeColor 13 Specifies color combine mode.
CombineModeHue 14 Specifies hue combine mode.
CombineModeSaturation 15 Specifies saturation combine mode.
CombineModeLuminosity 16 Specifies luminosity combine mode.
CombineModeInvert 17 Specifies invert combine mode.


There is no explanation of what will happen if you choose one of these option. The name of the parameter is just reiterated.

I don't have a lot of experience in graphics processing, so perhaps my criticism is unfair and most people already know what these options mean. But when I go to look how to use a function, its not clear to me.

Well, thanks for your support! I've managed to get everything working in the way I wanted and I'm really glad that this forum is here - you and Fedor have been a great help!

B
Andrew  
#6 Posted : Wednesday, May 12, 2004 12:54:00 PM(UTC)
Andrew

Rank: Advanced Member

Groups: Member, Administration
Joined: 8/2/2003(UTC)
Posts: 876

Thanks: 2 times
Was thanked: 27 time(s) in 27 post(s)
These combine modes works in the same way as appropriate layer modes in Photoshop. So if you familiar to Photoshop you should have an idea how does it work. Usually you need use only Copy and Alpha combine modes, all others should be used only in some specific cases.

However you are right, we combine modes description should be added to the documentation. I think in the next release of Graphics Mill we will add an appropriate topic.

Anyway, feel free to let us know if you find something unclear again.
Users browsing this topic
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.