The R Project SVN R

Rev

Rev 5278 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 5278 Rev 5458
1
/*
1
/*
2
 *  R : A Computer Language for Statistical Data Analysis
2
 *  R : A Computer Language for Statistical Data Analysis
3
 *  Copyright (C) 1998--1999  Guido Masarotto
3
 *  Copyright (C) 1998--1999  Guido Masarotto
4
 *
4
 *
5
 *  This program is free software; you can redistribute it and/or modify
5
 *  This program is free software; you can redistribute it and/or modify
6
 *  it under the terms of the GNU General Public License as published by
6
 *  it under the terms of the GNU General Public License as published by
7
 *  the Free Software Foundation; either version 2 of the License, or
7
 *  the Free Software Foundation; either version 2 of the License, or
8
 *  (at your option) any later version.
8
 *  (at your option) any later version.
9
 *
9
 *
10
 *  This program is distributed in the hope that it will be useful,
10
 *  This program is distributed in the hope that it will be useful,
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 *  GNU General Public License for more details.
13
 *  GNU General Public License for more details.
14
 *
14
 *
15
 *  You should have received a copy of the GNU General Public License
15
 *  You should have received a copy of the GNU General Public License
16
 *  along with this program; if not, write to the Free Software
16
 *  along with this program; if not, write to the Free Software
17
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
 */
18
 */
19
 
19
 
20
/*
20
/*
21
   bitmap -> image conversion
21
   bitmap -> image conversion
22
   Very easy to write: routines here are the graphapp
22
   Very easy to write: routines here are the graphapp
23
   image to image conversion routines with assignement
23
   image to image conversion routines with assignement
24
   changed.
24
   changed.
25
 */
25
 */
26
 
26
 
27
#include "ga.h"
27
#include "ga.h"
28
 
28
 
29
/*
29
/*
30
 *  Try to generate an 8-bit version.
30
 *  Try to generate an 8-bit version.
31
 *  If there are less than 256 unique colours
31
 *  If there are less than 256 unique colours
32
 *  this routine will return the corresponding
32
 *  this routine will return the corresponding
33
 *  indexed 8-bit image.
33
 *  indexed 8-bit image.
34
 *  Returns NULL if more than 256 colours are found.
34
 *  Returns NULL if more than 256 colours are found.
35
 */
35
 */
36
static image copy2image8 (drawing dw)
36
static image copy2image8 (drawing dw)
37
{
37
{
38
    image new_img;
38
    image new_img;
39
    long   x, y, w, h, j;
39
    long   x, y, w, h, j;
40
    byte * pixel8;
40
    byte * pixel8;
41
    int    cmapsize;
41
    int    cmapsize;
42
    int low, high, mid;
42
    int low, high, mid;
43
    rgb    col;
43
    rgb    col;
44
    rgb    cmap[256];
44
    rgb    cmap[256];
45
    point p;
45
    point p;
46
    w = getwidth(dw);
46
    w = getwidth(dw);
47
    h = getheight(dw);
47
    h = getheight(dw);
48
    /* the first colour goes into the cmap automatically: */
48
    /* the first colour goes into the cmap automatically: */
49
    cmapsize = 0;  mid = 0;
49
    cmapsize = 0;  mid = 0;
50
 
50
 
51
    for (y = 0; y < h; y++) {
51
    for (y = 0; y < h; y++) {
52
	p.y = y;
52
	p.y = y;
53
	for (x = 0; x < w; x++) {
53
	for (x = 0; x < w; x++) {
54
	    p.x = x;
54
	    p.x = x;
55
	    col = ggetpixel(dw, p);
55
	    col = ggetpixel(dw, p);
56
	    /* only allow one transparent colour in the cmap: */
56
	    /* only allow one transparent colour in the cmap: */
57
	    if (col & 0xF0000000UL)
57
	    if (col & 0xF0000000UL)
58
		col  = 0xFFFFFFFFUL;	/* transparent */
58
		col  = 0xFFFFFFFFUL;	/* transparent */
59
	    else
59
	    else
60
		col &= 0x00FFFFFFUL;	/* opaque */
60
		col &= 0x00FFFFFFUL;	/* opaque */
61
	    /* binary search the cmap: */
61
	    /* binary search the cmap: */
62
	    low = 0;  high = cmapsize - 1;
62
	    low = 0;  high = cmapsize - 1;
63
	    while (low <= high) {
63
	    while (low <= high) {
64
		mid = (low + high)/2;
64
		mid = (low + high)/2;
65
		if      (col < cmap[mid]) high = mid - 1;
65
		if      (col < cmap[mid]) high = mid - 1;
66
		else if (col > cmap[mid]) low  = mid + 1;
66
		else if (col > cmap[mid]) low  = mid + 1;
67
		else break;
67
		else break;
68
	    }
68
	    }
69
 
69
 
70
	    if (high < low) {
70
	    if (high < low) {
71
		/* didn't find colour in cmap, insert it: */
71
		/* didn't find colour in cmap, insert it: */
72
		if (cmapsize >= 256)
72
		if (cmapsize >= 256)
73
		    return NULL;
73
		    return NULL;
74
		for (j = cmapsize; j > low; j--)
74
		for (j = cmapsize; j > low; j--)
75
		    cmap[j] = cmap[j-1];
75
		    cmap[j] = cmap[j-1];
76
		cmap[low] = col;
76
		cmap[low] = col;
77
		cmapsize ++;
77
		cmapsize ++;
78
	    }
78
	    }
79
	}
79
	}
80
    }
80
    }
81
 
81
 
82
    /* now create the 8-bit indexed image: */
82
    /* now create the 8-bit indexed image: */
83
 
83
 
84
    new_img = newimage(w, h, 8);
84
    new_img = newimage(w, h, 8);
85
    if (! new_img)
85
    if (! new_img)
86
	return new_img;
86
	return new_img;
87
    setpalette(new_img, cmapsize, cmap);
87
    setpalette(new_img, cmapsize, cmap);
88
    /* now convert each 32-bit pixel into an 8-bit pixel: */
88
    /* now convert each 32-bit pixel into an 8-bit pixel: */
89
 
89
 
90
    pixel8 = (byte *) new_img->pixels;
90
    pixel8 = (byte *) new_img->pixels;
91
 
91
 
92
    for (y = 0; y < h; y++) {
92
    for (y = 0; y < h; y++) {
93
	p.y = y;
93
	p.y = y;
94
	for (x = 0; x < w; x++) {
94
	for (x = 0; x < w; x++) {
95
	    p.x =  x;
95
	    p.x =  x;
96
	    col = ggetpixel(dw, p);
96
	    col = ggetpixel(dw, p);
97
	    /* only allow one transparent colour in the cmap: */
97
	    /* only allow one transparent colour in the cmap: */
98
	    if (col & 0xF0000000UL)
98
	    if (col & 0xF0000000UL)
99
		col  = 0xFFFFFFFFUL;	/* transparent */
99
		col  = 0xFFFFFFFFUL;	/* transparent */
100
	    else
100
	    else
101
		col &= 0x00FFFFFFUL;	/* opaque */
101
		col &= 0x00FFFFFFUL;	/* opaque */
102
	    /* binary search the cmap (the colour must be there): */
102
	    /* binary search the cmap (the colour must be there): */
103
	    low = 0;  high = cmapsize - 1;
103
	    low = 0;  high = cmapsize - 1;
104
	    while (low <= high) {
104
	    while (low <= high) {
105
		mid = (low + high)/2;
105
		mid = (low + high)/2;
106
		if      (col < cmap[mid]) high = mid - 1;
106
		if      (col < cmap[mid]) high = mid - 1;
107
		else if (col > cmap[mid]) low  = mid + 1;
107
		else if (col > cmap[mid]) low  = mid + 1;
108
		else break;
108
		else break;
109
	    }
109
	    }
110
 
110
 
111
	    if (high < low) {
111
	    if (high < low) {
112
		/* impossible situation */
112
		/* impossible situation */
113
		delimage(new_img);
113
		delimage(new_img);
114
		return NULL;
114
		return NULL;
115
	    }
115
	    }
116
	    *(pixel8++) = mid;
116
	    *(pixel8++) = mid;
117
	}
117
	}
118
    }
118
    }
119
    return new_img;
119
    return new_img;
120
}
120
}
121
 
