File: merge.c

package info (click to toggle)
xli 1.17.0-18sarge1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 900 kB
  • ctags: 1,378
  • sloc: ansic: 16,554; makefile: 46
file content (275 lines) | stat: -rw-r--r-- 7,284 bytes parent folder | download | duplicates (9)
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
/* merge.c:
 *
 * merge two images
 *
 * jim frost 09.27.89
 *
 * Copyright 1989, 1990, 1991 Jim Frost.
 * See included file "copyright.h" for complete copyright information.
 */

#include "copyright.h"
#include "xli.h"

/* if merging bitmaps they don't have to be converted to
 * 24-bit.  this saves a lot of space.
 */

static Image *bitmapToBitmap(Image *src, Image *dst, unsigned int atx, unsigned int aty, unsigned int clipw, unsigned int cliph)
{ unsigned int  dstlinelen, srclinelen;
  unsigned int  dststart;
  unsigned int  flip;
  unsigned int  x, y;
  byte         *dstline, *srcline;
  byte          dststartmask;
  byte          dstmask, srcmask;
  byte         *dstpixel, *srcpixel;

  dstlinelen= (dst->width / 8) + (dst->width % 8 ? 1 : 0);
  srclinelen= (src->width / 8) + (src->width % 8 ? 1 : 0);
  dstline= dst->data + (aty * dstlinelen);
  srcline= src->data;
  dststart= atx / 8;
  dststartmask= 0x80 >> (atx % 8);
  flip= ((*dst->rgb.red == *(src->rgb.red + 1)) &&
	 (*dst->rgb.green == *(src->rgb.green + 1)) &&
	 (*dst->rgb.blue == *(src->rgb.blue + 1)));
  for (y= 0; y < cliph; y++) {
    dstpixel= dstline + dststart;
    srcpixel= srcline;
    dstmask= dststartmask;
    srcmask= 0x80;
    for (x= 0; x < clipw; x++) {
      if (flip)
	if (*srcpixel & srcmask)
	  *dstpixel &= ~dstmask;
	else
	  *dstpixel |= dstmask;
      else
	if (*srcpixel & srcmask)
	  *dstpixel |= dstmask;
	else
	  *dstpixel &= ~dstmask;
      dstmask >>= 1;
      srcmask >>= 1;
      if (dstmask == 0) {
	dstmask= 0x80;
	dstpixel++;
      }
      if (srcmask == 0) {
	srcmask= 0x80;
	srcpixel++;
      }
    }
    dstline += dstlinelen;
    srcline += srclinelen;
  }
  if (globals.verbose)
    printf("done\n");
  return(dst);
}

#ifndef FABS
#define FABS(x) ((x) < 0.0 ? -(x) : (x))
#endif

