Coverage Report

Created: 2026-12-01 06:53

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libvips/libvips/mosaicing/lrmerge.c
Line
Count
Source
1
/* Merge two images left-right.
2
 *
3
 * Copyright: 1990, 1991 N. Dessipris
4
 * Author: N. Dessipris
5
 * Written on: 20/09/1990
6
 * Updated on: 17/04/1991
7
 * 1/6/92: JC
8
 *  - check for difference bug fixed
9
 *  - geometry calculations improved and simplified
10
 *  - small speedups
11
 Kirk Martinez for Sys5 29/4/93
12
 * 7/8/93 JC
13
 *  - ANSIfied
14
 *  - memory leaks fixed, ready for partial v2
15
 *  - now does IM_CODING_LABQ too
16
 * 8/11/93 JC
17
 *  - now propagates both input histories
18
 *  - adds magic lines for global mosaic optimisation
19
 *
20
 *
21
 *
22
 May/1994 Ahmed Abbood
23
 *
24
 *  - Modified to use partials on all IO
25
 *
26
 June/1995 Ahmed Abbood
27
 *
28
 *  - Modified to work with different types of images.
29
 *
30
 * 16/6/95 JC
31
 *  - tidied up a little
32
 *  - added to VIPS!
33
 * 7/9/95 JC
34
 *  - split into two parts: im_lrmerge() and im__lrmerge()
35
 *  - latter called by im_lrmosaic()
36
 *  - just the same as public im_lrmerge(), but adds no history
37
 *  - necessary for im_global_balance()
38
 *  - small bugs fixed
39
 * 10/10/95 JC
40
 *  - better checks that parameters are sensible
41
 * 11/10/95 JC
42
 *  - Kirk spotted what a load of rubbish Ahmed's code is
43
 *  - rewritten - many, many bugs fixed
44
 * 24/1/97 JC
45
 *  - now outputs bounding area of input images, rather than clipping
46
 *  - ignores 0 pixels in blend
47
 *  - small tidies
48
 * 7/2/97 JC
49
 *  - new blend, caching
50
 * 25/2/97 JC
51
 *  - old blend back, much simpler
52
 *  - speed this up at some point if you think of an easy way to do it
53
 * 29/7/97 JC
54
 *  - IM_CODING_LABQ blend now works, was bug in im_wrapone()
55
 *  - small tidies
56
 * 10/1/98 JC
57
 *  - merge LUTs now shared between all running mergers
58
 *  - frees memory explicitly in im__stop_merge, for much better memory
59
 *    use in large mosaics, huge improvement!
60
 * 18/2/98 JC
61
 *  - im_demand_hint() call added
62
 * 19/2/98 JC
63
 *  - now works for any dx/dy by calling im_insert() for bizarre cases
64
 * 26/9/99 JC
65
 *  - ooops, blend lut was wrong! wonder how long that's been broken,
66
 *    since feb97 I guess
67
 * 2/2/01 JC
68
 *  - added tunable max blend width
69
 * 8/3/01 JC
70
 *  - switched to integer arithmetic for integer blends
71
 * 7/11/01 JC
72
 *  - more sophisticated transparency handling
73
 *  - tiny blend speed up
74
 * 19/3/02 JC
75
 *  - move fl cache to main state for better sharing
76
 * 15/8/02 JC
77
 *  - records Xoffset/Yoffset
78
 * 20/6/05
79
 *  - now requires all bands == 0 for transparency (used to just check
80
 *    band 0)
81
 * 24/1/11
82
 *  - gtk-doc
83
 *  - match formats and bands automatically
84
 * 22/5/14
85
 *  - wrap as a class
86
 * 18/6/20 kleisauke
87
 *  - convert to vips8
88
 */
89
90
/*
91
92
  This file is part of VIPS.
93
94
  VIPS is free software; you can redistribute it and/or modify
95
  it under the terms of the GNU Lesser General Public License as published by
96
  the Free Software Foundation; either version 2 of the License, or
97
  (at your option) any later version.
98
99
  This program is distributed in the hope that it will be useful,
100
  but WITHOUT ANY WARRANTY; without even the implied warranty of
101
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
102
  GNU Lesser General Public License for more details.
103
104
  You should have received a copy of the GNU Lesser General Public License
105
  along with this program; if not, write to the Free Software
106
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
107
  02110-1301  USA
108
109
 */
110
111
/*
112
113
  These files are distributed with VIPS - http://www.vips.ecs.soton.ac.uk
114
115
 */
116
117
#ifdef HAVE_CONFIG_H
118
#include <config.h>
119
#endif /*HAVE_CONFIG_H*/
120
#include <glib/gi18n-lib.h>
121
122
#include <stdio.h>
123
#include <stdlib.h>
124
#include <string.h>
125
#include <math.h>
126
#include <limits.h>
127
128
/* Define for debug output.
129
#define DEBUG
130
 */
131
132
#include <vips/vips.h>
133
#include <vips/transform.h>
134
#include <vips/internal.h>
135
136
#include "pmosaicing.h"
137
138
/* Blend luts. Shared between all lr and tb blends.
139
 */
140
double *vips__coef1 = NULL;
141
double *vips__coef2 = NULL;
142
int *vips__icoef1 = NULL;
143
int *vips__icoef2 = NULL;
144
145
/* Create a lut for the merging area. Always BLEND_SIZE entries, we
146
 * scale later when we index it.
147
 */
