File: Visu_colors.c

package info (click to toggle)
ftools-fv 5.5.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 7,072 kB
  • sloc: tcl: 50,774; ansic: 17,858; exp: 2,004; makefile: 172; cpp: 169; sh: 109; csh: 10
file content (417 lines) | stat: -rw-r--r-- 11,867 bytes parent folder | download | duplicates (10)
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/*
 * colors.c --
 *
 * A source file for Pict images 
 *
 * Copyright (c) 1995 The Regents of the University of California.
 *
 * Author: Pierre-Louis Bossart
 * Date: November 17, 1995
 *
 * Derived from tkImgPhoto.c in the tk4.0b2 distribution 
 * copyrighted as follows:
 *
 *    Copyright (c) 1994 The Australian National University.
 *    Copyright (c) 1994-1995 Sun Microsystems, Inc.
 *
 *    Author: Paul Mackerras (paulus@cs.anu.edu.au),
 *	      Department of Computer Science,
 *	      Australian National University.
 *
 * See the file "license.terms" for information on usage and redistribution
 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
 *
 */

#include "tkpict.h"


extern XColor	 lut_colorcell_defs[256];
extern int ncolors;
extern int lut_start;

static int Default_Shared_Allocated  = 0;
static int Default_Screen_Allocated  = 0;
static int Default_Private_Allocated = 0;
int Pow_Allocated = 0;

static PictColorTable *DefaultSharedColorTable;
static PictColorTable *DefaultScreenColorTable;
static PictColorTable *DefaultPrivateColorTable;
extern PictColorTable *PowColorTable;

/*
 *----------------------------------------------------------------------
 *
 * DitherInstance --
 *
 *	This procedure is called to update an area of an instance's
 *	pixmap by dithering the corresponding area of the master.
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	The instance's pixmap gets updated.
 *
 *----------------------------------------------------------------------
 */


void
DitherInstance(instancePtr, xStart, yStart, width, height)
    PictInstance *instancePtr;	/* The instance to be updated. */
    int xStart, yStart;		/* Coordinates of the top-left pixel in the
				 * block to be dithered. */
    int width, height;		/* Dimensions of the block to be dithered. */
{
    PictMaster *masterPtr;
    PictColorTable *colorTable;
    XImage *imagePtr;
    int nLines;
    int i, c, x, y;
    int xEnd, yEnd;
    int bitsPerPixel, bytesPerLine, lineLength;
    unsigned char *srcLinePtr, *srcPtr;
    unsigned char *destBytePtr, *dstLinePtr;
    pixel *destLongPtr;
   
 
#ifdef DEBUG
    printf("DitherInstance\n");
#endif

    masterPtr = instancePtr->masterPtr;
    colorTable = instancePtr->colorTable;

    /*
     * First work out how many lines to do at a time,
     * then how many bytes we'll need for pixel storage,
     * and allocate it.
     */

    nLines = (MAX_PIXELS + width - 1) / width;
    if (nLines < 1) {
	nLines = 1;
    }
    if (nLines > height ) {
	nLines = height;
    }

    imagePtr = instancePtr->imagePtr;
    if (imagePtr == NULL) {
	return;			/* we must be really tight on memory */
      }
    bitsPerPixel = imagePtr->bits_per_pixel;
    bytesPerLine = ((bitsPerPixel * width + 31) >> 3) & ~3;
    imagePtr->width = width;
    imagePtr->height = nLines;
    imagePtr->bytes_per_line = bytesPerLine;
    imagePtr->data = (char *) ckalloc((unsigned) (imagePtr->bytes_per_line * nLines));
    if( imagePtr->data == NULL ) {
      (void)fprintf(stderr,"DitherInstance: ckalloc failed \n");
      return;
    }

    lineLength = masterPtr->width;
    srcLinePtr = masterPtr->bytedata + (yStart + height - 1) * lineLength + xStart;
    xEnd = xStart + width;

    
    if (bitsPerPixel > 1) {
      /*
     * Loop over the image, doing at most nLines lines before
     * updating the screen image.
     */

      for (; height > 0; height -= nLines) {
	if (nLines > height) {
	  nLines = height;
	}
	dstLinePtr = (unsigned char *) imagePtr->data;
	yEnd = yStart + nLines;
	for (y = yStart; y < yEnd; ++y) {
	  srcPtr = srcLinePtr;
	  destBytePtr = dstLinePtr;
	  destLongPtr = (pixel *) dstLinePtr;
	  	   
	  for (x = xStart; x < xEnd; ++x) {
	    c = srcPtr[0];
	    srcPtr += 1;
	    if (c < 0) {
	      c = 0;
	    } else if (c > 255) {
	      c = 255;
	    }
		
	    i = colorTable->redValues[c];
	    switch (bitsPerPixel) {
	    case NBBY:
	      *destBytePtr++ = i;
	      break;
	    case NBBY * sizeof(pixel):
	      *destLongPtr++ = i;
	      break;
	    default:
	      XPutPixel(imagePtr, x - xStart, y - yStart,
			(unsigned) i);
	    }
	  }
	
	  srcLinePtr -= lineLength;
	  dstLinePtr += bytesPerLine;
      
	}
	/* Update the pixmap for this instance with the block of
	   pixels that we have just computed. */
	XPutImage(instancePtr->display, instancePtr->pixels,
		  instancePtr->gc, imagePtr, 0, 0, xStart, yStart,
		  (unsigned) width, (unsigned) nLines);
	yStart = yEnd;
      }
    }
    ckfree(imagePtr->data);
    imagePtr->data = NULL;
  
} /* end DitherInstance */








