AKTIVE

sparse.tcl at trunk
Login

sparse.tcl at trunk

File etc/generator/virtual/sparse.tcl artifact 35d56c028a on branch trunk


     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
## -*- mode: tcl ; fill-column: 90 -*-
# # ## ### ##### ######## ############# #####################
## Generators -- Virtual image from set of points, deltas, or row ranges.

operator image::from::sparse::points {
    section generator virtual

    example {
	coords {0 0} {4 3} {5 5} {6 2} | ; times 8
    }

    note Returns a single-band image where pixels are set to white at exactly the \
	specified coordinates.

    point... coords  Coordinates of the pixels to set in the image

    note Generally, the bounding box specifies the geometry, especially also the image origin
    note Width is implied by the bounding box of the points
    note Height is implied by the bounding box of the points
    note Depth is fixed at 1
    note Pixel value is fixed at 1.0

    state -setup {
	// Compute the bounding box from the points and use that for the geometry.
	aktive_rectangle bb;
	aktive_point_union (&bb, param->coords.c, param->coords.v);

	aktive_geometry_set_rectangle (domain, &bb);
	domain->depth = 1;
    }
    pixels {
	// First clear the destination area, then iterate over the points and set all
	// which are contained in the request.

	aktive_blit_clear (block, dst);

	aktive_point* base = aktive_rectangle_as_point (request);

	for (aktive_uint i = 0; i < param->coords.c; i++) {
	    #define P param->coords.v[i]
	    TRACE ("check [%d] @(%d,%d)", i, P.x, P.x);
	    if (!aktive_rectangle_contains (request, &P)) continue;

	    // P is in the image domain. Translate it into the request/dst/block domain
	    aktive_point_def_as (rdst, &P);
	    aktive_point_sub (&rdst, base);

	    TRACE ("set", 0);
	    aktive_blit_set (block, &rdst, 1.0);
	    #undef P
        }
    }
}

operator image::from::sparse::deltas {
    section generator virtual

    example {
	width 7 deltas 0 20 5 15 | ; times 8
    }

    note Returns a single-band image where pixels are set to white at exactly the \
	specified points. In contrast to `sparse points` the points are specified \
	as linear distances from the origin.

    note The height is infered from the points

    uint    width   Width of the returned image. This is needed for the conversion \
	of the linear indices to (x,y) coordinates.

    uint... deltas  Linear distances between points to set

    note The first delta is relative to index 0
    note Converts the deltas internally to points and then operates like `sparse points`
    note Depth is fixed at 1
    note Pixel value is fixed at 1.0

    state -fields {
	aktive_point_vector points; // Points computed from the deltas
    } -setup {
	// deltas to points ... this is image state

	aktive_point_vector_new (&state->points, param->deltas.c);

	aktive_uint index = 0;
	for (aktive_uint i = 0; i < param->deltas.c; i++) {
	    index += param->deltas.v[i];
	    state->points.v[i].x = index % param->width;
	    state->points.v[i].y = index / param->width;
	}

	// sparse points ... forced to given width!
	aktive_rectangle bb;
	aktive_point_union (&bb, state->points.c, state->points.v);

	aktive_geometry_set_rectangle (domain, &bb);
	domain->width = param->width;
	domain->depth = 1;
    } -cleanup {
	aktive_point_vector_free (&state->points);
    }
    pixels {
	// First clear the destination area, then iterate over the points and set all
	// which are contained in the request.

	aktive_blit_clear (block, dst);

	aktive_point* base = aktive_rectangle_as_point (request);

	for (aktive_uint i = 0; i < istate->points.c; i++) {
	    #define P istate->points.v[i]
	    TRACE ("check [%d] @(%d,%d)", i, P.x, P.y);
	    if (!aktive_rectangle_contains (request, &P)) continue;

	    // P is in the image domain. Translate it into the request/dst/block domain
	    aktive_point_def_as (rdst, &P);
	    aktive_point_sub (&rdst, base);

	    TRACE ("set", 0);
	    aktive_blit_set (block, &rdst, 1.0);
	    #undef P
        }
    }
}