148
int
149
vips__make_blend_luts(void)
150
0
{
151
0
  int x;
152
153
  /* Already done?
154
   */
155
0
  if (vips__coef1 && vips__coef2)
156
0
    return 0;
157
158
  /* Allocate and fill.
159
   */
160
0
  vips__coef1 = VIPS_ARRAY(NULL, BLEND_SIZE, double);
161
0
  vips__coef2 = VIPS_ARRAY(NULL, BLEND_SIZE, double);
162
0
  vips__icoef1 = VIPS_ARRAY(NULL, BLEND_SIZE, int);
163
0
  vips__icoef2 = VIPS_ARRAY(NULL, BLEND_SIZE, int);
164
0
  if (!vips__coef1 || !vips__coef2 || !vips__icoef1 || !vips__icoef2)
165
0
    return -1;
166
167
0
  for (x = 0; x < BLEND_SIZE; x++) {
168
0
    double a = VIPS_PI * x / (BLEND_SIZE - 1.0);
169
170
0
    vips__coef1[x] = (cos(a) + 1.0) / 2.0;
171
0
    vips__coef2[x] = 1.0 - vips__coef1[x];
172
0
    vips__icoef1[x] = vips__coef1[x] * BLEND_SCALE;
173
0
    vips__icoef2[x] = vips__coef2[x] * BLEND_SCALE;
174
0
  }
175
176
0
  return 0;
177
0
}
178
179
/* Return the position of the first non-zero pel from the left.
180
 */
181
static int
182
find_first(VipsRegion *ir, int *pos, int x, int y, int w)
183
0
{
184
0
  VipsPel *pr = VIPS_REGION_ADDR(ir, x, y);
185
0
  VipsImage *im = ir->im;
186
0
  int ne = w * im->Bands;
187
0
  int i;
188
189
  /* Double the number of bands in a complex.
190
   */
191
0
  if (vips_band_format_iscomplex(im->BandFmt))
192
0
    ne *= 2;
193
194
/* Search for the first non-zero band element from the left edge of the image.
195
 */
196
0
#define lsearch(TYPE) \
197
0
  { \
198
0
    TYPE *p = (TYPE *) pr; \
199
0
\
200
0
    for (i = 0; i < ne; i++) \
201
0
      if (p[i]) \
202
0
        break; \
203
0
  }
204
205
0
  switch (im->BandFmt) {
206
0
  case VIPS_FORMAT_UCHAR:
207
0
    lsearch(unsigned char);
208
0
    break;
209
0
  case VIPS_FORMAT_CHAR:
210
0
    lsearch(signed char);
211
0
    break;
212
0
  case VIPS_FORMAT_USHORT:
213
0
    lsearch(unsigned short);
214
0
    break;
215
0
  case VIPS_FORMAT_SHORT:
216
0
    lsearch(signed short);
217
0
    break;
218
0
  case VIPS_FORMAT_UINT:
219
0
    lsearch(unsigned int);
220
0
    break;
221
0
  case VIPS_FORMAT_INT:
222
0
    lsearch(signed int);
223
0
    break;
224
0
  case VIPS_FORMAT_FLOAT:
225
0
    lsearch(float);
226
0
    break;
227
0
  case VIPS_FORMAT_DOUBLE:
228
0
    lsearch(double);
229
0
    break;
230
0
  case VIPS_FORMAT_COMPLEX:
231
0
    lsearch(float);
232
0
    break;
233
0
  case VIPS_FORMAT_DPCOMPLEX:
234
0
    lsearch(double);
235
0
    break;
236
237
0
  default:
238
0
    g_assert_not_reached();
239
0
    return -1;
240
0
  }
241
242
  /* i is first non-zero band element, we want first non-zero pixel.
243
   */
244
0
  *pos = x + i / im->Bands;
245
246
0
  return 0;
247
0
}
248
249
/* Return the position of the first non-zero pel from the right.
250
 */
251
static int
252
find_last(VipsRegion *ir, int *pos, int x, int y, int w)
253
0
{
254
0
  VipsPel *pr = VIPS_REGION_ADDR(ir, x, y);
255
0
  VipsImage *im = ir->im;
256
0
  int ne = w * im->Bands;
257
0
  int i;
258
259
  /* Double the number of bands in a complex.
260
   */
261
0
  if (vips_band_format_iscomplex(im->BandFmt))
262
0
    ne *= 2;
263
264
/* Search for the first non-zero band element from the right.
265
 */
266
0
#define rsearch(TYPE) \
267
0
  { \
268
0
    TYPE *p = (TYPE *) pr; \
269
0
\
270
0
    for (i = ne - 1; i >= 0; i--) \
271
0
      if (p[i]) \
272
0
        break; \
273
0
  }
274
275
0
  switch (im->BandFmt) {
276
0
  case VIPS_FORMAT_UCHAR:
277
0
    rsearch(unsigned char);
278
0
    break;
279
0
  case VIPS_FORMAT_CHAR:
280
0
    rsearch(signed char);
281
0
    break;
282
0
  case VIPS_FORMAT_USHORT:
283
0
    rsearch(unsigned short);
284
0
    break;
285
0
  case VIPS_FORMAT_SHORT:
286
0
    rsearch(signed short);
287
0
    break;
288
0
  case VIPS_FORMAT_UINT:
289
0
    rsearch(unsigned int);
290
0
    break;
291
0
  case VIPS_FORMAT_INT:
292
0
    rsearch(signed int);
293
0
    break;
294
0
  case VIPS_FORMAT_FLOAT:
295
0
    rsearch(float);
296
0
    break;
297
0
  case VIPS_FORMAT_DOUBLE:
298
0
    rsearch(double);
299
0
    break;
300
0
  case VIPS_FORMAT_COMPLEX:
301
0
    rsearch(float);
302
0
    break;
303
0
  case VIPS_FORMAT_DPCOMPLEX:
304
0
    rsearch(double);
305
0
    break;
306
307
0
  default:
308
0
    vips_error("lrmerge", "%s", _("internal error"));
309
0
    return -1;
310
0
  }
311
312
  /* i is first non-zero band element, we want first non-zero pixel.
313
   */
314
0
  *pos = x + i / im->Bands;
315
316
0
  return 0;
317
0
}
318
319
/* Make sure we have first/last for this area.
320
 */