/* merge any to true */
static Image *anyToTrue(Image *src, Image *dst,
	unsigned int atx, unsigned int aty,
	unsigned int clipw, unsigned int cliph)
{ Pixel         fg, bg;
  unsigned int  dstlinelen, srclinelen;
  unsigned int  dststart;
  unsigned int  x, y;
  byte         *dstline, *srcline;
  byte         *dstpixel, *srcpixel;
  byte          srcmask;
  Pixel         pixval;

  /* The gamma of our two images just need to be equal. */
  /* Move towards our output gamma to minimize gamma changes */
  if (GAMMA_NOT_EQUAL(src->gamma, dst->gamma)) {
    if (FABS(src->gamma-globals.display_gamma) > FABS(dst->gamma-globals.display_gamma))
      gammacorrect(src, dst->gamma, globals.verbose);
    else
      gammacorrect(dst, src->gamma, globals.verbose);
  }

  switch (src->type) {
  case IBITMAP:
    fg= RGB_TO_TRUE(src->rgb.red[1], src->rgb.green[1], src->rgb.blue[1]);
    bg= RGB_TO_TRUE(src->rgb.red[0], src->rgb.green[0], src->rgb.blue[0]);
    dstlinelen= dst->width * dst->pixlen;
    srclinelen= (src->width / 8) + (src->width % 8 ? 1 : 0);
    dstline= dst->data + (aty * dstlinelen);
    srcline= src->data;
    dststart= atx * dst->pixlen;
    for (y= 0; y < cliph; y++) {
      dstpixel= dstline + dststart;
      srcpixel= srcline;
      srcmask= 0x80;
      if(dst->pixlen == 3)	/* the ususal case */
        for (x= 0; x < clipw; x++) {
          valToMem((*srcpixel & srcmask ? fg : bg), dstpixel, 3);
          dstpixel += 3;
          srcmask >>= 1;
          if (srcmask == 0) {
            srcpixel++;
            srcmask= 0x80;
          }
        }
      else	/* less ususal case */
        for (x= 0; x < clipw; x++) {
          valToMem((*srcpixel & srcmask ? fg : bg), dstpixel, dst->pixlen);
          dstpixel += dst->pixlen;
          srcmask >>= 1;
          if (srcmask == 0) {
            srcpixel++;
            srcmask= 0x80;
          }
        }
      dstline += dstlinelen;
      srcline += srclinelen;
    }
    break;
  
  case IRGB:
    dstlinelen= dst->width * dst->pixlen;
    srclinelen= src->width * src->pixlen;
    dststart= atx * dst->pixlen;
    dstline= dst->data + (aty * dstlinelen);
    srcline= src->data;

    if(src->pixlen == 1)	/* the usual case */
      for (y= 0; y < cliph; y++) {
        dstpixel= dstline + dststart;
        srcpixel= srcline;
        for (x= 0; x < clipw; x++) {
          pixval= memToVal(srcpixel, 1);
          *(dstpixel++)= src->rgb.red[pixval] >> 8;
          *(dstpixel++)= src->rgb.green[pixval] >> 8;
          *(dstpixel++)= src->rgb.blue[pixval] >> 8;
          srcpixel += 1;
        }
        dstline += dstlinelen;
        srcline += srclinelen;
      }
    else	/* the less ususal */
      for (y= 0; y < cliph; y++) {
        dstpixel= dstline + dststart;
        srcpixel= srcline;
        for (x= 0; x < clipw; x++) {
          pixval= memToVal(srcpixel, src->pixlen);
          *(dstpixel++)= src->rgb.red[pixval] >> 8;
          *(dstpixel++)= src->rgb.green[pixval] >> 8;
          *(dstpixel++)= src->rgb.blue[pixval] >> 8;
          srcpixel += src->pixlen;
        }
        dstline += dstlinelen;
        srcline += srclinelen;
      }
    break;

  case ITRUE:
    dstlinelen= dst->width * dst->pixlen;
    srclinelen= src->width * src->pixlen;
    dststart= atx * dst->pixlen;
    dstline= dst->data + (aty * dstlinelen);
    srcline= src->data;

    for (y= 0; y < cliph; y++) {
      dstpixel= dstline + dststart;
      srcpixel= srcline;
      for (x= 0; x < clipw; x++) {
	*(dstpixel++)= *(srcpixel++);
	*(dstpixel++)= *(srcpixel++);
	*(dstpixel++)= *(srcpixel++);
      }
      dstline += dstlinelen;
      srcline += srclinelen;
    }
    break;
  }
  if (globals.verbose)
    printf("done\n");
  return(dst);
}

/* put src image on dst image
 */

Image *merge(Image *idst, Image *isrc, int atx, int aty, ImageOptions *imgopp)
{ int clipw, cliph;
  Image *src = isrc,*dst = idst,*outimage;

  if (globals.verbose) {
    printf("  Merging...");
    fflush(stdout);
  }

  /* adjust clipping of src to fit within dst
   */

  clipw= src->width;
  cliph= src->height;
  if ((atx + clipw < 0) || (aty + cliph < 0) ||
      (atx >= (int)dst->width) ||
      (aty >= (int)dst->height)) /* not on dst, ignore */
    return dst;

  if (atx + clipw > dst->width)
    clipw = dst->width - atx;
  if (aty + cliph > dst->height)
    cliph = dst->height - aty;

  /* extra clipping required for negative offsets
   */

  if ( atx < 0 || aty < 0 ) {
    int clipx, clipy;
    Image *tmp;
 
    if ( atx < 0 ) {
      clipx = -atx;
      clipw += atx;
      atx = 0;
    }
    else
      clipx = 0;
    
    if ( aty < 0 ) {
      clipy = -aty;
      cliph += aty;
      aty = 0;
    }
    else
      clipy = 0;
    
    tmp = clip(src, clipx, clipy, clipw, cliph, imgopp);
    if (src != tmp && src != isrc)	/* free imtermediate, but preserve input */
      freeImage(src);
    src = tmp;
  }
 
  if (BITMAPP(dst) && BITMAPP(src)) {
    outimage= bitmapToBitmap(src, dst, (unsigned int)atx, (unsigned int)aty,
			     clipw, cliph);
  } else {
    if (!TRUEP(dst)) {		/* convert to true */
      Image *tmp;
      tmp = expandtotrue(dst);
      if (dst != tmp && dst != idst)
	freeImage(dst);
     dst = tmp;
    }
    outimage = anyToTrue(src, dst, (unsigned int)atx, (unsigned int)aty,
			  clipw, cliph);
  }
  /* do the right thing */
  if (dst != outimage && dst != idst)
    freeImage(dst);
  if (src != outimage && src != isrc)
    freeImage(src);

  return(outimage);
}