Skip to content

Commit 4fbbfae

Browse files
committed
add Sergey's PR 2 yars ago
1 parent b5a0bf8 commit 4fbbfae

File tree

3 files changed

+309
-3
lines changed

3 files changed

+309
-3
lines changed

module.xml

+2-3
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@
33
<Document name="ikon.ZPM">
44
<Module>
55
<Name>Ikon</Name>
6-
<Version>1.0.1</Version>
6+
<Version>1.0.2</Version>
77
<Description>Identicon generator for Intersystems Caché. Good to use in Mojo applications.</Description>
88
<Packaging>module</Packaging>
99
<SourcesRoot>src</SourcesRoot>
10-
<Resource Name="Ikon.Identicon.CLS"/>
11-
<Resource Name="Ikon.Processor.CLS"/>
10+
<Resource Name="Ikon.PKG"/>
1211
</Module>
1312
</Document>
1413
</Export>

src/cls/Ikon/Identicon.cls

+224
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,224 @@
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+

src/cls/Ikon/Processor.cls

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/// This class is a refactored version from Captcha generator originally made by Fabio Gonçalves
2+
/// <br/>This refactored version to do the generation of identicons was made by Andrei Luiz Nenevê
3+
Class Ikon.Processor Extends %RegisteredObject [ ClassType = "", Not ProcedureBlock ]
4+
{
5+
6+
Property Data [ MultiDimensional ];
7+
8+
Property Width As %Integer;
9+
10+
Property Height As %Integer;
11+
12+
Property PenColor As %String(TRUNCATE = 1);
13+
14+
ClassMethod New(width As %Integer, height As %Integer, red As %Integer = 255, green As %Integer = 255, blue As %Integer = 255) As Ikon.Processor
15+
{
16+
Set img = ##class(Ikon.Processor).%New()
17+
Set img.Width = width,img.Height = height
18+
For x = 1:1:width{
19+
For y = 1:1:height{
20+
Set img.Data(x,y) = $Char(blue,green,red)
21+
}
22+
}
23+
Do img.SetPen(0,0,0)
24+
Quit img
25+
}
26+
27+
Method SetPen(red As %Integer, green As %Integer, blue As %Integer) As %Status
28+
{
29+
Set ..PenColor = $Char(blue,green,red)
30+
Quit $$$OK
31+
}
32+
33+
Method Plot(x As %Integer, y As %Integer) As %Status
34+
{
35+
Quit:(x<1||y<1||y>..Height||x>..Width) 1
36+
Set ..Data(x,y) = ..PenColor
37+
Quit $$$OK
38+
}
39+
40+
ClassMethod SetBytes(decimal As %Integer, length As %Integer) As %String
41+
{
42+
Set ret = ""
43+
For i = 1:1:length{
44+
Set ret = ret_$Char(decimal#256), decimal = decimal/256
45+
}
46+
Quit ret
47+
}
48+
49+
Method WriteBitmap()
50+
{
51+
Set bfType = "BM",
52+
bfSize = 0,
53+
bfReserved1 = ..SetBytes(0,2),
54+
bfReserved2=..SetBytes(0,2),
55+
bfOffsetBits=..SetBytes(54,4),
56+
biSize=..SetBytes(40,4),
57+
biWidth=..SetBytes(..Width,4),
58+
biHeight=..SetBytes(..Height,4),
59+
biPlanes=..SetBytes(1,2),
60+
biBitCount=..SetBytes(24,2),
61+
biCompression=..SetBytes(0,4),
62+
biSizeImage=..SetBytes(0,4),
63+
biXPelsPerMeter=..SetBytes(0,4),
64+
biYPelsPerMeter=..SetBytes(0,4),
65+
biColorsUsed=..SetBytes(0,4),
66+
biColorsImportant=..SetBytes(0,4),
67+
padding=(..Width*3)#4,
68+
padding=$s(padding=0:"",1:$e($c(0,0,0),1,4-padding)),
69+
sizeimage=((..Width*3)+$l(padding))*..Height,
70+
bfSize=..SetBytes(14+40+sizeimage,4),
71+
biSizeImage=..SetBytes(sizeimage,4)
72+
Write bfType_bfSize_bfReserved1_bfReserved2_bfOffsetBits
73+
Write biSize_biWidth_biHeight_biPlanes_biBitCount_biCompression_biSizeImage_biXPelsPerMeter_biYPelsPerMeter_biColorsUsed_biColorsImportant
74+
For y = ..Height:-1:1 {
75+
For x=1:1:..Width {
76+
Write ..Data(x,y)
77+
}
78+
Write padding
79+
}
80+
}
81+
82+
}
83+

0 commit comments

Comments
 (0)