summaryrefslogtreecommitdiffstats
path: root/dis/Neighborhood/initializeImage.c
diff options
context:
space:
mode:
authorJoshua Bakita <jbakita@cs.unc.edu>2020-10-17 14:43:46 -0400
committerJoshua Bakita <jbakita@cs.unc.edu>2020-10-17 14:43:46 -0400
commita3886552003d031acb9039e920b7c9ddce946ad6 (patch)
treec3b6a400a9ba31e744e6218365d63bdfbbc45a83 /dis/Neighborhood/initializeImage.c
parent917499f6257ac51c05e8302af877d56a22f28cb5 (diff)
DIS fixes used for (rejected) RTSS'20 submission
- All: Output times to stderr and nothing to stdout - Field, Update, Pointer: change definition of a job to match other stressmark execution times more closely - Matrix: move all allocations into main() - Update: Use volatile to prevent computations from being optimized out - Transitive: Use volatile to prevent computations from being optimized out - Neighborhood: Use working version of drawLineSegment from original DIS sample code
Diffstat (limited to 'dis/Neighborhood/initializeImage.c')
-rw-r--r--dis/Neighborhood/initializeImage.c371
1 files changed, 371 insertions, 0 deletions
diff --git a/dis/Neighborhood/initializeImage.c b/dis/Neighborhood/initializeImage.c
new file mode 100644
index 0000000..a030a1a
--- /dev/null
+++ b/dis/Neighborhood/initializeImage.c
@@ -0,0 +1,371 @@
1/* Copyright 2000, Atlantic Aerospace Electronics Corp */
2/*
3 * Name: initializeImage
4 * Input: dimension dimension of image
5 * maxImageValue max legal image value
6 * numberLines number line segments for image
7 * minThickness min thickness for line segment
8 * maxThickness max thickness for line segment
9 * Output: image image initialized
10 * Comment: This routine allocates memory to store an image and fills
11 * in the image structure except for the image data. The
12 * pixels of the image are of type ShortInt. If there
13 * is an error allocating memory, than a NULL pointer is
14 * returned. Assumptions: the image dimensions given are
15 * positive.
16 * Calls: errorMessage()
17 * (system) malloc()
18 * free()
19 * Return: image structure pointer (partially filled in - no data -
20 * and allocated)
21 * NULL (error allocating memory for image/image structure)
22 *
23 * Revision History:
24 * Date Name Revision
25 * --------- ----------- -----------------------------
26 * 25May2000 May Leung Created
27 * 05Jun2000 May Leung Add maxImageValue as input to initializeImage()
28 * XXMay2020 Joshua Bakita Commented out significant portions to allow this to
29 * be #include(d) elsewhere.
30 */
31typedef int Int;
32typedef short int ShortInt;
33typedef float Float;
34void drawRowPoint (Int value,
35 Int pointThickness,
36 Int row,
37 Int column,
38 Image *image);
39void drawColumnPoint (Int value,
40 Int pointThickness,
41 Int row,
42 Int column,
43 Image *image);
44#define MIN_RANDOM_VALUE 0
45#if 0
46#ifndef NDEBUG
47#include <stdio.h>
48#endif /* NDEBUG */
49#include <assert.h> /* define assert() */
50#include <stdlib.h> /* define NULL, malloc(), free() */
51#include "stressmark.h" /* system include file */
52#include "neighborhood.h" /* define types, structures, etc */
53#include "errorMessage.h" /* define errorMessage() */
54#include "random.h" /* define randomUInt() */
55#include "initializeImage.h" /* define initializeImage() */
56
57Image *initializeImage (Int dimension, /* dimension of image */
58 Int maxImageValue, /* max legal image value */
59 Int numberLines, /* number line segments for image */
60 Int minThickness, /* min thickness for line segment */
61 Int maxThickness) /* max thickness for line segment */
62{ /* beginning of initializeImage() */
63
64 Image *image; /* pointer to image structure to allocate */
65 ShortInt *data; /* pointer to the data for the image structure*/
66 Int lineIndex; /* loop index variable for line segments */
67 Int pixelIndex; /* loop index variable for pixels */
68 Int limit; /* limit for loop using pixelIndex */
69
70 Coord startPoint; /* starting point for a line segment */
71 Coord endPoint; /* ending point for a line segment */
72 Int lineThickness; /* thickness for a line segment */
73 Int startValue; /* starting value for a line segment */
74 Int endValue; /* ending value for a line segment */
75
76 /*
77 * Assert the assumptions that the number of columns and number of rows
78 * are positive values. No test on the maximum possible value is done since
79 * numColumns and numRows are of type Int and the maximum value of an Int
80 * type is possible.
81 */
82 assert(dimension > 0);
83 assert(numberLines > 0);
84 assert((minThickness > 0) && (minThickness < dimension));
85 assert((maxThickness > 0) && (maxThickness < dimension));
86 assert(minThickness <= maxThickness);
87
88 /* create image */
89 image = createImage(dimension,dimension,maxImageValue);
90 if (image == NULL) {
91 return(NULL);
92 } /* end error check from createImage */
93
94 /* zero out the image */
95 limit = dimension * dimension;
96 data = image->data;
97 for (pixelIndex = 0; pixelIndex < limit; pixelIndex++) {
98 *data++ = 0;
99 } /* end for loop */
100
101 /*
102 * Populate the image with line segments where the endpoints, values, and
103 * thickness of each line segment are randomly generated.
104 */
105 for (lineIndex = 0; lineIndex < numberLines; lineIndex++) {
106 randomImageLocation(image, &startPoint);
107 randomImageLocation(image, &endPoint);
108 lineThickness = randomUInt(minThickness,maxThickness);
109 startValue = randomUInt(MIN_RANDOM_VALUE, maxImageValue);
110 endValue = randomUInt(MIN_RANDOM_VALUE, maxImageValue);
111
112 /* add line segment to the image */
113 drawLineSegment(&startPoint, &endPoint, startValue, endValue, lineThickness,
114 image);
115
116 } /* end for loop */
117
118 return(image);
119} /* end initializeImage() */
120#endif
121
122/*
123 * drawLineSegment() This routine draws a line segment into the given image
124 * starting at the coordinate startPoint and ending at the
125 * coordinate endPoint. The values along this line segment
126 * vary linearly from the given starting value startValue to
127 * the end value endValue and have a lineThickness associated
128 * to it.
129 *
130 * This routine is modeled after the routine draw_line() from
131 * "Practical Computer Vision using C", by J.R. Parker, Wiley, 1994, pp 114-116.
132 * It has been enhanced to have values on the line segment vary and to have
133 * a thickness to the line segment.
134 */
135void drawLineSegment (Coord *startPoint, /* starting point for line segment */
136 Coord *endPoint, /* ending point for line segment */
137 Int startValue, /* starting value for line segment */
138 Int endValue, /* ending value for line segment */
139 Int lineThickness, /* thickness for line segment */
140 Image *image) /* image to add line segment to */
141{ /* beginning of drawLineSegment() */
142
143 /*
144 * Modeled after draw_line() from "Practical Computer Vision using C",
145 * by J.R. Parker, Wiley, 1994, pp 114-116. Enhanced to have values
146 * on the line segment vary and to have a thickness to the line segment.
147 * The value along the line segment is kept in a float variable so that
148 * fractional increments along the line segment can accumulate to integer
149 * values.
150 */
151 Int changeColumn, changeRow; /* change along line segment in col/row */
152 Int absColumn, absRow; /* absolute value of change in col/row */
153 Int signColumn, signRow; /* sign of change along line segment col/row */
154 Int delta; /* increment alone line segment */
155 Int column, row; /* column & row of coordinate along line seg */
156 Float value, valueDelta; /* value & value change along line segment */
157
158// assert(validImage(image));
159 assert(startPoint != NULL);
160 assert(endPoint != NULL);
161
162#ifdef DEBUG0
163 fprintf(stderr,
164 "drawLineSegment: start row %6ld col %6ld\tend row %6ld col %6ld\n",
165 startPoint->row,startPoint->column,endPoint->row,endPoint->column);
166 fprintf(stderr," startVal %6ld endVal %6ld thickness %6ld\n",
167 startValue, endValue, lineThickness);
168#endif /* DEBUG0 */
169
170 changeColumn = endPoint->column - startPoint->column;
171 if (changeColumn < 0) {
172 absColumn = - changeColumn;
173 signColumn = - 1;
174 } /* end changeColumn < 0 */
175 else { /* changeColumn >= 0 */
176 absColumn = changeColumn;
177 signColumn = 1;
178 } /* end changeColumn >= 0 */
179 absColumn *= 2;
180
181 changeRow = endPoint->row - startPoint->row;
182 if (changeRow < 0) {
183 absRow = - changeRow;
184 signRow = - 1;
185 } /* end changeRow < 0 */
186 else { /* changeRow >= 0 */
187 absRow = changeRow;
188 signRow = 1;
189 } /* end changeRow >= 0 */
190 absRow *= 2;
191
192 column = startPoint->column;
193 row = startPoint->row;
194 value = startValue; /* add changing value to algorithm */
195
196 if (signColumn * changeColumn > signRow * changeRow) { /* column dominant */
197 /* determine the change in value for each step (added to algorithm) */
198 valueDelta = ((Float) endValue - startValue) /
199 ((Float) changeColumn * signColumn);
200
201 delta = absRow - (absColumn / 2);
202 while (TRUE) { /* inifinite loop */
203 /*
204 * Line is column dominant, so line thickness will be along rows. Make
205 * sure the ending value doesn't go beyond endValue. (Added to algorithm)
206 */
207 drawRowPoint((Int) value, lineThickness, row, column, image);
208 if (valueDelta >=0) {
209 value = MIN(endValue, value+valueDelta);
210 } /* values are incrementing */
211 else { /* valueDelta < 0 */
212 value = MAX(endValue, value+valueDelta);
213 } /* values are decreasing */
214
215 if (column == endPoint->column) {
216 return; /* done with line segment exit routine */
217 } /* end exit criteria */
218 if (delta >= 0) {
219 row += signRow;
220 delta -= absColumn;
221 } /* end if delta >= 0 */
222 column += signColumn;
223 delta += absRow;
224 } /* end while infinite loop */
225 } /* end row dominant case */
226 else { /* row dominant case */
227 /* determine the change in value for each step (added to algorithm) */
228 if (changeRow == 0) {
229 valueDelta = 0;
230 assert(changeColumn == 0);
231 } /* end if changeRow == 0 */
232 else {
233 valueDelta = ((Float) endValue - startValue) /
234 ((Float) changeRow * signRow);
235 } /* end else */
236
237 delta = absColumn - (absRow / 2);
238 while (TRUE) { /* inifinite loop */
239 /*
240 * Line is row dominant, so line thickness will be along columns. Make
241 * sure the ending value doesn't go beyond endValue. (Added to algorithm)
242 */
243 drawColumnPoint((Int) value, lineThickness, row, column, image);
244 if (valueDelta >=0) {
245 value = MIN(endValue, value+valueDelta);
246 } /* values are incrementing */
247 else { /* valueDelta < 0 */
248 value = MAX(endValue, value+valueDelta);
249 } /* values are decreasing */
250
251 if (row == endPoint->row) {
252 return; /* done with line segment exit routine */
253 } /* end exit criteria */
254 if (delta >= 0) {
255 column += signColumn;
256 delta -= absRow;
257 } /* end if delta >= 0 */
258 row += signRow;
259 delta += absColumn;
260 } /* end while infinite loop */
261 } /* end column dominant case */
262
263 return;
264} /* end of drawLineSegment() */
265
266/*
267 * drawRowPoint() This routine draws a point with a row thickness onto the
268 * given image for the coordinate and value provided.
269 */
270void drawRowPoint (Int value, /* value for point */
271 Int pointThickness, /* thickness for point */
272 Int row, /* row coordinate of point */
273 Int column, /* column coordinate of point */
274 Image *image) /* image to draw point onto */
275{ /* beginning of drawRowPoint() */
276 Int halfThickness; /* half the point thickness */
277 Int lowerRow, upperRow, rowIndex; /* loop boundaries and index for row */
278 ShortInt *data; /* image data pointer */
279
280// assert(validImage(image));
281 assert(value >= MIN_RANDOM_VALUE);
282 assert(value <= image->maxImageValue);
283 assert(pointThickness > 0);
284
285#ifdef DEBUG0
286 fprintf(stderr,
287 "drawRowPoint: value %5ld, thickness %6ld, row %6ld, column %6ld\n",
288 value,pointThickness,row,column);
289#endif /* DEBUG0 */
290
291 halfThickness = (Int) pointThickness / 2; /* use integer division */
292 lowerRow = MAX(0, row - halfThickness);
293 upperRow = MIN(image->numRows, row + pointThickness - halfThickness);
294
295 data = image->data + (lowerRow * image->numColumns) + column;
296 for (rowIndex = lowerRow; rowIndex < upperRow; rowIndex++) {
297 *data = (ShortInt) value;
298 data += image->numColumns; /* increment pointer to next row */
299 } /* end for loop */
300
301 return;
302} /* end of drawRowPoint() */
303
304/*
305 * drawColumnPoint() This routine draws a point with a column thickness onto the
306 * given image for the coordinate and value provided.
307 */
308void drawColumnPoint (Int value, /* value for point */
309 Int pointThickness, /* thickness for point */
310 Int row, /* row coordinate of point */
311 Int column, /* column coordinate of point */
312 Image *image) /* image to draw point onto */
313{ /* beginning of drawColumnPoint() */
314 Int halfThickness; /* half the point thickness */
315 Int leftColumn, rightColumn, columnIndex; /* col loop boundaries&index*/
316 ShortInt *data; /* image data pointer */
317
318// assert(validImage(image));
319 assert(value >= MIN_RANDOM_VALUE);
320 assert(value <= image->maxImageValue);
321 assert(pointThickness > 0);
322
323#ifdef DEBUG0
324 fprintf(stderr,
325 "drawColumnPoint: value %5ld, thickness %6ld, row %6ld, column %6ld\n",
326 value,pointThickness,row,column);
327#endif /* DEBUG0 */
328
329 halfThickness = (Int) pointThickness / 2; /* use integer division */
330 leftColumn = MAX(0, column - halfThickness);
331 rightColumn = MIN(image->numColumns, column + pointThickness - halfThickness);
332
333 data = image->data + (row * image->numColumns) + leftColumn;
334 for (columnIndex = leftColumn; columnIndex < rightColumn; columnIndex++) {
335 *data = (ShortInt) value;
336 data++; /* increment pointer to next column */
337 } /* end for loop */
338
339 return;
340} /* end of drawColumnPoint() */
341
342/*
343 * randomImageLocation() This routine generates a random coordinate yielding a
344 * coordinate in the given image using randomUInt().
345 */
346#if 0
347#include "random.h"
348
349void randomImageLocation (Image *image, /* image in question */
350 Coord *location) /* random location generated */
351{ /* beginning of randomImageLocation() */
352 Int index; /* random location in image */
353 Int maxIndex; /* maximum index in image */
354
355 assert(validImage(image));
356 assert(location != NULL);
357
358 maxIndex = image->numColumns * image->numRows - 1;
359 index = randomUInt(0, maxIndex);
360
361 /* use integer division to clip to closest integer value */
362 location->row = (Int) index / image->numColumns;
363 location->column = index - (location->row * image->numColumns);
364
365 assert((location->row >= 0) && (location->row < image->numRows));
366 assert((location->column >= 0) && (location->column < image->numColumns));
367 assert(index = location->column + (location->row * image->numColumns));
368
369 return;
370} /* end of randomImageLocation() */
371#endif