File: gifx_image.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 (154 lines) | stat: -rw-r--r-- 3,394 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

#include "global.h"

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

#include "gifx_image.H"
#include "color_mapper.H"

GifXImage::GifXImage(class Port *port,const char *filename,int autocrop)
: p(port), 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
}

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

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

void GifXImage::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 GifXImage::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 GifXImage::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 GifXImage::DropData() {
	if (ximage) {
		delete [] (ximage->data-offset_bytes);
		ximage->data = 0L;
		XDestroyImage(ximage);
		ximage=0;
	}
}

unsigned long GifXImage::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 GifXImage::Reset32() {
#	include "reset_image.H"
}
#undef DATA_TYPE
#undef DATA_PAD
/*----------------------------------------------------------------------------*/
#define	DATA_TYPE	CARD16
#define	DATA_PAD		2
void GifXImage::Reset16() {
#	include "reset_image.H"
}
#undef DATA_TYPE
#undef DATA_PAD
/*----------------------------------------------------------------------------*/
#define	DATA_TYPE	CARD8
#define	DATA_PAD		1
void GifXImage::Reset8() {
#	include "reset_image.H"
}
#undef DATA_TYPE
#undef DATA_PAD

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

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

	pixmap=0;

	const char	*ext=GetExtensionData( SUBSIZE_EXTENSION );

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

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

Pixmap GifPixmap::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 GifPixmap::CreateData(int w,int h) {
	w*=xmult;
	h*=ymult;
	if (pixmap)		{ XFreePixmap(dpy,pixmap); pixmap=0; }
	GifXImage::CreateData(w,h);
}