321
static int
322
make_firstlast(MergeInfo *inf, Overlapping *ovlap, VipsRect *oreg)
323
0
{
324
0
  VipsRegion *rir = inf->rir;
325
0
  VipsRegion *sir = inf->sir;
326
0
  VipsRect rr, sr;
327
0
  int y, yr, ys;
328
0
  int missing;
329
330
  /* We're going to build first/last ... lock it from other generate
331
   * threads. In fact it's harmless if we do get two writers, but we may
332
   * avoid duplicating work.
333
   */
334
0
  g_mutex_lock(&ovlap->fl_lock);
335
336
  /* Do we already have first/last for this area? Bail out if we do.
337
   */
338
0
  missing = 0;
339
0
  for (y = oreg->top; y < VIPS_RECT_BOTTOM(oreg); y++) {
340
0
    const int j = y - ovlap->overlap.top;
341
0
    const int first = ovlap->first[j];
342
343
0
    if (first < 0) {
344
0
      missing = 1;
345
0
      break;
346
0
    }
347
0
  }
348
0
  if (!missing) {
349
    /* No work to do!
350
     */
351
0
    g_mutex_unlock(&ovlap->fl_lock);
352
0
    return 0;
353
0
  }
354
355
  /* Entire width of overlap in ref for scan-lines we want.
356
   */
357
0
  rr.left = ovlap->overlap.left;
358
0
  rr.top = oreg->top;
359
0
  rr.width = ovlap->overlap.width;
360
0
  rr.height = oreg->height;
361
0
  rr.left -= ovlap->rarea.left;
362
0
  rr.top -= ovlap->rarea.top;
363
364
  /* Entire width of overlap in sec for scan-lines we want.
365
   */
366
0
  sr.left = ovlap->overlap.left;
367
0
  sr.top = oreg->top;
368
0
  sr.width = ovlap->overlap.width;
369
0
  sr.height = oreg->height;
370
0
  sr.left -= ovlap->sarea.left;
371
0
  sr.top -= ovlap->sarea.top;
372
373
#ifdef DEBUG
374
  printf("lrmerge: making first/last for areas:\n");
375
  printf("ref: left = %d, top = %d, width = %d, height = %d\n",
376
    rr.left, rr.top, rr.width, rr.height);
377
  printf("sec: left = %d, top = %d, width = %d, height = %d\n",
378
    sr.left, sr.top, sr.width, sr.height);
379
#endif
380
381
  /* Make pixels.
382
   */
383
0
  if (vips_region_prepare(rir, &rr) ||
384
0
    vips_region_prepare(sir, &sr)) {
385
0
    g_mutex_unlock(&ovlap->fl_lock);
386
0
    return -1;
387
0
  }
388
389
  /* Make first/last cache.
390
   */
391
0
  for (y = oreg->top, yr = rr.top, ys = sr.top;
392
0
     y < VIPS_RECT_BOTTOM(oreg); y++, yr++, ys++) {
393
0
    const int j = y - ovlap->overlap.top;
394
0
    int *first = &ovlap->first[j];
395
0
    int *last = &ovlap->last[j];
396
397
    /* Done this line already?
398
     */
399
0
    if (*first < 0) {
400
      /* Search for start/end of overlap on this scan-line.
401
       */
402
0
      if (find_first(sir, first,
403
0
          sr.left, ys, sr.width) ||
404
0
        find_last(rir, last,
405
0
          rr.left, yr, rr.width)) {
406
0
        g_mutex_unlock(&ovlap->fl_lock);
407
0
        return -1;
408
0
      }
409
410
      /* Translate to output space.
411
       */
412
0
      *first += ovlap->sarea.left;
413
0
      *last += ovlap->rarea.left;
414
415
      /* Clip to maximum blend width, if necessary.
416
       */
417
0
      if (ovlap->mwidth >= 0 &&
418
0
        *last - *first > ovlap->mwidth) {
419
0
        int shrinkby = (*last - *first) - ovlap->mwidth;
420
421
0
        *first += shrinkby / 2;
422
0
        *last -= shrinkby / 2;
423
0
      }
424
0
    }
425
0
  }
426
427
0
  g_mutex_unlock(&ovlap->fl_lock);
428
429
0
  return 0;
430
0
}
431
432
/* Test pixel == 0.
433
 */
434
#define TEST_ZERO(TYPE, T, RESULT) \
435
0
  { \
436
0
    TYPE *tt = (T); \
437
0
    int ii; \
438
0
\
439
0
    for (ii = 0; ii < cb; ii++) \
440
0
      if (tt[i + ii]) \
441
0
        break; \
442
0
    if (ii == cb) \
443
0
      (RESULT) = 1; \
444
0
  }
445
446
/* Blend two integer images.
447
 */
