File: Pixmap.cpp

package info (click to toggle)
pymol 2.5.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 42,288 kB
  • sloc: cpp: 476,472; python: 76,538; ansic: 29,510; javascript: 6,792; sh: 47; makefile: 24
file content (235 lines) | stat: -rw-r--r-- 6,082 bytes parent folder | download | duplicates (2)
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


/* 
A* -------------------------------------------------------------------
B* This file contains source code for the PyMOL computer program
C* Copyright (c) Schrodinger, LLC. 
D* -------------------------------------------------------------------
E* It is unlawful to modify or remove this copyright notice.
F* -------------------------------------------------------------------
G* Please see the accompanying LICENSE file for further information. 
H* -------------------------------------------------------------------
I* Additional authors of this source file include:
-* 
-* 
-*
Z* -------------------------------------------------------------------
*/

#include "Pixmap.h"
#include "OOMac.h"
#include "Util.h"

void PixmapInit(PyMOLGlobals * G, CPixmap * I, int width, int height)
{
  UtilZeroMem(I, sizeof(CPixmap));
  I->G = G;
  I->height = height;
  I->width = width;
  if((height >= 0) && (width >= 0)) {
    I->buffer = pymol::malloc<unsigned char>(4 * height * width);
  }
}

CPixmap *PixmapNew(PyMOLGlobals * G, int width, int height)
{
  OOAlloc(G, CPixmap);
  PixmapInit(G, I, width, height);
  return I;
}

void PixmapInitFromBitmap(PyMOLGlobals * G, CPixmap * I, int width, int height,
                          unsigned char *bitmap, unsigned char *rgba, int sampling)
{
  if(I) {

    int x, y, bit_cnt;
    unsigned char cur = 0;
    unsigned char *src;
    unsigned char *dst;
    unsigned char red, blue, green, alpha;
    PixmapInit(G, I, width * sampling, height * sampling);
    red = rgba[0];
    green = rgba[1];
    blue = rgba[2];
    alpha = rgba[3];
    UtilZeroMem(I->buffer, 4 * width * height);
    src = bitmap;
    dst = I->buffer;
    for(y = 0; y < height; y++) {
      bit_cnt = 7;
      for(x = 0; x < width; x++) {
        bit_cnt++;
        if(bit_cnt > 7) {
          cur = *(src++);
          bit_cnt = 0;
        }
        if(cur & 0x80) {
          *(dst++) = red;
          *(dst++) = green;
          *(dst++) = blue;
          *(dst++) = alpha;
        } else {
          *(dst++) = 0;
          *(dst++) = 0;
          *(dst++) = 0;
          *(dst++) = 0;
        }
        cur <<= 1;
      }
    }
    if(sampling > 1) {
      unsigned int *p, *pp, *q, *row;
      int row_cnt, col_cnt, width_sampling = width * sampling;

      p = (unsigned int *) (I->buffer + 4 * width * height);
      q = (unsigned int *) (I->buffer + 4 * width * height * sampling * sampling);
      while(p > (unsigned int *) I->buffer) {
        row_cnt = sampling - 1;
        row = q;
        for(x = 0; x < width; x++) {    /* first row */
          col_cnt = sampling;
          p--;
          while(col_cnt--) {
            *(--q) = *p;
          }
        }
        if(row_cnt) {
          while(row_cnt--) {    /* remaining rows */
            pp = row;
            for(x = 0; x < width_sampling; x++) {
              *(--q) = *(--pp);
            }
          }
        }
      }
    }
  }
}

void PixmapInitFromBytemap(PyMOLGlobals * G, CPixmap * I,
                           int width,
                           int height,
                           int pitch,
                           unsigned char *bytemap,
                           unsigned char *rgba, unsigned char *outline_rgb, int flat)
{
  if(I) {

    int x, y;
    unsigned char *src, *sa, alp;
    unsigned char *dst;
    unsigned char red, blue, green, alpha, no_alpha;
    unsigned char ored = 0, oblue = 0, ogreen = 0;
    if(!outline_rgb[3])
      outline_rgb = NULL;
    else {
      ored = outline_rgb[0];
      ogreen = outline_rgb[1];
      oblue = outline_rgb[2];
    }
    PixmapInit(G, I, width, height);
    red = rgba[0];
    green = rgba[1];
    blue = rgba[2];
    alpha = rgba[3];
    UtilZeroMem(I->buffer, 4 * width * height);
    src = bytemap;
    dst = I->buffer;
    no_alpha = flat;
    for(y = 0; y < height; y++) {
      sa = src;
      if(no_alpha) {
        for(x = 0; x < width; x++) {
          alp = *(sa++);
          if(alp) {
            *(dst++) = red;
            *(dst++) = green;
            *(dst++) = blue;
            *(dst++) = 0xFF;
          } else {
            *(dst++) = 0;
            *(dst++) = 0;
            *(dst++) = 0;
            *(dst++) = 0;
          }
        }
      } else {
        for(x = 0; x < width; x++) {
          if(outline_rgb) {
            unsigned char amax = 0, amin;
            if(y > 0) {
              alp = 255 - *(sa - pitch);
            } else {
              alp = 255;
            }
            if(amax < alp)
              amax = alp;
            if(y < (height - 1)) {
              alp = 255 - *(sa + pitch);
            } else {
              alp = 255;
            }
            if(amax < alp)
              amax = alp;
            if(x > 0) {
              alp = 255 - *(sa - 1);
            } else {
              alp = 255;
            }
            if(amax < alp)
              amax = alp;
            if(x < (width - 1)) {
              alp = 255 - *(sa + 1);
            } else {
              alp = 255;
            }
            if(amax < alp)
              amax = alp;
            amin = 255 - amax;
            alp = *(sa++);
            if(alp) {
              *(dst++) = (red * amin + ored * amax) / 255;
              *(dst++) = (green * amin + ogreen * amax) / 255;
              *(dst++) = (blue * amin + oblue * amax) / 255;
              *(dst++) = (alpha * alp) / 255;
            } else {
              *(dst++) = 0;
              *(dst++) = 0;
              *(dst++) = 0;
              *(dst++) = 0;
            }
          } else {
            alp = *(sa++);
            if(alp) {
              *(dst++) = red;
              *(dst++) = green;
              *(dst++) = blue;
              *(dst++) = (alpha * alp) >> 8;
            } else {
              *(dst++) = 0;
              *(dst++) = 0;
              *(dst++) = 0;
              *(dst++) = 0;
            }
          }

        }
      }
      src += pitch;
    }
  }
}

void PixmapPurge(CPixmap * I)
{
  if(I) {
    FreeP(I->buffer);
  }
}

void PixmapFreeP(CPixmap * I)
{
  PixmapPurge(I);
  OOFreeP(I);
}