1/*
2 * SGI FREE SOFTWARE LICENSE B (Version 2.0, Sept. 18, 2008)
3 * Copyright (C) 1991-2000 Silicon Graphics, Inc. All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice including the dates of first publication and
13 * either this permission notice or a reference to
14 * http://oss.sgi.com/projects/FreeB/
15 * shall be included in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * SILICON GRAPHICS, INC. BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
22 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 *
25 * Except as contained in this notice, the name of Silicon Graphics, Inc.
26 * shall not be used in advertising or otherwise to promote the sale, use or
27 * other dealings in this Software without prior written authorization from
28 * Silicon Graphics, Inc.
29 */
30
31#include "packsingle.h"
32#include "indirect.h"
33#include "glapi.h"
34#include <GL/glxproto.h>
35
36void
37__indirect_glGetSeparableFilter(GLenum target, GLenum format, GLenum type,
38                                GLvoid * row, GLvoid * column, GLvoid * span)
39{
40   __GLX_SINGLE_DECLARE_VARIABLES();
41   const __GLXattribute *state;
42   xGLXGetSeparableFilterReply reply;
43   GLubyte *rowBuf, *colBuf;
44
45   if (!dpy)
46      return;
47   __GLX_SINGLE_LOAD_VARIABLES();
48   state = gc->client_state_private;
49
50   /* Send request */
51   __GLX_SINGLE_BEGIN(X_GLsop_GetSeparableFilter, __GLX_PAD(13));
52   __GLX_SINGLE_PUT_LONG(0, target);
53   __GLX_SINGLE_PUT_LONG(4, format);
54   __GLX_SINGLE_PUT_LONG(8, type);
55   __GLX_SINGLE_PUT_CHAR(12, state->storePack.swapEndian);
56   __GLX_SINGLE_READ_XREPLY();
57   compsize = reply.length << 2;
58
59   if (compsize != 0) {
60      GLint width, height;
61      GLint widthsize, heightsize;
62
63      width = reply.width;
64      height = reply.height;
65
66      widthsize = __glImageSize(width, 1, 1, format, type, 0);
67      heightsize = __glImageSize(height, 1, 1, format, type, 0);
68
69      /* Allocate a holding buffer to transform the data from */
70      rowBuf = malloc(widthsize);
71      if (!rowBuf) {
72         /* Throw data away */
73         _XEatData(dpy, compsize);
74         __glXSetError(gc, GL_OUT_OF_MEMORY);
75         UnlockDisplay(dpy);
76         SyncHandle();
77         return;
78      }
79      else {
80         __GLX_SINGLE_GET_CHAR_ARRAY(((char *) rowBuf), widthsize);
81         __glEmptyImage(gc, 1, width, 1, 1, format, type, rowBuf, row);
82         free((char *) rowBuf);
83      }
84      colBuf = malloc(heightsize);
85      if (!colBuf) {
86         /* Throw data away */
87         _XEatData(dpy, compsize - __GLX_PAD(widthsize));
88         __glXSetError(gc, GL_OUT_OF_MEMORY);
89         UnlockDisplay(dpy);
90         SyncHandle();
91         return;
92      }
93      else {
94         __GLX_SINGLE_GET_CHAR_ARRAY(((char *) colBuf), heightsize);
95         __glEmptyImage(gc, 1, height, 1, 1, format, type, colBuf, column);
96         free((char *) colBuf);
97      }
98   }
99   else {
100      /*
101       ** don't modify user's buffer.
102       */
103   }
104   __GLX_SINGLE_END();
105
106}
107
108
109/* it is defined to gl_dispatch_stub_NNN in indirect.h */
110void gl_dispatch_stub_GetSeparableFilterEXT (GLenum target, GLenum format,
111                                             GLenum type, GLvoid * row,
112                                             GLvoid * column, GLvoid * span)
113{
114   struct glx_context *const gc = __glXGetCurrentContext();
115
116#if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
117   if (gc->isDirect) {
118      const _glapi_proc *const table = (_glapi_proc *) GET_DISPATCH();
119      PFNGLGETSEPARABLEFILTEREXTPROC p =
120         (PFNGLGETSEPARABLEFILTEREXTPROC) table[359];
121
122      p(target, format, type, row, column, span);
123      return;
124   }
125   else
126#endif
127   {
128      Display *const dpy = gc->currentDpy;
129      const GLuint cmdlen = __GLX_PAD(13);
130
131      if (dpy != NULL) {
132         const __GLXattribute *const state = gc->client_state_private;
133         xGLXGetSeparableFilterReply reply;
134         GLubyte const *pc =
135            __glXSetupVendorRequest(gc, X_GLXVendorPrivateWithReply,
136                                    X_GLvop_GetSeparableFilterEXT, cmdlen);
137         unsigned compsize;
138
139
140         (void) memcpy((void *) (pc + 0), (void *) (&target), 4);
141         (void) memcpy((void *) (pc + 4), (void *) (&format), 4);
142         (void) memcpy((void *) (pc + 8), (void *) (&type), 4);
143         *(int8_t *) (pc + 12) = state->storePack.swapEndian;
144
145         (void) _XReply(dpy, (xReply *) & reply, 0, False);
146
147         compsize = reply.length << 2;
148
149         if (compsize != 0) {
150            const GLint width = reply.width;
151            const GLint height = reply.height;
152            const GLint widthsize =
153               __glImageSize(width, 1, 1, format, type, 0);
154            const GLint heightsize =
155               __glImageSize(height, 1, 1, format, type, 0);
156            GLubyte *const buf =
157               malloc((widthsize > heightsize) ? widthsize : heightsize);
158
159            if (buf == NULL) {
160               /* Throw data away */
161               _XEatData(dpy, compsize);
162               __glXSetError(gc, GL_OUT_OF_MEMORY);
163
164               UnlockDisplay(dpy);
165               SyncHandle();
166               return;
167            }
168            else {
169               int extra;
170
171               extra = 4 - (widthsize & 3);
172               _XRead(dpy, (char *) buf, widthsize);
173               if (extra < 4) {
174                  _XEatData(dpy, extra);
175               }
176
177               __glEmptyImage(gc, 1, width, 1, 1, format, type, buf, row);
178
179               extra = 4 - (heightsize & 3);
180               _XRead(dpy, (char *) buf, heightsize);
181               if (extra < 4) {
182                  _XEatData(dpy, extra);
183               }
184
185               __glEmptyImage(gc, 1, height, 1, 1, format, type, buf, column);
186
187               free((char *) buf);
188            }
189         }
190      }
191   }
192}
193