~ubuntu-branches/ubuntu/saucy/python-scipy/saucy

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
/*
 * images.c -- $Id: images.c 685 2003-03-08 15:26:51Z travo $
 * p_pixmap and p_rgbmap for X11
 *
 * Copyright (c) 1998.  See accompanying LEGAL file for details.
 */

#include "config.h"
#include "playx.h"

#include "pstdlib.h"

static void x_image(p_win *w, unsigned char *bytes, int rgb, int ncols,
                    int nrows, int x0, int y0, int x1, int y1);

static int x_put1(XImage *im, int i, int j, p_col_t pixel);
static int x_put2b(XImage *im, int i, int j, p_col_t pixel);
static int x_put2l(XImage *im, int i, int j, p_col_t pixel);
static int x_put3b(XImage *im, int i, int j, p_col_t pixel);
static int x_put3l(XImage *im, int i, int j, p_col_t pixel);
static int x_put4b(XImage *im, int i, int j, p_col_t pixel);
static int x_put4l(XImage *im, int i, int j, p_col_t pixel);

void
p_ndx_cell(p_win *w, unsigned char *ndxs, int ncols, int nrows,
           int x0, int y0, int x1, int y1)
{
  x_image(w, ndxs, 0, ncols, nrows, x0, y0, x1, y1);
}

void
p_rgb_cell(p_win *w, unsigned char *rgbs, int ncols, int nrows,
           int x0, int y0, int x1, int y1)
{
  x_image(w, rgbs, 1, ncols, nrows, x0, y0, x1, y1);
}

static void
x_image(p_win *w, unsigned char *bytes, int rgb, int ncols,
        int nrows, int x0, int y0, int x1, int y1)
{
  unsigned char *cell;
  int bpp, xa, ya, xb, yb, r, dr, dy, c, dc, dx;
  int width = x1-x0;
  int height = y1-y0;
  int yexpand = (height>=nrows);
  int xexpand = (width>=ncols);
  int drow = rgb? 3 : 1;
  int dcol = rgb? 3*ncols : ncols;
  p_scr *s = w->s;
  Display *dpy = s->xdpy->dpy;
  GC gc = x_getgc(s, w, FillSolid);
  XImage *im;

  if (s->image) x_imzap(s);
  s->own_image_data = 1;
  im = s->image = XCreateImage(dpy, DefaultVisual(dpy, s->scr_num),
                               s->depth, ZPixmap, 0, (char *)0,
                               width, height, BitmapPad(dpy), 0);
  bpp = im->bits_per_pixel;

  if (yexpand) {
    r = ((unsigned int)nrows)>>1;
    dr = height%nrows - nrows;
    dy = height/nrows;
  } else {
    r = ((unsigned int)height)>>1;
    dr = nrows%height - height;
    dy = (nrows/height)*dcol;
  }
  if (xexpand) {
    c = ((unsigned int)ncols)>>1;
    dc = width%ncols - ncols;
    dx = width/ncols;
  } else {
    c = ((unsigned int)width)>>1;
    dc = ncols%width - width;
    dx = (ncols/width)*drow;
  }

  if ((long)bpp*(long)width*(long)height > 288L*(long)ncols*(long)nrows) {
    /* less X protocol traffic in a series of filled rectangles */
    int c0 = ((unsigned int)c)>>1;
    x_imzap(s);  /* wasteful to have created image,
                  * but easiest way to get bpp... */
    for (ya=yb=y0 ; ya<y1 ; ya=yb) {
      r += dr;
      if (yexpand) {
        yb += dy;
        if (r>=0) yb++;
        else r += nrows;
      } else {
        yb++;
      }
      cell = bytes;
      c = c0;
      for (xa=xb=x0 ; xa<x1 ; xa=xb) {
        c += dc;
        if (xexpand) {
          xb += dx;
          if (c>=0) xb++;
          else c += ncols;
        } else {
          xb++;
        }
        p_color(w, rgb? P_RGB(cell[0], cell[1], cell[2]) : cell[0]);
        XFillRectangle(dpy, w->d, gc, xa, ya, xb-xa, yb-ya);
        if (xexpand) {
          cell += drow;
        } else {
          cell += dx;
          if (c>=0) cell += drow;
          else c += width;
        }
      }
      if (yexpand) {
        bytes += dcol;
      } else {
        bytes += dy;
        if (r>=0) bytes += dcol;
        else r += height;
      }
    }

  } else {
    /* less X protocol traffic in one image */
    int (*put_pixel)(XImage *, int, int, p_col_t)= 0;
    int bpl = im->bytes_per_line;
    p_col_t pixel;

    im->data = p_malloc(bpl*height);
    if (!im->data) { x_imzap(s); return; }

    if (bpp==8) {
      put_pixel = x_put1, bpp = 1;
    } else if (im->byte_order==MSBFirst) {
      if      (bpp==32) put_pixel = x_put4b, bpp = 4;
      else if (bpp==16) put_pixel = x_put2b, bpp = 2;
      else if (bpp==24) put_pixel = x_put3b, bpp = 3;
    } else {
      if      (bpp==32) put_pixel = x_put4l, bpp = 4;
      else if (bpp==16) put_pixel = x_put2l, bpp = 2;
      else if (bpp==24) put_pixel = x_put3l, bpp = 3;
    }

    if (put_pixel) {
      y1 = height*bpl;
      if (yexpand) dy *= bpl;
      x1 = width*bpp;
      if (xexpand) dx *= bpp;
    } else {
      /* generic case is slow, but still more efficient than setting
       * the image parameters and letting XPutImage do the work --
       * XPutImage uses XGetPixel as well as XPutPixel for this case
       * (this is same as XPutPixel macro in Xutil.h) */
      put_pixel = (int (*)(XImage *, int, int, p_col_t))im->f.put_pixel;
      if (bpp==1) p_color(w, P_FG);
      y1 = height;
      bpl = 1;
      x1 = width;
      bpp = 1;
    }

    if (ncols==width && nrows==height) {
      for (r=0 ; r<y1 ; r+=bpl)
        for (c=0 ; c<x1 ; c+=bpp) {
          pixel = rgb? P_RGB(bytes[0], bytes[1], bytes[2]) : bytes[0];
          bytes += drow;
          put_pixel(im, c, r, x_getpixel(w, pixel));
        }

    } else {
      int i, j;
      int c0 = ((unsigned int)c)>>1;
      for (ya=yb=0 ; yb<y1 ; ya=yb) {
        r += dr;
        if (yexpand) {
          yb += dy;
          if (r>=0) yb += bpl;
          else r += nrows;
        } else {
          yb += bpl;
        }
        cell = bytes;
        c = c0;
        for (xa=xb=0 ; xb<x1 ; xa=xb) {
          c += dc;
          if (xexpand) {
            xb += dx;
            if (c>=0) xb += bpp;
            else c += ncols;
          } else {
            xb += bpp;
          }
          pixel = rgb? P_RGB(cell[0], cell[1], cell[2]) : cell[0];
          pixel = x_getpixel(w, pixel);
          for (j=ya ; j<yb ; j+=bpl)
            for (i=xa ; i<xb ; i+=bpp)
              put_pixel(im, i, j, pixel);
          if (xexpand) {
            cell += drow;
          } else {
            cell += dx;
            if (c>=0) cell += drow;
            else c += width;
          }
        }
        if (yexpand) {
          bytes += dcol;
        } else {
          bytes += dy;
          if (r>=0) bytes += dcol;
          else r += height;
        }
      }
    }

    XPutImage(dpy, w->d, gc, im, 0,0, x0, y0, width, height);
    x_imzap(s);
  }
  if (p_signalling) p_abort();
}

