Platforms to show: All Mac Windows Linux Cross-Platform
/Images/JPEG/JPEG with LCMS/JPEG with LCMS
Required plugins for this example: MBS MacBase Plugin, MBS Images Plugin, MBS MacCG Plugin, MBS MacCF Plugin, MBS Main Plugin
You find this example project in your Plugins Download as a Xojo project file within the examples folder: /Images/JPEG/JPEG with LCMS/JPEG with LCMS
This example is the version from Tue, 21th Jan 2019.
Project "JPEG with LCMS.xojo_binary_project"
Class App Inherits Application
Const kEditClear = "&Löschen"
Const kFileQuit = "Beenden"
Const kFileQuitShortcut = ""
End Class
Class Window1 Inherits Window
Control canGrayGeneric Inherits Canvas
ControlInstance canGrayGeneric Inherits Canvas
End Control
Control canRgbGeneric Inherits Canvas
ControlInstance canRgbGeneric Inherits Canvas
End Control
Control canCmykGeneric Inherits Canvas
ControlInstance canCmykGeneric Inherits Canvas
End Control
Control Label1 Inherits Label
ControlInstance Label1 Inherits Label
End Control
Control Label2 Inherits Label
ControlInstance Label2 Inherits Label
End Control
Control Label3 Inherits Label
ControlInstance Label3 Inherits Label
End Control
Control canGrayCMS Inherits Canvas
ControlInstance canGrayCMS Inherits Canvas
End Control
Control canRgbCMS Inherits Canvas
ControlInstance canRgbCMS Inherits Canvas
End Control
Control canCmykCMS Inherits Canvas
ControlInstance canCmykCMS Inherits Canvas
End Control
Control Label4 Inherits Label
ControlInstance Label4 Inherits Label
End Control
Control lbProfileGray Inherits Label
ControlInstance lbProfileGray Inherits Label
End Control
Control lbProfileRGB Inherits Label
ControlInstance lbProfileRGB Inherits Label
End Control
Control lbProfileCMYK Inherits Label
ControlInstance lbProfileCMYK Inherits Label
End Control
Control Label5 Inherits Label
ControlInstance Label5 Inherits Label
End Control
Control canGrayPO Inherits Canvas
ControlInstance canGrayPO Inherits Canvas
End Control
Control canRgbPO Inherits Canvas
ControlInstance canRgbPO Inherits Canvas
End Control
Control canCmykPO Inherits Canvas
ControlInstance canCmykPO Inherits Canvas
End Control
Control Label6 Inherits Label
ControlInstance Label6 Inherits Label
End Control
EventHandler Sub Close()
quit
End EventHandler
EventHandler Sub Open()
LoadCMYK
LoadRGB
LoadGray
End EventHandler
Function FindFile(name as string) As FolderItem
// Look for file in parent folders from executable on
dim parent as FolderItem = app.ExecutableFile.Parent
while parent<>Nil
dim file as FolderItem = parent.Child(name)
if file<>Nil and file.Exists then
Return file
end if
parent = parent.Parent
wend
End Function
Sub LoadCMYK()
dim file as FolderItem = FindFile("cmyk.jpg")
canCmykPO.Backdrop = Picture.Open(file)
// 1. simply code
dim jpeg as new JPEGImporterMBS
jpeg.File = file
jpeg.Import
canCmykGeneric.Backdrop = jpeg.Picture
// 2. better code
jpeg = new JPEGImporterMBS
jpeg.Mode = jpeg.ModeRaw
jpeg.ReadProfileData = true
jpeg.File = file
jpeg.Import
dim JPEGProfile as LCMS2ProfileMBS
dim profile as string = jpeg.ProfileData
JPEGProfile = LCMS2ProfileMBS.OpenProfileFromString(profile)
if JPEGProfile = nil then
// default profile
dim f as FolderItem = FindFile("Generic CMYK Profile.icc")
JPEGProfile = LCMS2ProfileMBS.OpenProfileFromFile(f)
else
lbProfileCMYK.Text = JPEGProfile.Name
end if
dim ScreenProfile as LCMS2ProfileMBS
#if TargetCocoa then
// for cocoa, we need to match to the picture color space
dim dummy as new Picture(10,10,32)
dim c as CGColorSpaceMBS = dummy.CGColorSpaceMBS
dim data as string = c.ICCProfile
ScreenProfile = LCMS2ProfileMBS.OpenProfileFromString(data)
#endif
if ScreenProfile = nil then
dim ScreenProfileFile as FolderItem = getScreenProfile
ScreenProfile = LCMS2ProfileMBS.OpenProfileFromFile(ScreenProfileFile)
if ScreenProfile = nil then
// default profile
ScreenProfile = LCMS2ProfileMBS.CreateSRGBProfile
end if
end if
dim InputFormat as integer = JPEGProfile.FormatterForBitmap
dim InputChannels as integer = LCMS2MBS.T_CHANNELS(InputFormat)
dim InputBytesPerSample as integer = LCMS2MBS.T_BYTES(InputFormat)
dim OutputFormat as integer = ScreenProfile.FormatterForBitmap
dim OutputChannels as integer = LCMS2MBS.T_CHANNELS(OutputFormat)
dim OutputBytesPerSample as integer = LCMS2MBS.T_BYTES(OutputFormat)
// now create a transform
dim Transform as LCMS2TransformMBS = LCMS2TransformMBS.CreateTransform(JPEGProfile, InputFormat, ScreenProfile, OutputFormat, LCMS2MBS.kINTENT_RELATIVE_COLORIMETRIC, LCMS2MBS.kcmsFLAGS_BLACKPOINTCOMPENSATION)
// Target picture
dim pic as new Picture(jpeg.Width, jpeg.Height, 32)
// and bitmaps for it
dim inBitmap as new LCMS2BitmapMBS
dim outBitmap as new LCMS2BitmapMBS(pic,8)
inBitmap.data = jpeg.PictureData
inBitmap.Height = jpeg.Height
inBitmap.Width = jpeg.Width
inBitmap.RowBytes = jpeg.width * 4
inBitmap.Bits = 8
inBitmap.Invert
// and run it
if Transform.Transform(inBitmap, outBitmap) then
canCmykCMS.Backdrop = outBitmap.Picture
end if
End Sub
Sub LoadGray()
dim file as FolderItem = FindFile("gray.jpg")
canGrayPO.Backdrop = Picture.Open(file)
// 1. simply code
dim jpeg as new JPEGImporterMBS
jpeg.File = file
jpeg.Import
canGrayGeneric.Backdrop = jpeg.Picture
// 2. better code
jpeg = new JPEGImporterMBS
jpeg.Mode = jpeg.ModeRaw
jpeg.ReadProfileData = true
jpeg.File = file
jpeg.Import
dim JPEGProfile as LCMS2ProfileMBS
dim profile as string = jpeg.ProfileData
JPEGProfile = LCMS2ProfileMBS.OpenProfileFromString(profile)
if JPEGProfile = nil then
// default profile
dim f as FolderItem = FindFile("Generic Gray Profile.icc")
JPEGProfile = LCMS2ProfileMBS.OpenProfileFromFile(f)
else
lbProfileGray.Text = JPEGProfile.Name
end if
dim ScreenProfile as LCMS2ProfileMBS
#if TargetCocoa then
// for cocoa, we need to match to the picture color space
dim dummy as new Picture(10,10,32)
dim c as CGColorSpaceMBS = dummy.CGColorSpaceMBS
dim data as string = c.ICCProfile
ScreenProfile = LCMS2ProfileMBS.OpenProfileFromString(data)
#endif
if ScreenProfile = nil then
dim ScreenProfileFile as FolderItem = getScreenProfile
ScreenProfile = LCMS2ProfileMBS.OpenProfileFromFile(ScreenProfileFile)
if ScreenProfile = nil then
// default profile
ScreenProfile = LCMS2ProfileMBS.CreateSRGBProfile
end if
end if
dim InputFormat as integer = JPEGProfile.FormatterForBitmap
dim OutputFormat as integer = ScreenProfile.FormatterForBitmap
// just looking on what's inside
dim InputChannels as integer = LCMS2MBS.T_CHANNELS(InputFormat)
dim OutputChannels as integer = LCMS2MBS.T_CHANNELS(OutputFormat)
dim InputBytesPerSample as integer = LCMS2MBS.T_BYTES(InputFormat)
dim OutputBytesPerSample as integer = LCMS2MBS.T_BYTES(OutputFormat)
dim t as LCMS2TransformMBS = LCMS2TransformMBS.CreateTransform(JPEGProfile, InputFormat, ScreenProfile, OutputFormat, LCMS2MBS.kINTENT_RELATIVE_COLORIMETRIC, LCMS2MBS.kcmsFLAGS_BLACKPOINTCOMPENSATION)
dim pic as new Picture(jpeg.Width, jpeg.Height, 32)
dim Bitmap1 as new LCMS2BitmapMBS(pic,8)
dim Bitmap2 as new LCMS2BitmapMBS
Bitmap2.data = jpeg.PictureData
Bitmap2.Height = jpeg.Height
Bitmap2.Width = jpeg.Width
Bitmap2.RowBytes = jpeg.width
Bitmap2.Bits = 8
if t.Transform(Bitmap2, Bitmap1) then
canGrayCMS.Backdrop = Bitmap1.Picture
end if
End Sub
Sub LoadRGB()
dim file as FolderItem = FindFile("rgb.jpg")
canRGBPO.Backdrop = Picture.Open(file)
// 1. simply code
dim jpeg as new JPEGImporterMBS
jpeg.File = file
jpeg.Import
canRgbGeneric.Backdrop = jpeg.Picture
// 2. better code
jpeg = new JPEGImporterMBS
jpeg.Mode = jpeg.ModeRaw
jpeg.File = file
jpeg.ReadProfileData = true
jpeg.Import
dim JPEGProfile as LCMS2ProfileMBS
dim profile as string = jpeg.ProfileData
JPEGProfile = LCMS2ProfileMBS.OpenProfileFromString(profile)
if JPEGProfile = nil then
// default profile
JPEGProfile = LCMS2ProfileMBS.CreateSRGBProfile
else
lbProfileRGB.Text = JPEGProfile.Name
end if
dim ScreenProfile as LCMS2ProfileMBS
#if TargetCocoa then
// for cocoa, we need to match to the picture color space
dim dummy as new Picture(10,10,32)
dim c as CGColorSpaceMBS = dummy.CGColorSpaceMBS
dim data as string = c.ICCProfile
ScreenProfile = LCMS2ProfileMBS.OpenProfileFromString(data)
#endif
if ScreenProfile = nil then
dim ScreenProfileFile as FolderItem = getScreenProfile
ScreenProfile = LCMS2ProfileMBS.OpenProfileFromFile(ScreenProfileFile)
if ScreenProfile = nil then
// default profile
ScreenProfile = LCMS2ProfileMBS.CreateSRGBProfile
end if
end if
dim InputFormat as integer = JPEGProfile.FormatterForBitmap
dim OutputFormat as integer = ScreenProfile.FormatterForBitmap
// just looking on what's inside
dim InputChannels as integer = LCMS2MBS.T_CHANNELS(InputFormat)
dim OutputChannels as integer = LCMS2MBS.T_CHANNELS(OutputFormat)
dim InputBytesPerSample as integer = LCMS2MBS.T_BYTES(InputFormat)
dim OutputBytesPerSample as integer = LCMS2MBS.T_BYTES(OutputFormat)
dim t as LCMS2TransformMBS = LCMS2TransformMBS.CreateTransform(JPEGProfile, InputFormat, ScreenProfile, OutputFormat, LCMS2MBS.kINTENT_RELATIVE_COLORIMETRIC, LCMS2MBS.kcmsFLAGS_BLACKPOINTCOMPENSATION)
dim pic as new Picture(jpeg.Width, jpeg.Height, 32)
dim Bitmap1 as new LCMS2BitmapMBS(pic,8)
dim Bitmap2 as new LCMS2BitmapMBS
Bitmap2.data = jpeg.PictureData
Bitmap2.Height = jpeg.Height
Bitmap2.Width = jpeg.Width
Bitmap2.RowBytes = jpeg.width * 3
Bitmap2.Bits = 8
if t.Transform(Bitmap2, Bitmap1) then
canRgbCMS.Backdrop = Bitmap1.Picture
end if
End Sub
Function getScreenProfile() As FolderItem
#if TargetMacOS then
dim screen as NSScreenMBS = NSScreenMBS.mainScreen
dim colorspace as NSColorSpaceMBS = screen.colorSpace
return colorspace.file
#elseif TargetWin32 then
dim f as FolderItem = GetWindowsColorProfileMBS
Return f
#else
// todo
#endif
End Function
End Class
MenuBar MenuBar1
MenuItem FileMenu = "&Ablage"
MenuItem FileQuit = "#App.kFileQuit"
MenuItem EditMenu = "&Bearbeiten"
MenuItem EditUndo = "&Rückgängig"
MenuItem UntitledMenu1 = "-"
MenuItem EditCut = "&Ausschneiden"
MenuItem EditCopy = "&Kopieren"
MenuItem EditPaste = "&Einfügen"
MenuItem EditClear = "#App.kEditClear"
MenuItem UntitledMenu0 = "-"
MenuItem EditSelectAll = "&Alles auswählen"
End MenuBar
End Project
See also:
The items on this page are in the following plugins: MBS Images Plugin.