448
#define iblend(TYPE, B, IN1, IN2, OUT) \
449
0
  { \
450
0
    TYPE *tr = (TYPE *) (IN1); \
451
0
    TYPE *ts = (TYPE *) (IN2); \
452
0
    TYPE *tq = (TYPE *) (OUT); \
453
0
    const int cb = (B); \
454
0
    const int left = VIPS_CLIP(0, first - oreg->left, oreg->width); \
455
0
    const int right = VIPS_CLIP(left, last - oreg->left, oreg->width); \
456
0
    int ref_zero; \
457
0
    int sec_zero; \
458
0
    int x, b; \
459
0
    int i; \
460
0
\
461
0
    /* Left of the blend area. \
462
0
     */ \
463
0
    for (i = 0, x = 0; x < left; x++) { \
464
0
      ref_zero = 0; \
465
0
      TEST_ZERO(TYPE, tr, ref_zero); \
466
0
      if (!ref_zero) \
467
0
        for (b = 0; b < cb; b++, i++) \
468
0
          tq[i] = tr[i]; \
469
0
      else \
470
0
        for (b = 0; b < cb; b++, i++) \
471
0
          tq[i] = ts[i]; \
472
0
    } \
473
0
\
474
0
    /* In blend area. \
475
0
     */ \
476
0
    for (x = left; x < right; x++) { \
477
0
      ref_zero = 0; \
478
0
      sec_zero = 0; \
479
0
      TEST_ZERO(TYPE, tr, ref_zero); \
480
0
      TEST_ZERO(TYPE, ts, sec_zero); \
481
0
\
482
0
      if (!ref_zero && !sec_zero) { \
483
0
        int inx = ((x + oreg->left - first) << BLEND_SHIFT) / bwidth; \
484
0
        int c1 = vips__icoef1[inx]; \
485
0
        int c2 = vips__icoef2[inx]; \
486
0
\
487
0
        for (b = 0; b < cb; b++, i++) \
488
0
          tq[i] = c1 * tr[i] / BLEND_SCALE + \
489
0
            c2 * ts[i] / BLEND_SCALE; \
490
0
      } \
491
0
      else if (!ref_zero) \
492
0
        for (b = 0; b < cb; b++, i++) \
493
0
          tq[i] = tr[i]; \
494
0
      else \
495
0
        for (b = 0; b < cb; b++, i++) \
496
0
          tq[i] = ts[i]; \
497
0
    } \
498
0
\
499
0
    /* Right of blend. \
500
0
     */ \
501
0
    for (x = right; x < oreg->width; x++) { \
502
0
      sec_zero = 0; \
503
0
      TEST_ZERO(TYPE, ts, sec_zero); \
504
0
      if (!sec_zero) \
505
0
        for (b = 0; b < cb; b++, i++) \
506
0
          tq[i] = ts[i]; \
507
0
      else \
508
0
        for (b = 0; b < cb; b++, i++) \
509
0
          tq[i] = tr[i]; \
510
0
    } \
511
0
  }
512
513
/* Blend two float images.
514
 */
515
#define fblend(TYPE, B, IN1, IN2, OUT) \
516
0
  { \
517
0
    TYPE *tr = (TYPE *) (IN1); \
518
0
    TYPE *ts = (TYPE *) (IN2); \
519
0
    TYPE *tq = (TYPE *) (OUT); \
520
0
    const int cb = (B); \
521
0
    const int left = VIPS_CLIP(0, first - oreg->left, oreg->width); \
522
0
    const int right = VIPS_CLIP(left, last - oreg->left, oreg->width); \
523
0
    int ref_zero; \
524
0
    int sec_zero; \
525
0
    int x, b; \
526
0
    int i; \
527
0
\
528
0
    /* Left of the blend area. \
529
0
     */ \
530
0
    for (i = 0, x = 0; x < left; x++) { \
531
0
      ref_zero = 0; \
532
0
      TEST_ZERO(TYPE, tr, ref_zero); \
533
0
      if (!ref_zero) \
534
0
        for (b = 0; b < cb; b++, i++) \
535
0
          tq[i] = tr[i]; \
536
0
      else \
537
0
        for (b = 0; b < cb; b++, i++) \
538
0
          tq[i] = ts[i]; \
539
0
    } \
540
0
\
541
0
    /* In blend area. \
542
0
     */ \
543
0
    for (x = left; x < right; x++) { \
544
0
      ref_zero = 0; \
545
0
      sec_zero = 0; \
546
0
      TEST_ZERO(TYPE, tr, ref_zero); \
547
0
      TEST_ZERO(TYPE, ts, sec_zero); \
548
0
\
549
0
      if (!ref_zero && !sec_zero) { \
550
0
        int inx = ((x + oreg->left - first) << BLEND_SHIFT) / bwidth; \
551
0
        double c1 = vips__coef1[inx]; \
552
0
        double c2 = vips__coef2[inx]; \
553
0
\
554
0
        for (b = 0; b < cb; b++, i++) \
555
0
          tq[i] = c1 * tr[i] + c2 * ts[i]; \
556
0
      } \
557
0
      else if (!ref_zero) \
558
0
        for (b = 0; b < cb; b++, i++) \
559
0
          tq[i] = tr[i]; \
560
0
      else \
561
0
        for (b = 0; b < cb; b++, i++) \
562
0
          tq[i] = ts[i]; \
563
0
    } \
564
0
\
565
0
    /* Right of blend. \
566
0
     */ \
567
0
    for (x = right; x < oreg->width; x++) { \
568
0
      sec_zero = 0; \
569
0
      TEST_ZERO(TYPE, ts, sec_zero); \
570
0
      if (!sec_zero) \
571
0
        for (b = 0; b < cb; b++, i++) \
572
0
          tq[i] = ts[i]; \
573
0
      else \
574
0
        for (b = 0; b < cb; b++, i++) \
575
0
          tq[i] = tr[i]; \
576
0
    } \
577
0
  }
578
579
/* Left-right blend function for non-labpack images.
580
 */
581
static int
582
lr_blend(VipsRegion *out_region, MergeInfo *inf, Overlapping *ovlap,
583
  VipsRect *oreg)
