/
test008
executable file
·273 lines (240 loc) · 11.3 KB
/
test008
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#!/usr/bin/env ruby
# Copyright (C) 2015 Zukini Ltd.
# Script for testing smig and moving images.
# Please refer to the file LICENSE for the license agreement.
# You are granted the rights to modify this file for your own purposes
# or for distribution to third parties. In either case you accept all
# responsibility for liability.
# This scripts tests drawing paths. After drawing the paths the script compares
# the generated image with a previously generated image.
#
# Draw the same path 3 times, with different props. Use translate to separate.
require 'Open3'
require 'JSON'
require 'tmpdir'
require 'securerandom'
require 'moving_images'
include MovingImages
include MICGDrawing
$errorcode = 0
$filename = File.basename(__FILE__)
$directory = File.expand_path(File.dirname(__FILE__))
$teststring = ""
$compareImageDir = File.join($directory, "images/test008ForComparison")
$compareImages = File.join($directory, "compareimages")
def AreImageFilesSame(filePath1, filePath2, distance=0)
# assume paths already expanded.
# distance is a integer that will need to be a string. Default value
# assumes that we want the difference to be zero.
result, exitVal = Open3.capture2($compareImages, "-file1", filePath1,
"-file2", filePath2, "-distance", distance.to_s)
unless exitVal.exitstatus.zero?
raise "failed to compare files"
end
return result.eql? "SAME"
end
# 1. Create a bitmap context.
# 2. Create a path made up of lines and triangles.
# 3. Stroke the path with a border thickness 10 in black
# 4. Translate the path to a new location.
# 5. Fill the path with the color cyan.
# 6. Translate the path to a new location.
# 7. Stroke and fill the path.
# 8. Create an image exporter with filetype public.jpeg and path
# 9. Generate image from bitmap context and add to image exporter.
# 10. Export from image exporter
# 11. Close base object
def create_path(startPoint)
thePath = MIPath.new
triangle1Points = []
triangle1Points.push(startPoint)
triangle1Points.push(MIShapes.make_point(117.5, 100.5))
triangle1Points.push(MIShapes.make_point(183.5, 50.5))
thePath.add_triangle(points: triangle1Points)
thePath.add_moveto(MIShapes.make_point(50.5, 120.5))
thePath.add_lineto(MIShapes.make_point(183.5, 120.5))
triangle2Points = []
triangle2Points.push(MIShapes.make_point(50.5, 200.5))
triangle2Points.push(MIShapes.make_point(117.5, 150.5))
triangle2Points.push(MIShapes.make_point(183.5, 200.5))
thePath.add_triangle(points: triangle2Points)
thePath.add_moveto(MIShapes.make_point(50.5, 220.5))
thePath.add_lineto(MIShapes.make_point(183.5, 360.5))
triangle3Points = []
triangle3Points.push(MIShapes.make_point(50.5, 380.5))
triangle3Points.push(MIShapes.make_point(117.5, 430.5))
triangle3Points.push(MIShapes.make_point(183.5, 380.5))
thePath.add_triangle(points: triangle3Points)
end
def create_pathdrawelement(pathDrawnAs: "strokepath")
startPoint = MIShapes.make_point(50.5, 50.5)
thePath = create_path(startPoint)
pathDrawElement = MIDrawElement.new(pathDrawnAs)
pathDrawElement.arrayofpathelements = thePath
pathDrawElement.startpoint = startPoint
pathDrawElement.linewidth = 10.0
pathDrawElement.linejoin = 'kCGLineJoinBevel'
pathDrawElement.fillcolor = MIColor.make_rgbacolor(0, 1, 1)
pathDrawElement.strokecolor = MIColor.make_rgbacolor(0,0,0)
pathDrawElement
end
def test01()
baseImageName = "shapepaths.jpg"
begin
# Create the list of commands object, ready to have commands added to it.
theCommands = CommandModule::SmigCommands.new
theCommands.saveresultstype = :lastcommandresult
# 1. Create the bitmap context command
bitmapContextName = SecureRandom.uuid
createBitmapContextCommand = CommandModule.make_createbitmapcontext(
width: 600, height: 450,
name: bitmapContextName)
bitmapContextObject = SmigIDHash.make_objectid(objecttype: :bitmapcontext,
objectname: bitmapContextName)
theCommands.add_command(createBitmapContextCommand)
theCommands.add_tocleanupcommands_closeobject(bitmapContextObject)
# 2. Create a path made up of lines and triangles.
pathDrawElement = create_pathdrawelement(pathDrawnAs: "strokepath")
# 2. Create the draw path command and add it to the list of commands
drawPathCommand = CommandModule.make_drawelement(bitmapContextObject,
drawinstructions: pathDrawElement)
theCommands.add_command(drawPathCommand)
# 2. Now do the first context transformation to move where path is drawn.
# Also draw the path as a fill, rather than stroke.
pathDrawElement = create_pathdrawelement(pathDrawnAs: "fillpath")
contextTransformations = MITransformations.make_contexttransformation()
offset = MIShapes.make_point(190.0, 0.0)
MITransformations.add_translatetransform(contextTransformations, offset)
pathDrawElement.contexttransformations = contextTransformations
drawPathCommand = CommandModule.make_drawelement(bitmapContextObject,
drawinstructions: pathDrawElement)
theCommands.add_command(drawPathCommand)
# 3. Now, create a path which will be filled and stroked.
pathDrawElement = create_pathdrawelement(pathDrawnAs: "fillandstrokepath")
offset = MIShapes.make_point(380.0, 0.0)
contextTransformations = MITransformations.make_contexttransformation()
MITransformations.add_translatetransform(contextTransformations, offset)
pathDrawElement.contexttransformations = contextTransformations
drawPathCommand = CommandModule.make_drawelement(bitmapContextObject,
drawinstructions: pathDrawElement)
theCommands.add_command(drawPathCommand)
tempFile = File.join(Dir.tmpdir(), baseImageName)
# 5. Make the create image exporter object command
imageExporterName = SecureRandom.uuid
createExporterCommand = CommandModule.make_createexporter(tempFile,
export_type: :"public.jpeg",
name: imageExporterName)
exporterObject = SmigIDHash.make_objectid(objecttype: :imageexporter,
objectname: imageExporterName)
theCommands.add_command(createExporterCommand)
theCommands.add_tocleanupcommands_closeobject(exporterObject)
# 6. Make an add image to the exporter object command.
# Add the command to list of commands.
addImageToExporterCommand = CommandModule.make_addimage(
exporterObject, bitmapContextObject)
theCommands.add_command(addImageToExporterCommand)
# 7. Make an export exporter command and add it to list of commands.
exportImageToFileCommand = CommandModule.make_export(exporterObject)
theCommands.add_command(exportImageToFileCommand)
# puts JSON.pretty_generate(theCommands.commandshash)
$teststring = theCommands.commandshash.to_json
theResult = Smig.perform_commands(theCommands)
origFile = File.join($compareImageDir, baseImageName)
# 8. Compare with previously saved image.
unless AreImageFilesSame(origFile, tempFile)
# 9. Report if different.
raise "Different image files: " + origFile + " and " + tempFile
end
rescue RuntimeError => e
$errorcode = Smig.exitvalue
unless $errorcode.zero?
puts "Exit string: " + Smig.exitstring
puts $teststring + " Exit status: " + $errorcode.to_s
end
puts e.message
puts e.backtrace.to_s
# exit 240
ensure
FileUtils.rm_f(tempFile) unless tempFile.nil?
# Open3.capture2("open", tempFile) unless tempFile.nil?
end
end
def test02()
baseImageName = "fillshapewithinnershadow.jpg"
begin
# Create the list of commands object, ready to have commands added to it.
theCommands = CommandModule::SmigCommands.new
theCommands.saveresultstype = :lastcommandresult
# 1. Create the bitmap context command
bitmapContextName = SecureRandom.uuid
createBitmapContextCommand = CommandModule.make_createbitmapcontext(
width: 400, height: 450,
name: bitmapContextName)
bitmapContextObject = SmigIDHash.make_objectid(objecttype: :bitmapcontext,
objectname: bitmapContextName)
theCommands.add_command(createBitmapContextCommand)
theCommands.add_tocleanupcommands_closeobject(bitmapContextObject)
# 2. Create a path made up of lines and triangles.
startPoint = MIShapes.make_point(50.5, 50.5)
thePath = create_path(startPoint)
pathDrawElement = MIDrawFillPathWithInnerShadowElement.new()
pathDrawElement.arrayofpathelements = thePath
pathDrawElement.startpoint = startPoint
pathDrawElement.fillcolor = MIColor.make_rgbacolor(0, 1, 1)
# Add the inner shadow.
innerShadow = MIShadow.new
innerShadow.blur = 10
innerShadow.color = MIColor.make_rgbacolor(1,0,0)
innerShadow.offset = MIShapes.make_size(6, -6)
pathDrawElement.innershadow = innerShadow
# 2. Create the draw path command and add it to the list of commands
drawPathCommand = CommandModule.make_drawelement(bitmapContextObject,
drawinstructions: pathDrawElement)
theCommands.add_command(drawPathCommand)
tempFile = File.join(Dir.tmpdir(), baseImageName)
# tempFile = File.join($compareImageDir, baseImageName)
# 5. Make the create image exporter object command
imageExporterName = SecureRandom.uuid
createExporterCommand = CommandModule.make_createexporter(tempFile,
export_type: :"public.jpeg",
name: imageExporterName)
exporterObject = SmigIDHash.make_objectid(objecttype: :imageexporter,
objectname: imageExporterName)
theCommands.add_command(createExporterCommand)
theCommands.add_tocleanupcommands_closeobject(exporterObject)
# 6. Make an add image to the exporter object command.
# Add the command to list of commands.
addImageToExporterCommand = CommandModule.make_addimage(
exporterObject, bitmapContextObject)
theCommands.add_command(addImageToExporterCommand)
# 7. Make an export exporter command and add it to list of commands.
exportImageToFileCommand = CommandModule.make_export(exporterObject)
theCommands.add_command(exportImageToFileCommand)
theResult = Smig.perform_commands(theCommands)
origFile = File.join($compareImageDir, baseImageName)
# 8. Compare with previously saved image.
unless AreImageFilesSame(origFile, tempFile)
# 9. Report if different.
raise "Different image files: " + origFile + " and " + tempFile
end
rescue RuntimeError => e
$errorcode = Smig.exitvalue
unless $errorcode.zero?
puts "Exit string: " + Smig.exitstring
end
puts e.message
puts e.backtrace.to_s
ensure
FileUtils.rm_f(tempFile) unless tempFile.nil?
# Open3.capture2("open", tempFile) unless tempFile.nil?
# Open3.capture2("open", origFile) unless origFile.nil?
end
end
def LocalTestMain008()
puts "test01"
test01()
puts "test02"
test02()
return $errorcode
end
exit(LocalTestMain008())