int GetColorTable(Tk_Window tkwin,
		  PictColorTable **colorTable,
		  XVisualInfo **visInfoPtr)
{ 
  Display *disp;
  Colormap cmap;
  
  int ncolors;
  int lut_start;
  char colormap_level;
  char atom = 0;
  static int gave_message=0;

  disp = Tk_Display(tkwin);
  (*visInfoPtr) = get_visual(disp);
  if ((*visInfoPtr) == NULL) {
    (void)fprintf (stderr,"GetColorTable:  No PseudoColor visuals found .  \n");
    exit (0);
  }
  colormap_level = Private_Colormap;
  
  /* first make sure the default Display is PseudoColor */
  if( ((*visInfoPtr)->visual != DefaultVisual(disp,DefaultScreen(disp))) 
     && (colormap_level < DEFAULT_PRIVATE_COLORMAP) ) {
    (void)fprintf (stderr,
		   "ERROR: Default Display is not PseudoColor \n Allocating a Shareable Private Colormap instead \n");
    colormap_level = DEFAULT_PRIVATE_COLORMAP;
  }
  
  /* shared colormap */
  if(colormap_level == READ_SHARED_COLORMAP) {
    if( Default_Shared_Allocated == 0 ) {
      if( !init_colors(disp,&cmap,(*visInfoPtr),
		       &colormap_level,&ncolors,&lut_start,&atom,tkwin) ) {
	(void)fprintf (stderr,
		       "ERROR: no shared colormap exists.\n");
	(void)fprintf (stderr,
		       "Using the default colormap instead \n");
	colormap_level = DEFAULT_SCREEN_COLORMAP;
      }
      else { 
	if( !AllocateColorTable((PictColorTable **)&DefaultSharedColorTable,
				disp,cmap,colormap_level,ncolors,lut_start,atom) )
	  return 0;
	else {
	  Default_Shared_Allocated = 1;
	  *colorTable = DefaultSharedColorTable;
	}
      }
    }
    else { /* use existing color table */
      DefaultSharedColorTable->refCount ++;
      *colorTable = DefaultSharedColorTable;
    }
  } /* end shared colormap*/

  /* default screen colormap */
  if(colormap_level == DEFAULT_SCREEN_COLORMAP) {
    if( Default_Screen_Allocated == 0 ) {
      if( !init_colors(disp,&cmap,(*visInfoPtr),
		       &colormap_level,&ncolors,&lut_start,&atom,tkwin) ) {
	/* from deway garett: eliminate multiple messages on stderr:*/
	if (!gave_message) {
	  gave_message=1;
	  (void)fprintf(stderr,"ERROR: not enough colors in screen Default Colormap\n");
	  (void)fprintf(stderr,"Creating a default private colormap instead \n");
	}
	colormap_level = DEFAULT_PRIVATE_COLORMAP;
      }
      else {
	if( !AllocateColorTable((PictColorTable **)&DefaultScreenColorTable,
				disp,cmap,colormap_level,ncolors,lut_start,atom) )
	  return 0;
	else {
	  Default_Screen_Allocated = 1;
	  *colorTable = DefaultScreenColorTable;
	}
      }
    }
    else { /* use existing color table */
      DefaultScreenColorTable->refCount += 1;
      *colorTable = DefaultScreenColorTable;
    }
  } /* end default screen colormap */

  /* shared private colormap */
  if(colormap_level == DEFAULT_PRIVATE_COLORMAP) {
    if( Default_Private_Allocated == 0 ) {
      if( !init_colors(disp,&cmap,(*visInfoPtr),
		       &colormap_level,&ncolors,&lut_start,&atom,tkwin) ) {
	(void)fprintf(stderr,"init_colors failed \n");
	return 0;
      }
      else {
	if( !AllocateColorTable((PictColorTable **)&DefaultPrivateColorTable,
				disp,cmap,colormap_level,ncolors,lut_start,atom) )
	  return 0;
	else { 
	  Default_Private_Allocated = 1;
	  *colorTable = DefaultPrivateColorTable;
	}
      }
    }
    else { /* use existing color table */
      DefaultPrivateColorTable->refCount += 1;
      *colorTable = DefaultPrivateColorTable;
    }
  } /* end shared private colormap */

  /* private colormap */
  if(colormap_level == NEW_PRIVATE_COLORMAP) {
    if( !init_colors(disp,&cmap,(*visInfoPtr),
		     &colormap_level,&ncolors,&lut_start,&atom,tkwin) ) {
      (void)fprintf(stderr,"init_colors failed \n");
      return 0;
    } 
    else {
      if( !AllocateColorTable((PictColorTable **)colorTable,
			      disp,cmap,colormap_level,ncolors,lut_start,atom) )
	return 0;
    }
  } /* end private colormap */

  /* POW colormap */
  if(colormap_level == POW_COLORMAP) {
    if( Pow_Allocated == 0 ) {
      if( !init_colors(disp,&cmap,(*visInfoPtr),
		       &colormap_level,&ncolors,&lut_start,&atom,tkwin) ) {
	(void)fprintf(stderr,"init_colors failed \n");
	return 0;
      } 
      else {
	if( !AllocateColorTable((PictColorTable **)&PowColorTable,
				disp,cmap,colormap_level,ncolors,lut_start,atom) )
	  return 0;
	else { 
	  Pow_Allocated = 1;
	  *colorTable = PowColorTable;
	}
      }
    } else {
      /* use existing color table */
      PowColorTable->refCount ++;
      *colorTable = PowColorTable;
    }
  } /* end POW colormap */

  /* change default back to screen colormap. This is useless in visu,
     since the scripts always check for the colormap level of the
     current image. This might be useful in the future though */
  /*  Private_Colormap = DEFAULT_SCREEN_COLORMAP;*/
    
  /* set window attributes */
  if( Tk_SetWindowVisual(tkwin, (*visInfoPtr)->visual,(*visInfoPtr)->depth,
			 (*colorTable)->colormap) == 0 ){

    /* window already exists. See if the new colormap can be installed */
    /*    if(((*visInfoPtr)->visual == (((Tk_FakeWin *)(tkwin)))->visual) &&
       ((*visInfoPtr)->depth  == (((Tk_FakeWin *)(tkwin)))->depth)) { */
    /*Let's try not doing any testing, HPUX barfs if we do*/

      Tk_SetWindowColormap(tkwin,(*colorTable)->colormap); 

      /*    }
    else {
      (void)fprintf(stderr,"Window already exists with Visual class %d and depth %d. Change the Tk script to create image widget with PseudoColor 8 visual\n",
                    ((Tk_FakeWin *)(tkwin))->visual->class,
		    ((Tk_FakeWin *)(tkwin))->depth); 
      return 0;
    } */
  } else {
    /* The visual was set, but the colormap needs to be installed. The
       window managers should do it but for now let's do it ourselves */
    /* Tk_SetWindowColormap(tkwin,(*colorTable)->colormap); */
  }
  return 1;
}