584
0
{
585
0
  VipsRegion *rir = inf->rir;
586
0
  VipsRegion *sir = inf->sir;
587
0
  VipsImage *im = out_region->im;
588
589
0
  VipsRect prr, psr;
590
0
  int y, yr, ys;
591
592
  /* Make sure we have a complete first/last set for this area.
593
   */
594
0
  if (make_firstlast(inf, ovlap, oreg))
595
0
    return -1;
596
597
  /* Part of rr which we will output.
598
   */
599
0
  prr = *oreg;
600
0
  prr.left -= ovlap->rarea.left;
601
0
  prr.top -= ovlap->rarea.top;
602
603
  /* Part of sr which we will output.
604
   */
605
0
  psr = *oreg;
606
0
  psr.left -= ovlap->sarea.left;
607
0
  psr.top -= ovlap->sarea.top;
608
609
  /* Make pixels.
610
   */
611
0
  if (vips_region_prepare(rir, &prr) ||
612
0
    vips_region_prepare(sir, &psr))
613
0
    return -1;
614
615
  /* Loop down overlap area.
616
   */
617
0
  for (y = oreg->top, yr = prr.top, ys = psr.top;
618
0
     y < VIPS_RECT_BOTTOM(oreg); y++, yr++, ys++) {
619
0
    VipsPel *pr = VIPS_REGION_ADDR(rir, prr.left, yr);
620
0
    VipsPel *ps = VIPS_REGION_ADDR(sir, psr.left, ys);
621
0
    VipsPel *q = VIPS_REGION_ADDR(out_region, oreg->left, y);
622
623
0
    const int j = y - ovlap->overlap.top;
624
0
    const int first = ovlap->first[j];
625
0
    const int last = ovlap->last[j];
626
0
    const int bwidth = last - first;
627
628
0
    switch (im->BandFmt) {
629
0
    case VIPS_FORMAT_UCHAR:
630
0
      iblend(unsigned char, im->Bands, pr, ps, q);
631
0
      break;
632
0
    case VIPS_FORMAT_CHAR:
633
0
      iblend(signed char, im->Bands, pr, ps, q);
634
0
      break;
635
0
    case VIPS_FORMAT_USHORT:
636
0
      iblend(unsigned short, im->Bands, pr, ps, q);
637
0
      break;
638
0
    case VIPS_FORMAT_SHORT:
639
0
      iblend(signed short, im->Bands, pr, ps, q);
640
0
      break;
641
0
    case VIPS_FORMAT_UINT:
642
0
      iblend(unsigned int, im->Bands, pr, ps, q);
643
0
      break;
644
0
    case VIPS_FORMAT_INT:
645
0
      iblend(signed int, im->Bands, pr, ps, q);
646
0
      break;
647
0
    case VIPS_FORMAT_FLOAT:
648
0
      fblend(float, im->Bands, pr, ps, q);
649
0
      break;
650
0
    case VIPS_FORMAT_DOUBLE:
651
0
      fblend(double, im->Bands, pr, ps, q);
652
0
      break;
653
0
    case VIPS_FORMAT_COMPLEX:
654
0
      fblend(float, im->Bands * 2, pr, ps, q);
655
0
      break;
656
0
    case VIPS_FORMAT_DPCOMPLEX:
657
0
      fblend(double, im->Bands * 2, pr, ps, q);
658
0
      break;
659
660
0
    default:
661
0
      g_assert_not_reached();
662
0
      return -1;
663
0
    }
664
0
  }
665
666
0
  return 0;
667
0
}
668
669
/* Left-right blend function for VIPS_CODING_LABQ images.
670
 */
671
static int
672
lr_blend_labpack(VipsRegion *out_region, MergeInfo *inf, Overlapping *ovlap,
673
  VipsRect *oreg)
674
0
{
675
0
  VipsRegion *rir = inf->rir;
676
0
  VipsRegion *sir = inf->sir;
677
0
  VipsRect prr, psr;
678
0
  int y, yr, ys;
679
680
  /* Make sure we have a complete first/last set for this area. This
681
   * will just look at the top 8 bits of L, not all 10, but should be OK.
682
   */
683
0
  if (make_firstlast(inf, ovlap, oreg))
684
0
    return -1;
685
686
  /* Part of rr which we will output.
687
   */
688
0
  prr = *oreg;
689
0
  prr.left -= ovlap->rarea.left;
690
0
  prr.top -= ovlap->rarea.top;
691
692
  /* Part of sr which we will output.
693
   */
694
0
  psr = *oreg;
695
0
  psr.left -= ovlap->sarea.left;
696
0
  psr.top -= ovlap->sarea.top;
697
698
  /* Make pixels.
699
   */
700
0
  if (vips_region_prepare(rir, &prr) ||
701
0
    vips_region_prepare(sir, &psr))
702
0
    return -1;
703
704
  /* Loop down overlap area.
705
   */
706
0
  for (y = oreg->top, yr = prr.top, ys = psr.top;
707
0
     y < VIPS_RECT_BOTTOM(oreg); y++, yr++, ys++) {
708
0
    VipsPel *pr = VIPS_REGION_ADDR(rir, prr.left, yr);
709
0
    VipsPel *ps = VIPS_REGION_ADDR(sir, psr.left, ys);
710
0
    VipsPel *q = VIPS_REGION_ADDR(out_region, oreg->left, y);
711
712
0
    const int j = y - ovlap->overlap.top;
713
0
    const int first = ovlap->first[j];
714
0
    const int last = ovlap->last[j];
715
0
    const int bwidth = last - first;
716
717
0
    float *fq = inf->merge;
718
0
    float *r = inf->from1;
719
0
    float *s = inf->from2;
720
721
    /* Unpack two bits we want.
722
     */
723
0
    vips__LabQ2Lab_vec(r, pr, oreg->width);
724
0
    vips__LabQ2Lab_vec(s, ps, oreg->width);
725
726
    /* Blend as floats.
727
     */
728
0
    fblend(float, 3, r, s, fq);
729
730
    /* Re-pack to output buffer.
731
     */
732
0
    vips__Lab2LabQ_vec(q, inf->merge, oreg->width);
733
0
  }
734
735
0
  return 0;
736
0
}
737
738
static void
739
ovlap_free(VipsImage *image, Overlapping *ovlap)
740
0
{
741
0
  g_mutex_clear(&ovlap->fl_lock);
742
0
}
743
744
/* Build basic per-call state and do some geometry calculations. Shared with
745
 * tbmerge, so not static.
746
 */
747
Overlapping *
748
vips__build_mergestate(const char *domain,
749
  VipsImage *ref, VipsImage *sec, VipsImage *out,
750
  int dx, int dy, int mwidth)
