- bwrite(fb, &sig->vec, sizeof(sig->vec));
- bputc(fb, sig->len);
- bwrite(fb, sig->reg, sig->len * sizeof(*sig->reg));
-}
-
-struct block {
- u32 l, u, v; /* average Luv coefficients */
- u32 lh, hl, hh; /* energies in Daubechies wavelet bands */
- u32 x, y; /* block position */
- struct block *next;
-};
-
-struct region {
- u32 l, u, v;
- u32 lh, hl, hh;
- u32 sum_l, sum_u, sum_v;
- u32 sum_lh, sum_hl, sum_hh;
- u32 count;
- u64 w_sum;
- struct block *blocks;
-};
-
-static inline uns
-dist(uns a, uns b)
-{
- int d = a - b;
- return d * d;
-}
-
-/* FIXME: SLOW! */
-static uns
-compute_k_means(struct block *blocks, uns blocks_count, struct region *regions, uns regions_count)
-{
- ASSERT(regions_count <= blocks_count);
- struct block *mean[IMAGE_REG_MAX], *b, *blocks_end = blocks + blocks_count;
- struct region *r, *regions_end = regions + regions_count;
-
- /* Select means_count random blocks as initial regions pivots */
- if (regions_count <= blocks_count - regions_count)
- {
- for (b = blocks; b != blocks_end; b++)
- b->next = NULL;
- for (uns i = 0; i < regions_count; )
- {
- uns j = random_max(blocks_count);
- b = blocks + j;
- if (!b->next)
- b->next = mean[i++] = b;
- }
- }
- else
- {
- uns j = blocks_count;
- for (uns i = regions_count; i; j--)
- if (random_max(j) <= i)
- mean[--i] = blocks + j - 1;
- }
- r = regions;
- for (uns i = 0; i < regions_count; i++, r++)
- {
- b = mean[i];
- r->l = b->l;
- r->u = b->u;
- r->v = b->v;
- r->lh = b->lh;
- r->hl = b->hl;
- r->hh = b->hh;
- }
-
- /* Convergation cycle */
- for (uns conv_i = 4; ; conv_i--)
+ struct image *image = data->image;
+ struct image_sig_block *block = data->blocks;
+ uns sum[IMAGE_VEC_F];
+ bzero(sum, sizeof(sum));
+
+ /* Every block of 4x4 pixels */
+ byte *row_start = image->pixels;
+ for (uns block_y = 0; block_y < data->rows; block_y++, row_start += image->row_size * 4)