Eyeon:Script/Reference/Applications/Fuse/Classes/Image/Constructor

From VFXPedia

< Eyeon:Script | Reference | Applications | Fuse | Classes | Image
Revision as of 05:10, 9 November 2010 by Daniel (Talk | contribs)
(diff) ← Older revision | Current revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Contents


Summary

The Image function is used when a new image needs to be created in memory. Its sole argument is a table of attributes which describe the new image. The image function returns a handle to the new Image object.

Usage

Image(table image_attributes)

image_attributes
The Image function's only argument is a table of attributes which describe the images width, height, color depth, and so forth. In the majority of cases the width, height, aspect depth and other attributes of the new image should exactly match those of another image already in memory. In that case the attribute table would be as simple as
    local out = Image({IMG_Like=image}
There are occasionally times when it will be necessary to specify one or more of the values explicitly, instead of taking them from another image input. For example, if we wanted to create an image exactly like the image src_image, but with a different color depth, we might use :
    local out = Image({IMG_Like = img, IMG_Depth = src_depth})
In a source tool like the native Background tool or the Plasma tool, it is usually necessary to specify all of the Image attributes.
See the full table of image attributes for more details.

Example

The following example Fuse takes two image inputs, creates a new image with exactly the same attributes as the first Image input, then adds the two together.

FuRegisterClass("Add", CT_Tool, {
	REGS_Category = "Fuses",
	REGS_OpIconString = "fAd",
	REGS_OpDescription = "Add Fuse",
	})
 
function Create()
	InImage1 = self:AddInput("Input 1", "Input1", {
		LINKID_DataType = "Image",
		LINK_Main = 1,
		})
 
	InImage2 = self:AddInput("Input 2", "Input2", {
		LINKID_DataType = "Image",
		LINK_Main = 2,
		})
 
	OutImage = self:AddOutput("Output", "Output", {
		LINKID_DataType = "Image",
		LINK_Main = 1,
		})				
end
 
-- Function table for our operations
function func(x,y,p1,p2) -- add
	p1.R = p1.R + p2.R
	p1.G = p1.G + p2.G
	p1.B = p1.B + p2.B
	p1.A = p1.A + p2.A
	return p1
end
 
 
function Process(req) 
	local img1 = InImage1:GetValue(req)
	local img2 = InImage2:GetValue(req)
	
	local out = nil -- fail if we don't meet below conditions
	
	-- Must have a valid operation function, and images must be same dimensions
	if (img1.Width == img2.Width) and (img1.Height == img2.Height) then
	
		out = Image({IMG_Like = img1})
		
		out:ProcessPixels(0,0, img1.Width, img1.Height, img1, img2, func)
		
	end
	OutImage:Set(req, out)
end

The following is a copy of the example SourceTest.Fuse from the Example Fuses page. It creates an image from scratch using the full range of attributes for an Image object.

FuRegisterClass("Source", CT_SourceTool, {
	REGS_Category = "Fuses",
	REGS_OpIconString = "fSc",
	REGS_OpDescription = "Source Fuse",
	
	REG_Source_GlobalCtrls = true,
	REG_Source_SizeCtrls = true,
	REG_Source_AspectCtrls = true,
	REG_Source_DepthCtrls = true,
	
	REG_TimeVariant = true,
	})
 
 
function Create()
end
 
function Process(req) 
	local realwidth = Width;
	local realheight = Height;
	
	-- We'll handle proxy ourselves
	Width = Width / Scale
	Height = Height / Scale
	Scale = 1
	
	local imgattrs = {
		IMG_Document = self.Comp,
		{ IMG_Channel = "Red", },
		{ IMG_Channel = "Green", },
		{ IMG_Channel = "Blue", },
		{ IMG_Channel = "Alpha", },
		IMG_Width = Width,
		IMG_Height = Height,
		IMG_XScale = XAspect,
		IMG_YScale = YAspect,
		IMAT_OriginalWidth = realwidth,
		IMAT_OriginalHeight = realheight,
		IMG_Quality = not req:IsQuick(),
		IMG_MotionBlurQuality = not req:IsNoMotionBlur(),
		}
	
	if not req:IsStampOnly() then
		imgattrs.IMG_ProxyScale = 1
	end
	
	if SourceDepth ~= 0 then
		imgattrs.IMG_Depth = SourceDepth
	end
	
	
	local img = Image(imgattrs)
	
	local random = math.random -- faster in a local
	
	local p = Pixel({A=1})
	
	for y=0,Height-1 do
		if self.Status ~= "OK" then break end
		
		for x=0,Width-1 do
			p.R = random()
			p.G = random()
			p.B = random()
			img:SetPixel(x,y, p)
		end
	end
	
	OutImage:Set(req, img)
	
end


Tips for Constructor (edit)

  • An image created by using {IMG_Like = ...} is not cleared. Its pixels may contain old data from the image cache. If you don't use the image as a target for methods that overwrite the pixels with valid ones you have to clear it yourself using the Fill method.
  • If you want to work on a 32bit floating point version of the input image internally to prevent quality loss when chaining multiple color corrections, use the Crop method to copy the original image to a temporary one:
 local orig = InImage:GetValue(req)
 -- copy to a float32 image
 local temp32 = Image({IMG_Like = orig, IMG_Depth = 8})
 orig:Crop(temp32, {CROP_XOffset = 0, CROP_YOffset = 0})
 
 --- do processing ---
 
 -- copy to an image of the original bit depth
 out = Image({IMG_Like = orig})
 temp32:Crop(out, {CROP_XOffset = 0, CROP_YOffset = 0})
 OutImage:Set(req, out)
  • Single-channel images (IMG_Depth < 5, see Attributes) have an alpha channel only. Methods like Gain() still need to be called with four parameters, the first three will be ignored.