File: dvec.c

package info (click to toggle)
libpuzzle 0.11-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 2,092 kB
  • sloc: sh: 9,723; ansic: 1,811; makefile: 977; php: 269; sql: 148
file content (587 lines) | stat: -rw-r--r-- 18,149 bytes parent folder | download | duplicates (3)
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
#include "puzzle_common.h"
#include "puzzle_p.h"
#include "puzzle.h"
#include "globals.h"

static void puzzle_init_view(PuzzleView * const view)
{
    view->width = view->height = 0U;
    view->sizeof_map = (size_t) 0U;
    view->map = NULL;
}

static void puzzle_free_view(PuzzleView * const view)
{
    free(view->map);
    view->map = NULL;
}

static void puzzle_init_avglvls(PuzzleAvgLvls * const avglvls)
{
    avglvls->lambdas = 0U;
    avglvls->sizeof_lvls = (size_t) 0U;
    avglvls->lvls = NULL;
}

static void puzzle_free_avglvls(PuzzleAvgLvls * const avglvls)
{
    free(avglvls->lvls);
    avglvls->lvls = NULL;
}

void puzzle_init_dvec(PuzzleContext * const context, PuzzleDvec * const dvec)
{
    (void) context;
    dvec->sizeof_vec = dvec->sizeof_compressed_vec = (size_t) 0U;
    dvec->vec = NULL;
}

void puzzle_free_dvec(PuzzleContext * const context, PuzzleDvec * const dvec)
{
    (void) context;
    free(dvec->vec);
    dvec->vec = NULL;
}

static PuzzleImageTypeCode puzzle_get_image_type_from_fp(FILE * const fp)
{
    #define MAX_SIGNATURE_LENGTH 8U
    static const PuzzleImageType image_types[] = {
        { (size_t) 4U, (const unsigned char *)
            "GIF8", PUZZLE_IMAGE_TYPE_GIF },
        { (size_t) 3U, (const unsigned char *)
            "\xff\xd8\xff", PUZZLE_IMAGE_TYPE_JPEG },
        { (size_t) 8U, (const unsigned char *)
            "\x89PNG\r\n\x1a\n", PUZZLE_IMAGE_TYPE_PNG },
        { (size_t) 0U, NULL, PUZZLE_IMAGE_TYPE_UNKNOWN }
    };
    const PuzzleImageType *image_type = image_types;
    PuzzleImageTypeCode ret = PUZZLE_IMAGE_TYPE_ERROR;
    unsigned char header[MAX_SIGNATURE_LENGTH];
    fpos_t pos;
    
    if (fgetpos(fp, &pos) != 0) {
        return PUZZLE_IMAGE_TYPE_ERROR;
    }
    rewind(fp);
    if (fread(header, (size_t) 1U, sizeof header, fp) != sizeof header) {
        goto bye;
    }
    ret = PUZZLE_IMAGE_TYPE_UNKNOWN;
    do {
        if (image_type->sizeof_signature > sizeof header) {
            puzzle_err_bug(__FILE__, __LINE__);
        }
        if (memcmp(header, image_type->signature,
                   image_type->sizeof_signature) == 0) {
            ret = image_type->image_type_code;
            break;
        }
        image_type++;
    } while (image_type->signature != NULL);
    bye:
    if (fsetpos(fp, &pos) != 0) {
        puzzle_err_bug(__FILE__, __LINE__);
    }    
    return ret;
}

