File: pygdioctx.c

package info (click to toggle)
mapserver 7.0.4-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 16,500 kB
  • ctags: 20,398
  • sloc: ansic: 172,418; cpp: 57,449; python: 2,972; xml: 1,676; cs: 997; yacc: 855; lex: 758; java: 588; perl: 428; makefile: 375; sh: 190; tcl: 158; ruby: 55
file content (161 lines) | stat: -rw-r--r-- 5,599 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
/******************************************************************************
 * $Id$
 *
 * Project:  MapServer
 * Purpose:  Interface Python file-like objects with GD through IOCtx
 * Author:   Sean Gillies, sgillies@frii.com
 *
 ******************************************************************************
 * The PyFileIfaceObj_IOCtx API is
 *
 * Copyright 1995 Richard Jones, Bureau of Meteorology Australia.
 * richard@bofh.asn.au
 *
 * Current maintainer is
 * Chris Gonnerman <chris.gonnerman@newcenturycomputers.net>
 * Please direct all questions and problems to me.
 *
 ******************************************************************************
 * GDMODULE LICENSE:
 * gdmodule - Python GD module Copyright (c) 1995 Richard Jones All rights
 * reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in
 * the documentation and/or other materials provided with the
 * distribution.  Neither the name of the Bureau of Meteorology
 * Australia nor the names of its contributors may be used to endorse
 * or promote products derived from this software without specific
 * prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * ***************************************************************************/

#ifdef USE_GD

#include "pygdioctx.h"

int PyFileIfaceObj_IOCtx_GetC(msIOCtx *ctx)
{
  struct PyFileIfaceObj_gdIOCtx *pctx = (struct PyFileIfaceObj_gdIOCtx *)ctx;
  if (pctx->strObj) {
    Py_DECREF(pctx->strObj);
    pctx->strObj = NULL;
  }
  pctx->strObj = PyObject_CallMethod(pctx->fileIfaceObj, "read", "i", 1);
  if (!pctx->strObj || !PyString_Check(pctx->strObj)) {
    return EOF;
  }
  if (PyString_GET_SIZE(pctx->strObj) == 1) {
    return (int)(unsigned char)PyString_AS_STRING(pctx->strObj)[0];
  }
  return EOF;
}

int PyFileIfaceObj_IOCtx_GetBuf(msIOCtx *ctx, void *data, int size)
{
  int err;
  char *value;
  struct PyFileIfaceObj_gdIOCtx *pctx = (struct PyFileIfaceObj_gdIOCtx *)ctx;
  if (pctx->strObj) {
    Py_DECREF(pctx->strObj);
    pctx->strObj = NULL;
  }
  pctx->strObj = PyObject_CallMethod(pctx->fileIfaceObj, "read", "i", size);
  if (!pctx->strObj) {
    return 0;
  }
  err = PyString_AsStringAndSize(pctx->strObj, &value, &size);
  if (err < 0) {
    /* this throws away the python exception since the gd library
     * won't pass it up properly.  gdmodule should create its own
     * since the "file" couldn't be read properly.  */
    PyErr_Clear();
    return 0;
  }
  memcpy(data, value, size);
  return size;
}

void PyFileIfaceObj_IOCtx_Free(msIOCtx *ctx)
{
  struct PyFileIfaceObj_gdIOCtx *pctx = (struct PyFileIfaceObj_gdIOCtx *)ctx;
  if (pctx->strObj) {
    Py_DECREF(pctx->strObj);
    pctx->strObj = NULL;
  }
  if (pctx->fileIfaceObj) {
    Py_DECREF(pctx->fileIfaceObj);
    pctx->fileIfaceObj = NULL;
  }
  /* NOTE: we leave deallocation of the ctx structure itself to outside
   * code for memory allocation symmetry.  This function is safe to
   * call multiple times (gd should call it + we call it to be safe). */
}

struct PyFileIfaceObj_gdIOCtx * alloc_PyFileIfaceObj_IOCtx(PyObject *fileIfaceObj) {
  struct PyFileIfaceObj_gdIOCtx *pctx;
  pctx = calloc(1, sizeof(struct PyFileIfaceObj_gdIOCtx));
  if (!pctx)
    return NULL;
  pctx->ctx.getC = PyFileIfaceObj_IOCtx_GetC;
  pctx->ctx.getBuf = PyFileIfaceObj_IOCtx_GetBuf;
  pctx->ctx.gd_free = PyFileIfaceObj_IOCtx_Free;
  Py_INCREF(fileIfaceObj);
  pctx->fileIfaceObj = fileIfaceObj;
  return pctx;
}

void free_PyFileIfaceObj_IOCtx(struct PyFileIfaceObj_gdIOCtx *pctx)
{
  if (!pctx)
    return;
  assert(pctx->ctx.gd_free != NULL);
  pctx->ctx.gd_free((gdIOCtxPtr)pctx);
  free(pctx);
}

/* ===========================================================================
 * Mapserver function createImageObjFromPyFile by Sean Gillies,
 * <sgillies@frii.com>
 * ======================================================================== */

imageObj *createImageObjFromPyFile(PyObject *file, const char *driver)
{
  imageObj *image=NULL;
  struct PyFileIfaceObj_gdIOCtx *pctx;

  if (file == Py_None) {
    msSetError(MS_IMGERR, "NULL file object",
               "createImageObjFromPyFile()");
    return NULL;
  } else if (!driver) {
    msSetError(MS_IMGERR, "NULL or invalid driver string",
               "createImageObjFromPyFile()");
    return NULL;
  } else {
    pctx = alloc_PyFileIfaceObj_IOCtx(file);
    //image = msImageLoadGDCtx((gdIOCtx *) pctx, driver);
    free_PyFileIfaceObj_IOCtx(pctx);
    return image;
  }
}

#endif