static int
x_put1(XImage *im, int i, int j, p_col_t pixel)
{
  unsigned char *data = (unsigned char *)im->data;
  data[j+i] = pixel;
  return 0;
}

static int
x_put2b(XImage *im, int i, int j, p_col_t pixel)
{
  unsigned char *data = (unsigned char *)im->data;
  data[j+i+0] = pixel>>8;
  data[j+i+1] = pixel;
  return 0;
}

static int
x_put2l(XImage *im, int i, int j, p_col_t pixel)
{
  unsigned char *data = (unsigned char *)im->data;
  data[j+i+0] = pixel;
  data[j+i+1] = pixel>>8;
  return 0;
}

static int
x_put3b(XImage *im, int i, int j, p_col_t pixel)
{
  unsigned char *data = (unsigned char *)im->data;
  data[j+i+0] = pixel>>16;
  data[j+i+1] = pixel>>8;
  data[j+i+2] = pixel;
  return 0;
}

static int
x_put3l(XImage *im, int i, int j, p_col_t pixel)
{
  unsigned char *data = (unsigned char *)im->data;
  data[j+i+0] = pixel;
  data[j+i+1] = pixel>>8;
  data[j+i+2] = pixel>>16;
  return 0;
}

static int
x_put4b(XImage *im, int i, int j, p_col_t pixel)
{
  unsigned char *data = (unsigned char *)im->data;
  data[j+i+0] = pixel>>24;
  data[j+i+1] = pixel>>16;
  data[j+i+2] = pixel>>8;
  data[j+i+3] = pixel;
  return 0;
}

static int
x_put4l(XImage *im, int i, int j, p_col_t pixel)
{
  unsigned char *data = (unsigned char *)im->data;
  data[j+i+0] = pixel;
  data[j+i+1] = pixel>>8;
  data[j+i+2] = pixel>>16;
  data[j+i+3] = pixel>>24;
  return 0;
}