static int puzzle_autocrop_axis(PuzzleContext * const context,
                                PuzzleView * const view,
                                unsigned int * const crop0,
                                unsigned int * const crop1,
                                const unsigned int axisn,
                                const unsigned int axiso,
                                const int omaptrinc, const int nmaptrinc)
{
    double *chunk_contrasts;
    size_t sizeof_chunk_contrasts;
    double chunk_contrast = 0.0, total_contrast = 0.0, barrier_contrast;
    unsigned char level = 0U;
    unsigned char previous_level = 0U;
    unsigned int chunk_n, chunk_o;
    unsigned int chunk_n1, chunk_o1;
    unsigned int max_crop;
    const unsigned char *maptr;

    chunk_n1 = axisn - 1U;
    chunk_o1 = axiso - 1U;
    *crop0 = 0U;
    *crop1 = chunk_n1;
    if (axisn < (unsigned int) PUZZLE_MIN_SIZE_FOR_CROPPING ||
        axiso < (unsigned int) PUZZLE_MIN_SIZE_FOR_CROPPING) {
        return 1;
    }
    sizeof_chunk_contrasts = chunk_n1 + 1U;
    if ((chunk_contrasts = calloc(sizeof_chunk_contrasts,
                                  sizeof *chunk_contrasts)) == NULL) {
        return -1;
    }
    maptr = view->map;
    if (axisn >= INT_MAX || axiso >= INT_MAX) {
        puzzle_err_bug(__FILE__, __LINE__);
    }
    if (INT_MAX / axisn < axiso) {
        puzzle_err_bug(__FILE__, __LINE__);
    }
    chunk_n = chunk_n1;
    do {
        chunk_contrast = 0.0;
        chunk_o = chunk_o1;
        do {
            level = *maptr;
            if (previous_level > level) {
                chunk_contrast += (double) (previous_level - level);
            } else {
                chunk_contrast += (double) (level - previous_level);
            }
            maptr += omaptrinc;
        } while (chunk_o-- != 0U);
        chunk_contrasts[chunk_n] = chunk_contrast;
        total_contrast += chunk_contrast;
        maptr += nmaptrinc;
    } while (chunk_n-- != 0U);
    barrier_contrast =
        total_contrast * context->puzzle_contrast_barrier_for_cropping;
    total_contrast = 0.0;
    *crop0 = 0U;
    do {
        total_contrast += chunk_contrasts[*crop0];
        if (total_contrast >= barrier_contrast) {
            break;
        }
    } while ((*crop0)++ < chunk_n1);
    total_contrast = 0.0;    
    *crop1 = chunk_n1;
    do {
        total_contrast += chunk_contrasts[*crop1];
        if (total_contrast >= barrier_contrast) {
            break;
        }        
    } while ((*crop1)-- > 0U);
    free(chunk_contrasts);
    if (*crop0 > chunk_n1 || *crop1 > chunk_n1) {
        puzzle_err_bug(__FILE__, __LINE__);
    }
    max_crop = (unsigned int)
        round((double) chunk_n1 * context->puzzle_max_cropping_ratio);
    if (max_crop > chunk_n1) {
        puzzle_err_bug(__FILE__, __LINE__);        
    }
    *crop0 = MIN(*crop0, max_crop);
    *crop1 = MAX(*crop1, chunk_n1 - max_crop);

    return 0;
}

static int puzzle_autocrop_view(PuzzleContext * context,
                                PuzzleView * const view)
{
    unsigned int cropx0, cropx1;
    unsigned int cropy0, cropy1;
    unsigned int x, y;
    unsigned char *maptr;

    if (puzzle_autocrop_axis(context, view, &cropx0, &cropx1,
                             view->width, view->height,
                             (int) view->width,
                             1 - (int) (view->width * view->height)) < 0 ||
        puzzle_autocrop_axis(context, view, &cropy0, &cropy1,
                             view->height, view->width,
                             1, 0) < 0) {
        return -1;
    }
    if (cropx0 > cropx1 || cropy0 > cropy1) {
        puzzle_err_bug(__FILE__, __LINE__);
    }
    maptr = view->map;
    y = cropy0;
    do {
        x = cropx0;
        do {
            *maptr++ = PUZZLE_VIEW_PIXEL(view, x, y);
        } while (x++ != cropx1);
    } while (y++ != cropy1);
    view->width = cropx1 - cropx0 + 1U;
    view->height = cropy1 - cropy0 + 1U;
    view->sizeof_map = (size_t) view->width * (size_t) view->height;
    if (view->width <= 0U || view->height <= 0U ||
        SIZE_MAX / view->width < view->height) {
        puzzle_err_bug(__FILE__, __LINE__);
    }
    return 0;
}

