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