int DisposeColorTable(PictColorTable *colorTable)
{
  unsigned long *pixels;
  int i,j;
  /* if still instances using colorTable  *or* if using POW Colormap, don't
     do this.  pict images in POW use the POW windows colormap.  Once
     allocated, the POW colorTable should remain (as far as VISU is 
     concerned).  The Pow colorTable is handled in PowCleanUp if POW is
     exitted. */
  if(colorTable->refCount != 0 ||  colorTable->colormap_level == POW_COLORMAP)
    return 0;

  /* destroy atom */
  if(colorTable->atom == 1) 
    deinit_disp(colorTable->display);

  if(colorTable->colormap_level > READ_SHARED_COLORMAP) {

    pixels = (unsigned long *)ckalloc(colorTable->ncolors*
				     sizeof(unsigned long));
    if( pixels == NULL )
      return 0;

    /* create pixel array */
    for(j=colorTable->lut_start,i=0;i<colorTable->ncolors;i++,j++)
      pixels[i] = j;

    /* free colors */
    XFreeColors(colorTable->display,colorTable->colormap,
		pixels,colorTable->ncolors,0);
    ckfree((void*)pixels);

    /* free colormap */
    XFreeColormap(colorTable->display,
		  colorTable->colormap);
  }

  if( colorTable->colormap_level ==  READ_SHARED_COLORMAP )
    Default_Shared_Allocated = 0;
  if( colorTable->colormap_level == DEFAULT_SCREEN_COLORMAP)
    Default_Screen_Allocated = 0;
  else if( colorTable->colormap_level == DEFAULT_PRIVATE_COLORMAP )
    Default_Private_Allocated = 0;

  ckfree((void*)colorTable);
  colorTable = NULL;

  return 1;
} /* end DisposeColorTable */