static int puzzle_getview_from_gdimage(PuzzleContext * const context,
                                       PuzzleView * const view,
                                       gdImagePtr gdimage)
{
    unsigned int x, y;
    const unsigned int x0 = 0U, y0 = 0U;
    unsigned int x1, y1;
    unsigned char *maptr;
    int pixel;
    
    view->map = NULL;    
    view->width = (unsigned int) gdImageSX(gdimage);
    view->height = (unsigned int) gdImageSY(gdimage);
    view->sizeof_map = (size_t) (view->width * view->height);
    if (view->width > context->puzzle_max_width ||
        view->height > context->puzzle_max_height) {
        return -1;
    }
    if (view->sizeof_map <= (size_t) 0U ||
        INT_MAX / view->width < view->height ||
        SIZE_MAX / view->width < view->height ||
        (unsigned int) view->sizeof_map != view->sizeof_map) {
        puzzle_err_bug(__FILE__, __LINE__);
    }
    x1 = view->width - 1U;
    y1 = view->height - 1U;
    if (view->width <= 0U || view->height <= 0U) {
        puzzle_err_bug(__FILE__, __LINE__);
    }
    if ((view->map = calloc(view->sizeof_map, sizeof *view->map)) == NULL) {
        return -1;
    }
    if (x1 > INT_MAX || y1 > INT_MAX) { /* GD uses "int" for coordinates */
        puzzle_err_bug(__FILE__, __LINE__);
    }
    maptr = view->map;
    x = x1;
    if (gdImageTrueColor(gdimage) != 0) {
        do {
            y = y1;
            do {
                pixel = gdImageGetTrueColorPixel(gdimage, (int) x, (int) y);
                *maptr++ = (unsigned char)
                    ((gdTrueColorGetRed(pixel) * 77 +
                      gdTrueColorGetGreen(pixel) * 151 +
                      gdTrueColorGetBlue(pixel) * 28 + 128) / 256);
            } while (y-- != y0);
        } while (x-- != x0);
    } else {
        do {
            y = y1;
            do {
                pixel = gdImagePalettePixel(gdimage, x, y);
                *maptr++ = (unsigned char)
                    ((gdimage->red[pixel] * 77 +
                      gdimage->green[pixel] * 151 +
                      gdimage->blue[pixel] * 28 + 128) / 256);
            } while (y-- != y0);
        } while (x-- != x0);
    }
    return 0;
}

static double puzzle_softedgedlvl(const PuzzleView * const view,
                                  const unsigned int x, const unsigned int y)
{
    unsigned int lvl = 0U;
    unsigned int ax, ay;
    unsigned int count = 0U;
    const unsigned int xlimit = x + PUZZLE_PIXEL_FUZZ_SIZE;
    const unsigned int ylimit = y + PUZZLE_PIXEL_FUZZ_SIZE;
    if (x >= view->width || y >= view->height || xlimit <= x || ylimit <= y) {
        puzzle_err_bug(__FILE__, __LINE__);
    }
    if (x > PUZZLE_PIXEL_FUZZ_SIZE) {
        ax = x - PUZZLE_PIXEL_FUZZ_SIZE;
    } else {
        ax = 0U;
    }
    do {
        if (ax >= view->width) {
            break;
        }
        if (y > PUZZLE_PIXEL_FUZZ_SIZE) {            
            ay = y - PUZZLE_PIXEL_FUZZ_SIZE;
        } else {
            ay = 0U;
        }
        do {
            if (ay >= view->height) {
                break;
            }
            count++;
            lvl += (unsigned int) PUZZLE_VIEW_PIXEL(view, ax, ay);
        } while (ay++ < ylimit);
    } while (ax++ < xlimit);
    if (count <= 0U) {
        return 0.0;
    }
    return (double) lvl / (double) count;
}

static double puzzle_get_avglvl(const PuzzleView * const view,
                                const unsigned int x, const unsigned int y,
                                const unsigned int width,
                                const unsigned int height)
{
    double lvl = 0.0;
    const unsigned int xlimit = x + width - 1U;
    const unsigned int ylimit = y + height - 1U;
    unsigned int ax, ay;

    if (width <= 0U || height <= 0U) {
        puzzle_err_bug(__FILE__, __LINE__);
    }
    if (xlimit < x || ylimit < y) {
        puzzle_err_bug(__FILE__, __LINE__);
    }
    ax = x;
    do {
        if (ax >= view->width) {
            puzzle_err_bug(__FILE__, __LINE__);
        }
        ay = y;
        do {
            if (ay >= view->height) {
                puzzle_err_bug(__FILE__, __LINE__);
            }
            lvl += puzzle_softedgedlvl(view, ax, ay);
        } while (ay++ < ylimit);
    } while (ax++ < xlimit);
    
    return lvl / (double) (width * height);
}

