File: color.c

package info (click to toggle)
swig1.3 1.3.24-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 19,336 kB
  • ctags: 10,604
  • sloc: cpp: 27,917; ansic: 24,160; yacc: 4,412; python: 4,255; java: 4,156; makefile: 3,735; sh: 3,552; cs: 2,250; ruby: 2,150; lisp: 1,605; tcl: 1,136; perl: 980; php: 879; ml: 825
file content (140 lines) | stat: -rw-r--r-- 3,805 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
/* ----------------------------------------------------------------------------- 
 * color.c
 *
 *     Colormaps
 * 
 * Author(s) : David Beazley (beazley@cs.uchicago.edu)
 * Copyright (C) 1995-1996
 *
 * See the file LICENSE for information on usage and redistribution.	
 * ----------------------------------------------------------------------------- */

#define COLORMAP
#include "gifplot.h"
#include <string.h>

/* ------------------------------------------------------------------------
   ColorMap *new_ColorMap(char *filename)

   Read a colormap from a file.
   ------------------------------------------------------------------------ */

ColorMap *new_ColorMap(char *filename) {
  ColorMap *c;
  FILE *cm;
  void ColorMap_default(ColorMap *);
  
  if (!filename) {
    c = (ColorMap *) malloc(sizeof(ColorMap));
    c->cmap = (unsigned char *) malloc(768*sizeof(char));
    c->name = 0;
    ColorMap_default(c);
    return c;
  }
  if (strlen(filename) == 0) {
    c = (ColorMap *) malloc(sizeof(ColorMap));
    c->cmap = (unsigned char *) malloc(768*sizeof(char));
    ColorMap_default(c);
    return c;
  }
  if ((cm = fopen(filename,"rb")) == NULL) {
    return (ColorMap *) 0;
  }
  c = (ColorMap *) malloc(sizeof(ColorMap));
  c->cmap = (unsigned char *) malloc(768*sizeof(char));
  if (fread(c->cmap, 768, 1, cm) != 1) {
    free((char *)c->cmap);
    free((char *)c);
    fclose(cm);
    return (ColorMap *) 0;
  }
  fclose(cm);
  c->name = (char *) malloc(strlen(filename)+1);
  strcpy(c->name, filename);
  ColorMap_default(c);
  return c;
}

/* --------------------------------------------------------------------------
   delete_ColorMap(ColorMap *cm)

   Destroy a ColorMap
   -------------------------------------------------------------------------- */

void delete_ColorMap(ColorMap *cm) {
  if (cm) {
    free((char *)cm->cmap);
    if (cm->name)
      free((char *)cm->name);
    free((char *)cm);
  }
}

/* --------------------------------------------------------------------------
   ColorMap_default(ColorMap *cm)

   This function fills in default values for the first 8 entries of the
   colormap.  These are *fixed* values---used internally.
   -------------------------------------------------------------------------- */

void ColorMap_default(ColorMap *cm) {
  unsigned char *r,*g,*b;
  if (cm) {
    r = cm->cmap;
    g = cm->cmap+256;
    b = cm->cmap+512;

    r[0] = 0;   g[0] = 0;   b[0] = 0;      /* BLACK */
    r[1] = 255; g[1] = 255; b[1] = 255;    /* WHITE */
    r[2] = 255; g[2] = 0;   b[2] = 0;      /* RED   */
    r[3] = 0;   g[3] = 255; b[3] = 0;      /* GREEN */
    r[4] = 0;   g[4] = 0;   b[4] = 255;    /* BLUE  */
    r[5] = 255; g[5] = 255; b[5] = 0;      /* YELLOW */
    r[6] = 0;   g[6] = 255; b[6] = 255;    /* CYAN   */
    r[7] = 255; g[7] = 0;   b[7] = 255;    /* MAGENTA */
  }
}

void ColorMap_assign(ColorMap *cm, int index, int r, int g, int b) {
  unsigned char *rb,*gb,*bb;

  rb = cm->cmap;
  gb = cm->cmap+256;
  bb = cm->cmap+512;

  rb[index] = r;
  gb[index] = g;
  bb[index] = b;
}

int ColorMap_getitem(ColorMap *cm, int index) {
  if ((index < 0) && (index >= 768)) return -1;
  return cm->cmap[index];
}

void ColorMap_setitem(ColorMap *cm, int index, int value) {
  if ((index < 0) && (index >= 768)) return;
  cm->cmap[index]=value;
}  

/* --------------------------------------------------------------------
   ColorMap_write(ColorMap *cm, char *filename)

   Write out a colormap to disk.
   -------------------------------------------------------------------- */

int ColorMap_write(ColorMap *cm, char *filename) {

  FILE *f;
  if (!cm) return -1;
  if (!filename) return -1;
  if (strlen(filename) == 0) return -1;

  f = fopen(filename,"w");
  
  fwrite(cm->cmap,768,1,f);
  fclose(f);
  return 0;
}

#undef COLORMAP