121
 
122
 
122
 
123
/*
123
/*
124
 *  Try to generate a 32-bit version.
124
 *  Try to generate a 32-bit version.
125
 *  Return NULL if there is no memory left.
125
 *  Return NULL if there is no memory left.
126
 */
126
 */
127
static image copy2image32 (drawing dw)
127
static image copy2image32 (drawing dw)
128
{
128
{
129
    image new_img;
129
    image new_img;
130
    rgb *pixel32;
130
    rgb *pixel32;
131
    long  x, y, w, h;
131
    long  x, y, w, h;
132
    point p;
132
    point p;
133
    w = getwidth(dw);
133
    w = getwidth(dw);
134
    h = getheight(dw);
134
    h = getheight(dw);
135
    new_img = newimage(w, h, 32);
135
    new_img = newimage(w, h, 32);
136
    if (! new_img)
136
    if (! new_img)
137
	return new_img;
137
	return new_img;
138
 
138
 
139
    pixel32 = (rgb *) new_img->pixels;
139
    pixel32 = (rgb *) new_img->pixels;
140
    for (y = 0; y < h; y++) {
140
    for (y = 0; y < h; y++) {
141
	p.y = y;
141
	p.y = y;
142
	for (x = 0; x < w; x++) {
142
	for (x = 0; x < w; x++) {
143
	    p.x = x;
143
	    p.x = x;
144
	    *(pixel32++) = ggetpixel(dw, p);
144
	    *(pixel32++) = ggetpixel(dw, p);
145
	}
145
	}
146
    }
146
    }
147
    return new_img;
147
    return new_img;
148
}
148
}
149
 
149
 
150
 
150
 
151
 
151
 
152
/*
152
/*
153
 *  Try to generate an image (with depth 8 or 32)  from drawing dw:
153
 *  Try to generate an image (with depth 8 or 32)  from drawing dw:
154
 *  Return NULL on failure.
154
 *  Return NULL on failure.
155
 */
155
 */
156
image bitmaptoimage (drawing dw)
156
image bitmaptoimage (drawing dw)
157
{
157
{
158
    image new_img;
158
    image new_img;
159
    rect r = ggetcliprect(dw);
159
    rect r = ggetcliprect(dw);
160
 
160
 
161
    gsetcliprect(dw, getrect(dw));
161
    gsetcliprect(dw, getrect(dw));
162
    new_img = copy2image8(dw);
162
    new_img = copy2image8(dw);
163
    if (!new_img)
163
    if (!new_img)
164
	new_img = copy2image32(dw);
164
	new_img = copy2image32(dw);
165
    gsetcliprect(dw, r);
165
    gsetcliprect(dw, r);
166
    return new_img;
166
    return new_img;
167
}
167
}
168
 
168
 
169
 
169
 
170
 
170