751
0
{
752
0
  VipsImage **t = (VipsImage **)
753
0
    vips_object_local_array(VIPS_OBJECT(out), 4);
754
755
0
  VipsImage **array;
756
0
  Overlapping *ovlap;
757
0
  int x;
758
759
  /* TODO(kleisauke): Copied from vips_insert, perhaps we
760
   * need a separate function for this?
761
   * (just like im__insert_base)
762
   */
763
0
  if (vips_image_pio_input(ref) ||
764
0
    vips_image_pio_input(sec) ||
765
0
    vips_check_bands_1orn(domain, ref, sec) ||
766
0
    vips_check_coding_known(domain, ref) ||
767
0
    vips_check_coding_same(domain, ref, sec))
768
0
    return NULL;
769
770
  /* Cast our input images up to a common format and bands.
771
   */
772
0
  if (vips__formatalike(ref, sec, &t[0], &t[1]) ||
773
0
    vips__bandalike(domain, t[0], t[1], &t[2], &t[3]))
774
0
    return NULL;
775
776
0
  if (!(array = vips_allocate_input_array(out, t[2], t[3], NULL)))
777
0
    return NULL;
778
779
0
  if (vips_image_pipeline_array(out, VIPS_DEMAND_STYLE_SMALLTILE, array))
780
0
    return NULL;
781
782
0
  if (mwidth < -1) {
783
0
    vips_error(domain, "%s", _("mwidth must be -1 or >= 0"));
784
0
    return NULL;
785
0
  }
786
787
0
  if (!(ovlap = VIPS_NEW(out, Overlapping)))
788
0
    return NULL;
789
790
0
  ovlap->ref = array[0];
791
0
  ovlap->sec = array[1];
792
0
  ovlap->out = out;
793
0
  ovlap->dx = dx;
794
0
  ovlap->dy = dy;
795
0
  ovlap->mwidth = mwidth;
796
797
  /* Area occupied by ref image. Place at (0,0) to start with.
798
   */
799
0
  ovlap->rarea.left = 0;
800
0
  ovlap->rarea.top = 0;
801
0
  ovlap->rarea.width = ovlap->ref->Xsize;
802
0
  ovlap->rarea.height = ovlap->ref->Ysize;
803
804
  /* Area occupied by sec image.
805
   */
806
0
  ovlap->sarea.left = -dx;
807
0
  ovlap->sarea.top = -dy;
808
0
  ovlap->sarea.width = ovlap->sec->Xsize;
809
0
  ovlap->sarea.height = ovlap->sec->Ysize;
810
811
  /* Compute overlap.
812
   */
813
0
  vips_rect_intersectrect(&ovlap->rarea, &ovlap->sarea, &ovlap->overlap);
814
0
  if (vips_rect_isempty(&ovlap->overlap)) {
815
0
    vips_error(domain, "%s", _("no overlap"));
816
0
    return NULL;
817
0
  }
818
819
  /* Find position and size of output image.
820
   */
821
0
  vips_rect_unionrect(&ovlap->rarea, &ovlap->sarea, &ovlap->oarea);
822
823
  /* Now: translate everything, so that the output image, not the left
824
   * image, is at (0,0).
825
   */
826
0
  ovlap->rarea.left -= ovlap->oarea.left;
827
0
  ovlap->rarea.top -= ovlap->oarea.top;
828
0
  ovlap->sarea.left -= ovlap->oarea.left;
829
0
  ovlap->sarea.top -= ovlap->oarea.top;
830
0
  ovlap->overlap.left -= ovlap->oarea.left;
831
0
  ovlap->overlap.top -= ovlap->oarea.top;
832
0
  ovlap->oarea.left = 0;
833
0
  ovlap->oarea.top = 0;
834
835
  /* Make sure blend luts are built.
836
   */
837
0
  vips__make_blend_luts();
838
839
  /* Size of first/last cache. Could be either of these ... just pick
840
   * the larger.
841
   */
842
0
  ovlap->flsize = VIPS_MAX(ovlap->overlap.width, ovlap->overlap.height);
843
844
  /* Build first/last cache.
845
   */
846
0
  ovlap->first = VIPS_ARRAY(out, ovlap->flsize, int);
847
0
  ovlap->last = VIPS_ARRAY(out, ovlap->flsize, int);
848
0
  if (!ovlap->first || !ovlap->last)
849
0
    return NULL;
850
0
  for (x = 0; x < ovlap->flsize; x++)
851
0
    ovlap->first[x] = -1;
852
853
0
  g_mutex_init(&ovlap->fl_lock);
854
855
0
  g_signal_connect(out, "close", G_CALLBACK(ovlap_free), ovlap);
856
857
0
  return ovlap;
858
0
}
859
860
/* Build per-call state.
861
 */
862
static Overlapping *
863
build_lrstate(VipsImage *ref, VipsImage *sec, VipsImage *out,
864
  int dx, int dy, int mwidth)
