type conversions/Conversion Codes

Learning APL or new to Dyalog? Ask "silly" questions here, without fear...

type conversions/Conversion Codes

I'm working with matrices of 0's and 1's (binary images). These have a conversion code of 11 before I start doing anything interesting with them. But after I've subjected them to a few fairly simple operations, they have conversion code 326, which I'm not sure even exists (see Dyalog Native Files CHEAT SHEET). This screws up further processing. Is there a way to convert these matrices back to code 11 (1-bit boolean)? I tried stuff like M←M=1, which I was hoping would get me back to 1-bit Boolean, but it's still code 326.
Stu

Posts: 97
Joined: Thu Dec 31, 2015 1:30 am

Re: type conversions/Conversion Codes

Hi Stu .. welcome to Dyalog APL !

An array of type 326 is an array of pointers. I think that your problem is that you've not got a simple boolean array - at least one element is nested or is a reference to a namespace or similar.

The full table of types can be found in the documentation for ⎕DR .. 326 is not listed in the Native File cheat sheet since it's not a valid conversion code for native file functions.

Be aware that ⎕DR returns the type of the argument _after_ the argument has been squeezed .. you may find that 181⌶ is more useful.

What follows is a shameless plug for the presentation that I gave in Sicily last year .. I discuss this I-Beam and others, as well as how data is held in a Dyalog workspace from about 17 minutes onwards: http://video.dyalog.com/Dyalog15/?v=4sG3GX0STtI

AndyS|Dyalog

Posts: 235
Joined: Tue May 12, 2009 6:06 pm

Re: type conversions/Conversion Codes

`      ⊢mat ← 2|4 5⍴⍳20    ⍝ matrix of 0s and 1s1 0 1 0 10 1 0 1 01 0 1 0 10 1 0 1 0            ⎕dr mat             ⍝ data represented as 1-bit numbers11      (2 2⌷mat)←'1'       ⍝ insert rogue item            mat                 ⍝ looks OK1 0 1 0 10 1 0 1 01 0 1 0 10 1 0 1 0            ⎕dr mat             ⍝ data represented as 32-bit pointers326      mat∊0 1             ⍝ where's the bad item?1 1 1 1 11 0 1 1 11 1 1 1 11 1 1 1 1`
JohnS|Dyalog

Re: type conversions/Conversion Codes

Thanks to Andy and John I'm on to my next problem: is there an existing workspace that contains functions to write APL matrices out to any standard image file type (.jpg, .bmp, .gif, etc.) and to read such files into APL matrices. I've already written functions to read and write .pbm files, which are hugely wasteful of memory and not that commonly used.

Thanks for the nice welcome to the Dyalog community!
Stu

Posts: 97
Joined: Thu Dec 31, 2015 1:30 am

Re: type conversions/Conversion Codes

Hello Stu, if you want to use WPF there is an example at: http://aplwiki.com/wpfXamlDemo#Fixing_Images

If you want to use Windows Form there is an example at: viewtopic.php?f=18&t=950

Welcome to Dyalog APL

Pierre Gilbert

PGilbert

Posts: 401
Joined: Sun Dec 13, 2009 8:46 pm

Re: type conversions/Conversion Codes

Thanks for the pointers.

These two examples appear to address different issues from the ones I'm having. I'd just like to be able to read in some images in standard formats, do some image processing operations on them, and write them back out. I was hoping somebody could spare me the work of writing a program to deal with the different image formats, but it looks like I'm on my own.

-Stu
Stu

Posts: 97
Joined: Thu Dec 31, 2015 1:30 am

Re: type conversions/Conversion Codes

Tell us what kind of image processing you want to do. Take a color bitmap and convert it to gray scale ? Take a Bitmap and convert it to JPEG ? etc. etc. The choices are endless.

PGilbert

Posts: 401
Joined: Sun Dec 13, 2009 8:46 pm

