source: Sophya/trunk/Poubelle/PI/picmapx.cc@ 222

Last change on this file since 222 was 222, checked in by ansari, 26 years ago

Creation module DPC/PI Reza 09/04/99

File size: 4.4 KB
Line 
1#include <stdio.h>
2
3#include <X11/Xlib.h>
4#include <X11/Xutil.h>
5#include <X11/Xatom.h>
6
7#include "piwdgx.h"
8#include "picmapx.h"
9
10/* --Methode-- */
11PIColorMapX::PIColorMapX()
12: PIColorMapGen()
13{
14mColors = NULL;
15mColRGB = NULL;
16mNewCol = NULL;
17}
18
19/* --Methode-- */
20PIColorMapX::PIColorMapX(int id, int nc, string const& nom)
21: PIColorMapGen(id, nc, nom)
22{
23
24int n = nc;
25mColors = new PIXColor[n];
26mColRGB = new PIColor[n];
27mNewCol = new bool[n];
28int i;
29for(i=0; i<n; i++)
30 { mColors[i] = BlackPixel(PIXDisplay(), PIXScreen());
31 mColRGB[i].red = mColRGB[i].green = mColRGB[i].blue = 0;
32 mNewCol[i] = false; }
33// printf("PIColorMapX::PIColorMapX(CMapId, int) %d %d \n", mNCol, Type());
34}
35
36
37/* --Methode-- */
38PIColorMapX::~PIColorMapX()
39{
40if (mColors) delete[] mColors;
41if (mColRGB) delete[] mColRGB;
42if (mNewCol) delete[] mNewCol;
43}
44
45
46/* --Methode-- */
47PIColor PIColorMapX::GetColor(int n)
48{
49if ((n<0) || (n >= NCol()))
50 {
51 PIColor picr;
52 picr.red = picr.green = picr.blue = 0;
53 return(picr);
54 }
55return(mColRGB[n]);
56}
57
58static Colormap defxcmap;
59static int xinicmap = 0;
60static int NTotColors = 0;
61static int NTotAllocCol = 0;
62
63static int nerrallocol = 0;
64
65/* --Methode-- */
66bool PIColorMapX::AllocColor(PIColor const& col, int index)
67{
68XColor myxcol;
69
70if (!xinicmap)
71 {
72 defxcmap = XDefaultColormap (PIXDisplay(), PIXScreen());
73 xinicmap = 1;
74 }
75
76if ( (index < 0) || (index >= mNCol) ) return(false);
77
78if ( (col.red == 0) && (col.green == 0) && (col.blue == 0) )
79 {
80 mColors[index] = BlackPixel(PIXDisplay(), PIXScreen());
81 mColRGB[index] = col;
82 mNewCol[index] = false;
83 return(true);
84 }
85
86// Pour diminuer les requetes X, on verifie si la couleur
87// ne se trouve pas deja dans la table Reza 19/05/98
88int kc;
89for(kc=0; kc<mNCol; kc++)
90 if ( (col.red == mColRGB[kc].red) && (col.green == mColRGB[kc].green) && (col.blue == mColRGB[kc].blue) )
91 { mColors[index] = mColors[kc]; mColRGB[index] = mColRGB[kc];
92 mNewCol[index] = false; return(true); }
93
94
95myxcol.red = col.red;
96myxcol.blue = col.blue;
97myxcol.green = col.green;
98
99
100if ( XAllocColor(PIXDisplay(), defxcmap, &myxcol) ) {
101 mColors[index] = myxcol.pixel;
102 mColRGB[index] = col;
103 mNewCol[index] = true;
104// printf("PIColorMapX::AllocColor() RGB= %d %d %d \n",
105// (int) col.red, (int) col.blue, (int) col.green);
106 NTotAllocCol++;
107 return(true);
108 }
109else {
110 nerrallocol++;
111 if (nerrallocol < 21) printf("PIColorMapX::AllocColor() Error: Pb Allocation RGB= %d %d %d \n",
112 (int) col.red, (int) col.blue, (int) col.green);
113 if (nerrallocol == 20) printf("No more PIColorMapX::AllocColor() Errors will be reported \n");
114 return(false);
115 }
116}
117
118/* --Methode--
119void PIColorMapX::MakeColMap()
120{
121}
122*/
123
124/* --Methode-- */
125void PIColorMapX::FreeColors()
126{
127PIXColor bpix;
128PIColor blc;
129
130if (!xinicmap)
131 {
132 defxcmap = XDefaultColormap (PIXDisplay(), PIXScreen());
133 xinicmap = 1;
134 }
135
136for (int i=0; i<mNCol; i++)
137 if ( mNewCol[i] ) XFreeColors(PIXDisplay(), defxcmap, mColors+i, 1, 0);
138
139bpix = BlackPixel(PIXDisplay(), PIXScreen());
140blc.red = blc.green = blc.blue = 0;
141{for (int i=0; i<mNCol; i++)
142 { mColors[i] = bpix; mColRGB[i] = blc; mNewCol[i] = false; }
143}
144// printf("PIColorMapX::FreeColors() Type= %d mNCol= %d \n",
145// Type(), NCol());
146
147}
148
149
150/* --Methode-- */
151long PIColorMapX::TotNbColors()
152{
153if (NTotColors > 0) return(NTotColors);
154// Visual* vis;
155// DefaultVisual(PIXDisplay(), PIXScreen() );
156int k,kk;
157// On calcule le nombre de couleurs a partir du nb de plans image
158kk = DefaultDepth(PIXDisplay(), PIXScreen() );
159NTotColors = 1;
160for(k=0; k<kk; k++) NTotColors *= 2;
161/*
162 printf("PIColorMapX::TotNbColor() / Debug : Nb Tot Colors = %d - D=%d - NCol=%d NP= %d \n", NTotColors,
163 DefaultDepth(PIXDisplay(), PIXScreen()),
164 DisplayCells(PIXDisplay(), PIXScreen()),
165 DisplayPlanes(PIXDisplay(), PIXScreen()) );
166*/
167return(NTotColors);
168}
169
170/* --Methode-- */
171int PIColorMapX::NbAllocColors()
172{
173return(NTotAllocCol);
174}
175
176
177/* --Methode-- */
178void PIColorMapX::CopyFrom(PIColorMapGen* x)
179{
180 if (mColors) delete mColors;
181 if (mColRGB) delete mColRGB;
182 if (mNewCol) delete mNewCol;
183 CopyFromGen(x);
184 mColors = new PIXColor[mNCol];
185 mColRGB = new PIColor[mNCol];
186 mNewCol = new bool[mNCol];
187 for (int i=0; i<mNCol; i++)
188 { mColors[i] = ((PIColorMapX *)x)->mColors[i];
189 mColRGB[i] = ((PIColorMapX *)x)->mColRGB[i];
190// C'est la table de couleur originale qui est responsable de liberer les couleurs - Reza 8/2/98
191 mNewCol[i] = false; }
192}
Note: See TracBrowser for help on using the repository browser.