Attachment 'netFreeImage.v1.0.txt'

Download

   1 :Class netFreeImage
   2 
   3 ⍝ Utility class for bitmap resizing, cropping, compression and format change using .Net.
   4 ⍝ It is using an open source project called FreeImage created by Floris van den Berg originally written in C++.
   5 ⍝ The project is maintained today by Hervé Drolon. The .Net wrapper is the work of Jean-Philippe Goerke and Carsten Klein.
   6 
   7 ⍝ FreeImage supports:
   8 ⍝  Loading and saving of as many bitmap types as possible
   9 ⍝  Easy access to bitmap components, such as palettes, data bits and metadata
  10 ⍝  Converting bitmap’s bit depths from one to another
  11 ⍝  Accessing pages in a bitmap when there are multiple, such as in TIFF
  12 ⍝  Basic manipulation of bitmaps, such as rotation, flipping and resampling or point
  13 ⍝   operations such as brightness and contrast adjustment
  14 ⍝  Alpha compositing and alpha blending
  15 
  16 ⍝ FreeImage does not support:
  17 ⍝  Advanced image processing operations such as convolution and transforms
  18 ⍝  Bitmap drawing
  19 ⍝  Vector graphics
  20 
  21 ⍝ For this class to work you need to download the original dll in C++ (FreeImage.dll) and the .Net wrapper (FreeImageNET.dll)
  22 ⍝ and copy them both in the same directory as the dyalog.exe program. For commodity the version 3.17.04 can be downloaded from the Wiki page.
  23 
  24 ⍝ Usefull links:
  25 ⍝ http://freeimage.sourceforge.net/index.html                  ⍝ Original project location
  26 ⍝ http://downloads.sourceforge.net/freeimage/FreeImage3170.pdf ⍝ Original project documentation
  27 ⍝ https://sourceforge.net/p/freeimage/discussion/              ⍝ Forum of original project
  28 
  29 ⍝ https://freeimagenet.codeplex.com                            ⍝ FreeImage.NET wrapper project page
  30 ⍝ https://freeimagenet.codeplex.com/releases                   ⍝ Page to Download .NET wrapper
  31 
  32 ⍝ http://paulbourke.net/dataformats/bitmaps/                   ⍝ A Beginners Guide to Bitmaps
  33 
  34 
  35 ⍝ Original location of the Dyalog APL class: http://www.AplWiki/netFreeImage
  36 
  37 ⍝ Methods in FreeImage:
  38 
  39 ⍝ Section LoadAndSave ***************************************************************************************************
  40 
  41 ⍝ LoadFromFile                    ⍝ Get a FreeImageBitmap object from a file on disk.
  42 ⍝ LoadFromBase64                  ⍝ Get a FreeImageBitmap object from a Base64 string.
  43 ⍝ LoadFromMemoryStream            ⍝ Get a FreeImageBitmap object from a MemoryStream.
  44 ⍝ LoadFromBitmap                  ⍝ Get a FreeImageBitmap object from a System.Drawing.Bitmap (WindowsForms).
  45 
  46 ⍝ SaveToFile                      ⍝ Save the loaded bitmap to a file on disk with default values (format conversion allowed).
  47 ⍝ SaveToBase64                    ⍝ Save the loaded bitmap to a Base64 character vector with the current bitmap format.
  48 ⍝ SaveToMemoryStream              ⍝ Save the loaded bitmap to a MemoryStream object with the specified bitmap format.
  49 ⍝ SaveToBitmap                    ⍝ Save the loaded bitmap to a System.Drawing.Bitmap (WindowsForms).
  50 ⍝ SaveToHtml                      ⍝ Save the loaded bitmap to an HTML inline image with the current bitmap format.
  51 
  52 ⍝ Dispose                         ⍝ Release all the resources.
  53 
  54 ⍝ Section BitmapOperation ***********************************************************************************************
  55 
  56 ⍝ AdjustBrightness                ⍝ Adjusts the brightness of a 8-, 24- or 32-bit image by a certain amount.
  57 ⍝ AdjustColors                    ⍝ Adjusts an image's brightness, contrast, gamma and invert image in a single operation.
  58 ⍝ AdjustContrast                  ⍝ Adjusts the contrast of a 8-, 24- or 32-bit image by a certain amount.
  59 ⍝ AdjustGamma                     ⍝ Performs gamma correction on a 8-, 24- or 32-bit image.
  60 ⍝ ConvertFormat                   ⍝ Convert the format of the loaded bitmap to another bitmap format.
  61 ⍝ ConvertTo4Bits                  ⍝ Converts the loaded bitmap to 4 bits per pixel using the Xiaolin Wu color quantization algorithm
  62 ⍝ ConvertTo8Bits                  ⍝ Converts the loaded bitmap to 8 bits per pixel using the Xiaolin Wu color quantization algorithm
  63 ⍝ ConvertTo16Bits                 ⍝ Converts the loaded bitmap to 16 bits per pixel (RGB565).
  64 ⍝ ConvertTo24Bits                 ⍝ Converts the loaded bitmap to 24 bits per pixel.
  65 ⍝ ConvertTo32Bits                 ⍝ Converts the loaded bitmap to 32 bits per pixel.
  66 ⍝ ConvertToGreyscale              ⍝ Converts the loaded bitmap to 256, 16 or 1 tone of grey.
  67 ⍝ ConvertToThumbnail              ⍝ Converts the loaded bitmap to a thumbnail while keeping aspect ratio.
  68 ⍝ Crop                            ⍝ Crop the loaded bitmap.
  69 ⍝ FlipHorizontal                  ⍝ Flip horizontally the loaded bitmap.
  70 ⍝ FlipVertical                    ⍝ Flip vertically the loaded bitmap.
  71 ⍝ Invert                          ⍝ Invert the loaded bitmap.
  72 ⍝ Resize                          ⍝ Resize the loaded bitmap.
  73 ⍝ Rotate                          ⍝ Rotate the loaded bitmap.
  74 ⍝ JPEGCrop                        ⍝ Performs a lossless crop on a JPEG file.
  75 ⍝ JPEGTransform                   ⍝ Performs a lossless rotation or flipping on a JPEG file.
  76 
  77 ⍝ Section PixelAccess ***************************************************************************************************
  78 
  79 ⍝ GetPalette                      ⍝ Get the colors of the palette of loaded bitmap if existing.
  80 ⍝ GetPixelsColors                 ⍝ Get the RGB colors for all pixels of loaded bitmap.
  81 ⍝ GetSinglePixelColor             ⍝ Get the RGB value of a single pixel of loaded bitmap.
  82 
  83 ⍝ Section Information ***************************************************************************************************
  84 
  85 ⍝ GetMetaData                     ⍝ Get a Key/Value pair for each MetaData of the loaded bitmap.
  86 ⍝ Info                            ⍝ Show information about the loaded bitmap.
  87 ⍝ SetComment                      ⍝ Sets the comment of the loaded bitmap. Supported formats are JPEG, PNG and GIF.
  88 ⍝ Show                            ⍝ Show the loaded bitmap in a WPF window.
  89 ⍝ ShowInWebBrowser                ⍝ Show the loaded bitmap in WindowForm browser using 'SaveToHtml'.
  90 ⍝ Height                          ⍝ Height of the loaded bitmap in pixel.
  91 ⍝ Width                           ⍝ Width of the loaded bitmap in pixel.
  92 
  93 ⍝ ***********************************************************************************************************************
  94 
  95 ⍝ Version 1.0 April 2016 by Pierre Gilbert
  96 
  97   ⍝ System variables used:
  98     (⎕IO ⎕ML ⎕WX)←1 3 3
  99 
 100   ⍝ Microsoft .NET Search Path:
 101     ⎕USING ← 'FreeImageAPI,FreeImageNET.dll' 'System.IO,mscorlib.dll' 'System,mscorlib.dll' 'System.Drawing,System.Drawing.dll'
 102     ⎕USING,← 'System.Windows.Media,WPF/PresentationCore.dll' 'System.Windows.Media.Imaging,WPF/PresentationCore.dll'
 103     ⎕USING,←⊂'System.Windows,WPF/PresentationFramework.dll'
 104 
 105   ⍝ Global variables:
 106     :Field Public FIB ← ⎕NULL           ⍝ FreeImageBitmap object
 107     :Field Public MS  ← ⎕NULL           ⍝ MemoryStream object
 108     :Field Public LastFileName ← ⎕NULL  ⍝ Last file name
 109 
 110    ⍝ Constructor:
 111     ∇ Init0
 112       :Access Public
 113       :Implements Constructor
 114 
 115 
 116     :Section LoadAndSave  ⍝ *************************************************************************
 117 
 118     ∇ r←{options}LoadFromFile fileName;format
 119       :Access Public
 120     ⍝ Get a FreeImageBitmap object from a file on disk.
 121     ⍝ Tries to find the format of the file automatically.
 122     ⍝ The image is stored in memory outside the workspace.
 123     ⍝ Use the method 'SaveToFile' to save back to a file.
 124 
 125     ⍝ Format supported:
 126     ⍝ Windows or OS/2 Bitmap File (*.BMP)
 127     ⍝ Windows Icon (*.ICO)
 128     ⍝ Independent JPEG Group (*.JPG, *.JIF, *.JPEG, *.JPE)
 129     ⍝ JPEG Network Graphics (*.JNG)
 130     ⍝ Commodore 64 Koala format (*.KOA)
 131     ⍝ Amiga IFF (*.IFF, *.LBM)
 132     ⍝ Amiga IFF (*.IFF, *.LBM)
 133     ⍝ Multiple Network Graphics (*.MNG)
 134     ⍝ Portable Bitmap (ASCII) (*.PBM)
 135     ⍝ Portable Bitmap (BINARY) (*.PBM)
 136     ⍝ Kodak PhotoCD (*.PCD)
 137     ⍝ Zsoft Paintbrush PCX bitmap format (*.PCX)
 138     ⍝ Portable Graymap (ASCII) (*.PGM)
 139     ⍝ Portable Graymap (BINARY) (*.PGM)
 140     ⍝ Portable Network Graphics (*.PNG)
 141     ⍝ Portable Pixelmap (ASCII) (*.PPM)
 142     ⍝ Portable Pixelmap (BINARY) (*.PPM)
 143     ⍝ Sun Rasterfile (*.RAS)
 144     ⍝ Truevision Targa files (*.TGA, *.TARGA)
 145     ⍝ Tagged Image File Format (*.TIF, *.TIFF)
 146     ⍝ Wireless Bitmap (*.WBMP)
 147     ⍝ Adobe Photoshop (*.PSD)
 148     ⍝ Dr. Halo (*.CUT)
 149     ⍝ X11 Bitmap Format (*.XBM)
 150     ⍝ X11 Pixmap Format (*.XPM)
 151     ⍝ DirectDraw Surface (*.DDS)
 152     ⍝ Graphics Interchange Format (*.GIF)
 153     ⍝ High Dynamic Range (*.HDR)
 154     ⍝ Raw Fax format CCITT G3 (*.G3)
 155     ⍝ Silicon Graphics SGI image format (*.SGI)
 156     ⍝ OpenEXR format (*.EXR)
 157     ⍝ JPEG-2000 format (*.J2K, *.J2C)
 158     ⍝ JPEG-2000 format (*.JP2)
 159     ⍝ Portable FloatMap (*.PFM)
 160     ⍝ Macintosh PICT (*.PICT)
 161     ⍝ RAW camera image (*.*)
 162     ⍝ Google WebP image format (*.webp)
 163     ⍝ JPEG XR image format (*.jxr)
 164 
 165     ⍝ options available when loading Graphics Interchange Format (*.GIF)
 166     ⍝ options = 1 load the image as a 256 color image with unused palette entries, if it's 16 or 2 color
 167     ⍝         = 2 'Play' the GIF to generate each frame (as 32 bpp) instead of returning raw frame data when loading
 168 
 169     ⍝ options available when loading Windows Icon (*.ICO)
 170     ⍝ options = 1 Convert to 32bpp and create an alpha channel from the AND-mask when loading
 171 
 172     ⍝ options available when loading Independent JPEG Group (*.JPG, *.JIF, *.JPEG, *.JPE)
 173     ⍝ options =  1 load the file as fast as possible, sacrificing some quality (default)
 174     ⍝         =  2 load the file with the best quality, sacrificing some speed
 175     ⍝         = +4 load separated CMYK 'as is' (combine with other flags)
 176     ⍝         =  8 load and rotate according to Exif 'Orientation' tag if available
 177 
 178     ⍝ options available when loading Kodak PhotoCD (*.PCD)
 179     ⍝ options = 1 load the bitmap sized 768 x 512
 180     ⍝         = 2 load the bitmap sized 384 x 256
 181     ⍝         = 3 load the bitmap sized 192 x 128
 182 
 183     ⍝ options available when loading Portable Network Graphics (*.PNG)
 184     ⍝ options = 1 avoid gammaa correction on loading
 185 
 186     ⍝ options available when loading Truevision Targa files (*.TGA, *.TARGA)
 187     ⍝ options = 1 converts RGB555 and ARGB8888 to RGB888
 188 
 189     ⍝ options available when loading Tagged Image File Format (*.TIF, *.TIFF)
 190     ⍝ options = 1 load CMYK bitmaps as separated CMYK (default is conversin to RGB)
 191 
 192     ⍝ options available when loading RAW camera image (*.*)
 193     ⍝ default is load the file as linear RGB 48-bit
 194     ⍝ options = 1 tries to load the JPEG preview image, embedded in Exif Metadata or
 195     ⍝             load the image as RGB 24-bit if no preview image is available
 196     ⍝         = 2 loads the image as RGB 24-bit
 197     ⍝         = 4 load as half-size color image
 198     ⍝         = 8 load as FIT_UINT16 raw Bayer image
 199 
 200     ⍝ fileName = fully qualified file name with extension.
 201 
 202     ⍝ r = 1 if successfull
 203     ⍝   = 0 'error message' if failure
 204      
 205       Dispose ⍝ Release any previous resources.
 206       LastFileName←fileName
 207      
 208       format←FREE_IMAGE_FORMAT.FIF_UNKNOWN
 209      
 210       :If 0=⎕NC'options'
 211           options←FREE_IMAGE_LOAD_FLAGS.DEFAULT
 212       :EndIf
 213      
 214       :Trap 0
 215           FIB←⎕NEW FreeImageBitmap(fileName format options)
 216           r←1
 217       :Else
 218           r←0 ⎕EXCEPTION.Message
 219       :EndTrap
 220 
 221 
 222     ∇ r←{options}SaveToFile fileName;format
 223       :Access Public
 224     ⍝ Save the loaded bitmap to a file on disk with default values or with options.
 225     ⍝ The bitmap can have a different extension then when it was loaded.
 226     ⍝ Checks whether the extension is valid for the file type and if the plugin can
 227     ⍝ use the colordepth of the bitmap. If not it will automatically convert
 228     ⍝ the bitmap into the next best colordepth and save it.
 229     ⍝ If the file already exists it is overwritten (except for .jpeg file ?).
 230     ⍝ See 'LoadFromFile' for the list of supported file format.
 231 
 232     ⍝ Default values when saving:
 233     ⍝ OpenEXR format (*.EXR) = Save data as half with piz-based wavelet compression
 234     ⍝ JPEG-2000 format (*.J2K, *.J2C) = Save with a 16:1 rate
 235     ⍝ JPEG-2000 format (*.JP2) = Save with a 16:1 rate
 236     ⍝ Independent JPEG Group (*.JPG, *.JIF, *.JPEG, *.JPE) = Saves with good quality (75:1) and medium 2x2 chroma subsampling (4:2:0)
 237     ⍝ JPEG XR image format (*.jxr) = Save with quality 80 and no chroma subsampling (4:4:4)
 238     ⍝ Portable Network Graphics (*.PNG) = Save with ZLib level 6 compression and no interlacing
 239     ⍝ Portable Bitmap (ASCII) (*.PBM) = Save the bitmap as a binary file
 240     ⍝ Portable Graymap (ASCII) (*.PGM) = Save the bitmap as a binary file
 241     ⍝ Portable Pixelmap (ASCII) (*.PPM) = Save the bitmap as a binary file
 242     ⍝ Tagged Image File Format (*.TIF, *.TIFF) = Save using CCITT Group 4 fax encoding for 1-bit bitmaps and LZW compression for any other bitmaps
 243     ⍝ Truevision Targa files (*.TGA, *.TARGA) = Save without compression
 244     ⍝ Google WebP image format (*.webp) = Save with good quality (75:1)
 245 
 246     ⍝ Options available when saving an Independent JPEG Group (*.JPG, *.JIF, *.JPEG, *.JPE) file:
 247     ⍝ options =   128 save with compression ratio 100:1
 248     ⍝         =   256 save with compression ratio  75:1 (default)
 249     ⍝         =   512 save with compression ratio  50:1
 250     ⍝         =  1024 save with compression ratio  25:1
 251     ⍝         =  2048 save with compression ratio  10:1
 252     ⍝         =  or 0 to 100 for specific compression ratio
 253     ⍝         =  4096 save with high 4x1 chroma subsampling (4:1:1)
 254     ⍝         = 16384 save with medium 2x2 medium chroma (4:2:0)
 255     ⍝         = 32768 save with low 2x1 chroma subsampling (4:2:2)
 256     ⍝         = 65536 save with no chroma subsampling (4:4:4)
 257     ⍝         = +   8192 save as progressive JPEG (to combine with other options)
 258     ⍝         = + 131072 compute optimal Huffman coding (to combine with other options)
 259     ⍝         = + 262144 remove all metadata (to combine with other options)
 260 
 261     ⍝ Suggested for web:  options ← 256+8192+131072+262144
 262 
 263     ⍝ Options available when saving a Tagged Image File Format (*.TIF, *.TIFF) file:
 264     ⍝ options =   + 1  Stores tags for separated CMYK (to combine with other options)
 265     ⍝         =   256  Save using PACKBITS compression.
 266     ⍝         =   512  Save using DEFLATE compression (a.k.a. ZLIB compression).
 267     ⍝         =  1024  Save using ADOBE DEFLATE compression.
 268     ⍝         =  2048  Save without any compression.
 269     ⍝         =  4096  Save using CCITT Group 3 fax encoding.
 270     ⍝         =  8192  Save using CCITT Group 4 fax encoding.
 271     ⍝         = 16384  Save using LZW compression (default).
 272     ⍝         = 32768  Save using JPEG compression.
 273 
 274     ⍝ Options available when saving a Portable Network Graphics (*.PNG) file:
 275     ⍝ options =     1 Save using ZLib level 1 compression flag
 276     ⍝         =     6 Save using ZLib level 6 compression flag (default)
 277     ⍝         =     9 Save using ZLib level 9 compression flag
 278     ⍝         =   256 Save without ZLib compression.
 279     ⍝         = + 512 Save using Adam7 interlacing (to combine with other options)
 280 
 281     ⍝ fileName = fully qualified file name with extension.
 282 
 283     ⍝ r = 1 if successfull
 284     ⍝   = 0 'error message' if failure
 285      
 286       :If ⎕NULL≡FIB
 287           r←0 'SaveToFile: no bitmap loaded in memory !'
 288           →0
 289       :EndIf
 290      
 291       format←FREE_IMAGE_FORMAT.FIF_UNKNOWN
 292      
 293       :If 0=⎕NC'options'
 294           options←FREE_IMAGE_SAVE_FLAGS.DEFAULT
 295       :EndIf
 296      
 297       :Trap 0
 298           FIB.Save(fileName format options)
 299           r←1
 300       :Else
 301           r←0 ⎕EXCEPTION.Message
 302       :EndTrap
 303 
 304 
 305     ∇ r←LoadFromMemoryStream ms
 306       :Access Public
 307     ⍝ Get a FreeImageBitmap object from a MemoryStream.
 308     ⍝ Finds the format of the MemoryStream automatically.
 309     ⍝ Note: You must keep the stream open for the lifetime of the FreeImageBitmap.
 310 
 311     ⍝ ms = MemoryStream
 312 
 313     ⍝ r = 1 if successfull
 314     ⍝   = 0 'error message' if failure
 315      
 316       Dispose ⍝ Release any previous resources.
 317       LastFileName←⎕NULL
 318      
 319     ⍝ To set the position to 0, otherwise it may not load.
 320       ms.Position←Convert.ToInt64 0
 321      
 322       :Trap 0
 323           FIB←⎕NEW FreeImageBitmap(ms)
 324           r←1
 325       :Else
 326           r←0 ⎕EXCEPTION.Message
 327       :EndTrap
 328 
 329 
 330     ∇ ms←{options}SaveToMemoryStream format
 331       :Access Public
 332     ⍝ Save the loaded bitmap to a Base64 character vector with the specified bitmap format.
 333 
 334     ⍝ Format supported:
 335     ⍝ ¯1 = Format of loaded bitmap
 336     ⍝  0 = Windows or OS/2 Bitmap File (*.BMP)
 337     ⍝  1 = Windows Icon (*.ICO)
 338     ⍝  2 = Independent JPEG Group (*.JPG, *.JIF, *.JPEG, *.JPE)
 339     ⍝  3 = JPEG Network Graphics (*.JNG)
 340     ⍝  4 = Commodore 64 Koala format (*.KOA)
 341     ⍝  5 = Amiga IFF (*.IFF, *.LBM)
 342     ⍝  5 = Amiga IFF (*.IFF, *.LBM)
 343     ⍝  6 = Multiple Network Graphics (*.MNG)
 344     ⍝  7 = Portable Bitmap (ASCII) (*.PBM)
 345     ⍝  8 = Portable Bitmap (BINARY) (*.PBM)
 346     ⍝  9 = Kodak PhotoCD (*.PCD)
 347     ⍝ 10 = Zsoft Paintbrush PCX bitmap format (*.PCX)
 348     ⍝ 11 = Portable Graymap (ASCII) (*.PGM)
 349     ⍝ 12 = Portable Graymap (BINARY) (*.PGM)
 350     ⍝ 13 = Portable Network Graphics (*.PNG)
 351     ⍝ 14 = Portable Pixelmap (ASCII) (*.PPM)
 352     ⍝ 15 = Portable Pixelmap (BINARY) (*.PPM)
 353     ⍝ 16 = Sun Rasterfile (*.RAS)
 354     ⍝ 17 = Truevision Targa files (*.TGA, *.TARGA)
 355     ⍝ 18 = Tagged Image File Format (*.TIF, *.TIFF)
 356     ⍝ 19 = Wireless Bitmap (*.WBMP)
 357     ⍝ 20 = Adobe Photoshop (*.PSD)
 358     ⍝ 21 = Dr. Halo (*.CUT)
 359     ⍝ 22 = X11 Bitmap Format (*.XBM)
 360     ⍝ 23 = X11 Pixmap Format (*.XPM)
 361     ⍝ 24 = DirectDraw Surface (*.DDS)
 362     ⍝ 25 = Graphics Interchange Format (*.GIF)
 363     ⍝ 26 = High Dynamic Range (*.HDR)
 364     ⍝ 27 = Raw Fax format CCITT G3 (*.G3)
 365     ⍝ 28 = Silicon Graphics SGI image format (*.SGI)
 366     ⍝ 29 = OpenEXR format (*.EXR)
 367     ⍝ 30 = JPEG-2000 format (*.J2K, *.J2C)
 368     ⍝ 31 = JPEG-2000 format (*.JP2)
 369     ⍝ 32 = Portable FloatMap (*.PFM)
 370     ⍝ 33 = Macintosh PICT (*.PICT)
 371     ⍝ 34 = RAW camera image (*.*)
 372     ⍝ 35 = Google WebP image format (*.webp)
 373     ⍝ 36 = JPEG XR image format (*.jxr)
 374 
 375     ⍝ Options available when saving an Independent JPEG Group (*.JPG, *.JIF, *.JPEG, *.JPE) file:
 376     ⍝ options =   128 save with compression ratio 100:1
 377     ⍝         =   256 save with compression ratio  75:1 (default)
 378     ⍝         =   512 save with compression ratio  50:1
 379     ⍝         =  1024 save with compression ratio  25:1
 380     ⍝         =  2048 save with compression ratio  10:1
 381     ⍝         =  or 0 to 100 for specific compression ratio
 382     ⍝         =  4096 save with high 4x1 chroma subsampling (4:1:1)
 383     ⍝         = 16384 save with medium 2x2 medium chroma (4:2:0)
 384     ⍝         = 32768 save with low 2x1 chroma subsampling (4:2:2)
 385     ⍝         = 65536 save with no chroma subsampling (4:4:4)
 386     ⍝         = +   8192 save as progressive JPEG (to combine with other options)
 387     ⍝         = + 131072 compute optimal Huffman coding (to combine with other options)
 388     ⍝         = + 262144 remove all metadata (to combine with other options)
 389 
 390     ⍝ Suggested for web:  options ← 256+8192+131072+262144
 391 
 392     ⍝ Options available when saving a Tagged Image File Format (*.TIF, *.TIFF) file:
 393     ⍝ options =   + 1  Stores tags for separated CMYK (to combine with other options)
 394     ⍝         =   256  Save using PACKBITS compression.
 395     ⍝         =   512  Save using DEFLATE compression (a.k.a. ZLIB compression).
 396     ⍝         =  1024  Save using ADOBE DEFLATE compression.
 397     ⍝         =  2048  Save without any compression.
 398     ⍝         =  4096  Save using CCITT Group 3 fax encoding.
 399     ⍝         =  8192  Save using CCITT Group 4 fax encoding.
 400     ⍝         = 16384  Save using LZW compression (default).
 401     ⍝         = 32768  Save using JPEG compression.
 402 
 403     ⍝ Options available when saving a Portable Network Graphics (*.PNG) file:
 404     ⍝ options =     1 Save using ZLib level 1 compression flag
 405     ⍝         =     6 Save using ZLib level 6 compression flag (default)
 406     ⍝         =     9 Save using ZLib level 9 compression flag
 407     ⍝         =   256 Save without ZLib compression.
 408     ⍝         = + 512 Save using Adam7 interlacing (to combine with other options)
 409 
 410     ⍝ You need to do the following to dispose of the MemoryStream after use:
 411     ⍝  ms.Close ⋄ ms.Dispose ⋄ ms←⎕NULL
 412 
 413     ⍝ format = Format to use to save the memory stream.
 414     ⍝ ms     = MemoryStream containing the bitmap if successfull
 415     ⍝        = ⎕NULL 'error message' if failure
 416      
 417       :If ⎕NULL≡FIB
 418           ms←⎕NULL'SaveToMemoryStream: no bitmap loaded in memory !'
 419           →0
 420       :EndIf
 421      
 422       :If format=¯1
 423           format←FIB.ImageFormat.value__
 424       :EndIf
 425      
 426       :If 0=⎕NC'options'
 427           options←FREE_IMAGE_SAVE_FLAGS.DEFAULT
 428       :EndIf
 429      
 430       :Trap 0
 431         ⍝ Create a new empty MemoryStream
 432           ms←⎕NEW MemoryStream
 433      
 434         ⍝ Save to the MemoryStream
 435           FIB.Save(ms format options)
 436       :Else
 437           ms←⎕NULL ⎕EXCEPTION.Message
 438       :EndTrap
 439 
 440 
 441     ∇ r←LoadFromBase64 base64String;imageBytes;ms
 442       :Access Public
 443     ⍝ Get a FreeImageBitmap object from a Base64 string.
 444     ⍝ Finds the format of the file automatically.
 445     ⍝ The image is stored in memory outside the workspace.
 446 
 447     ⍝ base64String = Base64 character vector
 448 
 449     ⍝ r = 1 if successfull
 450     ⍝   = 0 'error message' if failure
 451      
 452       Dispose ⍝ Release any previous resources.
 453       LastFileName←⎕NULL
 454      
 455       :Trap 0
 456         ⍝ Convert Base64 String to byte[]
 457           imageBytes←Convert.FromBase64String(⊂base64String)
 458      
 459         ⍝ Create a new MemoryStream with the byte[]
 460           ms←⎕NEW MemoryStream(⊂imageBytes)
 461      
 462         ⍝ Create a FreeImageBitmap from the MemoryStream
 463           FIB←⎕NEW FreeImageBitmap(ms)
 464           r←1
 465       :Else
 466           r←0 ⎕EXCEPTION.Message
 467       :EndTrap
 468 
 469 
 470     ∇ base64String←SaveToBase64;ms
 471       :Access Public
 472     ⍝ Save the loaded bitmap to a Base64 character vector with the current bitmap format.
 473 
 474     ⍝ base64String = Base64 encoded bitmap as a character vector if successfull
 475     ⍝              = ⎕NULL 'error message' if failure
 476      
 477       :If ⎕NULL≡FIB
 478           base64String←⎕NULL'SaveToBase64: no bitmap loaded in memory !'
 479           →0
 480       :EndIf
 481      
 482       :If ⎕NULL≠1↑ms←SaveToMemoryStream ¯1  ⍝ ¯1 = same format as loaded bitmap
 483         ⍝ Convert the MemoryStream to a Base64String
 484           base64String←Convert.ToBase64String(⊂ms.ToArray)
 485           ms.Close ⋄ ms.Dispose ⋄ ms←⎕NULL
 486       :Else
 487           base64String←ms
 488       :End
 489      
 490 ⍝ The following function can be used to create a 'BitmapImage' from a base64 string.
 491      
 492 ⍝     bmpimg←ImageFromBase64String base64String;imageBytes;MS;⎕USING
 493 ⍝   ⍝ Converts a Base64String to a System.Windows.Media.Imaging.BitmapImage.
 494 
 495 ⍝    ⎕USING←'System.IO,mscorlib.dll' 'System,mscorlib.dll' 'System.Windows.Media.Imaging,WPF/PresentationCore.dll'
 496 
 497 ⍝  ⍝ Convert Base64 String to byte[] as numbers.
 498 ⍝    imageBytes←Convert.FromBase64String(⊂base64String)
 499 
 500 ⍝  ⍝ Create a new MemoryStream with the byte[]
 501 ⍝    MS←⎕NEW MemoryStream(⊂imageBytes)
 502 
 503 ⍝  ⍝ Create a BitmapImage Object from the Memory Stream
 504 ⍝    bmpimg←⎕NEW BitmapImage
 505 ⍝    bmpimg.BeginInit
 506 ⍝    bmpimg.CacheOption←bmpimg.CacheOption.OnLoad
 507 ⍝    bmpimg.StreamSource←MS
 508 ⍝    bmpimg.EndInit
 509 
 510 ⍝  ⍝ Erase the MemoryStream
 511 ⍝    MS.Close ⋄ MS.Dispose ⋄ MS←⎕NULL
 512      
 513 
 514 
 515     ∇ html←SaveToHtml;base64String;formatValue;imageHeight;imageWidth;ms
 516       :Access Public
 517     ⍝ Save the loaded bitmap to an HTML inline image with the current bitmap format.
 518     ⍝ Tries to find the most appropriate permitted format for HTML inline publishing.
 519     ⍝ The method .ShowInWebBrowser use this method to show the bitmap in a web browser.
 520 
 521     ⍝ html   = HTML inline image as character vector if successfull
 522     ⍝        = ⎕NULL 'error message' if failure
 523      
 524       :If ⎕NULL≡FIB
 525           html←⎕NULL'SaveToHtml: no bitmap loaded in memory !'
 526           →0
 527       :EndIf
 528      
 529       (imageHeight imageWidth)←⍕¨FIB.(Height Width)
 530      
 531       formatValue←FIB.ImageFormat.value__
 532      
 533     ⍝ Get the Base64String for a format supported by the html <img> tag
 534       :If ~formatValue∊2 3 30 31 36 25 13 ⍝ 'JPEG JNG J2K JP2 JXR' 'GIF' 'PNG'
 535           formatValue←0 ⍝ BMP
 536       :EndIf
 537      
 538       :If ⎕NULL≠1↑ms←SaveToMemoryStream formatValue
 539         ⍝ Convert the MemoryStream to a Base64String
 540           base64String←Convert.ToBase64String(⊂ms.ToArray)
 541           ms.Close ⋄ ms.Dispose ⋄ ms←⎕NULL
 542       :Else
 543           html←ms
 544           →0
 545       :End
 546      
 547     ⍝ Get the <img> tag with the inline base64 encoded image
 548       :Select formatValue
 549       :CaseList 2 3 30 31 36 ⍝ 'JPEG JNG J2K JP2 JXR'
 550           html←'<img height="',imageHeight,'" width="',imageWidth,'" src="data:image/jpg;base64,',base64String,'">'
 551      
 552       :Case 25 ⍝'GIF'
 553           html←'<img height="',imageHeight,'" width="',imageWidth,'" src="data:image/gif;base64,',base64String,'">'
 554      
 555       :Case 13 ⍝'PNG'
 556           html←'<img height="',imageHeight,'" width="',imageWidth,'" src="data:image/png;base64,',base64String,'">'
 557      
 558       :Else ⍝'BMP'
 559           html←'<img height="',imageHeight,'" width="',imageWidth,'" src="data:image/bmp;base64,',base64String,'">'
 560      
 561       :EndSelect
 562      
 563     ⍝ Add the <body> tag
 564       html←'<!DOCTYPE html><html><body>',html,'</body></html>'
 565 
 566 
 567     ∇ Dispose
 568       :Access Public
 569     ⍝ Release all the resources.
 570      
 571       :Trap 0
 572           FIB.Dispose ⋄ FIB←⎕NULL
 573       :EndTrap
 574      
 575       :Trap 0
 576           MS.Close ⋄ MS.Dispose ⋄ MS←⎕NULL
 577       :EndTrap
 578 
 579 
 580     :EndSection
 581 
 582 
 583     :Section Information  ⍝ *************************************************************************
 584 
 585     ∇ Info;mData
 586       :Access Public
 587     ⍝ Show information about the loaded bitmap.
 588       :If ⎕NULL≢LastFileName
 589           'File name: ',LastFileName
 590       :EndIf
 591       'Size in memory (bytes): ',⍕FIB.DataSize
 592       'Width (pixel): ',⍕FIB.Width
 593       'Height (pixel): ',⍕FIB.Height
 594       'Color depth (bits): ',⍕FIB.ColorDepth
 595       'Image Type: ',⍕FIB.ImageType
 596       'Image Format: ',⍕FIB.ImageFormat
 597       'Frame count: ',⍕FIB.FrameCount
 598       'Pixel Format: ',⍕FIB.PixelFormat
 599       'X-axis dpi: ',⍕FIB.HorizontalResolution
 600       'Y-axis dpi: ',⍕FIB.VerticalResolution
 601      
 602       :Trap 0
 603           'Number of unique colors: ',⍕FIB.UniqueColors
 604       :Else
 605           'Number of unique colors: Unknow'
 606       :EndTrap
 607      
 608       'Has Palette: ',⍕FIB.HasPalette
 609       'Number of palette colors: ',⍕FIB.ColorsUsed
 610       'Color Type: ',⍕FIB.ColorType
 611       'HasBackgroundColor: ',⍕FIB.HasBackgroundColor
 612       'IsTransparent: ',⍕FIB.IsTransparent
 613       'TransparencyCount: ',⍕FIB.TransparencyCount
 614      
 615       mData←FIB.Metadata
 616       mData.HideEmptyModels←1
 617       :If 0=mData.List.Count
 618           'Metadata Count: 0'
 619       :Else
 620           'Metadata Count: ',⍕+/(⌷mData.List).Count
 621       :EndIf
 622      
 623       :Trap 0
 624           'Comment: ',⍕FIB.Comment
 625       :Else
 626           'Comment: '
 627       :EndTrap
 628 
 629 
 630     ∇ mData←GetMetaData
 631       :Access Public
 632     ⍝ Get a Key/Value pair for each MetaData of the loaded bitmap.
 633      
 634       mData←FIB.Metadata
 635       mData.HideEmptyModels←1
 636      
 637       :If 0=mData.List.Count
 638           mData←⎕NULL
 639       :Else
 640           mData←⊃,/{(⌷⍵).(Key ToString)}¨(⌷mData.List).List
 641       :End
 642 
 643 
 644     ∇ r←SetComment comment
 645       :Access Public
 646     ⍝ Sets the comment of the loaded bitmap. Supported formats are JPEG, PNG and GIF.
 647 
 648     ⍝ r = 1 if successfull
 649     ⍝   = 0 if failure
 650      
 651       :Trap 0
 652           FIB.Comment←comment
 653           r←1
 654       :Else
 655           r←0
 656       :EndTrap
 657 
 658 
 659     ∇ Show;bmpImg;img;ms;win
 660       :Access Public
 661     ⍝ Show the current loaded bitmap in a WPF window.
 662      
 663       :If ⎕NULL≡FIB
 664           ⎕←'Show: No bitmap loaded in memory !'
 665           →0
 666       :End
 667      
 668       :If ⎕NULL=↑ms←SaveToMemoryStream 0   ⍝ 0 = BMP
 669           'ShowImage: Not able to Show the Bitmap ',2⊃ms
 670           →0
 671       :EndIf
 672      
 673     ⍝ Create a BitmapImage from the MemoryStream of the Bitmap
 674       bmpImg←⎕NEW BitmapImage
 675       bmpImg.BeginInit
 676       bmpImg.CacheOption←bmpImg.CacheOption.OnLoad
 677       bmpImg.StreamSource←ms
 678       bmpImg.EndInit
 679      
 680     ⍝ Dispose Of the BitmapImage's MemoryStream previously created.
 681       ms.Close ⋄ ms.Dispose ⋄ ms←⎕NULL
 682      
 683     ⍝ Create an Image with the BitmapImage
 684       img←⎕NEW Controls.Image
 685       img.Source←bmpImg
 686       img.(Width Height)←FIB.(Width Height)
 687      
 688     ⍝ Create a WPF window with the Image
 689       win←⎕NEW Window
 690       win.Content←img
 691       win.SizeToContent←win.SizeToContent.WidthAndHeight
 692      
 693       :If ⎕NULL≢LastFileName
 694           win.Title←' File name: ',LastFileName,' Pixel format: ',⍕FIB.PixelFormat
 695       :Else
 696           win.Title←' Pixel format: ',⍕FIB.PixelFormat
 697       :EndIf
 698      
 699       win.Show
 700 
 701 
 702     ∇ ShowInWebBrowser;frm;html;wb;⎕USING
 703       :Access Public
 704      ⍝ Show the current loaded bitmap in WindowForm browser.
 705      
 706       :If ⎕NULL≡FIB
 707           ⎕←'ShowInWebBrowser: No bitmap loaded in memory !'
 708           →0
 709       :End
 710      
 711       :If ⎕NULL=1↑html←SaveToHtml
 712           'ShowInWebBrowser error: ',2⊃html
 713           →0
 714       :End
 715      
 716       ⎕USING←'System.Windows.Forms,System.Windows.Forms.dll' 'System.Drawing,System.Drawing.dll'
 717       wb←⎕NEW WebBrowser
 718       wb.DocumentText←html
 719       wb.Dock←wb.Dock.Fill
 720      
 721       frm←⎕NEW Form
 722       frm.Size←⎕NEW Size(65 70+FIB.Width FIB.Height)
 723       frm.Controls.Add(wb)
 724      
 725       :If ⎕NULL≢LastFileName
 726           frm.Text←⊂' File name: ',LastFileName,' Pixel format: ',⍕FIB.PixelFormat
 727       :Else
 728           frm.Text←⊂' Pixel format: ',⍕FIB.PixelFormat
 729       :EndIf
 730      
 731       {}frm.ShowDialog ⍬
 732       wb.Dispose
 733       frm.Dispose
 734 
 735 
 736     ∇ r←Height
 737       :Access Public
 738     ⍝ Height of the loaded bitmap in pixel.
 739      
 740       r←FIB.Height
 741 
 742 
 743     ∇ r←Width
 744       :Access Public
 745     ⍝ Width of the loaded bitmap in pixel.
 746      
 747       r←FIB.Width
 748 
 749 
 750     :EndSection
 751 
 752 
 753     :Section PixelAccess  ⍝ *************************************************************************
 754 
 755     ∇ palette←GetPalette
 756       :Access Public
 757      ⍝ Get the colors of the palette of loaded bitmap if existing.
 758      ⍝ Equivalent to CMap in APL.
 759 
 760      ⍝ palette = RGB color of the palette if existing
 761      ⍝         = ⎕NULL otherwise
 762      
 763       :If FIB.HasPalette
 764           palette←256⊥¨(⌷FIB.Palette).Color.(R G B)
 765           palette←(FIB.ColorsUsed)↑palette
 766 ⍝         palette←(⌷FIB.Palette).Color.(R G B)
 767       :Else
 768           palette←⎕NULL
 769       :EndIf
 770 
 771 
 772     ∇ colors←GetPixelsColors;lines;palette
 773       :Access Public
 774     ⍝ Get the RGB colors for all pixels of loaded bitmap.
 775     ⍝ If the bitmap is palettized it will return the index (origin 0) of the color in the palette.
 776     ⍝ In tha case you can do BMP.GetPalette[⎕IO+BMP.GetPixelsColors] to get the equivalent to 'CBits'
 777 
 778     ⍝ colors = RGB color or index of color in palette
 779      
 780       lines←⌽⌷FIB.GetScanlines    ⍝ Because FreeImage bitmaps are bottum up we need to do a ⌽
 781      
 782     ⍝ ↓This line covers all the cases but is too slow.
 783     ⍝ colors←⊃{256⊥¨(⌷⍵).Color.(R G B)}¨lines
 784     ⍝ It is faster to use .ToByteArray and get the colors from the bytes.
 785      
 786       :Select FIB.ColorDepth
 787      
 788       :Case 1 ⍝ '[FreeImageAPI.FI1BIT]'
 789         ⍝ Number from 0 to 1
 790         ⍝ Each number is made up of 1 bit.
 791           colors←⊃{,⍉(8⍴2)⊤⍵.ToByteArray}¨lines
 792           colors←(FIB.(Height Width))↑colors
 793      
 794       :Case 4 ⍝ '[FreeImageAPI.FI4BIT]'
 795         ⍝ Numbers from 0 to 15
 796         ⍝ Each number is made up of 4 bits.
 797           colors←⊃{{2⊥¨((⍴⍵)⍴2 1 1 1)⊂⍵},⍉(8⍴2)⊤⍵.ToByteArray}¨lines
 798           colors←(FIB.(Height Width))↑colors
 799      
 800       :Case 8 ⍝ '[System.Byte]'
 801         ⍝ Numbers from 0 to 255
 802         ⍝ Each number is made up of 8 bits.
 803           colors←⊃lines.ToByteArray
 804      
 805       :Case 16 ⍝ '[FreeImageAPI.FI16RGB555]' '[FreeImageAPI.FI16RGB565]' '[FreeImageAPI.FIRGB16]'
 806      
 807           :Select ⍕FIB.PixelFormat
 808           :Case 'Format16bppRgb565'
 809             ⍝ The 'FI16RGB565' structure describes a color consisting of relative
 810             ⍝ intensities of red, green, blue. R and B components consumes 31 bits
 811             ⍝ and G component consumes 6 bits.
 812             ⍝ (R × 31)÷255, (G × 63)÷255, (B × 31)÷255
 813               colors←⊃{{{256⊥⌊31 63 31÷⍨255×2⊥¨((⍴⍵)⍴2 1 1 1 1 2 1 1 1 1 1 2 1 1 1 1)⊂⍵},⍉(8⍴2)⊤⌽⍵}¨{((⍴⍵)⍴2 1)⊂⍵}⍵.ToByteArray}¨lines
 814      
 815           :Case 'Format16bppRgb555'
 816             ⍝ The 'FI16RGB565' structure describes a color consisting of relative
 817             ⍝ intensities of red, green, blue. Each single color
 818             ⍝ component consumes 5 bits and so, takes values in the range from 0 to 31.
 819             ⍝ (R × 31)÷255, (G × 31)÷255, (B × 31)÷255
 820             ⍝ The first bit is drop and the remaining 15 bits are used.
 821               colors←⊃{{{256⊥⌊31 31 31÷⍨255×2⊥¨((⍴⍵)⍴0 2 1 1 1 1 2 1 1 1 1 2 1 1 1 1)⊂⍵},⍉(8⍴2)⊤⌽⍵}¨{((⍴⍵)⍴2 1)⊂⍵}⍵.ToByteArray}¨lines
 822      
 823           :Else
 824             ⍝ The 'FIRGB16' structure describes a color consisting of relative
 825             ⍝ intensities of red, green, blue. Each single color
 826             ⍝ component consumes 16 bits and so, takes values in the range from 0 to 65535.
 827               colors←⊃{256⊥¨(⌷⍵).Color.(R G B)}¨lines
 828      
 829           :EndSelect
 830      
 831       :Case 24 ⍝ '[FreeImageAPI.RGBTRIPLE]' '[FreeImageAPI.FIRGBF]'
 832         ⍝ Each pixels has a 3 bytes (R G B) color structure.
 833           colors←⊃{{{256⊥⌽⍵}¨((⍴⍵)⍴2 1 1)⊂⍵}⍵.ToByteArray}¨lines    ⍝ Equivalent to 'CBits' in APL
 834      
 835       :Case 32 ⍝ '[FreeImageAPI.RGBQUAD]' '[FreeImageAPI.FIRGBA16]' '[FreeImageAPI.FIRGBAF]'
 836         ⍝ Each pixels has a 4 bytes (R G B A) color structure.
 837           colors←⊃{{{256⊥⌽⍵}¨((⍴⍵)⍴2 1 1 0)⊂⍵}⍵.ToByteArray}¨lines  ⍝ Equivalent to 'CBits' in APL
 838      
 839       :Else
 840           colors←⊃{256⊥¨(⌷⍵).Color.(R G B)}¨lines
 841      
 842       :EndSelect
 843      
 844      ⍝ Get each pixel color by indexing the palette when bitmap is palettized.
 845       :If FIB.HasPalette
 846           palette←256⊥¨(⌷FIB.Palette).Color.(R G B)
 847           colors←palette[⎕IO+colors]
 848       :EndIf
 849 
 850 
 851     ∇ color←GetSinglePixelColor(row col)
 852       :Access Public
 853     ⍝ Get the color value of a single pixel
 854     ⍝ If the bitmap has a palette it will return the color not the index of the color in the palette.
 855     ⍝ Note: (0 0) is the upper-left corner of the bitmap
 856 
 857     ⍝ col = The x-coordinate of the pixel to retrieve (origin 0)
 858     ⍝ row = The y-coordinate of the pixel to retrieve (origin 0)
 859     ⍝ color = 256⊥R G B value if successfull
 860     ⍝       = ⎕NULL 'error message' if failure
 861      
 862       :Trap 0
 863           row←¯1+FIB.Height-row   ⍝ The rows are reverse in the FIB bitmap.
 864           color←FIB.GetPixel(col row)
 865           color←256⊥color.(R G B)
 866       :Else
 867           color←⎕NULL ⎕EXCEPTION.Message
 868       :EndTrap
 869 
 870 
 871     :EndSection
 872 
 873 
 874     :Section BitmapOperation  ⍝ *************************************************************************
 875 
 876     ∇ r←AdjustBrightness percentage
 877       :Access Public
 878     ⍝ Adjusts the brightness of a 8-, 24- or 32-bit image by a certain amount.
 879     ⍝ A value 0 means no change, less than 0 will make the image darker and greater than 0 will make the image brighter.
 880     ⍝ 'percentage' is a value between -100 and 100.
 881 
 882     ⍝ r = 1 if successfull
 883     ⍝   = 0 if failure
 884      
 885       r←FIB.AdjustBrightness percentage
 886 
 887 
 888     ∇ r←AdjustColors(brightness contrast gamma invert)
 889       :Access Public
 890     ⍝ Adjusts an image's brightness, contrast and gamma as well as it may optionally invert the image within a single operation.
 891 
 892     ⍝ brightness = A value 0 means no change, less than 0 will make the image darker and greater than 0 will make the image brighter.
 893     ⍝ contrast   = A value 0 means no change, less than 0 will decrease the contrast and greater than 0 will increase the contrast of the image.
 894     ⍝ gamma      = A value of 1.0 leaves the image alone, less than one darkens it, and greater than one lightens it.
 895     ⍝ invert     = 1 = invert the image, 0 = do not invert the image.
 896     ⍝ Permitted range:  ¯100 ≤ 'brightness' ≤ 100 , ¯100 ≤ 'contrast' ≤ 100, 'gamma' > 0
 897 
 898     ⍝ Note: It is faster to use this method than using AdjustBrightness, AdjustContrast, AdjustGamma and Invert separately.
 899      
 900       r←FIB.AdjustColors(brightness contrast gamma invert)
 901 
 902 
 903     ∇ r←AdjustContrast percentage
 904       :Access Public
 905     ⍝ Adjusts the contrast of a 8-, 24- or 32-bit image by a certain amount.
 906     ⍝ A value 0 means no change, less than 0 will decrease the contrast and greater than 0 will increase the contrast of the image.
 907     ⍝ 'percentage' is a value between -100 and 100.
 908 
 909     ⍝ r = 1 if successfull
 910     ⍝   = 0 if failure
 911      
 912       r←FIB.AdjustContrast percentage
 913 
 914 
 915     ∇ r←AdjustGamma gamma
 916       :Access Public
 917     ⍝ Performs gamma correction on a 8-, 24- or 32-bit image.
 918     ⍝ The parameter represents the gamma value to use ('gamma' > 0).
 919     ⍝ A value of 1.0 leaves the image alone, less than one darkens it, and greater than one lightens it.
 920 
 921     ⍝ r = 1 if successfull
 922     ⍝   = 0 if failure
 923      
 924       r←FIB.AdjustGamma gamma
 925 
 926 
 927     ∇ r←{options}ConvertFormat format;ms
 928       :Access Public
 929     ⍝ Convert the loaded bitmap to another bitmap format.
 930 
 931     ⍝ Format supported:
 932     ⍝  0 = Windows or OS/2 Bitmap File (*.BMP)
 933     ⍝  1 = Windows Icon (*.ICO)
 934     ⍝  2 = Independent JPEG Group (*.JPG, *.JIF, *.JPEG, *.JPE)
 935     ⍝  3 = JPEG Network Graphics (*.JNG)
 936     ⍝  4 = Commodore 64 Koala format (*.KOA)
 937     ⍝  5 = Amiga IFF (*.IFF, *.LBM)
 938     ⍝  5 = Amiga IFF (*.IFF, *.LBM)
 939     ⍝  6 = Multiple Network Graphics (*.MNG)
 940     ⍝  7 = Portable Bitmap (ASCII) (*.PBM)
 941     ⍝  8 = Portable Bitmap (BINARY) (*.PBM)
 942     ⍝  9 = Kodak PhotoCD (*.PCD)
 943     ⍝ 10 = Zsoft Paintbrush PCX bitmap format (*.PCX)
 944     ⍝ 11 = Portable Graymap (ASCII) (*.PGM)
 945     ⍝ 12 = Portable Graymap (BINARY) (*.PGM)
 946     ⍝ 13 = Portable Network Graphics (*.PNG)
 947     ⍝ 14 = Portable Pixelmap (ASCII) (*.PPM)
 948     ⍝ 15 = Portable Pixelmap (BINARY) (*.PPM)
 949     ⍝ 16 = Sun Rasterfile (*.RAS)
 950     ⍝ 17 = Truevision Targa files (*.TGA, *.TARGA)
 951     ⍝ 18 = Tagged Image File Format (*.TIF, *.TIFF)
 952     ⍝ 19 = Wireless Bitmap (*.WBMP)
 953     ⍝ 20 = Adobe Photoshop (*.PSD)
 954     ⍝ 21 = Dr. Halo (*.CUT)
 955     ⍝ 22 = X11 Bitmap Format (*.XBM)
 956     ⍝ 23 = X11 Pixmap Format (*.XPM)
 957     ⍝ 24 = DirectDraw Surface (*.DDS)
 958     ⍝ 25 = Graphics Interchange Format (*.GIF)
 959     ⍝ 26 = High Dynamic Range (*.HDR)
 960     ⍝ 27 = Raw Fax format CCITT G3 (*.G3)
 961     ⍝ 28 = Silicon Graphics SGI image format (*.SGI)
 962     ⍝ 29 = OpenEXR format (*.EXR)
 963     ⍝ 30 = JPEG-2000 format (*.J2K, *.J2C)
 964     ⍝ 31 = JPEG-2000 format (*.JP2)
 965     ⍝ 32 = Portable FloatMap (*.PFM)
 966     ⍝ 33 = Macintosh PICT (*.PICT)
 967     ⍝ 34 = RAW camera image (*.*)
 968     ⍝ 35 = Google WebP image format (*.webp)
 969     ⍝ 36 = JPEG XR image format (*.jxr)
 970 
 971     ⍝ Options available when saving an Independent JPEG Group (*.JPG, *.JIF, *.JPEG, *.JPE) file:
 972     ⍝ options =   128 save with compression ratio 100:1
 973     ⍝         =   256 save with compression ratio  75:1 (default)
 974     ⍝         =   512 save with compression ratio  50:1
 975     ⍝         =  1024 save with compression ratio  25:1
 976     ⍝         =  2048 save with compression ratio  10:1
 977     ⍝         =  or 0 to 100 for specific compression ratio
 978     ⍝         =  4096 save with high 4x1 chroma subsampling (4:1:1)
 979     ⍝         = 16384 save with medium 2x2 medium chroma (4:2:0)
 980     ⍝         = 32768 save with low 2x1 chroma subsampling (4:2:2)
 981     ⍝         = 65536 save with no chroma subsampling (4:4:4)
 982     ⍝         = +   8192 save as progressive JPEG (to combine with other options)
 983     ⍝         = + 131072 compute optimal Huffman coding (to combine with other options)
 984     ⍝         = + 262144 remove all metadata (to combine with other options)
 985 
 986     ⍝ Suggested for web:  options ← 256+8192+131072+262144
 987 
 988     ⍝ Options available when saving a Tagged Image File Format (*.TIF, *.TIFF) file:
 989     ⍝ options =   + 1  Stores tags for separated CMYK (to combine with other options)
 990     ⍝         =   256  Save using PACKBITS compression.
 991     ⍝         =   512  Save using DEFLATE compression (a.k.a. ZLIB compression).
 992     ⍝         =  1024  Save using ADOBE DEFLATE compression.
 993     ⍝         =  2048  Save without any compression.
 994     ⍝         =  4096  Save using CCITT Group 3 fax encoding.
 995     ⍝         =  8192  Save using CCITT Group 4 fax encoding.
 996     ⍝         = 16384  Save using LZW compression (default).
 997     ⍝         = 32768  Save using JPEG compression.
 998 
 999     ⍝ Options available when saving a Portable Network Graphics (*.PNG) file:
