File: ximage.C

package info (click to toggle)
xjig 2.4-14.1
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 892 kB
  • sloc: cpp: 10,177; makefile: 1,142; perl: 23
file content (169 lines) | stat: -rw-r--r-- 3,897 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

#include "global.h"

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xmd.h>

#include "ximage.H"
#include "color_mapper.H"
#include "gif_image.H"
#include "ppm_image.H"
#include "jpeg_image.H"

XjigXImage::XjigXImage(class Port *port,const char *filename,int autocrop)
: p(port) {
	const char *suffix = strrchr(filename, '.');
	if(suffix && (!strcasecmp(suffix, ".jpg") ||
					  !strcasecmp(suffix, ".jpeg") ||
					  !strcasecmp(suffix, ".jpe")))
		img=new jpegImage(filename, autocrop);
	else if(suffix && (!strcasecmp(suffix, ".ppm") ||
							 !strcasecmp(suffix, ".pgm") ||
							 !strcasecmp(suffix, ".pbm")))
		img=new ppmImage(filename, autocrop);
	else
		img=new GifImage(filename, autocrop);
	xwidth  = Width();
	xheight = Height();
	dpy=p->GetDisplay();
	scr=DefaultScreen(dpy);
	ximage = 0;
	gif_cols=0;
	SetupMapper();
#ifndef RANGE_CHECK
	offset_rows=(int)(sqrt((double)xwidth*(double)xwidth+(double)xheight*(double)xheight)/2.0)+1;
#else
	offset_rows=0;
#endif
}

XjigXImage::~XjigXImage() {
	DropData();
	if (gif_cols)		delete [] gif_cols;
}

void XjigXImage::SetupMapper() {
	if (gif_cols)		delete [] gif_cols;
	SetupTrueMapper();
}

void XjigXImage::SetupTrueMapper() {
	gif_cols=new unsigned long[GetNCols()];
	for (int i=0;i<GetNCols();i++) {
		XColor	def;
		GetColor(i,&def.red,&def.green,&def.blue);
		gif_cols[i]=p->GetMapper()->alloc_color(&def);
	}
}

void XjigXImage::TraceMapper() {
	for (int i=0;i<GetNCols();i++) {
		XColor   def;
		GetColor(i,&def.red,&def.green,&def.blue);
		printf( "%3d: %02x %02x %02x - %ld\n", i, def.red>>8, def.green>>8, def.blue>>8, gif_cols[i] );
	}
}

void XjigXImage::CreateData( int w, int h ) {
	xwidth  = w;
	xheight = h;
#ifndef RANGE_CHECK
	offset_rows=(int)(sqrt((double)w*(double)w+(double)h*(double)h)/2.0)+1;
#else
	offset_rows=0;
#endif
	DropData();
	switch(texture_mode) {
	case 1:		Reset8();		break;
	case 2:		Reset16();		break;
	case 3:		Reset32();		break;
	default:		fprintf( stderr, "depth not supported\n" );
					exit(0);
	}
}

void XjigXImage::DropData() {
	if (ximage) {
		delete [] (ximage->data-offset_bytes);
		ximage->data = 0L;
		XDestroyImage(ximage);
		ximage=0;
	}
}

unsigned long XjigXImage::GetPixel(int x, int y) {
	if (x<0||x>=xwidth||y<0||y>=xheight) {
		return 0;
	}
	else {
		return XGetPixel(ximage,x,y);
	}
}

/*----------------------------------------------------------------------------*/
#define	DATA_TYPE	CARD32
#define	DATA_PAD		4
void XjigXImage::Reset32() {
#	include "reset_image.H"
}
#undef DATA_TYPE
#undef DATA_PAD
/*----------------------------------------------------------------------------*/
#define	DATA_TYPE	CARD16
#define	DATA_PAD		2
void XjigXImage::Reset16() {
#	include "reset_image.H"
}
#undef DATA_TYPE
#undef DATA_PAD
/*----------------------------------------------------------------------------*/
#define	DATA_TYPE	CARD8
#define	DATA_PAD		1
void XjigXImage::Reset8() {
#	include "reset_image.H"
}
#undef DATA_TYPE
#undef DATA_PAD

// ========================================================================

XjigPixmap::XjigPixmap( Port *port, const char *filename, int autocrop )
: XjigXImage(port,filename,autocrop) {
int w, h;

	pixmap=0;

	const char	*ext=GetExtensionData( SUBSIZE_EXTENSION );

	if (ext&&sscanf( ext, "%dx%d", &w, &h)==2) {
		xmult=XjigXImage::Width()/w;
		ymult=XjigXImage::Height()/h;
	}
	else {
		xmult=1;
		ymult=1;
	}
}

XjigPixmap::~XjigPixmap() {
	if (pixmap)		XFreePixmap(dpy,pixmap);
}

Pixmap XjigPixmap::GetPixmap() {
	if (!pixmap) {
		pixmap=XCreatePixmap(dpy,RootWindow(dpy,scr),xwidth,xheight,DefaultDepth(dpy,scr));
		XPutImage(dpy,pixmap,DefaultGC(dpy,scr),GetImage(),0,0,0,0,xwidth,xheight);
	}
	return pixmap;
}

void XjigPixmap::CreateData(int w,int h) {
	w*=xmult;
	h*=ymult;
	if (pixmap)		{ XFreePixmap(dpy,pixmap); pixmap=0; }
	XjigXImage::CreateData(w,h);
}