File: clip.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 (115 lines) | stat: -rw-r--r-- 2,872 bytes parent folder | download | duplicates (10)
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
/* clip.c:
 *
 * return a new image which is a clipped subsection of the old image
 *
 * jim frost 10.04.89
 *
 * Copyright 1989, 1991 Jim Frost.
 * See included file "copyright.h" for complete copyright information.
 */

#include "copyright.h"
#include "image.h"

Image *clip(simage, clipx, clipy, clipw, cliph, verbose)
     Image        *simage;
     unsigned int  clipx, clipy, clipw, cliph;
     unsigned int  verbose;
{ Image *image;
  unsigned int  x, y;
  unsigned int  slinelen, dlinelen;
  unsigned int  start;
  byte          startmask, smask, dmask;
  byte         *sp, *sline, *dp, *dline;

  goodImage(simage, "clip");

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

  /* sane-ify clip area with respect to image
   */

  if (clipx + clipw > simage->width)
    clipw -= (simage->width - (clipx + clipw));
  if (clipy + cliph > simage->height)
    cliph -= (simage->height - (clipy + cliph));

  switch (simage->type) {
  case IBITMAP:

    /* this could be sped up; i don't care
     */

    image= newBitImage(clipw, cliph);
    for (x= 0; x < simage->rgb.used; x++) {
      *(image->rgb.red + x)= *(simage->rgb.red + x);
      *(image->rgb.green + x)= *(simage->rgb.green + x);
      *(image->rgb.blue + x)= *(simage->rgb.blue + x);
    }
    slinelen= (simage->width / 8) + (simage->width % 8 ? 1 : 0);
    dlinelen= (clipw / 8) + (clipw % 8 ? 1 : 0);
    start= clipx / 8;
    startmask= 0x80 >> (clipx % 8);
    sline= simage->data + (slinelen * clipy);
    dline= image->data;
    for (y= 0; y < cliph; y++) {
      sp= sline + start;
      dp= dline;
      smask= startmask;
      dmask= 0x80;
      for (x= 0; x < clipw; x++) {
	if (*sp & smask)
	  *dp |= dmask;
	if (! (smask >>= 1)) {
	  smask= 0x80;
	  sp++;
	}
	if (! (dmask >>= 1)) {
	  dmask= 0x80;
	  dp++;
	}
      }
      sline += slinelen;
      dline += dlinelen;
    }
    break;

  case IRGB:
  case ITRUE:
    if (RGBP(simage)) {
      image= newRGBImage(clipw, cliph, simage->depth);
      for (x= 0; x < simage->rgb.used; x++) {
	*(image->rgb.red + x)= *(simage->rgb.red + x);
	*(image->rgb.green + x)= *(simage->rgb.green + x);
	*(image->rgb.blue + x)= *(simage->rgb.blue + x);
      }
      image->rgb.used= simage->rgb.used;
    }
    else
      image= newTrueImage(clipw, cliph);
    slinelen= simage->width * simage->pixlen;
    start= clipx * simage->pixlen;
    sline= simage->data + (clipy * slinelen);
    dp= image->data;
    for (y= 0; y < cliph; y++) {
      sp= sline + start;
      for (x= 0; x < clipw; x++) {
	valToMem(memToVal(sp, simage->pixlen), dp, simage->pixlen);
	sp += simage->pixlen;
	dp += simage->pixlen;
      }
      sline += slinelen;
    }
    break;
  default:
    printf("clip: Unsupported image type\n");
    exit(1);
  }
  image->title= dupString(simage->title);
  if (verbose)
    printf("done\n");
  return(image);
}