File: merge.c

package info (click to toggle)
xloadimage 4.1-25
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 4,820 kB
  • sloc: ansic: 36,084; asm: 284; makefile: 282; sh: 280
file content (274 lines) | stat: -rw-r--r-- 6,694 bytes parent folder | download | duplicates (4)
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
/* 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 "image.h"

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

static Image *bitmapToBitmap(src, dest, atx, aty, clipw, cliph, verbose)
     Image        *src, *dest;
     unsigned int  atx, aty, clipw, cliph;
     unsigned int  verbose;
{ unsigned int  destlinelen, srclinelen;
  unsigned int  deststart;
  unsigned int  flip;
  unsigned int  x, y;
  byte         *destline, *srcline;
  byte          deststartmask;
  byte          destmask, srcmask;
  byte         *destpixel, *srcpixel;

  destlinelen= (dest->width / 8) + (dest->width % 8 ? 1 : 0);
  srclinelen= (src->width / 8) + (src->width % 8 ? 1 : 0);
  destline= dest->data + (aty * destlinelen);
  srcline= src->data;
  deststart= atx / 8;
  deststartmask= 0x80 >> (atx % 8);
  flip= ((*dest->rgb.red == *(src->rgb.red + 1)) &&
	 (*dest->rgb.green == *(src->rgb.green + 1)) &&
	 (*dest->rgb.blue == *(src->rgb.blue + 1)));
  for (y= 0; y < cliph; y++) {
    destpixel= destline + deststart;
    srcpixel= srcline;
    destmask= deststartmask;
    srcmask= 0x80;
    for (x= 0; x < clipw; x++) {
      if (flip)
	if (*srcpixel & srcmask)
	  *destpixel &= ~destmask;
	else
	  *destpixel |= destmask;
      else
	if (*srcpixel & srcmask)
	  *destpixel |= destmask;
	else
	  *destpixel &= ~destmask;
      destmask >>= 1;
      srcmask >>= 1;
      if (destmask == 0) {
	destmask= 0x80;
	destpixel++;
      }
      if (srcmask == 0) {
	srcmask= 0x80;
	srcpixel++;
      }
    }
    destline += destlinelen;
    srcline += srclinelen;
  }
  if (verbose)
    printf("done\n");
  return(dest);
}

static Image *anyToTrue(src, dest, atx, aty, clipw, cliph, verbose)
     Image        *src, *dest;
     unsigned int  atx, aty, clipw, cliph;
     unsigned int  verbose;
{ Pixel         fg, bg;
  unsigned int  destlinelen, srclinelen;
  unsigned int  deststart;
  unsigned int  x, y;
  byte         *destline, *srcline;
  byte         *destpixel, *srcpixel;
  byte          srcmask;
  Pixel         pixval;

  if (!TRUEP(dest))
    dest= expand(dest);

  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]);
    destlinelen= dest->width * dest->pixlen;
    srclinelen= (src->width / 8) + (src->width % 8 ? 1 : 0);
    destline= dest->data + (aty * destlinelen);
    srcline= src->data;
    deststart= atx * dest->pixlen;
    for (y= 0; y < cliph; y++) {
      destpixel= destline + deststart;
      srcpixel= srcline;
      srcmask= 0x80;
      for (x= 0; x < clipw; x++) {
	valToMem((*srcpixel & srcmask ? fg : bg), destpixel, 3);
	destpixel += 3;
	srcmask >>= 1;
	if (srcmask == 0) {
	  srcpixel++;
	  srcmask= 0x80;
	}
      }
      destline += destlinelen;
      srcline += srclinelen;
    }
    break;
  
  case IRGB:
    destlinelen= dest->width * dest->pixlen;
    srclinelen= src->width * src->pixlen;
    deststart= atx * dest->pixlen;
    destline= dest->data + (aty * destlinelen);
    srcline= src->data;

    for (y= 0; y < cliph; y++) {
      destpixel= destline + deststart;
      srcpixel= srcline;
      for (x= 0; x < clipw; x++) {
	pixval= memToVal(srcpixel, src->pixlen);
	*(destpixel++)= src->rgb.red[pixval] >> 8;
	*(destpixel++)= src->rgb.green[pixval] >> 8;
	*(destpixel++)= src->rgb.blue[pixval] >> 8;
	srcpixel += src->pixlen;
      }
      destline += destlinelen;
      srcline += srclinelen;
    }
    break;

  case ITRUE:
    destlinelen= dest->width * dest->pixlen;
    srclinelen= src->width * src->pixlen;
    deststart= atx * dest->pixlen;
    destline= dest->data + (aty * destlinelen);
    srcline= src->data;

    for (y= 0; y < cliph; y++) {
      destpixel= destline + deststart;
      srcpixel= srcline;
      for (x= 0; x < clipw; x++) {
	*(destpixel++)= *(srcpixel++);
	*(destpixel++)= *(srcpixel++);
	*(destpixel++)= *(srcpixel++);
      }
      destline += destlinelen;
      srcline += srclinelen;
    }
    break;
  }
  if (verbose)
    printf("done\n");
  return(dest);
}

/* put src image on dest image
 */

Image *merge(dest, src, atx, aty, verbose)
     Image        *dest;
     Image        *src;
     int           atx, aty;
     unsigned int  verbose;
{ int clipw, cliph;
  int clipped = 0;
  Image *newimage;

  goodImage(dest, "merge");
  goodImage(src, "merge");

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

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

  clipw= src->width;
  cliph= src->height;
  if ((atx + clipw < 0) || (aty + cliph < 0) ||
      (atx >= (int)dest->width) ||
      (aty >= (int)dest->height)) { /* not on dest, ignore */
    if (verbose)
      printf("not on destination\n");
    return(dest);
  }

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

  /* extra clipping required for negative offsets
   */

  if ( atx < 0 || aty < 0 ) {
    int clipx, clipy;
 
    if ( atx < 0 ) {
      clipx = -atx;
      clipw += atx;
      atx = 0;
    }
    else
      clipx = 0;
    
    if ( aty < 0 ) {
      clipy = -aty;
      cliph += aty;
      aty = 0;
    }
    else
      clipy = 0;
    
    clipped = 1;
    src = clip(src, clipx, clipy, clipw, cliph, verbose);
  }
 
  if (BITMAPP(dest) && BITMAPP(src))
    newimage= bitmapToBitmap(src, dest, (unsigned int)atx, (unsigned int)aty,
			     clipw, cliph, verbose);
  else
    newimage= anyToTrue(src, dest, (unsigned int)atx, (unsigned int)aty,
			clipw, cliph, verbose);
  if (clipped)
    freeImage(src);
  return(newimage);
}

/* this creates an image of the specified size by tiling a base image
 */
Image *tile(image, x, y, width, height, verbose)
     Image *image;
     int x, y;
     unsigned int width, height, verbose;
{ Image *base, *tmp;
  int nx, ny;

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

  /* if the image is a bitmap merge onto a bitmap image (so we don't
   * need do dither on some displays).  otherwise promote to truecolor
   * since that's what merge() will do anyway.
   */
  if (BITMAPP(image))
    base = newBitImage(width, height);
  else
    base = newTrueImage(width, height);

  for (nx = x; nx < base->width; nx += image->width) {
    for(ny = y; ny < base->height; ny += image->height) {
      tmp = merge(base, image, nx, ny, 0);
      if (tmp != base) {
	freeImage(base);
	base = tmp;
      }
    }
  }
  printf("done.\n");
  return(base);
}