|
| 1 | +/// <b>Identicon Generator in COS</b><br/> |
| 2 | +/// Author: Andrei Luiz Nenevê - [email protected]<br/> |
| 3 | +/// Requires: Caché 2016.2 or Newer<br/><br/> |
| 4 | +/// Example:<br/> |
| 5 | +/// <example> |
| 6 | +/// ; Parameters=> Word, Directory, Size, Background amount of red, green, blue |
| 7 | +/// Do ##class(Ikon.Identicon).%New("test","C:\Identicons\",250,255,255,155) |
| 8 | +/// </example> |
| 9 | +Class Ikon.Identicon Extends %RegisteredObject |
| 10 | +{ |
| 11 | + |
| 12 | +Property Name As %String; |
| 13 | + |
| 14 | +Property Hash As %DynamicArray; |
| 15 | + |
| 16 | +Property Color As %DynamicArray; |
| 17 | + |
| 18 | +Property Grid As %DynamicArray; |
| 19 | + |
| 20 | +Property GridPoint As %DynamicArray; |
| 21 | + |
| 22 | +Property PixelMap As %DynamicArray; |
| 23 | + |
| 24 | +Property Directory As %String [ InitialExpression = "C:\Identicons\" ]; |
| 25 | + |
| 26 | +Property FileFullName As %String; |
| 27 | + |
| 28 | +Property Size As %Integer [ InitialExpression = 250 ]; |
| 29 | + |
| 30 | +Property BackgroundColor As %String [ InitialExpression = "255,255,255" ]; |
| 31 | + |
| 32 | +Method HashInput() As %Status [ Private ] |
| 33 | +{ |
| 34 | + Set tSC = $$$OK |
| 35 | + Try { |
| 36 | + Set identicon = ##class(%SYSTEM.Encryption).MD5Hash(..Name) |
| 37 | + Set ..Hash = ##class(%DynamicArray).%New() |
| 38 | + For i=1:1:$Length(identicon){ |
| 39 | + $$$THROWONERROR(tSC, ..Hash.%Push($ASCII($Extract(identicon,i)))) |
| 40 | + } |
| 41 | + } Catch tException { |
| 42 | + Set:$$$ISOK(tSC) tSC = tException.AsStatus() |
| 43 | + } |
| 44 | + Quit tSC |
| 45 | +} |
| 46 | + |
| 47 | +Method PickColor() As %Status [ Private ] |
| 48 | +{ |
| 49 | + Set tSC = $$$OK |
| 50 | + Try { |
| 51 | + Set ..Color = ##class(%DynamicArray).%New() |
| 52 | + $$$THROWONERROR(tSC, ..Color.%Push(..Hash.%Get(0))) //Red |
| 53 | + $$$THROWONERROR(tSC, ..Color.%Push(..Hash.%Get(1))) //Green |
| 54 | + $$$THROWONERROR(tSC, ..Color.%Push(..Hash.%Get(2))) //Blue |
| 55 | + } Catch tException { |
| 56 | + Set:$$$ISOK(tSC) tSC = tException.AsStatus() |
| 57 | + } |
| 58 | + Quit tSC |
| 59 | +} |
| 60 | + |
| 61 | +Method BuildGrid() As %Status [ Private ] |
| 62 | +{ |
| 63 | + Set tSC = $$$OK |
| 64 | + Try { |
| 65 | + Set ..Grid = ##class(%DynamicArray).%New() |
| 66 | + For i=0:3:..Hash.%Size(){ |
| 67 | + Quit:(i+3>(..Hash.%Size()-1)) |
| 68 | + Set chunk = ##class(%DynamicArray).%New() |
| 69 | + Set fst = ..Hash.%Get(i), sec = ..Hash.%Get(i+1) |
| 70 | + $$$THROWONERROR(tSC, ..Grid.%Push(fst)) |
| 71 | + $$$THROWONERROR(tSC, ..Grid.%Push(sec)) |
| 72 | + $$$THROWONERROR(tSC, ..Grid.%Push(..Hash.%Get(i+2))) |
| 73 | + $$$THROWONERROR(tSC, ..Grid.%Push(sec)) |
| 74 | + $$$THROWONERROR(tSC, ..Grid.%Push(fst)) |
| 75 | + } |
| 76 | + } Catch tException { |
| 77 | + Set:$$$ISOK(tSC) tSC = tException.AsStatus() |
| 78 | + } |
| 79 | + Quit tSC |
| 80 | +} |
| 81 | + |
| 82 | +Method FilterOddSquares() As %Status [ Private ] |
| 83 | +{ |
| 84 | + Set tSC = $$$OK |
| 85 | + Try { |
| 86 | + Set ..GridPoint = ##class(%DynamicArray).%New() |
| 87 | + Set iter = ..Grid.%GetIterator() |
| 88 | + While iter.%GetNext(.key,.value){ |
| 89 | + If value # 2 = 0{ |
| 90 | + Set obj = ##class(%DynamicObject).%New() |
| 91 | + Set obj.key = key |
| 92 | + Set obj.value = value |
| 93 | + $$$THROWONERROR(tSC, ..GridPoint.%Push(obj)) |
| 94 | + } |
| 95 | + } |
| 96 | + } Catch tException { |
| 97 | + Set:$$$ISOK(tSC) tSC = tException.AsStatus() |
| 98 | + } |
| 99 | + Quit tSC |
| 100 | +} |
| 101 | + |
| 102 | +Method BuildPixelMap() As %Status [ Private ] |
| 103 | +{ |
| 104 | + Set tSC = $$$OK |
| 105 | + Try { |
| 106 | + Set ..PixelMap = ##class(%DynamicArray).%New() |
| 107 | + Set iter = ..GridPoint.%GetIterator() |
| 108 | + While iter.%GetNext(.key,.value){ |
| 109 | + Set horizontal = (value.key # 5) * (..Size/5) |
| 110 | + Set vertical = (value.key \ 5) * (..Size/5) |
| 111 | + |
| 112 | + Set point = ##class(%DynamicObject).%New() |
| 113 | + |
| 114 | + Set cord = ##class(%DynamicObject).%New() |
| 115 | + $$$THROWONERROR(tSC, cord.%Set("x",horizontal)) |
| 116 | + $$$THROWONERROR(tSC, cord.%Set("y",vertical)) |
| 117 | + $$$THROWONERROR(tSC, point.%Set("topLeft",cord)) |
| 118 | + |
| 119 | + Set cord = ##class(%DynamicObject).%New() |
| 120 | + $$$THROWONERROR(tSC, cord.%Set("x",horizontal+(..Size/5))) |
| 121 | + $$$THROWONERROR(tSC, cord.%Set("y",vertical+(..Size/5))) |
| 122 | + $$$THROWONERROR(tSC, point.%Set("bottomRight",cord)) |
| 123 | + |
| 124 | + $$$THROWONERROR(tSC, ..PixelMap.%Push(point)) |
| 125 | + } |
| 126 | + } Catch tException { |
| 127 | + Set:$$$ISOK(tSC) tSC = tException.AsStatus() |
| 128 | + } |
| 129 | + Quit tSC |
| 130 | +} |
| 131 | + |
| 132 | +Method GenerateFileName() As %Status [ Private ] |
| 133 | +{ |
| 134 | + Set tSC = $$$OK |
| 135 | + Try { |
| 136 | + If '##class(%File).DirectoryExists(..Directory) { |
| 137 | + Set tSC = ##class(%File).CreateDirectoryChain(..Directory) |
| 138 | + } |
| 139 | + Set ..FileFullName = ..Directory_..Name_".jpg" |
| 140 | + } Catch tException { |
| 141 | + Set:$$$ISOK(tSC) tSC = tException.AsStatus() |
| 142 | + } |
| 143 | + Quit tSC |
| 144 | +} |
| 145 | + |
| 146 | +Method CreateImage() As %Status [ Private ] |
| 147 | +{ |
| 148 | + Set tSC = $$$OK |
| 149 | + Try { |
| 150 | + Set tBack = $ListFromString(..BackgroundColor,",") |
| 151 | + Set tImage = ##class(Ikon.Processor).New(..Size,..Size,$List(tBack,1),$List(tBack,2),$List(tBack,3)) |
| 152 | + Do tImage.SetPen(..Color.%Get(0),..Color.%Get(1),..Color.%Get(2)) |
| 153 | + Set iter = ..PixelMap.%GetIterator() |
| 154 | + While iter.%GetNext(.key,.value){ |
| 155 | + Set posInicialX = value.topLeft.x |
| 156 | + Set posInicialY = value.topLeft.y |
| 157 | + Set posFinalX = value.bottomRight.x |
| 158 | + Set posFinalY = value.bottomRight.y |
| 159 | + For y=posInicialY:1:posFinalY{ |
| 160 | + For x=posInicialX:1:posFinalX{ |
| 161 | + Do tImage.Plot(x,y) |
| 162 | + } |
| 163 | + } |
| 164 | + } |
| 165 | + s a=..FileFullName |
| 166 | + o a:"WNS":10 e |
| 167 | + u a |
| 168 | + d tImage.WriteBitmap() |
| 169 | + c a |
| 170 | + d tImage.%Close() |
| 171 | + } Catch tException { |
| 172 | + Set:$$$ISOK(tSC) tSC = tException.AsStatus() |
| 173 | + } |
| 174 | + Quit tSC |
| 175 | +} |
| 176 | + |
| 177 | +Method Generate(pName As %String = "Caché", pDirectory As %String = "", pSize = 250, Red As %Integer = 255, Green As %Integer = 255, Blue As %Integer = 255) As %Status |
| 178 | +{ |
| 179 | + Set tSC = $$$OK |
| 180 | + Try { |
| 181 | + Set:pName'="" ..Name = pName |
| 182 | + Set:pDirectory'="" ..Directory = pDirectory |
| 183 | + Set ..BackgroundColor = Red_","_Green_","_Blue |
| 184 | + Set ..Size = pSize |
| 185 | + $$$THROWONERROR(tSC, ..HashInput()) |
| 186 | + $$$THROWONERROR(tSC, ..PickColor()) |
| 187 | + $$$THROWONERROR(tSC, ..BuildGrid()) |
| 188 | + $$$THROWONERROR(tSC, ..FilterOddSquares()) |
| 189 | + $$$THROWONERROR(tSC, ..BuildPixelMap()) |
| 190 | + $$$THROWONERROR(tSC, ..GenerateFileName()) |
| 191 | + $$$THROWONERROR(tSC, ..CreateImage()) |
| 192 | + Write !,"File '"_..Directory_..Name_"' successfully generated!",! |
| 193 | + } Catch tException { |
| 194 | + Set:$$$ISERR(tSC) tSC = tException.AsStatus() |
| 195 | + } |
| 196 | + Quit tSC |
| 197 | +} |
| 198 | + |
| 199 | +Method %OnNew(pName As %String = "", pDirectory As %String = "", pSize = 250, Red As %Integer = 255, Green As %Integer = 255, Blue As %Integer = 255) As %Status [ Private, ServerOnly = 1 ] |
| 200 | +{ |
| 201 | + Set tSC = $$$OK |
| 202 | + Try { |
| 203 | + Set:pName'="" ..Name = pName |
| 204 | + Set:pDirectory'="" ..Directory = pDirectory |
| 205 | + Set ..BackgroundColor = Red_","_Green_","_Blue |
| 206 | + Set ..Size = pSize |
| 207 | + If pName'=""{ |
| 208 | + $$$THROWONERROR(tSC, ..HashInput()) |
| 209 | + $$$THROWONERROR(tSC, ..PickColor()) |
| 210 | + $$$THROWONERROR(tSC, ..BuildGrid()) |
| 211 | + $$$THROWONERROR(tSC, ..FilterOddSquares()) |
| 212 | + $$$THROWONERROR(tSC, ..BuildPixelMap()) |
| 213 | + $$$THROWONERROR(tSC, ..GenerateFileName()) |
| 214 | + $$$THROWONERROR(tSC, ..CreateImage()) |
| 215 | + Write !,"File '"_..Directory_..Name_"' successfully generated!",! |
| 216 | + } |
| 217 | + } Catch tException { |
| 218 | + Set:$$$ISERR(tSC) tSC = tException.AsStatus() |
| 219 | + } |
| 220 | + Quit tSC |
| 221 | +} |
| 222 | + |
| 223 | +} |
| 224 | + |
0 commit comments