static int puzzle_fill_avglgls(PuzzleContext * const context,
                               PuzzleAvgLvls * const avglvls,
                               const PuzzleView * const view,
                               const unsigned int lambdas)
{
    double width = (double) view->width;
    double height = (double) view->height;
    double xshift, yshift;
    double x, y;
    unsigned int p;
    unsigned int lx, ly;
    unsigned int xd, yd;
    unsigned int px, py;
    unsigned int lwidth, lheight;
    double avglvl;

    avglvls->lambdas = lambdas;
    avglvls->sizeof_lvls = (size_t) lambdas * lambdas;
    if (UINT_MAX / lambdas < lambdas ||
        (unsigned int) avglvls->sizeof_lvls != avglvls->sizeof_lvls) {
        puzzle_err_bug(__FILE__, __LINE__);
    }
    if ((avglvls->lvls = calloc(avglvls->sizeof_lvls,
                                sizeof *avglvls->lvls)) == NULL) {
        return -1;
    }
    xshift = (width -
              (width * (double) lambdas / (double) SUCC(lambdas))) / 2.0;
    yshift = (height -
              (height * (double) lambdas / (double) SUCC(lambdas))) / 2.0;
    p = (unsigned int) round(MIN(width, height) /
                             (SUCC(lambdas) * context->puzzle_p_ratio));
    if (p < PUZZLE_MIN_P) {
        p = PUZZLE_MIN_P;
    }
    lx = 0U;
    do {
        ly = 0U;
        do {
            x = xshift + (double) lx * PRED(width) / SUCC(lambdas);
            y = yshift + (double) ly * PRED(height) / SUCC(lambdas);
            lwidth = (unsigned int) round
                (xshift + (double) SUCC(lx) * PRED(width) /
                 (double) SUCC(lambdas) - x);
            lheight = (unsigned int) round
                (yshift + (double) SUCC(ly) * PRED(height) /
                 (double) SUCC(lambdas) - y);
            if (p < lwidth) {                
                xd = (unsigned int) round(x + (lwidth - p) / 2.0);
            } else {
                xd = (unsigned int) round(x);
            }
            if (p < lheight) {
                yd = (unsigned int) round(y + (lheight - p) / 2.0);
            } else {
                yd = (unsigned int) round(y);
            }
            if (view->width - xd < p) {
                px = 1U;
            } else {
                px = p;
            }
            if (view->height - yd < p) {
                py = 1U;
            } else {
                py = p;
            }
            if (px > 0U && py > 0U) {
                avglvl = puzzle_get_avglvl(view, xd, yd, px, py);
            } else {
                avglvl = 0.0;
            }
            PUZZLE_AVGLVL(avglvls, lx, ly) = avglvl;
        } while (++ly < lambdas);
    } while (++lx < lambdas);
    
    return 0;
}

static unsigned int puzzle_add_neighbors(double ** const vecur,
                                         const unsigned int max_neighbors,
                                         const PuzzleAvgLvls * const avglvls,
                                         const unsigned int lx,
                                         const unsigned int ly)
{
    unsigned int ax, ay;
    unsigned int xlimit, ylimit;
    unsigned int neighbors = 0U;
    const double ref = PUZZLE_AVGLVL(avglvls, lx, ly);
    
    if (max_neighbors != 8U) {
        puzzle_err_bug(__FILE__, __LINE__);
    }
    if (lx >= avglvls->lambdas - 1U) {
        xlimit = avglvls->lambdas - 1U;
    } else {
        xlimit = lx + 1U;
    }
    if (ly >= avglvls->lambdas - 1U) {
        ylimit = avglvls->lambdas - 1U;
    } else {
        ylimit = ly + 1U;
    }
    if (lx <= 0U) {
        ax = 0U;
    } else {
        ax = lx - 1U;
    }
    do {
        if (ly <= 0U) {
            ay = 0U;
        } else {
            ay = ly - 1U;
        }
        do {
            if (ax == lx && ay == ly) {
                continue;
            }
            *(*vecur)++ = ref - PUZZLE_AVGLVL(avglvls, ax, ay);
            neighbors++;
            if (neighbors <= 0U) {
                puzzle_err_bug(__FILE__, __LINE__);
            }
        } while (ay++ < ylimit);
    } while (ax++ < xlimit);
    if (neighbors > max_neighbors) {
        puzzle_err_bug(__FILE__, __LINE__);
    }    
    return neighbors;
}

