Platforms to show: All Mac Windows Linux Cross-Platform
/MacCG/CoreImage/Quickly generate previews for JPEG and Tiff
Required plugins for this example: MBS MacCI Plugin, MBS MacBase Plugin, MBS Images Plugin, MBS Main Plugin, MBS MacCG Plugin, MBS MacCF Plugin
You find this example project in your Plugins Download as a Xojo project file within the examples folder: /MacCG/CoreImage/Quickly generate previews for JPEG and Tiff
This example is the version from Thu, 31th Jul 2019.
Project "Quickly generate previews for JPEG and Tiff.xojo_binary_project"
Class App Inherits Application
Const kEditClear = "&Delete"
Const kFileQuit = "&Quit"
Const kFileQuitShortcut = ""
End Class
Class Window1 Inherits Window
Control list Inherits Listbox
ControlInstance list Inherits Listbox
End Control
EventHandler Sub Open()
dim TestTiff as FolderItem = SpecialFolder.Desktop.Child("TestTiff")
if TestTiff.Exists = false then
MsgBox "Please provide folder TestTiff on desktop with a dozen sample images."
end if
dim TestJPEG as FolderItem = SpecialFolder.Desktop.Child("TestJPEG")
if TestJPEG.Exists = false then
MsgBox "Please provide folder TestJPEG on desktop with a dozen sample images."
end if
dim TestResult as FolderItem = SpecialFolder.Desktop.Child("TestResult")
TestResult.CreateAsFolder
TestJPEGwithRB TestJPEG, TestResult
TestTIFFwithRB TestTiff, TestResult
TestJPEGwithMBS TestJPEG, TestResult
TestTIFFwithMBS TestTiff, TestResult
TestJPEGwithFasterMBS TestJPEG, TestResult
TestTIFFwithFasterMBS TestTiff, TestResult
TestJPEGwithCoreImage TestJPEG, TestResult
TestTIFFwithCoreImage TestTiff, TestResult
End EventHandler
Function ProportinalScaled(pic as Picture, Width as integer, Height as Integer) As Picture
// Calculate scale factor
dim faktor as Double = min( Height / Pic.Height, Width / Pic.Width)
// Calculate new size
dim w as integer = Pic.Width * faktor
dim h as integer = Pic.Height * faktor
// create new picture
dim NewPic as new Picture(w,h,32)
// draw picture in the new size
NewPic.Graphics.DrawPicture Pic, 0, 0, w, h, 0, 0, Pic.Width, Pic.Height
// return result
Return NewPic
End Function
Sub TestJPEGwithCoreImage(source as FolderItem, dest as FolderItem)
dim t as integer = ticks
dim c as integer = source.Count
dim filter as new CIFilterLanczosScaleTransformMBS
for i as integer = 1 to c
dim item as FolderItem = source.TrueItem(i)
if Right(item.name,4) = ".jpg" then
dim image as new CIImageMBS(item)
dim w as integer = Image.Extent.Width
dim h as integer = image.Extent.Height
dim l as integer = max(w,h)
filter.inputImage = image
filter.inputAspectRatio = 1.0
filter.inputScale = 100.0/l
dim out as FolderItem = dest.Child(CurrentMethodName+" "+item.name)
dim img as CIImageMBS = filter.outputImage
dim nimg as NSImageMBS = img.RenderNSImage(false)
dim data as string = nimg.JPEGRepresentationWithCompressionFactor(0.8)
dim b as BinaryStream = BinaryStream.Create(out, true)
b.Write data
b.Close
end if
next
t = ticks -t
list.AddRow CurrentMethodName, str(t)
End Sub
Sub TestJPEGwithFasterMBS(source as FolderItem, dest as FolderItem)
dim t as integer = ticks
dim c as integer = source.Count
for i as integer = 1 to c
dim item as FolderItem = source.TrueItem(i)
if Right(item.name,4) = ".jpg" then
dim ji as new JPEGImporterMBS
ji.File = item
if ji.ReadHeader then
dim w as integer = ji.Width
dim h as integer = ji.Height
dim l as integer = max(w,h)
dim d as integer = l/100.0
if d>8 then
ji.ScaleFactor = 8
elseif d>4 then
ji.ScaleFactor = 4
elseif d>2 then
ji.ScaleFactor = 2
end if
ji.Import
dim p as Picture = ji.Picture
p = ProportinalScaled(p, 100, 100)
dim out as FolderItem = dest.Child(CurrentMethodName+" "+item.name)
if p<>Nil then
p.Save(out, p.SaveAsJPEG)
end if
end if
end if
next
t = ticks -t
list.AddRow CurrentMethodName, str(t)
End Sub
Sub TestJPEGwithMBS(source as FolderItem, dest as FolderItem)
dim t as integer = ticks
dim c as integer = source.Count
for i as integer = 1 to c
dim item as FolderItem = source.TrueItem(i)
if Right(item.name,4) = ".jpg" then
dim p as Picture = item.OpenAsJPEGMBS
p = ProportinalScaled(p, 100, 100)
dim out as FolderItem = dest.Child(CurrentMethodName+" "+item.name)
if p<>Nil then
p.Save(out, p.SaveAsJPEG)
end if
end if
next
t = ticks -t
list.AddRow CurrentMethodName, str(t)
End Sub
Sub TestJPEGwithRB(source as FolderItem, dest as FolderItem)
dim t as integer = ticks
dim c as integer = source.Count
for i as integer = 1 to c
dim item as FolderItem = source.TrueItem(i)
if Right(item.name,4) = ".jpg" then
dim p as Picture = Picture.Open(item)
p = ProportinalScaled(p, 100, 100)
dim out as FolderItem = dest.Child(CurrentMethodName+" "+item.name)
if p<>Nil then
p.Save(out, p.SaveAsJPEG)
end if
end if
next
t = ticks -t
list.AddRow CurrentMethodName, str(t)
End Sub
Sub TestTIFFwithCoreImage(source as FolderItem, dest as FolderItem)
dim t as integer = ticks
dim filter as new CIFilterLanczosScaleTransformMBS
dim c as integer = source.Count
for i as integer = 1 to c
dim item as FolderItem = source.TrueItem(i)
System.DebugLog item.name
if Right(item.name,4) = ".tif" or Right(item.name,5) = ".tiff" then
dim image as new CIImageMBS(item)
dim w as integer = Image.Extent.Width
dim h as integer = image.Extent.Height
dim l as integer = max(w,h)
filter.inputImage = image
filter.inputAspectRatio = 1.0
filter.inputScale = 100.0/l
dim out as FolderItem = dest.Child(CurrentMethodName+" "+item.name)
dim img as CIImageMBS = filter.outputImage
dim nimg as NSImageMBS = img.RenderNSImage(false)
dim data as string = nimg.JPEGRepresentationWithCompressionFactor(0.8)
dim b as BinaryStream = BinaryStream.Create(out, true)
b.Write data
b.Close
end if
next
t = ticks -t
list.AddRow CurrentMethodName, str(t)
End Sub
Sub TestTIFFwithFasterMBS(source as FolderItem, dest as FolderItem)
dim t as integer = ticks
dim c as integer = source.Count
for i as integer = 1 to c
dim item as FolderItem = source.TrueItem(i)
System.DebugLog item.name
if Right(item.name,4) = ".tif" or Right(item.name,5) = ".tiff" then
dim tt as TiffPictureMBS = item.OpenAsTiffMBS(true)
if tt<>nil then
dim w as integer = tt.Width
dim h as integer = tt.Height
dim l as integer = max(w,h)
dim d as integer = l/100.0
dim p as Picture
if d>1 then
call tt.ReadPreviewRGB(d)
else
call tt.ReadRGB
end if
p = tt.CombinePictureWithMask
if p<>Nil then
p = ProportinalScaled(p, 100, 100)
dim n as string = item.Name
n = ReplaceAll(n, ".tiff", "")
n = ReplaceAll(n, ".tif", "")
dim out as FolderItem = dest.Child(CurrentMethodName+" "+n+".jpg")
if p<>Nil then
p.Save(out, p.SaveAsJPEG)
end if
end if
else
System.DebugLog "Failed to read "+item.Name
end if
end if
next
t = ticks -t
list.AddRow CurrentMethodName, str(t)
End Sub
Sub TestTIFFwithMBS(source as FolderItem, dest as FolderItem)
dim t as integer = ticks
dim c as integer = source.Count
for i as integer = 1 to c
dim item as FolderItem = source.TrueItem(i)
System.DebugLog item.name
if Right(item.name,4) = ".tif" or Right(item.name,5) = ".tiff" then
dim tt as TiffPictureMBS = item.OpenAsTiffMBS
dim p as Picture = tt.CombinePictureWithMask
if p<>Nil then
p = ProportinalScaled(p, 100, 100)
dim n as string = item.Name
n = ReplaceAll(n, ".tiff", "")
n = ReplaceAll(n, ".tif", "")
dim out as FolderItem = dest.Child(CurrentMethodName+" "+n+".jpg")
if p<>Nil then
p.Save(out, p.SaveAsJPEG)
end if
else
System.DebugLog "Failed to read "+item.Name
end if
end if
next
t = ticks -t
list.AddRow CurrentMethodName, str(t)
End Sub
Sub TestTIFFwithRB(source as FolderItem, dest as FolderItem)
dim t as integer = ticks
dim c as integer = source.Count
for i as integer = 1 to c
dim item as FolderItem = source.TrueItem(i)
System.DebugLog item.name
if Right(item.name,4) = ".tif" or Right(item.name,5) = ".tiff" then
dim p as Picture = Picture.Open(item)
p = ProportinalScaled(p, 100, 100)
dim n as string = item.Name
n = ReplaceAll(n, ".tiff", "")
n = ReplaceAll(n, ".tif", "")
dim out as FolderItem = dest.Child(CurrentMethodName+" "+n+".jpg")
if p<>Nil then
p.Save(out, p.SaveAsJPEG)
end if
end if
next
t = ticks -t
list.AddRow CurrentMethodName, str(t)
End Sub
End Class
MenuBar MenuBar1
MenuItem FileMenu = "&File"
MenuItem FileQuit = "#App.kFileQuit"
MenuItem EditMenu = "&Edit"
MenuItem EditUndo = "&Undo"
MenuItem UntitledMenu1 = "-"
MenuItem EditCut = "Cu&t"
MenuItem EditCopy = "&Copy"
MenuItem EditPaste = "&Paste"
MenuItem EditClear = "#App.kEditClear"
MenuItem UntitledMenu0 = "-"
MenuItem EditSelectAll = "Select &All"
End MenuBar
End Project
The items on this page are in the following plugins: MBS MacCG Plugin.