operator image::from::sparse::ranges {
    section generator virtual

    example {
	ranges {1 24 30 1} {2 23 31 1} {3 22 32 1} {4 22 24 0.75} {4 30 32 0.75} {5 22 23 0.75} {5 31 32 0.75} {6 23 24 0.5} {6 30 31 0.5} {7 24 25 0.5} {7 29 30 0.5} | ; times 8
    }

    note Returns a single-band image where the pixels are set to the specified values as per the provided row ranges.

    note A single row range is specified by 4 numbers.
    note These are, in order, the row index, a range of columns, and the pixel value.
    note The latter is a floating point value, while the others are integers.

    note The bounding box over all provided the ranges specifies the result's geometry, including the origin.
    note The image depth is fixed at 1, i.e. the result is single-band.

    rect? {{0 0 0 0}} geometry	Image geometry. Defaults to the bounding box of the ranges.

    range...          ranges    The ranges to set in the result, and their values.

    state -fields {
	aktive_uint* rowstart; // index of where each row starts in the ranges.
	aktive_uint  isize;    // size of the index
    } -cleanup {
	FREE (state->rowstart);
    } -setup {
	// Either use the provided geometry, or compute the bounding box from the ranges
	// and use the result as the image geometry.
	if (aktive_rectangle_is_empty (&param->geometry)) {
	    aktive_rectangle bb;
	    aktive_range_union (&bb, param->ranges.c, param->ranges.v);
	    aktive_geometry_set_rectangle (domain, &bb);
	} else {
	    aktive_geometry_set_rectangle (domain, &param->geometry);
	}
	domain->depth = 1;

	// pre-sort the ranges for better iteration
	aktive_range_sort (param->ranges.c, param->ranges.v);

	// similarly, compute an index of row starts.

	state->rowstart = NALLOC (aktive_uint, param->ranges.c);
	aktive_uint i, top = 0;
	int row = param->ranges.v[0].y - 1;

	for (i=0; i < param->ranges.c; i++) {
	    if (row == param->ranges.v[i].y) continue;
	    state->rowstart[top] = i;
	    row = param->ranges.v[i].y;
	    top++;
	}

	// Shrink the index to actually used size.
	if (top < param->ranges.c) {
	    state->rowstart = REALLOC (state->rowstart, aktive_uint, top);
	}
	state->isize = top;
    }

    pixels {
	// First clear the destination area, then iterate over the ranges and set all
	// which are contained in the request.

	aktive_blit_clear (block, dst);

	int xmin = aktive_rectangle_get_x    (request);
	int xmax = aktive_rectangle_get_xmax (request);
	int ymin = aktive_rectangle_get_y    (request);
	int ymax = aktive_rectangle_get_ymax (request);

	aktive_point* base = aktive_rectangle_as_point (request);

	// locate start of first row in the request within the param ranges, via the index
	aktive_uint i, k = 0;
	for (i=0; i < istate->isize; i++) {
	    k = istate->rowstart[i];
	    if (param->ranges.v[k].y >= ymin) break;
	}

	// no rows overlapping the request
	if (i >= istate->isize) return;

	// iterate over the ranges until we fall out of the request, row-wise, or run out of ranges
	for ( ; (param->ranges.v[k].y <= ymax) &&
	        (k < param->ranges.c) ; k++) {
	    // Check for each range if they are in the proper x-range as well.

	    int ry    = param->ranges.v[k].y;
	    int rxmin = param->ranges.v[k].xmin;
	    int rxmax = param->ranges.v[k].xmax;

	    if ((rxmax < xmin) || (xmax < rxmin)) continue;
	    // range at least overlaps request

	    // contract to intersection of range and request
	    rxmin  = MAX (rxmin, xmin);
	    rxmax  = MIN (rxmax, xmax);
	    int rw = rxmax - rxmin + 1;

	    // compute blit destination in the request/dst/block domain
	    aktive_rectangle_def (rdst, rxmin, ry, rw, 1);
	    aktive_point_sub (aktive_rectangle_as_point (&rdst), base);

	    // and fill
	    double rvalue = param->ranges.v[k].value;
	    aktive_blit_fill (block, &rdst, rvalue);
	}
    }
}

##
# # ## ### ##### ######## ############# #####################
::return