static int puzzle_fill_dvec(PuzzleDvec * const dvec,
                            const PuzzleAvgLvls * const avglvls)
{
    unsigned int lambdas;
    unsigned int lx, ly;
    double *vecur;
    
    lambdas = avglvls->lambdas;
    dvec->sizeof_compressed_vec = (size_t) 0U;
    dvec->sizeof_vec = (size_t) (lambdas * lambdas * PUZZLE_NEIGHBORS);
    if (SIZE_MAX /
        ((size_t) (lambdas * lambdas)) < (size_t) PUZZLE_NEIGHBORS ||
        (unsigned int) dvec->sizeof_vec != dvec->sizeof_vec) {
        puzzle_err_bug(__FILE__, __LINE__);
    }
    if ((dvec->vec = calloc(dvec->sizeof_vec, sizeof *dvec->vec)) == NULL) {
        return -1;
    }
    vecur = dvec->vec;
    lx = 0U;
    do {
        ly = 0U;
        do {
            (void) puzzle_add_neighbors(&vecur, PUZZLE_NEIGHBORS,
                                        avglvls, lx, ly);
        } while (++ly < lambdas);
    } while (++lx < lambdas);
    dvec->sizeof_compressed_vec = (size_t) (vecur - dvec->vec);

    return 0;
}

int puzzle_fill_dvec_from_file(PuzzleContext * const context,
                               PuzzleDvec * const dvec,
                               const char * const file)
{
    gdImagePtr gdimage = NULL;
    FILE *fp;
    PuzzleView view;
    PuzzleAvgLvls avglvls;
    PuzzleImageTypeCode image_type_code;
    int ret = 0;

    if (context->magic != PUZZLE_CONTEXT_MAGIC) {
        puzzle_err_bug(__FILE__, __LINE__);
    }
    puzzle_init_view(&view);
    puzzle_init_avglvls(&avglvls);
    puzzle_init_dvec(context, dvec);
    if ((fp = fopen(file, "rb")) == NULL) {
        return -1;
    }
    image_type_code = puzzle_get_image_type_from_fp(fp);
    switch (image_type_code) {
    case PUZZLE_IMAGE_TYPE_JPEG:
        gdimage = gdImageCreateFromJpeg(fp);
        break;
    case PUZZLE_IMAGE_TYPE_PNG:
        gdimage = gdImageCreateFromPng(fp);
        break;
    case PUZZLE_IMAGE_TYPE_GIF:
        gdimage = gdImageCreateFromGif(fp);
        break;
    default:
        gdimage = NULL;
    }
    (void) fclose(fp);
    if (gdimage == NULL) {
        return -1;
    }
    ret = puzzle_getview_from_gdimage(context, &view, gdimage);
    gdImageDestroy(gdimage);
    if (ret != 0) {
        goto out;
    }
    if (context->puzzle_enable_autocrop != 0 &&
        (ret = puzzle_autocrop_view(context, &view)) < 0) {
        goto out;
    }
    if ((ret = puzzle_fill_avglgls(context, &avglvls,
                                   &view, context->puzzle_lambdas)) != 0) {
        goto out;
    }
    ret = puzzle_fill_dvec(dvec, &avglvls);
    out:    
    puzzle_free_view(&view);
    puzzle_free_avglvls(&avglvls);
    
    return ret;
}

int puzzle_dump_dvec(PuzzleContext * const context,
                     const PuzzleDvec * const dvec)
{
    size_t s = dvec->sizeof_compressed_vec;
    const double *vecptr = dvec->vec;

    (void) context;    
    if (s <= (size_t) 0U) {
        puzzle_err_bug(__FILE__, __LINE__);
    }
    do {
        printf("%g\n", *vecptr++);
    } while (--s != (size_t) 0U);
    
    return 0;
}