1000     ⍝ options =     1 Save using ZLib level 1 compression flag
1001     ⍝         =     6 Save using ZLib level 6 compression flag (default)
1002     ⍝         =     9 Save using ZLib level 9 compression flag
1003     ⍝         =   256 Save without ZLib compression.
1004     ⍝         = + 512 Save using Adam7 interlacing (to combine with other options)
1005 
1006     ⍝ You need to do the following to dispose of the MemoryStream after use:
1007     ⍝  ms.Close ⋄ ms.Dispose ⋄ ms←⎕NULL
1008 
1009     ⍝ format = Format to use to save the memory stream.
1010     ⍝ ms     = MemoryStream containing the bitmap if successfull
1011     ⍝        = ⎕NULL 'error message' if failure
1012      
1013       :If format=¯1
1014           format←FIB.ImageFormat.value__
1015       :EndIf
1016      
1017       :If 0=⎕NC'options'
1018           options←FREE_IMAGE_SAVE_FLAGS.DEFAULT
1019       :EndIf
1020      
1021       :Trap 0
1022         ⍝ Create a new empty MemoryStream
1023           ms←⎕NEW MemoryStream
1024      
1025         ⍝ Save to the MemoryStream
1026           FIB.Save(ms format options)
1027       :Else
1028           ms←⎕NULL ⎕EXCEPTION.Message
1029       :EndTrap
1030      
1031     ⍝ To set the position to 0, otherwise it may not load.
1032       ms.Position←Convert.ToInt64 0
1033      
1034       FIB.Dispose ⋄ FIB←⎕NULL
1035      
1036       :Trap 0
1037           FIB←⎕NEW FreeImageBitmap(ms)
1038           r←1
1039       :Else
1040           r←0 ⎕EXCEPTION.Message
1041       :EndTrap
1042 
1043 
1044     ∇ r←ConvertTo4Bits
1045       :Access Public
1046     ⍝ Converts the loaded bitmap to 4 bits per pixel using the Xiaolin Wu color quantization algorithm
1047     ⍝ and the Floyd and Steinberg error diffusion.
1048     ⍝ r = 1 if successfull
1049     ⍝   = 0 if failure
1050      
1051       r←FIB.ConvertColorDepth(FREE_IMAGE_COLOR_DEPTH.FICD_04_BPP)
1052 
1053 
1054     ∇ r←ConvertTo8Bits
1055       :Access Public
1056     ⍝ Converts the loaded bitmap to 8 bits per pixel using the Xiaolin Wu color quantization algorithm
1057     ⍝ and the Floyd and Steinberg error diffusion.
1058 
1059     ⍝ r = 1 if successfull
1060     ⍝   = 0 if failure
1061      
1062       r←FIB.ConvertColorDepth(FREE_IMAGE_COLOR_DEPTH.FICD_08_BPP)
1063 
1064 
1065     ∇ r←ConvertTo16Bits
1066       :Access Public
1067     ⍝ Converts the loaded bitmap to 16 bits per pixel (RGB565 → R=5 bits, G=6 bits, B=5 bits).
1068 
1069     ⍝ r = 1 if successfull
1070     ⍝   = 0 if failure
1071      
1072       r←FIB.ConvertColorDepth(FREE_IMAGE_COLOR_DEPTH.FICD_16_BPP)
1073 
1074 
1075     ∇ r←ConvertTo24Bits
1076       :Access Public
1077     ⍝ Converts the loaded bitmap to 24 bits per pixel (R=8 bits, G=8 bits, B=8 bits).
1078 
1079     ⍝ r = 1 if successfull
1080     ⍝   = 0 if failure
1081      
1082       r←FIB.ConvertColorDepth(FREE_IMAGE_COLOR_DEPTH.FICD_24_BPP)
1083 
1084 
1085     ∇ r←ConvertTo32Bits
1086       :Access Public
1087     ⍝ Converts the loaded bitmap to 32 bits per pixel (R=8 bits, G=8 bits, B=8 bits, Alpha=8 bits).
1088 
1089     ⍝ r = 1 if successfull
1090     ⍝   = 0 if failure
1091      
1092       r←FIB.ConvertColorDepth(FREE_IMAGE_COLOR_DEPTH.FICD_32_BPP)
1093 
1094 
1095     ∇ r←ConvertToGreyscale tones
1096       :Access Public
1097     ⍝ Converts the loaded bitmap to 256, 16 or 1 tone of grey.
1098 
1099     ⍝ tones = number of tones (256, 16 or 1)
1100 
1101     ⍝ r = 1 if successfull
1102     ⍝   = 0 if failure
1103      
1104       :Select tones
1105       :Case 256
1106           r←FIB.ConvertColorDepth(FREE_IMAGE_COLOR_DEPTH.FICD_08_BPP+FREE_IMAGE_COLOR_DEPTH.FICD_FORCE_GREYSCALE) ⍝ 256 tones of grey
1107       :Case 16
1108           r←FIB.ConvertColorDepth(FREE_IMAGE_COLOR_DEPTH.FICD_04_BPP+FREE_IMAGE_COLOR_DEPTH.FICD_FORCE_GREYSCALE) ⍝ 16 tones of grey
1109       :Case 1
1110           r←FIB.ConvertColorDepth(FREE_IMAGE_COLOR_DEPTH.FICD_01_BPP+FREE_IMAGE_COLOR_DEPTH.FICD_FORCE_GREYSCALE) ⍝ black or white pixel
1111       :Else
1112           r←0 'ConvertToGreyscale: Number of tones not supported'
1113       :EndSelect
1114 
1115 
1116     ∇ r←ConvertToThumbnail maxPixelSize;newFIB
1117       :Access Public
1118     ⍝ Converts the loaded bitmap to a thumbnail while keeping aspect ratio.
1119 
1120     ⍝ maxPixelSize = maximum width or height of the thumbnail.
1121 
1122     ⍝ r = 1 if successfull
1123     ⍝   = 0 'error message' if failure
1124      
1125       :Trap 0
1126           newFIB←FIB.MakeThumbnail(maxPixelSize 1) ⍝ 1 = HDR images are transperantly converted to standard images
1127           FIB.Dispose ⋄ FIB←⎕NULL
1128           FIB←newFIB
1129           r←1
1130       :Else
1131           r←0 ⎕EXCEPTION.Message
1132       :End
1133 
1134 
1135     ∇ r←Crop(x y width height);newFIB
1136       :Access Public
1137     ⍝ Crop the loaded bitmap.
1138 
1139     ⍝ x      = Specifies the x-coordinate of the upper-left corner of the cropped rectangle.
1140     ⍝ y      = Specifies the y-coordinate of the upper-left corner of the cropped rectangle.
1141     ⍝ width  = Specifies the width of the cropped rectangle.
1142     ⍝ height = Specifies the height of the cropped rectangle.
1143 
1144     ⍝ r = 1 if successfull
1145     ⍝   = 0 if failure
1146      
1147       :Trap 0
1148           newFIB←FIB.Copy(x y(x+width)(y+height))
1149           FIB.Dispose ⋄ FIB←⎕NULL
1150           FIB←newFIB
1151           r←1
1152       :Else
1153           r←0 ⎕EXCEPTION.Message
1154       :End
1155 
1156 
1157     ∇ r←FlipHorizontal
1158       :Access Public
1159     ⍝ FlipHorizontal the loaded bitmap. The following Flips/Rotate are also available:
1160 
1161     ⍝ Rotate180FlipNone 	= Specifies a 180-degree clockwise rotation without flipping.
1162     ⍝ Rotate180FlipX	    = Specifies a 180-degree clockwise rotation followed by a horizontal flip.
1163     ⍝ Rotate180FlipXY   	= Specifies a 180-degree clockwise rotation followed by a horizontal and vertical flip.
1164     ⍝ Rotate180FlipY    	= Specifies a 180-degree clockwise rotation followed by a vertical flip.
1165     ⍝ Rotate270FlipNone 	= Specifies a 270-degree clockwise rotation without flipping.
1166     ⍝ Rotate270FlipX	    = Specifies a 270-degree clockwise rotation followed by a horizontal flip.
1167     ⍝ Rotate270FlipXY   	= Specifies a 270-degree clockwise rotation followed by a horizontal and vertical flip.
1168     ⍝ Rotate270FlipY	    = Specifies a 270-degree clockwise rotation followed by a vertical flip.
1169     ⍝ Rotate90FlipNone  	= Specifies a 90-degree clockwise rotation without flipping.
1170     ⍝ Rotate90FlipX     	= Specifies a 90-degree clockwise rotation followed by a horizontal flip.
1171     ⍝ Rotate90FlipXY	    = Specifies a 90-degree clockwise rotation followed by a horizontal and vertical flip.
1172     ⍝ Rotate90FlipY	     = Specifies a 90-degree clockwise rotation followed by a vertical flip.
1173     ⍝ RotateNoneFlipNone	= Specifies no clockwise rotation and no flipping.
1174     ⍝ RotateNoneFlipX	   = Specifies no clockwise rotation followed by a horizontal flip.
1175     ⍝ RotateNoneFlipXY	  = Specifies no clockwise rotation followed by a horizontal and vertical flip.
1176     ⍝ RotateNoneFlipY   	= Specifies no clockwise rotation followed by a vertical flip.
1177 
1178     ⍝ r = 1 if successfull
1179     ⍝   = 0 'error message' if failure
1180      
1181       :Trap 0
1182           FIB.RotateFlip(RotateFlipType.RotateNoneFlipX)
1183       :Else
1184           r←0 ⎕EXCEPTION.Messag
1185       :EndTrap
1186 
1187 
1188     ∇ r←FlipVertical
1189       :Access Public
1190     ⍝ FlipVertical the loaded bitmap.
1191     ⍝ Note: See method FlipHorizontal for the list of alternative Flips/Rotate available.
1192 
1193     ⍝ r = 1 if successfull
1194     ⍝   = 0 'error message' if failure
1195      
1196       :Trap 0
1197           FIB.RotateFlip(RotateFlipType.RotateNoneFlipY)
1198       :Else
1199           r←0 ⎕EXCEPTION.Messag
1200       :EndTrap
1201 
1202 
1203     ∇ r←Invert
1204       :Access Public
1205     ⍝ Invert the loaded bitmap (255 - R, G or B value for each pixel).
1206 
1207     ⍝ r = 1 if successfull
1208     ⍝   = 0 if failure
1209      
1210       r←FIB.Invert
1211 
1212 
1213     ∇ r←Resize(width height);size
1214       :Access Public
1215     ⍝ Resize the loaded bitmap.
1216     ⍝ The Resize will fail when the bitdepth cannot be handled or when
1217     ⍝ there’s not enough memory (this may happen with very large images).
1218     ⍝ See also the method .ConvertToThumbnail that will resize while keeping the aspect ratio.
1219 
1220     ⍝ Images whose image type is FIT_BITMAP are returned as 8-bit or 24-bit, or as 32-bit if they
1221     ⍝ contain transparency. For example, 16-bit RGB bitmap are returned as 24-bit. Non
1222     ⍝ transparent palettized and 4-bit bitmap are returned as 24-bit images. The algorithm tries to
1223     ⍝ produce destination images with the smallest possible bit depth.
1224     ⍝ If you have transparency, you'll get a 32-bit image. If you have real colors, you'll get a 24-bit
1225     ⍝ image. For all other cases, you'll get an 8-bit image with a linear color palette.
1226 
1227     ⍝ Resampling refers to changing the pixel dimensions (and therefore display size) of an image.
1228     ⍝ When you downsample (or decrease the number of pixels), information is deleted from the
1229     ⍝ image. When you upsample (or increase the number of pixels), new pixels are added based
1230     ⍝ on color values of existing pixels. You specify an interpolation filter to determine how pixels
1231     ⍝ are added or deleted.
1232 
1233     ⍝ The resize image filter available are:
1234     ⍝  FILTER_BOX        = Box, pulse, Fourier window, 1st order (constant) b-spline
1235     ⍝  FILTER_BICUBIC    = Mitchell and Netravali's two-param cubic filter
1236     ⍝  FILTER_BILINEAR   = Bilinear filter
1237     ⍝  FILTER_BSPLINE    = 4th order (cubic) b-spline
1238     ⍝  FILTER_CATMULLROM = Catmull-Rom spline, Overhauser spline
1239     ⍝  FILTER_LANCZOS3   = Lanczos3 filter
1240 
1241     ⍝ r = 1 if successfull
1242     ⍝   = 0 if failure
1243      
1244       size←⎕NEW Size(width height)
1245      
1246       r←FIB.Rescale(size FREE_IMAGE_FILTER.FILTER_CATMULLROM)
1247 
1248 
1249     ∇ r←Rotate angle
1250       :Access Public
1251     ⍝ Rotate the loaded bitmap.
1252     ⍝ This method rotates a 1-, 4-, 8-bit greyscale or a 24-, 32-bit color image by means of 3 shears.
1253     ⍝ For 1- and 4-bit images, rotation is limited to angles whose value is an integer multiple of 90.
1254 
1255     ⍝ r = 1 if successfull
1256     ⍝   = 0 if failure
1257      
1258       r←FIB.Rotate(angle)
1259 
1260 
1261     ∇ r←JPEGCrop(srcFileName dstFileName x y width height)
1262       :Access Public
1263     ⍝ Performs a lossless crop on a JPEG file.
1264     ⍝ 'JPEGCrop' works by rearranging the compressed data (DCT coefficients), without
1265     ⍝ ever fully decoding the image. Therefore, the crop operation is lossless: there is no image
1266     ⍝ degradation at all, which would not be true if you used 'LoadFromFile' followed by
1267     ⍝ 'SaveToFile' to accomplish the same conversion.
1268 
1269     ⍝ To perform this lossless operation, however, the width and height of the cropped rectangle
1270     ⍝ must be adjusted so that the image dimensions are a multiple of the iMCU size (usually 8 or
1271     ⍝ 16 pixels), because the function can only transform complete blocks of DCT coefficient data
1272     ⍝ in the desired way. That’s why the output width or height can be slightly greater than the
1273     ⍝ requested image size.
1274 
1275     ⍝ srcFileName =  source filename
1276     ⍝ dstFileName =  destination filename (could be the same as srcFileName)
1277     ⍝ x      = Specifies the x-coordinate of the upper-left corner of the cropped rectangle.
1278     ⍝ y      = Specifies the y-coordinate of the upper-left corner of the cropped rectangle.
1279     ⍝ width  = Specifies the width of the cropped rectangle.
1280     ⍝ height = Specifies the height of the cropped rectangle.
1281 
1282     ⍝ r = 1 if successfull
1283     ⍝   = 0 if failure
1284      
1285       r←FIB.JPEGCrop(srcFileName dstFileName x y(x+width)(y+height))
1286 
1287 
1288     ∇ r←JPEGTransform(srcFile dstFile operation)
1289       :Access Public
1290     ⍝ Performs a lossless rotation or flipping on a JPEG file.
1291 
1292     ⍝ operation = operation to apply
1293     ⍝           = 'FLIP_H' is an horizontal flip
1294     ⍝           = 'FLIP_V' is a vertical flip
1295     ⍝           = 'ROTATE_180' is a 180 degree rotation
1296     ⍝           = 'ROTATE_270' is a 270 degree rotation (or 90 CCW)
1297     ⍝           = 'ROTATE_90'  is a 90 degree clockwise rotation
1298     ⍝           = 'TRANSPOSE'  is transpose across UL-to-LR axis
1299     ⍝           = 'TRANSVERSE' is transpose across UR-to-UL axis
1300     ⍝ srcFileName =  source filename
1301     ⍝ dstFileName =  destination filename (could be the same as srcFileName)
1302 
1303     ⍝ r = 1 if successfull
1304     ⍝   = 0 if failure
1305      
1306       operation←⍎'FREE_IMAGE_JPEG_OPERATION.FIJPEG_OP_',operation
1307      
1308       r←FIB.JPEGTransform(srcFile dstFile operation 0)
1309 
1310 
1311     :EndSection
1312 
1313 :EndClass

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2016-04-14 19:27:50, 4274.2 KB) [[attachment:FreeImage.NET_3.17.0.4.zip]]
  • [get | view] (2016-04-18 18:33:08, 55.4 KB) [[attachment:netFreeImage.v1.0.txt]]
  • [get | view] (2016-04-20 15:44:45, 56.5 KB) [[attachment:netFreeImage.v1.1.txt]]
 All files | Selected Files: delete move to page

You are not allowed to attach a file to this page.