Re: type conversions/Conversion Codes

I'm working exclusively with binary images (i.e., black and white only). My current problem is simply to have a convenient way to read image files into APL matrices and write APL matrices out to image files, preferably .jpg, .bmp, or .gif. In between the reads and writes, I do a mix of conventional binary image processing (dilation, erosion, fill, etc.) as well as some operations based on the system of Gerard Langlet, who used to be well known within the APL community. As I mentioned in an earlier post, I can read and write .pbm files. But this is an awful format, and I'd like to be able to work with the formats that are used for most serious work. Once I get an image into a matrix, the rest is easy because APL is so well suited to manipulating matrices.
Stu

Posts: 97
Joined: Thu Dec 31, 2015 1:30 am

Re: type conversions/Conversion Codes

Hi Stu,

Dyalog has some built-in support for reading and writing BMP files, via its "Bitmap" object. For example:

create a monochrome bitmap (64x64 of alternate vertical lines) specifying a colormap of black and white pixels:

Code: Select all
`      bits←64 64⍴0 1      'bm'⎕WC'bitmap'('Bits' bits)('Cmap' (2 3⍴0 0 0 255 255 255)) `

tell the bitmap where to be saved:
Code: Select all
`     bm.File←'c:\tmp\image'`

and save it:
Code: Select all
`       2 ⎕NQ'bm' 'FileWrite'`

You can manipulate the bits array as you like, and do the following to update the bitmap, and write it back to the file:
Code: Select all
`       bits←⍉bits       bm.Bits←bits       2 ⎕NQ'bm' 'FileWrite'`

Alternatively you can manipulate bm.Bits directly, but performance may suffer:
Code: Select all
`      bm.Bits←⍉bm.Bits       2 ⎕NQ'bm' 'FileWrite'`

To view the bitmap you can use the builtin Form object:
Code: Select all
`      'f' ⎕wc 'form' ('Picture' 'bm')`

Best Regards,
John Daintreee

JohnD|Dyalog

Posts: 66
Joined: Wed Oct 01, 2008 9:35 am

Re: type conversions/Conversion Codes

Uhm... so why don't you use the APL Bitmap object? You can grab the individual pixels from that with the CBIts property.

To convert between formats, use .net
`      test2;bmp;⎕USING ⎕USING←,⊂'System,system.dll' ⍝ Dunno if all these ⎕USING's are needed ⎕USING,←⊂'System.Drawing,System.Drawing.dll' ⎕USING,←⊂'System.Windows.Forms,System.Windows.Forms.dll' bmp←Bitmap.New⊂'c:\folder\splash.bmp' bmp.Save'c:\folder\tada.bmp'Drawing.Imaging.ImageFormat.Bmp bmp.Dispose 'And .net supports:' Drawing.Imaging.ImageFormat.⎕nl ¯2`

Bmp Emf Exif Gif Icon Jpeg MemoryBmp Png Tiff Wmf

To read the .bmp into APL:
`      bmp←⎕WC 'Bitmap' 'c:\folder\tada.bmp'bits←bmp.CBits⎕EX 'bmp'`

Now you have the pixels in bits variable as 32-bit integers. To extract the color channels:

`      argb←256 256 256 256⊤bits`

argb is a 3-dim matrix, alpha in [1;;], .rgb in [2 3 4;;]

Then convert back to 32-bit integers with 256⊥ and set the bitmap pixels using bmp.CBits←[your converted data].

APL also supprts .png directly, this will give you controlled access to alpha bitmaps have no alpha as u know).

(Didn't test these fns)

Ofc, this is not really the way to go. You should use DirectX for image processing, with for example the TrueVision3D engine. The you can write shaders that do the job on the GPU.

(Edit: Heh, you seem to have got 2 simultaneous answers telling mostly the same)
Tomas Gustafsson

Posts: 91
Joined: Mon Sep 19, 2011 6:43 pm

Next