865
0
{
866
0
  Overlapping *ovlap;
867
868
0
  if (!(ovlap = vips__build_mergestate("lrmerge",
869
0
        ref, sec, out, dx, dy, mwidth)))
870
0
    return NULL;
871
872
  /* Select blender.
873
   */
874
0
  switch (ovlap->ref->Coding) {
875
0
  case VIPS_CODING_LABQ:
876
0
    ovlap->blend = lr_blend_labpack;
877
0
    break;
878
879
0
  case VIPS_CODING_NONE:
880
0
    ovlap->blend = lr_blend;
881
0
    break;
882
883
0
  default:
884
0
    vips_error("lrmerge", "%s", _("unknown coding type"));
885
0
    return NULL;
886
0
  }
887
888
  /* Find the parts of output which come just from ref and just from sec.
889
   */
890
0
  ovlap->rpart = ovlap->rarea;
891
0
  ovlap->spart = ovlap->sarea;
892
0
  ovlap->rpart.width -= ovlap->overlap.width;
893
0
  ovlap->spart.left += ovlap->overlap.width;
894
0
  ovlap->spart.width -= ovlap->overlap.width;
895
896
  /* Is there too much overlap? ie. right edge of ref image is greater
897
   * than right edge of sec image, or left > left.
898
   */
899
0
  if (VIPS_RECT_RIGHT(&ovlap->rarea) >
900
0
      VIPS_RECT_RIGHT(&ovlap->sarea) ||
901
0
    ovlap->rarea.left > ovlap->sarea.left) {
902
0
    vips_error("lrmerge", "%s", _("too much overlap"));
903
0
    return NULL;
904
0
  }
905
906
  /* Max number of pixels we may have to blend over.
907
   */
908
0
  ovlap->blsize = ovlap->overlap.width;
909
910
0
  return ovlap;
911
0
}
912
913
/* The area being demanded can be filled using only pels from either the ref
914
 * or the sec images. Attach output to the appropriate part of the input image.
915
 * area is the position that ir->im occupies in the output image.
916
 *
917
 * Shared with tbmerge, so not static.
918
 */
919
int
920
vips__attach_input(VipsRegion *out_region, VipsRegion *ir, VipsRect *area)
921
0
{
922
0
  VipsRect r = out_region->valid;
923
924
  /* Translate to source coordinate space.
925
   */
926
0
  r.left -= area->left;
927
0
  r.top -= area->top;
928
929
  /* Demand input.
930
   */
931
0
  if (vips_region_prepare(ir, &r))
932
0
    return -1;
933
934
  /* Attach or to ir.
935
   */
936
0
  if (vips_region_region(out_region, ir, &out_region->valid, r.left, r.top))
937
0
    return -1;
938
939
0
  return 0;
940
0
}
941
942
/* The area being demanded requires pixels from the ref and sec images. As
943
 * above, but just do a sub-area of the output, and make sure we copy rather
944
 * than just pointer-fiddling. reg is the sub-area of out_region->valid we
945
 * should do.
946
 *
947
 * Shared with tbmerge, so not static.
948
 */
949
int
950
vips__copy_input(VipsRegion *out_region, VipsRegion *ir,
951
  VipsRect *area, VipsRect *reg)
952
0
{
953
0
  VipsRect r = *reg;
954
955
  /* Translate to source coordinate space.
956
   */
957
0
  r.left -= area->left;
958
0
  r.top -= area->top;
959
960
  /* Paint this area of ir into out_region.
961
   */
962
0
  if (vips_region_prepare_to(ir, out_region, &r, reg->left, reg->top))
963
0
    return -1;
964
965
0
  return 0;
966
0
}
967
968
/* Generate function for merge. This is shared between lrmerge and
969
 * tbmerge.
970
 */
971
int
972
vips__merge_gen(VipsRegion *out_region,
973
  void *seq, void *a, void *b, gboolean *stop)
974
0
{
975
0
  MergeInfo *inf = (MergeInfo *) seq;
976
0
  Overlapping *ovlap = (Overlapping *) a;
977
0
  VipsRect *r = &out_region->valid;
978
0
  VipsRect rreg, sreg, oreg;
979
980
  /* Find intersection with overlap, ref and sec parts.
981
   */
982
0
  vips_rect_intersectrect(r, &ovlap->rpart, &rreg);
983
0
  vips_rect_intersectrect(r, &ovlap->spart, &sreg);
984
985
  /* Do easy cases first: can we satisfy this demand with pixels just
986
   * from ref, or just from sec.
987
   */
988
0
  if (vips_rect_equalsrect(r, &rreg)) {
989
0
    if (vips__attach_input(out_region, inf->rir, &ovlap->rarea))
990
0
      return -1;
991
0
  }
992
0
  else if (vips_rect_equalsrect(r, &sreg)) {
993
0
    if (vips__attach_input(out_region, inf->sir, &ovlap->sarea))
994
0
      return -1;
995
0
  }
996
0
  else {
997
    /* Difficult case - do in three stages: black out whole area,
998
     * copy in parts of ref and sec we touch, write blend area.
999
     * This could be sped up somewhat ... we will usually black
1000
     * out far too much, and write to the blend area three times.
1001
     * Upgrade in the future!
1002
     */
1003
1004
    /* Need intersections with whole of left & right, and overlap
1005
     * too.
1006
     */
1007
0
    vips_rect_intersectrect(r, &ovlap->rarea, &rreg);
1008
0
    vips_rect_intersectrect(r, &ovlap->sarea, &sreg);
1009
0
    vips_rect_intersectrect(r, &ovlap->overlap, &oreg);
1010
1011
0
    vips_region_black(out_region);
1012
0
    if (!vips_rect_isempty(&rreg))
1013
0
      if (vips__copy_input(out_region,
1014
0
          inf->rir, &ovlap->rarea, &rreg))
1015
0
        return -1;
1016
0
    if (!vips_rect_isempty(&sreg))
1017
0
      if (vips__copy_input(out_region,
1018
0
          inf->sir, &ovlap->sarea, &sreg))
1019
0
        return -1;
1020
1021
    /* Nasty: inf->rir and inf->sir now point to the same bit of
1022
     * memory (part of out_region), and we've written twice. We
1023
     * need to make sure we get fresh pixels for the blend, so
1024
     * we must invalidate them both. Should maybe add a call to
1025
     * the API for this.
1026
     */
1027
0
    inf->rir->valid.width = inf->sir->valid.width = 0;
1028
1029
    /* Now blat in the blended area.
1030
     */
1031
0
    if (!vips_rect_isempty(&oreg))
1032
0
      if (ovlap->blend(out_region, inf, ovlap, &oreg))
1033
0
        return -1;
1034
0
  }
1035
1036
0
  return 0;
1037
0
}
1038
1039
/* Stop function. Shared with tbmerge. Free explicitly to reduce mem
1040
 * requirements quickly for large mosaics.
1041
 */
