UNDER CONSTRUCTION

netFreeImage

(Hide table-of-contents)

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.Original project location FreeImage.NET wrapper project page A Beginners Guide to Bitmaps

netFreeImage supports:

netFreeImage does not support:

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: 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).

 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.
 SaveToBase64WithFormat          ⍝ Save the loaded bitmap to a Base64 character vector with the specified bitmap format.
 SaveToMemoryStream              ⍝ Save the loaded bitmap to a MemoryStream object with the current bitmap format.
 SaveToMemoryStreamWithFormat    ⍝ 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.
 SaveToHtmlWithFormat            ⍝ Save the loaded bitmap to an HTML inline image with the specified bitmap format.
 SaveTo4bppBmpFile               ⍝ Save the loaded bitmap to a 4 bit per pixel bmp file.
 SaveTo8bppBmpFile               ⍝ Save the loaded bitmap to a 8 bit per pixel bmp file.
 SaveTo4bppPngFile               ⍝ Save the loaded bitmap to a 4 bit per pixel png file.
 SaveTo8bppPngFile               ⍝ Save the loaded bitmap to a 8 bit per pixel png file.
 SaveToJpegFile                  ⍝ Save the loaded bitmap to a jpeg file with options.
 SaveToTiffFile                  ⍝ Save the loaded bitmap to a tiff file with options.
 SaveToPngFile                   ⍝ Save the loaded bitmap to a png file with options.

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 and gamma as well as it may optionally invert the image within 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.
 ConvertTo4Bits                  ⍝ Converts the loaded bitmap to 4 bits per pixel using the default quantize algorithm.
 ConvertTo8Bits                  ⍝ Converts the loaded bitmap to 8 bits per pixel using the default quantize algorithm.
 ConvertTo16Bits                 ⍝ Converts the loaded bitmap to 16 bits per pixel.
 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 Greyscale and 8 bits per pixel.
 ConvertToThumbnail              ⍝ Converts the loaded bitmap to a thumbnail (keep the aspect ratio).
 Crop                            ⍝ Crop the loaded bitmap.
 FlipHorizontal                  ⍝ FlipHorizontal the loaded bitmap.
 FlipVertical                    ⍝ FlipVertical the loaded bitmap.
 Invert                          ⍝ Invert the loaded bitmap.
 Resize                          ⍝ Resize the loaded bitmap.
 Rotate                          ⍝ Rotate the loaded bitmap.
 SetResolution                   ⍝ Set the loaded bitmap to a new resolution (dpi).

 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.

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'.

Utility methods

 CaptureWpfVisual                ⍝ Capture a WPF visual element to a FreeImageBitmap.
 FileToBase64String              ⍝ Converts a bitmap, icon or cursor file to a Base64 String.
 CursorFromBase64String          ⍝ Converts a Base64String to a cursor.
 ImageFromBase64String           ⍝ Converts a Base64String to a System.Windows.Media.Imaging.BitmapImage (WPF).

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 netFreeImage.v1.0.txt

  2. Do a Select all (Ctrl+A) and a copy (Ctrl+C).
  3. In your workspace execute )ed ○ netFreeImage

  4. Paste (Ctrl+V) the text into the Dyalog editor
  5. Press Escape and ')save' your workspace
  6. Download FreeImage.NET_3.17.0.4.zip

  7. 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.
      ⍝ Use .SaveToJpegFile, .SaveToTiffFile and .SaveToPngFile to save to a file with particular 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

      options bmp.SaveToJpeg 'd:\image.jpeg' ⍝ Save as jpeg file with 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 info 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.SaveToJpegFile 'd:\imageWeb.jpeg'
1

Version Information

 Version 1.0 April 2016, Pierre Gilbert

Original author:

Pierre Gilbert

Responsible:

PierreGilbert

Email:

<apgil AT SPAMFREE videotron DOT ca>


CategoryDyalog - CategoryDyalogDotNet - CategoryDotNet