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

1.2.3 by Ondrej Certik
Import upstream version 0.6.0
1
/*
2
 * cell.m
3
 * p_ndx_cell, p_rgb_cell for Mac OS X.
4
 *
5
 * Copyright (c) 1999.  See accompanying LEGAL file for details.
6
 */
7
8
#include "playm.h"
9
10
static void m_cell(p_win *w, unsigned char *ndxs, unsigned char *rgbs,
11
                   int ncols, int nrows, int x0, int y0, int x1, int y1);
12
static void m_release_data(void *info, const void *data, size_t size);
13
14
15
void
16
p_ndx_cell(p_win *w, unsigned char *ndxs, int ncols, int nrows,
17
           int x0, int y0, int x1, int y1)
18
{
19
  m_cell(w, ndxs, 0, ncols, nrows, x0, y0, x1, y1);
20
}
21
22
void
23
p_rgb_cell(p_win *w, unsigned char *rgbs, int ncols, int nrows,
24
           int x0, int y0, int x1, int y1)
25
{
26
  m_cell(w, 0, rgbs, ncols, nrows, x0, y0, x1, y1);
27
}
28
29
void
30
p_rgb_read(p_win *w, unsigned char *rgbs,
31
           int x0, int y0, int x1, int y1)
32
{
33
  printf ("p_rgb_read\n");
34
  /* This function is needed for g_rgb_read, which is never called. */
35
}
36
37
static void
38
m_cell(p_win *w, unsigned char *ndxs, unsigned char *rgbs,
39
       int ncols, int nrows, int x0, int y0, int x1, int y1)
40
{
41
  View* view = w->view;
42
  if (p_signalling) {
43
    p_abort();
44
    return;
45
  }
46
  if (view) {
47
    CGContextRef cr = w->cr;
48
    const size_t nComponents = 3; /* red, green, blue */
49
    const size_t bytesPerComponent = 1;
50
    const size_t bitsPerComponent = 8 * bytesPerComponent;
51
    const size_t bitsPerPixel = bitsPerComponent * nComponents;
52
    const size_t bytesPerRow = nComponents * bytesPerComponent * ncols;
53
    const size_t size = bytesPerRow * nrows;
54
    unsigned char* data = (unsigned char*)malloc(size*sizeof(unsigned char));
55
    if (data)
56
    { int ii, jj;
57
      CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
58
      if (ndxs) {
59
        int kk = nrows*ncols;
60
        unsigned char* index = ndxs;
61
        for (ii = 0; ii < nrows; ii++) {
62
          kk -= ncols;
63
          for (jj = 0; jj < ncols; jj++, index++) {
64
            float* color = w->pixels[*index];
65
            data[3*(kk+jj)]   = 256 * color[0];
66
            data[3*(kk+jj)+1] = 256 * color[1];
67
            data[3*(kk+jj)+2] = 256 * color[2];
68
          }
69
        }
70
      } else {
71
        int kk = size;
72
        for (ii = 0; ii < size; ii+=bytesPerRow) {
73
          kk -= bytesPerRow;
74
          for (jj = 0; jj < bytesPerRow; jj++) {
75
            data[kk+jj] = rgbs[ii+jj]; 
76
          }
77
        }
78
      }
79
      CGDataProviderRef provider = CGDataProviderCreateWithData (NULL,
80
                                                                 data,
81
                                                                 size, 
82
                                                                 m_release_data); 
83
      CGImageRef bitmap = CGImageCreate (ncols,
84
                                         nrows,
85
                                         bitsPerComponent,
86
                                         bitsPerPixel,
87
                                         bytesPerRow,
88
                                         colorspace,
89
                                         kCGImageAlphaNone,
90
                                         provider,
91
                                         NULL,
92
                                         false,
93
                                         kCGRenderingIntentDefault);
94
      CGColorSpaceRelease(colorspace);
95
96
      if(bitmap)
97
      { CGContextDrawImage(cr, CGRectMake(x0,y0,x1-x0,y1-y0), bitmap);
98
        CGImageRelease(bitmap);
99
      }
100
      CGDataProviderRelease(provider);
101
    }
102
  }
103
}
104
105
static void
106
m_release_data(void *info, const void *data, size_t size)
107
{ unsigned char* p = (unsigned char*) data;
108
  free(p);
109
}