fixed a bunch of g++ warnings/errors. Compiling with g++ can help find lots of poten...
[mesa.git] / src / mesa / swrast / s_readpix.c
1 /* $Id: s_readpix.c,v 1.9 2001/03/07 05:06:12 brianp Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 3.5
6 *
7 * Copyright (C) 1999-2001 Brian Paul All Rights Reserved.
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
23 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 */
26
27
28 #include "glheader.h"
29 #include "colormac.h"
30 #include "convolve.h"
31 #include "context.h"
32 #include "feedback.h"
33 #include "image.h"
34 #include "macros.h"
35 #include "mem.h"
36 #include "pixel.h"
37
38 #include "s_alphabuf.h"
39 #include "s_context.h"
40 #include "s_depth.h"
41 #include "s_span.h"
42 #include "s_stencil.h"
43
44
45
46 /*
47 * Read a block of color index pixels.
48 */
49 static void
50 read_index_pixels( GLcontext *ctx,
51 GLint x, GLint y,
52 GLsizei width, GLsizei height,
53 GLenum type, GLvoid *pixels,
54 const struct gl_pixelstore_attrib *packing )
55 {
56 GLint i, readWidth;
57
58 /* error checking */
59 if (ctx->Visual.rgbMode) {
60 _mesa_error( ctx, GL_INVALID_OPERATION, "glReadPixels" );
61 return;
62 }
63
64 ASSERT(ctx->Driver.SetReadBuffer);
65 (*ctx->Driver.SetReadBuffer)(ctx, ctx->ReadBuffer,
66 ctx->Pixel.DriverReadBuffer);
67
68 readWidth = (width > MAX_WIDTH) ? MAX_WIDTH : width;
69
70 /* process image row by row */
71 for (i = 0; i < height; i++) {
72 GLuint index[MAX_WIDTH];
73 GLvoid *dest;
74
75 (*ctx->Driver.ReadCI32Span)(ctx, readWidth, x, y + i, index);
76
77 dest = _mesa_image_address(packing, pixels, width, height,
78 GL_COLOR_INDEX, type, 0, i, 0);
79
80 _mesa_pack_index_span(ctx, readWidth, type, dest, index,
81 &ctx->Pack, ctx->_ImageTransferState);
82 }
83
84 (*ctx->Driver.SetReadBuffer)(ctx, ctx->DrawBuffer,
85 ctx->Color.DriverDrawBuffer);
86 }
87
88
89
90 static void
91 read_depth_pixels( GLcontext *ctx,
92 GLint x, GLint y,
93 GLsizei width, GLsizei height,
94 GLenum type, GLvoid *pixels,
95 const struct gl_pixelstore_attrib *packing )
96 {
97 GLint readWidth;
98 GLboolean bias_or_scale;
99
100 /* Error checking */
101 if (ctx->Visual.depthBits <= 0) {
102 /* No depth buffer */
103 _mesa_error( ctx, GL_INVALID_OPERATION, "glReadPixels" );
104 return;
105 }
106
107 readWidth = (width > MAX_WIDTH) ? MAX_WIDTH : width;
108
109 if (type != GL_BYTE &&
110 type != GL_UNSIGNED_BYTE &&
111 type != GL_SHORT &&
112 type != GL_UNSIGNED_SHORT &&
113 type != GL_INT &&
114 type != GL_UNSIGNED_INT &&
115 type != GL_FLOAT) {
116 _mesa_error( ctx, GL_INVALID_OPERATION, "glReadPixels(depth type)");
117 return;
118 }
119
120 bias_or_scale = ctx->Pixel.DepthBias!=0.0 || ctx->Pixel.DepthScale!=1.0;
121
122 if (type==GL_UNSIGNED_SHORT && ctx->Visual.depthBits == 16
123 && !bias_or_scale && !packing->SwapBytes) {
124 /* Special case: directly read 16-bit unsigned depth values. */
125 GLint j;
126 for (j=0;j<height;j++,y++) {
127 GLdepth depth[MAX_WIDTH];
128 GLushort *dst = (GLushort*) _mesa_image_address( packing, pixels,
129 width, height, GL_DEPTH_COMPONENT, type, 0, j, 0 );
130 GLint i;
131 _mesa_read_depth_span(ctx, width, x, y, depth);
132 for (i = 0; i < width; i++)
133 dst[i] = depth[i];
134 }
135 }
136 else if (type==GL_UNSIGNED_INT && ctx->Visual.depthBits == 32
137 && !bias_or_scale && !packing->SwapBytes) {
138 /* Special case: directly read 32-bit unsigned depth values. */
139 GLint j;
140 for (j=0;j<height;j++,y++) {
141 GLdepth *dst = (GLdepth *) _mesa_image_address( packing, pixels,
142 width, height, GL_DEPTH_COMPONENT, type, 0, j, 0 );
143 _mesa_read_depth_span(ctx, width, x, y, dst);
144 }
145 }
146 else {
147 /* General case (slower) */
148 GLint j;
149 for (j=0;j<height;j++,y++) {
150 GLfloat depth[MAX_WIDTH];
151 GLvoid *dest;
152
153 _mesa_read_depth_span_float(ctx, readWidth, x, y, depth);
154
155 dest = _mesa_image_address(packing, pixels, width, height,
156 GL_DEPTH_COMPONENT, type, 0, j, 0);
157
158 _mesa_pack_depth_span(ctx, readWidth, (GLdepth *) dest, type,
159 depth, &ctx->Pack);
160 }
161 }
162 }
163
164
165
166 static void
167 read_stencil_pixels( GLcontext *ctx,
168 GLint x, GLint y,
169 GLsizei width, GLsizei height,
170 GLenum type, GLvoid *pixels,
171 const struct gl_pixelstore_attrib *packing )
172 {
173 GLint j, readWidth;
174
175 if (type != GL_BYTE &&
176 type != GL_UNSIGNED_BYTE &&
177 type != GL_SHORT &&
178 type != GL_UNSIGNED_SHORT &&
179 type != GL_INT &&
180 type != GL_UNSIGNED_INT &&
181 type != GL_FLOAT &&
182 type != GL_BITMAP) {
183 _mesa_error( ctx, GL_INVALID_OPERATION, "glReadPixels(stencil type)");
184 return;
185 }
186
187 readWidth = (width > MAX_WIDTH) ? MAX_WIDTH : width;
188
189 if (ctx->Visual.stencilBits <= 0) {
190 /* No stencil buffer */
191 _mesa_error( ctx, GL_INVALID_OPERATION, "glReadPixels" );
192 return;
193 }
194
195 /* process image row by row */
196 for (j=0;j<height;j++,y++) {
197 GLvoid *dest;
198 GLstencil stencil[MAX_WIDTH];
199
200 _mesa_read_stencil_span(ctx, readWidth, x, y, stencil);
201
202 dest = _mesa_image_address(packing, pixels, width, height,
203 GL_STENCIL_INDEX, type, 0, j, 0);
204
205 _mesa_pack_stencil_span(ctx, readWidth, type, dest, stencil, &ctx->Pack);
206 }
207 }
208
209
210
211 /*
212 * Optimized glReadPixels for particular pixel formats:
213 * GL_UNSIGNED_BYTE, GL_RGBA
214 * when pixel scaling, biasing and mapping are disabled.
215 */
216 static GLboolean
217 read_fast_rgba_pixels( GLcontext *ctx,
218 GLint x, GLint y,
219 GLsizei width, GLsizei height,
220 GLenum format, GLenum type,
221 GLvoid *pixels,
222 const struct gl_pixelstore_attrib *packing )
223 {
224 /* can't do scale, bias, mapping, etc */
225 if (ctx->_ImageTransferState)
226 return GL_FALSE;
227
228 /* can't do fancy pixel packing */
229 if (packing->Alignment != 1 || packing->SwapBytes || packing->LsbFirst)
230 return GL_FALSE;
231
232 {
233 GLint srcX = x;
234 GLint srcY = y;
235 GLint readWidth = width; /* actual width read */
236 GLint readHeight = height; /* actual height read */
237 GLint skipPixels = packing->SkipPixels;
238 GLint skipRows = packing->SkipRows;
239 GLint rowLength;
240
241 if (packing->RowLength > 0)
242 rowLength = packing->RowLength;
243 else
244 rowLength = width;
245
246 /* horizontal clipping */
247 if (srcX < ctx->ReadBuffer->_Xmin) {
248 skipPixels += (ctx->ReadBuffer->_Xmin - srcX);
249 readWidth -= (ctx->ReadBuffer->_Xmin - srcX);
250 srcX = ctx->ReadBuffer->_Xmin;
251 }
252 if (srcX + readWidth > ctx->ReadBuffer->_Xmax)
253 readWidth -= (srcX + readWidth - ctx->ReadBuffer->_Xmax);
254 if (readWidth <= 0)
255 return GL_TRUE;
256
257 /* vertical clipping */
258 if (srcY < ctx->ReadBuffer->_Ymin) {
259 skipRows += (ctx->ReadBuffer->_Ymin - srcY);
260 readHeight -= (ctx->ReadBuffer->_Ymin - srcY);
261 srcY = ctx->ReadBuffer->_Ymin;
262 }
263 if (srcY + readHeight > ctx->ReadBuffer->_Ymax)
264 readHeight -= (srcY + readHeight - ctx->ReadBuffer->_Ymax);
265 if (readHeight <= 0)
266 return GL_TRUE;
267
268 /*
269 * Ready to read!
270 * The window region at (destX, destY) of size (readWidth, readHeight)
271 * will be read back.
272 * We'll write pixel data to buffer pointed to by "pixels" but we'll
273 * skip "skipRows" rows and skip "skipPixels" pixels/row.
274 */
275 #if CHAN_BITS == 8
276 if (format == GL_RGBA && type == GL_UNSIGNED_BYTE) {
277 #elif CHAN_BITS == 16
278 if (format == GL_RGBA && type == GL_UNSIGNED_SHORT) {
279 #else
280 if (0) {
281 #endif
282 GLchan *dest = (GLchan *) pixels
283 + (skipRows * rowLength + skipPixels) * 4;
284 GLint row;
285 for (row=0; row<readHeight; row++) {
286 (*ctx->Driver.ReadRGBASpan)(ctx, readWidth, srcX, srcY,
287 (GLchan (*)[4]) dest);
288 if (ctx->DrawBuffer->UseSoftwareAlphaBuffers) {
289 _mesa_read_alpha_span(ctx, readWidth, srcX, srcY,
290 (GLchan (*)[4]) dest);
291 }
292 dest += rowLength * 4;
293 srcY++;
294 }
295 return GL_TRUE;
296 }
297 else {
298 /* can't do this format/type combination */
299 return GL_FALSE;
300 }
301 }
302 }
303
304
305
306 /*
307 * Read R, G, B, A, RGB, L, or LA pixels.
308 */
309 static void
310 read_rgba_pixels( GLcontext *ctx,
311 GLint x, GLint y,
312 GLsizei width, GLsizei height,
313 GLenum format, GLenum type, GLvoid *pixels,
314 const struct gl_pixelstore_attrib *packing )
315 {
316 GLint readWidth;
317
318 (*ctx->Driver.SetReadBuffer)(ctx, ctx->ReadBuffer, ctx->Pixel.DriverReadBuffer);
319
320 /* Try optimized path first */
321 if (read_fast_rgba_pixels( ctx, x, y, width, height,
322 format, type, pixels, packing )) {
323
324 (*ctx->Driver.SetReadBuffer)(ctx, ctx->DrawBuffer, ctx->Color.DriverDrawBuffer);
325 return; /* done! */
326 }
327
328 readWidth = (width > MAX_WIDTH) ? MAX_WIDTH : width;
329
330 /* do error checking on pixel type, format was already checked by caller */
331 switch (type) {
332 case GL_UNSIGNED_BYTE:
333 case GL_BYTE:
334 case GL_UNSIGNED_SHORT:
335 case GL_SHORT:
336 case GL_UNSIGNED_INT:
337 case GL_INT:
338 case GL_FLOAT:
339 case GL_UNSIGNED_BYTE_3_3_2:
340 case GL_UNSIGNED_BYTE_2_3_3_REV:
341 case GL_UNSIGNED_SHORT_5_6_5:
342 case GL_UNSIGNED_SHORT_5_6_5_REV:
343 case GL_UNSIGNED_SHORT_4_4_4_4:
344 case GL_UNSIGNED_SHORT_4_4_4_4_REV:
345 case GL_UNSIGNED_SHORT_5_5_5_1:
346 case GL_UNSIGNED_SHORT_1_5_5_5_REV:
347 case GL_UNSIGNED_INT_8_8_8_8:
348 case GL_UNSIGNED_INT_8_8_8_8_REV:
349 case GL_UNSIGNED_INT_10_10_10_2:
350 case GL_UNSIGNED_INT_2_10_10_10_REV:
351 /* valid pixel type */
352 break;
353 default:
354 _mesa_error( ctx, GL_INVALID_ENUM, "glReadPixels(type)" );
355 return;
356 }
357
358 if (!_mesa_is_legal_format_and_type(format, type) ||
359 format == GL_INTENSITY) {
360 _mesa_error(ctx, GL_INVALID_OPERATION, "glReadPixels(format or type)");
361 return;
362 }
363
364 if (ctx->Pixel.Convolution2DEnabled || ctx->Pixel.Separable2DEnabled) {
365 const GLuint transferOps = ctx->_ImageTransferState;
366 GLfloat *dest, *src, *tmpImage, *convImage;
367 GLint row;
368
369 tmpImage = (GLfloat *) MALLOC(width * height * 4 * sizeof(GLfloat));
370 if (!tmpImage) {
371 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glReadPixels");
372 return;
373 }
374 convImage = (GLfloat *) MALLOC(width * height * 4 * sizeof(GLfloat));
375 if (!convImage) {
376 FREE(tmpImage);
377 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glReadPixels");
378 return;
379 }
380
381 /* read full RGBA, FLOAT image */
382 dest = tmpImage;
383 for (row = 0; row < height; row++, y++) {
384 GLchan rgba[MAX_WIDTH][4];
385 if (ctx->Visual.rgbMode) {
386 _mesa_read_rgba_span(ctx, ctx->ReadBuffer, readWidth, x, y, rgba);
387 }
388 else {
389 GLuint index[MAX_WIDTH];
390 (*ctx->Driver.ReadCI32Span)(ctx, readWidth, x, y, index);
391 if (ctx->Pixel.IndexShift != 0 || ctx->Pixel.IndexOffset !=0 ) {
392 _mesa_map_ci(ctx, readWidth, index);
393 }
394 _mesa_map_ci_to_rgba_chan(ctx, readWidth, index, rgba);
395 }
396 _mesa_pack_rgba_span(ctx, readWidth, (const GLchan (*)[4]) rgba,
397 GL_RGBA, GL_FLOAT, dest, &_mesa_native_packing,
398 transferOps & IMAGE_PRE_CONVOLUTION_BITS);
399 dest += width * 4;
400 }
401
402 /* do convolution */
403 if (ctx->Pixel.Convolution2DEnabled) {
404 _mesa_convolve_2d_image(ctx, &readWidth, &height, tmpImage, convImage);
405 }
406 else {
407 ASSERT(ctx->Pixel.Separable2DEnabled);
408 _mesa_convolve_sep_image(ctx, &readWidth, &height, tmpImage, convImage);
409 }
410 FREE(tmpImage);
411
412 /* finish transfer ops and pack the resulting image */
413 src = convImage;
414 for (row = 0; row < height; row++) {
415 GLvoid *dest;
416 dest = _mesa_image_address(packing, pixels, readWidth, height,
417 format, type, 0, row, 0);
418 _mesa_pack_float_rgba_span(ctx, readWidth,
419 (const GLfloat (*)[4]) src,
420 format, type, dest, packing,
421 transferOps & IMAGE_POST_CONVOLUTION_BITS);
422 src += readWidth * 4;
423 }
424 }
425 else {
426 /* no convolution */
427 GLint row;
428 for (row = 0; row < height; row++, y++) {
429 GLchan rgba[MAX_WIDTH][4];
430 GLvoid *dst;
431 if (ctx->Visual.rgbMode) {
432 _mesa_read_rgba_span(ctx, ctx->ReadBuffer, readWidth, x, y, rgba);
433 }
434 else {
435 GLuint index[MAX_WIDTH];
436 (*ctx->Driver.ReadCI32Span)(ctx, readWidth, x, y, index);
437 if (ctx->Pixel.IndexShift != 0 || ctx->Pixel.IndexOffset != 0) {
438 _mesa_map_ci(ctx, readWidth, index);
439 }
440 _mesa_map_ci_to_rgba_chan(ctx, readWidth, index, rgba);
441 }
442 dst = _mesa_image_address(packing, pixels, width, height,
443 format, type, 0, row, 0);
444 if (ctx->Visual.redBits < CHAN_BITS ||
445 ctx->Visual.greenBits < CHAN_BITS ||
446 ctx->Visual.blueBits < CHAN_BITS) {
447 /* Requantize the color values into floating point and go from
448 * there. This fixes conformance failures with 16-bit color
449 * buffers, for example.
450 */
451 GLfloat rgbaf[MAX_WIDTH][4];
452 _mesa_chan_to_float_span(ctx, readWidth,
453 (CONST GLchan (*)[4]) rgba, rgbaf);
454 _mesa_pack_float_rgba_span(ctx, readWidth,
455 (CONST GLfloat (*)[4]) rgbaf,
456 format, type, dst, packing,
457 ctx->_ImageTransferState);
458 }
459 else {
460 /* GLubytes are fine */
461 _mesa_pack_rgba_span(ctx, readWidth, (CONST GLchan (*)[4]) rgba,
462 format, type, dst, packing,
463 ctx->_ImageTransferState);
464 }
465 }
466 }
467
468 (*ctx->Driver.SetReadBuffer)(ctx, ctx->DrawBuffer, ctx->Color.DriverDrawBuffer);
469 }
470
471
472
473 void
474 _swrast_ReadPixels( GLcontext *ctx,
475 GLint x, GLint y, GLsizei width, GLsizei height,
476 GLenum format, GLenum type,
477 const struct gl_pixelstore_attrib *pack,
478 GLvoid *pixels )
479 {
480 (void) pack;
481
482 if (SWRAST_CONTEXT(ctx)->NewState)
483 _swrast_validate_derived( ctx );
484
485 switch (format) {
486 case GL_COLOR_INDEX:
487 read_index_pixels(ctx, x, y, width, height, type, pixels, &ctx->Pack);
488 break;
489 case GL_STENCIL_INDEX:
490 read_stencil_pixels(ctx, x,y, width,height, type, pixels, &ctx->Pack);
491 break;
492 case GL_DEPTH_COMPONENT:
493 read_depth_pixels(ctx, x, y, width, height, type, pixels, &ctx->Pack);
494 break;
495 case GL_RED:
496 case GL_GREEN:
497 case GL_BLUE:
498 case GL_ALPHA:
499 case GL_RGB:
500 case GL_LUMINANCE:
501 case GL_LUMINANCE_ALPHA:
502 case GL_RGBA:
503 case GL_BGR:
504 case GL_BGRA:
505 case GL_ABGR_EXT:
506 read_rgba_pixels(ctx, x, y, width, height,
507 format, type, pixels, &ctx->Pack);
508 break;
509 default:
510 _mesa_error( ctx, GL_INVALID_ENUM, "glReadPixels(format)" );
511 }
512 }