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

From VFXPedia

Jump to: navigation, search

Contents


Summary

The Crop function can be used to produce a new image containing a portion of the image that calls the function.

Crop behaves differently depending on whether you pass the function a destination image or not. If you do provide a destination image, then the width and height of the crop are determined by that image, and you can only specify an offset. If you do not, then you can specify width and height, and a new image is returned.


Usage

img:Crop(object image, table attributes)


image (image, required)
This argument can be set to either nil, or to an Image object. If the image is provided, then the width and height of the crop are determined by the Image in the argument, and the only values which matter in the second arguments attributes table are the X and Y Offsets. If nil is provided, then a new image is returned by the function.
attributes (table, required)
This argument should consist of a table with the following attributes
  • CROP_XOffset - the X offset in pixels
  • CROP_YOffset - the Y offset in pixels
  • CROP_Width - the width of the result, in pixels
  • CROP_Height - the height of the result, in pixels

Example

The following example is a full featured Crop tool that can toggle between showing the full uncropped image with a cropping rectangle or the final cropped result.

FuRegisterClass("SuperCrop", CT_Tool, {
	REGS_Category = "Transform",
	REGS_OpIconString = "SCrp",
	REGS_OpDescription = "Super Crop",
	REG_NoAutoProxy = true,
	REG_NoMotionBlurCtrls = true,
	REG_NoObjMatCtrls = true,
	REG_NoBlendCtrls = true,
	REG_OpNoMask = true,
	})
	
function Create()
	InOperation = self:AddInput("Show", "Show", {
		LINKID_DataType = "Number",
		INPID_InputControl = "MultiButtonControl",
		INP_Default = 0.0,
		{ MBTNC_AddButton = "Uncropped", MBTNCD_ButtonWidth = 0.5, },
		{ MBTNC_AddButton = "Show Crop", MBTNCD_ButtonWidth = 0.5, },
		INP_DoNotifyChanged = true,
	})
	
	InCenter = self:AddInput("Center", "Center", {
		LINKID_DataType = "Point",
		INPID_InputControl = "OffsetControl",
		INPID_PreviewControl = "CrosshairControl",
		INP_DoNotifyChanged = true, -- We want to hear about changes on this control
		})
	
	InWidth = self:AddInput("Width", "Width", {
		LINKID_DataType = "Number",
		INPID_InputControl = "SliderControl",
		INP_Default = 1.0,
		INPID_PreviewControl = "RectangleControl",
		PC_ControlGroup = 1,
		PC_ControlID = 0,
		})			
 
	InHeight = self:AddInput("Height", "Height", {
		LINKID_DataType = "Number",
		INPID_InputControl = "SliderControl",
		INP_Default = 1.0,
		INPID_PreviewControl = "RectangleControl",
		PC_ControlGroup = 1,
		PC_ControlID = 1,
		})	
		
	InImage = self:AddInput("Input", "Input", {
		LINKID_DataType = "Image",
		LINK_Main = 1,
		})
 
	OutImage = self:AddOutput("Output", "Output", {
		LINKID_DataType = "Image",
		LINK_Main = 1,
		})				
		
end
 
function Process(req) 
	local img = InImage:GetValue(req)
	local center = InCenter:GetValue(req)
	local width = InWidth:GetValue(req).Value
	local height = InHeight:GetValue(req).Value
	local operation = InOperation:GetValue(req).Value
	
	
	if operation == 0 then 
		-- we are looking at the original, and doing nothing
		img:Use() -- increment the images Use count
		OutImage:Set(req, img)
		
	else 
		-- we are doing the actual crop
		
		local out = Image({
			IMG_Like = img,
			IMG_Width = math.floor(img.Width * width + 0.5),
			IMG_Height = math.floor(img.Height * height + 0.5),
			IMAT_OriginalWidth = math.floor(img.OriginalWidth * width + 0.5),
			IMAT_OriginalHeight = math.floor(img.OriginalHeight * height + 0.5),
			IMAT
		})
 
		img:Crop(out, {
			CROP_XOffset = math.floor(img.Width  * center.X - (img.Width  * width  / 2) + 0.5), 
			CROP_YOffset = math.floor(img.Height * center.Y - (img.Height * height / 2) + 0.5),
		})
		
		OutImage:Set(req, out)
	end
end
 
function NotifyChanged(inp, param, time)
 
	if inp == InCenter then   -- Center moved, update rectangle control position
		InHeight:SetAttrs({ RCD_SetX = param.X, RCD_SetY = param.Y })
		
	elseif inp == InOperation then
		
		InHeight:SetAttrs({ PC_Visible = (param.Value < 1) })
		InCenter:SetAttrs({ PC_Visible = (param.Value < 1) })
	end
	
end


Tips for Crop (edit)

  • If you don't specify any attributes in the attributes table, Crop will simply copy the input image to the output image. This is useful for several reasons: You can convert to a different bit depth that way and you can get rid of auxiliary channels (if the destination image doesn't have a channel, it won't be copied).