├── IMG.apl ├── LICENSE └── README.md /IMG.apl: -------------------------------------------------------------------------------- 1 | ⍝⍝⍝ Base Library section for L2 portability 2 | ∇out←∆ucs input 3 | out←⎕ucs input 4 | ∇ 5 | 6 | ∇out←fclose tn 7 | out←⎕fio[4] tn 8 | ∇ 9 | 10 | ∇tn←fopen_r path 11 | tn← ⎕fio[3] path 12 | ∇ 13 | 14 | ∇tn←fopen_wr path 15 | tn←"wr" ⎕fio[3] path 16 | ∇ 17 | 18 | ∇data←numberbytes fread tn 19 | data←numberbytes ⎕fio[6] tn 20 | ∇ 21 | 22 | ∇out← data fwrite tn 23 | out←data ⎕fio[7] tn 24 | ∇ 25 | ⍝⍝⍝ END Base Library section for L2 portability 26 | 27 | ∇out← om bool 28 | out←bool/⍳⍴bool 29 | ∇ 30 | 31 | (IMG∆BMP∆word_length IMG∆BMP∆dword_length IMG∆BMP∆char_length IMG∆BMP∆byte_length)←2 4 1 1 32 | 33 | ∇dword← num_bytes IMG∆BMP∆data2bytes data 34 | dword← ⌽(num_bytes/256)⊤ data 35 | ∇ 36 | 37 | ∇data← IMG∆BMP∆bytes2data bytes 38 | data←256 ⊥⌽⊃ bytes 39 | ∇ 40 | 41 | ∇filedata ← IMG∆BMP∆read_data_from_path im_path;tie;file_size;z 42 | tie ←fopen_r im_path 43 | file_size ←IMG∆BMP∆bytes2data 2↓6 fread tie 44 | z ←fclose tie 45 | tie ←fopen_r im_path 46 | filedata ←file_size fread tie 47 | z ←fclose tie 48 | ∇ 49 | 50 | ∇metadata←IMG∆BMP∆full_header_to_metadata full_header;IDbits;filesize;reservedbytes;bmp_offset 51 | IDbits ←∆ucs⊃full_header[1] 52 | filesize ←IMG∆BMP∆bytes2data⊃full_header[2] 53 | reservedbytes ←⊃full_header[3] 54 | bmp_offset ←IMG∆BMP∆bytes2data⊃full_header[4] 55 | metadata ←IDbits filesize reservedbytes bmp_offset 56 | ∇ 57 | 58 | ⍝partition-enclose on the head of a vector 59 | ∇out← contents IMG∆BMP∆PE_head pvector 60 | out←pvector⊂contents↑⍨⍴pvector 61 | ∇ 62 | 63 | ∇final_bitmap←IMG∆BMP∆40_read_from_path im_path;contents;bit_lengths;header_size;full_header;IDbits;filesize;reservedbytes;bmp_offset;DIB_full_header;remaining_contents;lh;hw;vw;np;bpp;cm;isz;hr;vr;cip;nic;bytes_perpixel 64 | contents←IMG∆BMP∆read_data_from_path im_path 65 | bit_lengths←2 1 4 1 ×IMG∆BMP∆char_length IMG∆BMP∆dword_length IMG∆BMP∆byte_length IMG∆BMP∆dword_length 66 | header_size←+/bit_lengths 67 | full_header←contents IMG∆BMP∆PE_head om bit_lengths 68 | (IDbits filesize reservedbytes bmp_offset)←IMG∆BMP∆full_header_to_metadata full_header 69 | →('BM'≡IDbits)/L∆SUPPORTEDFILE 70 | →0 ⍝remaining_contents←header_size↓contents 71 | ⍝lh=length_header hw=horiz_width vw=vert_width np=num_planes 72 | ⍝bpp=bits_perpixel cm=compression_method isz=image_size hr=horiz_resolution 73 | ⍝vr=vert_resolution cip=number_colorsinpallette nic=number_importantcolors 74 | L∆SUPPORTEDFILE: 75 | L∆DIB40: 76 | DIB40_format ←3 2 6 /IMG∆BMP∆dword_length IMG∆BMP∆word_length IMG∆BMP∆dword_length 77 | DIB_full_header ←(header_size↓contents) IMG∆BMP∆PE_head om DIB40_format 78 | (lh hw vw np bpp cm isz hr vr cip nic)←IMG∆BMP∆bytes2data¨DIB_full_header 79 | ⍝bytes_perpixel←bpp÷bitsinabyte←8 80 | final_bitmap ←⍉vw hw 3⍴bmp_offset↓contents 81 | ∇ 82 | 83 | ∇HD←IMG∆BMP∆init_header bmp 84 | HD←⍳0 85 | ⍝Always start with 'BM' 86 | HD ←HD,,⌽IMG∆BMP∆char_length IMG∆BMP∆data2bytes ∆ucs 'BM' 87 | 88 | ⍝then the filesize, blank for right now 89 | HD ←HD,, IMG∆BMP∆dword_length IMG∆BMP∆data2bytes 0 90 | 91 | ⍝Four Reserved bytes 92 | HD ←HD,, IMG∆BMP∆byte_length IMG∆BMP∆data2bytes 0 0 0 0 93 | 94 | ⍝bitmap offset. looks like it can default to 54 95 | HD ←HD,, IMG∆BMP∆dword_length IMG∆BMP∆data2bytes offset←54 96 | 97 | ⍝length of bitmap info header. 40 98 | HD ←HD,, IMG∆BMP∆dword_length IMG∆BMP∆data2bytes 40 99 | 100 | ⍝horizontal then vertical widths 101 | HD ←HD,,∈IMG∆BMP∆dword_length IMG∆BMP∆data2bytes¨⌽1↓⍴bmp 102 | 103 | ⍝number of planes. They say its always 1... 104 | HD ←HD,, IMG∆BMP∆word_length IMG∆BMP∆data2bytes 1 105 | 106 | ⍝bits per pixel...24? 107 | HD ←HD,, IMG∆BMP∆word_length IMG∆BMP∆data2bytes 24 108 | ∇ 109 | 110 | ∇sucess← path IMG∆BMP∆bitmap_image_to_file bmp;check;ERR;HD;tie;offset;img 111 | L∆GOODIMAGE: 112 | HD ←(offset←54)↑∈IMG∆BMP∆init_header bmp 113 | img ←HD,∈⍉bmp 114 | img[⎕io+ 2 3 4 5]←⌽,IMG∆BMP∆dword_length IMG∆BMP∆data2bytes ⍴img 115 | sucess←img fwrite tie← fopen_wr path 116 | z←fclose tie 117 | ∇ 118 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | APL Image Utilities: Library for interfacing images with GNU APL 2 | Copyright (C) 2016 Alex Weiner 3 | 4 | This program is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program. If not, see . 16 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # APL-Image-Utilities 2 | Utility functions to get images in and out of (GNU) APL. 3 | 4 | The goal of this library is meant to assist in getting images in and out of APL, and not for processing images within APL.For example, computer vision algorithms and resizing an image are not in scope. 5 | 6 | 7 | Currently only uncompressed BMP3 (Microsoft Windows bitmap image (V3)) format is supported. If you have ImageMagick installed on your machine, The following line will turn any supported image file (here, input.png) into an uncompressed BMP3 file: 8 | 9 | ```convert input.png -compress NONE BMP3:output.bmp``` 10 | 11 | If you don't know if your system supports a specific file type, run: 12 | 13 | ```identify -list format```. 14 | 15 | 16 | # Usage: 17 | There are only two top level functions that an application developer would use right now: 18 | 19 | 20 | ```IMG∆BMP∆40_read_from_path``` and ```IMG∆BMP∆bitmap_image_to_file``` 21 | 22 | ```IMG∆BMP∆40_read_from_path``` takes a path as its right argument and returns a three-dimension array. The ```40``` in the function name is related to the size of the metadata in the uncompressed BMP3 format. 23 | 24 | ```IMG∆BMP∆bitmap_image_to_file``` takes a path as a left argument, and expects a three-dimension array as the right argument. Any matrix that is of size 3×X×Y will work, for example: 25 | 26 | ```'image.bmp ' IMG∆BMP∆bitmap_image_to_file 3 20 30 ⍴ ⍳100``` 27 | --------------------------------------------------------------------------------