= netFreeImage =

~-<<SeeSaw(section="table-of-contents", show="true", seesaw="false", toshow="<<(Show>> table-of-contents)", tohide="<<(Hide>> table-of-contents)", speed="Slow")>>-~

<<TableOfContents>>

=== Overview ===

`netFreeImage`  is a Dyalog cover class for bitmap resizing, cropping, compression and format change using .Net. It is using an open source project called `FreeImage` created by Floris van den Berg originally written in C++. The project is maintained today by Hervé Drolon. The .Net wrapper is the work of Jean-Philippe Goerke and Carsten Klein.[[http://freeimage.sourceforge.net/|Original project location]] [[https://freeimagenet.codeplex.com//|FreeImage.NET wrapper project page]] [[http://paulbourke.net/dataformats/bitmaps/|A Beginners Guide to Bitmaps]]

`netFreeImage` supports:
 * Loading and saving of 40 bitmap types
 * Easy access to bitmap components, such as palettes, data bits and metadata
 * Converting bitmap’s bit depths from one to another
 * Accessing pages in a bitmap when there are multiple, such as in TIFF
 * Basic manipulation of bitmaps, such as rotation, flipping and resampling or point operations such as brightness and contrast adjustment
 * Alpha compositing and alpha blending

`netFreeImage` does not support:
 * Advanced image processing operations such as convolution and transforms
 * Bitmap drawing
 * Vector graphics

For this class to work you need to download the original dll in C++ (!FreeImage.dll) and the .Net wrapper (FreeImageNET.dll) and copy them both in the same directory as the dyalog.exe program. For commodity the version 3.17.04 of the dlls can be downloaded here: [[attachment:FreeImage.NET_3.17.0.4.zip]]

=== Instantiating and Disposing of the class: ===
{{{
   bmp ← ⎕NEW netFreeImage    ⍝ Instantiate the class.

   bmp.Dispose                ⍝ To do when finish using the class to release the memory of the last image.
                              ⍝ Between .LoadFromFile it is not necessary to use .Dispose to release the memory of
                              ⍝ the previous image because the class will do it automatically.
}}}
=== Methods to Load and Save a Bitmap ===
{{{
 LoadFromFile                    ⍝ Get a FreeImageBitmap object from a file on disk.
 LoadFromBase64                  ⍝ Get a FreeImageBitmap object from a Base64 string.
 LoadFromMemoryStream            ⍝ Get a FreeImageBitmap object from a MemoryStream.
 LoadFromBitmap                  ⍝ Get a FreeImageBitmap object from a System.Drawing.Bitmap (WindowsForms).
 PasteFromClipboard              ⍝ Paste Image from Clipboard.

 SaveToFile                      ⍝ Save the loaded bitmap to a file on disk with default values (format conversion allowed).
 SaveToBase64                    ⍝ Save the loaded bitmap to a Base64 character vector with the current bitmap format.
 SaveToMemoryStream              ⍝ Save the loaded bitmap to a MemoryStream object with the specified bitmap format.
 SaveToBitmap                    ⍝ Save the loaded bitmap to a System.Drawing.Bitmap (WindowsForms).
 SaveToHtml                      ⍝ Save the loaded bitmap to an HTML inline image with the current bitmap format.
 CopyToClipboard                 ⍝ Copy Image to Clipboard.
}}}
=== Methods for Bitmap operation ===
{{{
 AdjustBrightness                ⍝ Adjusts the brightness of a 8-, 24- or 32-bit image by a certain amount.
 AdjustColors                    ⍝ Adjusts an image's brightness, contrast, gamma and invert image in a single operation.
 AdjustContrast                  ⍝ Adjusts the contrast of a 8-, 24- or 32-bit image by a certain amount.
 AdjustGamma                     ⍝ Performs gamma correction on a 8-, 24- or 32-bit image.
 ConvertFormat                   ⍝ Convert the format of the loaded bitmap to another bitmap format.
 ConvertTo4Bits                  ⍝ Converts the loaded bitmap to 4 bits per pixel using the Xiaolin Wu color quantization algorithm
 ConvertTo8Bits                  ⍝ Converts the loaded bitmap to 8 bits per pixel using the Xiaolin Wu color quantization algorithm
 ConvertTo16Bits                 ⍝ Converts the loaded bitmap to 16 bits per pixel (RGB565).
 ConvertTo24Bits                 ⍝ Converts the loaded bitmap to 24 bits per pixel.
 ConvertTo32Bits                 ⍝ Converts the loaded bitmap to 32 bits per pixel.
 ConvertToGreyscale              ⍝ Converts the loaded bitmap to 256, 16 or 1 tone of grey.
 ConvertToThumbnail              ⍝ Converts the loaded bitmap to a thumbnail while keeping aspect ratio.
 Crop                            ⍝ Crop the loaded bitmap.
 FlipHorizontal                  ⍝ Flip horizontally the loaded bitmap.
 FlipVertical                    ⍝ Flip vertically the loaded bitmap.
 Invert                          ⍝ Invert the loaded bitmap.
 Resize                          ⍝ Resize the loaded bitmap.
 Rotate                          ⍝ Rotate the loaded bitmap.
 JPEGCrop                        ⍝ Performs a lossless crop on a JPEG file.
 JPEGTransform                   ⍝ Performs a lossless rotation or flipping on a JPEG file.
}}}
=== Methods for Pixel Access ===
{{{
 GetPalette                      ⍝ Get the colors of the palette of loaded bitmap if existing.
 GetPixelsColors                 ⍝ Get the RGB colors for all pixels of loaded bitmap.
 GetSinglePixelColor             ⍝ Get the RGB value of a single pixel of loaded bitmap.
}}}
=== Methods for Bitmap Information ===
{{{
 GetMetaData                     ⍝ Get a Key/Value pair for each MetaData of the loaded bitmap.
 Info                            ⍝ Show information about the loaded bitmap.
 SetComment                      ⍝ Sets the comment of the loaded bitmap. Supported formats are JPEG, PNG and GIF.
 Show                            ⍝ Show the loaded bitmap in a WPF window.
 ShowInWebBrowser                ⍝ Show the loaded bitmap in WindowForm browser using 'SaveToHtml'.
 Height                          ⍝ Height of the loaded bitmap in pixel.
 Width                           ⍝ Width of the loaded bitmap in pixel.
}}}

=== Known limitations ===
Not all methods of the .Net wrapper are coded in the `netFreeImage` Dyalog class. For example access to multipage bitmap is not implemented. While using the .Net wrapper the '''!FreeImageBitmap''' namespace is easier to use than the '''!FreeImage''' namespace that has a similar set of methods that are static only.

=== How to install netFreeImage in your workspace ===

 1. Download [[attachment:netFreeImage.v1.1.txt]]
 1. Do a Select all (Ctrl+A) and a copy (Ctrl+C).
 1. In your workspace execute `)ed ○ netFreeImage`
 1. Paste (Ctrl+V) the text into the Dyalog editor
 1. Press Escape and ')save' your workspace
 1. Download [[attachment:FreeImage.NET_3.17.0.4.zip]]
 1. Unzip and install !FreeImage.dll and FreeImageNET.dll in the dyalog.exe directory

=== Test Run ===
==== Change the format of an image file ====
{{{
      ⍝ Use .LoadFromFile and .SaveToFile to load and save a file to a different format with the default options.
      ⍝ See the comments of each methods for details.

      bmp ← ⎕NEW netFreeImage
      bmp.LoadFromFile 'd:\image.tiff'       ⍝ Load the bitmap as a tiff file
1
      bmp.SaveToFile 'd:\image.jpeg'         ⍝ Save as jpeg file with default options
1
}}}
==== Bitmap file to Png file with different bit depth ====
{{{
      bmp ← ⎕NEW netFreeImage
      bmp.LoadFromFile 'd:\image.bmp'  ⍝ Load the bitmap
1
      bmp.Info                         ⍝ Show useful information about the loaded bitmap
      bmp.ConvertTo8Bits               ⍝ Convert to a palette with 256 colors
1
      bmp.Show                         ⍝ Look if satisfactory
      cmap  ← bmp.GetPalette           ⍝ Get the 256 colors of the palette
      cbits ← bmp.GetPixelsColors      ⍝ Get an array with the colors as 256⊥(R G B)
      bmp.SaveToFile 'd:\image.png'    ⍝ Save to png file
1
}}}
==== Prepare Jpeg file for Web ====
{{{
      bmp ← ⎕NEW netFreeImage
      bmp.LoadFromFile 'd:\image.jpeg'  ⍝ Load the bitmap
1
      bmp.Resize 250 200                ⍝ Resize to new width and height
1
      bmp.Show                          ⍝ Look if satisfactory

      bmp.GetMetadata                   ⍝ Get a Key/Value pair for each MetaData

    ⍝ Save for web: 512=compression ration 50:1, 8192=progressive, 262144=remove metadata
      (512+8192+262144) bmp.SaveToFile 'd:\imageWeb.jpeg'
1
}}}

== Version Information ==
{{{
 Version 1.0 April 2016, Pierre Gilbert


 Version 1.1 April 2016
             Methods added: CopyToClipboard, PasteFromClipboard
}}}
||Original author: ||Pierre Gilbert ||
||Responsible: ||PierreGilbert ||
||Email: || <<MailTo(apgil AT SPAMFREE videotron DOT ca)>> ||

----
CategoryDyalog - CategoryDyalogDotNet - CategoryDotNet - CategoryDyalogDotNetUtilities