[222] | 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-- */
|
---|
| 11 | PIColorMapX::PIColorMapX()
|
---|
| 12 | : PIColorMapGen()
|
---|
| 13 | {
|
---|
| 14 | mColors = NULL;
|
---|
| 15 | mColRGB = NULL;
|
---|
| 16 | mNewCol = NULL;
|
---|
| 17 | }
|
---|
| 18 |
|
---|
| 19 | /* --Methode-- */
|
---|
| 20 | PIColorMapX::PIColorMapX(int id, int nc, string const& nom)
|
---|
| 21 | : PIColorMapGen(id, nc, nom)
|
---|
| 22 | {
|
---|
| 23 |
|
---|
| 24 | int n = nc;
|
---|
| 25 | mColors = new PIXColor[n];
|
---|
| 26 | mColRGB = new PIColor[n];
|
---|
| 27 | mNewCol = new bool[n];
|
---|
| 28 | int i;
|
---|
| 29 | for(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-- */
|
---|
| 38 | PIColorMapX::~PIColorMapX()
|
---|
| 39 | {
|
---|
| 40 | if (mColors) delete[] mColors;
|
---|
| 41 | if (mColRGB) delete[] mColRGB;
|
---|
| 42 | if (mNewCol) delete[] mNewCol;
|
---|
| 43 | }
|
---|
| 44 |
|
---|
| 45 |
|
---|
| 46 | /* --Methode-- */
|
---|
| 47 | PIColor PIColorMapX::GetColor(int n)
|
---|
| 48 | {
|
---|
| 49 | if ((n<0) || (n >= NCol()))
|
---|
| 50 | {
|
---|
| 51 | PIColor picr;
|
---|
| 52 | picr.red = picr.green = picr.blue = 0;
|
---|
| 53 | return(picr);
|
---|
| 54 | }
|
---|
| 55 | return(mColRGB[n]);
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | static Colormap defxcmap;
|
---|
| 59 | static int xinicmap = 0;
|
---|
| 60 | static int NTotColors = 0;
|
---|
| 61 | static int NTotAllocCol = 0;
|
---|
| 62 |
|
---|
| 63 | static int nerrallocol = 0;
|
---|
| 64 |
|
---|
| 65 | /* --Methode-- */
|
---|
| 66 | bool PIColorMapX::AllocColor(PIColor const& col, int index)
|
---|
| 67 | {
|
---|
| 68 | XColor myxcol;
|
---|
| 69 |
|
---|
| 70 | if (!xinicmap)
|
---|
| 71 | {
|
---|
| 72 | defxcmap = XDefaultColormap (PIXDisplay(), PIXScreen());
|
---|
| 73 | xinicmap = 1;
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | if ( (index < 0) || (index >= mNCol) ) return(false);
|
---|
| 77 |
|
---|
| 78 | if ( (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
|
---|
| 88 | int kc;
|
---|
| 89 | for(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 |
|
---|
| 95 | myxcol.red = col.red;
|
---|
| 96 | myxcol.blue = col.blue;
|
---|
| 97 | myxcol.green = col.green;
|
---|
| 98 |
|
---|
| 99 |
|
---|
| 100 | if ( 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 | }
|
---|
| 109 | else {
|
---|
| 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--
|
---|
| 119 | void PIColorMapX::MakeColMap()
|
---|
| 120 | {
|
---|
| 121 | }
|
---|
| 122 | */
|
---|
| 123 |
|
---|
| 124 | /* --Methode-- */
|
---|
| 125 | void PIColorMapX::FreeColors()
|
---|
| 126 | {
|
---|
| 127 | PIXColor bpix;
|
---|
| 128 | PIColor blc;
|
---|
| 129 |
|
---|
| 130 | if (!xinicmap)
|
---|
| 131 | {
|
---|
| 132 | defxcmap = XDefaultColormap (PIXDisplay(), PIXScreen());
|
---|
| 133 | xinicmap = 1;
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | for (int i=0; i<mNCol; i++)
|
---|
| 137 | if ( mNewCol[i] ) XFreeColors(PIXDisplay(), defxcmap, mColors+i, 1, 0);
|
---|
| 138 |
|
---|
| 139 | bpix = BlackPixel(PIXDisplay(), PIXScreen());
|
---|
| 140 | blc.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-- */
|
---|
| 151 | long PIColorMapX::TotNbColors()
|
---|
| 152 | {
|
---|
| 153 | if (NTotColors > 0) return(NTotColors);
|
---|
| 154 | // Visual* vis;
|
---|
| 155 | // DefaultVisual(PIXDisplay(), PIXScreen() );
|
---|
| 156 | int k,kk;
|
---|
| 157 | // On calcule le nombre de couleurs a partir du nb de plans image
|
---|
| 158 | kk = DefaultDepth(PIXDisplay(), PIXScreen() );
|
---|
| 159 | NTotColors = 1;
|
---|
| 160 | for(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 | */
|
---|
| 167 | return(NTotColors);
|
---|
| 168 | }
|
---|
| 169 |
|
---|
| 170 | /* --Methode-- */
|
---|
| 171 | int PIColorMapX::NbAllocColors()
|
---|
| 172 | {
|
---|
| 173 | return(NTotAllocCol);
|
---|
| 174 | }
|
---|
| 175 |
|
---|
| 176 |
|
---|
| 177 | /* --Methode-- */
|
---|
| 178 | void 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 | }
|
---|