1042
int
1043
vips__stop_merge(void *seq, void *a, void *b)
1044
0
{
1045
0
  MergeInfo *inf = (MergeInfo *) seq;
1046
1047
0
  VIPS_UNREF(inf->rir);
1048
0
  VIPS_UNREF(inf->sir);
1049
0
  VIPS_FREE(inf->from1);
1050
0
  VIPS_FREE(inf->from2);
1051
0
  VIPS_FREE(inf->merge);
1052
0
  g_free(inf);
1053
1054
0
  return 0;
1055
0
}
1056
1057
/* Start function. Shared with tbmerge.
1058
 */
1059
void *
1060
vips__start_merge(VipsImage *out, void *a, void *b)
1061
0
{
1062
0
  Overlapping *ovlap = (Overlapping *) a;
1063
0
  MergeInfo *inf;
1064
1065
0
  if (!(inf = VIPS_NEW(NULL, MergeInfo)))
1066
0
    return NULL;
1067
1068
0
  inf->rir = NULL;
1069
0
  inf->sir = NULL;
1070
0
  inf->from1 = NULL;
1071
0
  inf->from2 = NULL;
1072
0
  inf->merge = NULL;
1073
1074
  /* If this is going to be a VIPS_CODING_LABQ, we need VIPS_CODING_LABQ
1075
   * blend buffers.
1076
   */
1077
0
  if (out->Coding == VIPS_CODING_LABQ) {
1078
0
    inf->from1 = VIPS_ARRAY(NULL, ovlap->blsize * 3, float);
1079
0
    inf->from2 = VIPS_ARRAY(NULL, ovlap->blsize * 3, float);
1080
0
    inf->merge = VIPS_ARRAY(NULL, ovlap->blsize * 3, float);
1081
0
    if (!inf->from1 || !inf->from2 || !inf->merge) {
1082
0
      vips__stop_merge(inf, NULL, NULL);
1083
0
      return NULL;
1084
0
    }
1085
0
  }
1086
1087
0
  inf->rir = vips_region_new(ovlap->ref);
1088
0
  inf->sir = vips_region_new(ovlap->sec);
1089
1090
0
  if (!inf->rir || !inf->sir) {
1091
0
    vips__stop_merge(inf, NULL, NULL);
1092
0
    return NULL;
1093
0
  }
1094
1095
0
  return inf;
1096
0
}
1097
1098
int
1099
vips__lrmerge(VipsImage *ref, VipsImage *sec, VipsImage *out,
1100
  int dx, int dy, int mwidth)
1101
0
{
1102
0
  Overlapping *ovlap;
1103
1104
#ifdef DEBUG
1105
  printf("lrmerge %s %s %s %d %d %d\n",
1106
    ref->filename, sec->filename, out->filename,
1107
    dx, dy, mwidth);
1108
  printf("ref is %d x %d pixels\n", ref->Xsize, ref->Ysize);
1109
  printf("sec is %d x %d pixels\n", sec->Xsize, sec->Ysize);
1110
#endif
1111
1112
0
  if (dx > 0 || dx < 1 - ref->Xsize) {
1113
0
    VipsImage *x;
1114
1115
#ifdef DEBUG
1116
    printf("lrmerge: no overlap, using insert\n");
1117
#endif
1118
1119
    /* No overlap, use insert instead.
1120
     */
1121
0
    if (vips_insert(ref, sec, &x, -dx, -dy,
1122
0
        "expand", TRUE,
1123
0
        NULL))
1124
0
      return -1;
1125
0
    if (vips_image_write(x, out)) {
1126
0
      g_object_unref(x);
1127
0
      return -1;
1128
0
    }
1129
0
    g_object_unref(x);
1130
1131
0
    out->Xoffset = -dx;
1132
0
    out->Yoffset = -dy;
1133
1134
0
    return 0;
1135
0
  }
1136
1137
0
  if (!(ovlap = build_lrstate(ref, sec, out, dx, dy, mwidth)))
1138
0
    return -1;
1139
1140
0
  if (vips_image_pipelinev(out,
1141
0
      VIPS_DEMAND_STYLE_THINSTRIP, ovlap->ref, ovlap->sec, NULL))
1142
0
    return -1;
1143
1144
0
  out->Xsize = ovlap->oarea.width;
1145
0
  out->Ysize = ovlap->oarea.height;
1146
0
  out->Xoffset = -dx;
1147
0
  out->Yoffset = -dy;
1148
1149
0
  if (vips_image_generate(out,
1150
0
      vips__start_merge, vips__merge_gen, vips__stop_merge,
1151
0
      ovlap, NULL))
1152
0
    return -1;
1153
1154
0
  return 0;
1155
0
}
1156
1157
const char *
1158
vips__get_mosaic_name(VipsImage *image)
1159
0
{
1160
0
  const char *name;
1161
1162
0
  if (vips_image_get_typeof(image, "mosaic-name")) {
1163
0
    if (vips_image_get_string(image, "mosaic-name", &name))
1164
0
      return NULL;
1165
0
  }
1166
0
  else
1167
0
    name = image->filename;
1168
1169
0
  return name;
1170
0
}
1171
1172
void
1173
vips__add_mosaic_name(VipsImage *image)
1174
0
{
1175
0
  static int global_serial = 0;
1176
1177
  /* TODO(kleisauke): Could we call vips_image_temp_name instead?
1178
   */
1179
0
  int serial = g_atomic_int_add(&global_serial, 1);
1180
1181
0
  char name[256];
1182
1183
  /* We must override any inherited name, so don't test for doesn't
1184
   * exist before setting.
1185
   */
1186
0
  g_snprintf(name, 256, "mosaic-temp-%d", serial);
1187
0
  vips_image_set_string(image, "mosaic-name", name);
1188
0
}