guard ResetStipple calls
[mesa.git] / src / mesa / swrast / s_readpix.c
1 /* $Id: s_readpix.c,v 1.12 2001/05/16 20:27: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 SWcontext *swrast = SWRAST_CONTEXT(ctx);
57 GLint i, readWidth;
58
59 /* error checking */
60 if (ctx->Visual.rgbMode) {
61 _mesa_error( ctx, GL_INVALID_OPERATION, "glReadPixels" );
62 return;
63 }
64
65 ASSERT(swrast->Driver.SetReadBuffer);
66 (*swrast->Driver.SetReadBuffer)(ctx, ctx->ReadBuffer,
67 ctx->Pixel.DriverReadBuffer);
68
69 readWidth = (width > MAX_WIDTH) ? MAX_WIDTH : width;
70
71 /* process image row by row */
72 for (i = 0; i < height; i++) {
73 GLuint index[MAX_WIDTH];
74 GLvoid *dest;
75
76 (*swrast->Driver.ReadCI32Span)(ctx, readWidth, x, y + i, index);
77
78 dest = _mesa_image_address(packing, pixels, width, height,
79 GL_COLOR_INDEX, type, 0, i, 0);
80
81 _mesa_pack_index_span(ctx, readWidth, type, dest, index,
82 &ctx->Pack, ctx->_ImageTransferState);
83 }
84
85 (*swrast->Driver.SetReadBuffer)(ctx, ctx->DrawBuffer,
86 ctx->Color.DriverDrawBuffer);
87 }
88
89
90
91 static void
92 read_depth_pixels( GLcontext *ctx,
93 GLint x, GLint y,
94 GLsizei width, GLsizei height,
95 GLenum type, GLvoid *pixels,
96 const struct gl_pixelstore_attrib *packing )
97 {
98 GLint readWidth;
99 GLboolean bias_or_scale;
100
101 /* Error checking */
102 if (ctx->Visual.depthBits <= 0) {
103 /* No depth buffer */
104 _mesa_error( ctx, GL_INVALID_OPERATION, "glReadPixels" );
105 return;
106 }
107
108 readWidth = (width > MAX_WIDTH) ? MAX_WIDTH : width;
109
110 if (type != GL_BYTE &&
111 type != GL_UNSIGNED_BYTE &&
112 type != GL_SHORT &&
113 type != GL_UNSIGNED_SHORT &&
114 type != GL_INT &&
115 type != GL_UNSIGNED_INT &&
116 type != GL_FLOAT) {
117 _mesa_error( ctx, GL_INVALID_OPERATION, "glReadPixels(depth type)");
118 return;
119 }
120
121 bias_or_scale = ctx->Pixel.DepthBias!=0.0 || ctx->Pixel.DepthScale!=1.0;
122
123 if (type==GL_UNSIGNED_SHORT && ctx->Visual.depthBits == 16
124 && !bias_or_scale && !packing->SwapBytes) {
125 /* Special case: directly read 16-bit unsigned depth values. */
126 GLint j;
127 for (j=0;j<height;j++,y++) {
128 GLdepth depth[MAX_WIDTH];
129 GLushort *dst = (GLushort*) _mesa_image_address( packing, pixels,
130 width, height, GL_DEPTH_COMPONENT, type, 0, j, 0 );
131 GLint i;
132 _mesa_read_depth_span(ctx, width, x, y, depth);
133 for (i = 0; i < width; i++)
134 dst[i] = depth[i];
135 }
136 }
137 else if (type==GL_UNSIGNED_INT && ctx->Visual.depthBits == 32
138 && !bias_or_scale && !packing->SwapBytes) {
139 /* Special case: directly read 32-bit unsigned depth values. */
140 GLint j;
141 for (j=0;j<height;j++,y++) {
142 GLdepth *dst = (GLdepth *) _mesa_image_address( packing, pixels,
143 width, height, GL_DEPTH_COMPONENT, type, 0, j, 0 );
144 _mesa_read_depth_span(ctx, width, x, y, dst);
145 }
146 }
147 else {
148 /* General case (slower) */
149 GLint j;
150 for (j=0;j<height;j++,y++) {
151 GLfloat depth[MAX_WIDTH];
152 GLvoid *dest;
153
154 _mesa_read_depth_span_float(ctx, readWidth, x, y, depth);
155
156 dest = _mesa_image_address(packing, pixels, width, height,
157 GL_DEPTH_COMPONENT, type, 0, j, 0);
158
159 _mesa_pack_depth_span(ctx, readWidth, (GLdepth *) dest, type,
160 depth, &ctx->Pack);
161 }
162 }
163 }
164
165
166
167 static void
168 read_stencil_pixels( GLcontext *ctx,
169 GLint x, GLint y,
170 GLsizei width, GLsizei height,
171 GLenum type, GLvoid *pixels,
172 const struct gl_pixelstore_attrib *packing )
173 {
174 GLint j, readWidth;
175
176 if (type != GL_BYTE &&
177 type != GL_UNSIGNED_BYTE &&
178 type != GL_SHORT &&
179 type != GL_UNSIGNED_SHORT &&
180 type != GL_INT &&
181 type != GL_UNSIGNED_INT &&
182 type != GL_FLOAT &&
183 type != GL_BITMAP) {
184 _mesa_error( ctx, GL_INVALID_OPERATION, "glReadPixels(stencil type)");
185 return;
186 }
187
188 readWidth = (width > MAX_WIDTH) ? MAX_WIDTH : width;
189
190 if (ctx->Visual.stencilBits <= 0) {
191 /* No stencil buffer */
192 _mesa_error( ctx, GL_INVALID_OPERATION, "glReadPixels" );
193 return;
194 }
195
196 /* process image row by row */
197 for (j=0;j<height;j++,y++) {
198 GLvoid *dest;
199 GLstencil stencil[MAX_WIDTH];
200
201 _mesa_read_stencil_span(ctx, readWidth, x, y, stencil);
202
203 dest = _mesa_image_address(packing, pixels, width, height,
204 GL_STENCIL_INDEX, type, 0, j, 0);
205
206 _mesa_pack_stencil_span(ctx, readWidth, type, dest, stencil, &ctx->Pack);
207 }
208 }
209
210
211
212 /*
213 * Optimized glReadPixels for particular pixel formats:
214 * GL_UNSIGNED_BYTE, GL_RGBA
215 * when pixel scaling, biasing and mapping are disabled.
216 */
217 static GLboolean
218 read_fast_rgba_pixels( GLcontext *ctx,
219 GLint x, GLint y,
220 GLsizei width, GLsizei height,
221 GLenum format, GLenum type,
222 GLvoid *pixels,
223 const struct gl_pixelstore_attrib *packing )
224 {
225 SWcontext *swrast = SWRAST_CONTEXT(ctx);
226 /* can't do scale, bias, mapping, etc */
227 if (ctx->_ImageTransferState)
228 return GL_FALSE;
229
230 /* can't do fancy pixel packing */
231 if (packing->Alignment != 1 || packing->SwapBytes || packing->LsbFirst)
232 return GL_FALSE;
233
234 {
235 GLint srcX = x;
236 GLint srcY = y;
237 GLint readWidth = width; /* actual width read */
238 GLint readHeight = height; /* actual height read */
239 GLint skipPixels = packing->SkipPixels;
240 GLint skipRows = packing->SkipRows;
241 GLint rowLength;
242
243 if (packing->RowLength > 0)
244 rowLength = packing->RowLength;
245 else
246 rowLength = width;
247
248 /* horizontal clipping */
249 if (srcX < ctx->ReadBuffer->_Xmin) {
250 skipPixels += (ctx->ReadBuffer->_Xmin - srcX);
251 readWidth -= (ctx->ReadBuffer->_Xmin - srcX);
252 srcX = ctx->ReadBuffer->_Xmin;
253 }
254 if (srcX + readWidth > ctx->ReadBuffer->_Xmax)
255 readWidth -= (srcX + readWidth - ctx->ReadBuffer->_Xmax);
256 if (readWidth <= 0)
257 return GL_TRUE;
258
259 /* vertical clipping */
260 if (srcY < ctx->ReadBuffer->_Ymin) {
261 skipRows += (ctx->ReadBuffer->_Ymin - srcY);
262 readHeight -= (ctx->ReadBuffer->_Ymin - srcY);
263 srcY = ctx->ReadBuffer->_Ymin;
264 }
265 if (srcY + readHeight > ctx->ReadBuffer->_Ymax)
266 readHeight -= (srcY + readHeight - ctx->ReadBuffer->_Ymax);
267 if (readHeight <= 0)
268 return GL_TRUE;
269
270 /*
271 * Ready to read!
272 * The window region at (destX, destY) of size (readWidth, readHeight)
273 * will be read back.
274 * We'll write pixel data to buffer pointed to by "pixels" but we'll
275 * skip "skipRows" rows and skip "skipPixels" pixels/row.
276 */
277 #if CHAN_BITS == 8
278 if (format == GL_RGBA && type == GL_UNSIGNED_BYTE) {
279 #elif CHAN_BITS == 16
280 if (format == GL_RGBA && type == GL_UNSIGNED_SHORT) {
281 #else
282 if (0) {
283 #endif
284 GLchan *dest = (GLchan *) pixels
285 + (skipRows * rowLength + skipPixels) * 4;
286 GLint row;
287 for (row=0; row<readHeight; row++) {
288 (*swrast->Driver.ReadRGBASpan)(ctx, readWidth, srcX, srcY,
289 (GLchan (*)[4]) dest);
290 if (ctx->DrawBuffer->UseSoftwareAlphaBuffers) {
291 _mesa_read_alpha_span(ctx, readWidth, srcX, srcY,
292 (GLchan (*)[4]) dest);
293 }
294 dest += rowLength * 4;
295 srcY++;
296 }
297 return GL_TRUE;
298 }
299 else {
300 /* can't do this format/type combination */
301 return GL_FALSE;
302 }
303 }
304 }
305
306
307
308 /*
309 * Read R, G, B, A, RGB, L, or LA pixels.
310 */
311 static void
312 read_rgba_pixels( GLcontext *ctx,
313 GLint x, GLint y,
314 GLsizei width, GLsizei height,
315 GLenum format, GLenum type, GLvoid *pixels,
316 const struct gl_pixelstore_attrib *packing )
317 {
318 SWcontext *swrast = SWRAST_CONTEXT(ctx);
319 GLint readWidth;
320
321 (*swrast->Driver.SetReadBuffer)(ctx, ctx->ReadBuffer, ctx->Pixel.DriverReadBuffer);
322
323 /* Try optimized path first */
324 if (read_fast_rgba_pixels( ctx, x, y, width, height,
325 format, type, pixels, packing )) {
326
327 (*swrast->Driver.SetReadBuffer)(ctx, ctx->DrawBuffer, ctx->Color.DriverDrawBuffer);
328 return; /* done! */
329 }
330
331 readWidth = (width > MAX_WIDTH) ? MAX_WIDTH : width;
332
333 /* do error checking on pixel type, format was already checked by caller */
334 switch (type) {
335 case GL_UNSIGNED_BYTE:
336 case GL_BYTE:
337 case GL_UNSIGNED_SHORT:
338 case GL_SHORT:
339 case GL_UNSIGNED_INT:
340 case GL_INT:
341 case GL_FLOAT:
342 case GL_UNSIGNED_BYTE_3_3_2:
343 case GL_UNSIGNED_BYTE_2_3_3_REV:
344 case GL_UNSIGNED_SHORT_5_6_5:
345 case GL_UNSIGNED_SHORT_5_6_5_REV:
346 case GL_UNSIGNED_SHORT_4_4_4_4:
347 case GL_UNSIGNED_SHORT_4_4_4_4_REV:
348 case GL_UNSIGNED_SHORT_5_5_5_1:
349 case GL_UNSIGNED_SHORT_1_5_5_5_REV:
350 case GL_UNSIGNED_INT_8_8_8_8:
351 case GL_UNSIGNED_INT_8_8_8_8_REV:
352 case GL_UNSIGNED_INT_10_10_10_2:
353 case GL_UNSIGNED_INT_2_10_10_10_REV:
354 /* valid pixel type */
355 break;
356 default:
357 _mesa_error( ctx, GL_INVALID_ENUM, "glReadPixels(type)" );
358 return;
359 }
360
361 if (!_mesa_is_legal_format_and_type(format, type) ||
362 format == GL_INTENSITY) {
363 _mesa_error(ctx, GL_INVALID_OPERATION, "glReadPixels(format or type)");
364 return;
365 }
366
367 if (ctx->Pixel.Convolution2DEnabled || ctx->Pixel.Separable2DEnabled) {
368 const GLuint transferOps = ctx->_ImageTransferState;
369 GLfloat *dest, *src, *tmpImage, *convImage;
370 GLint row;
371
372 tmpImage = (GLfloat *) MALLOC(width * height * 4 * sizeof(GLfloat));
373 if (!tmpImage) {
374 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glReadPixels");
375 return;
376 }
377 convImage = (GLfloat *) MALLOC(width * height * 4 * sizeof(GLfloat));
378 if (!convImage) {
379 FREE(tmpImage);
380 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glReadPixels");
381 return;
382 }
383
384 /* read full RGBA, FLOAT image */
385 dest = tmpImage;
386 for (row = 0; row < height; row++, y++) {
387 GLchan rgba[MAX_WIDTH][4];
388 if (ctx->Visual.rgbMode) {
389 _mesa_read_rgba_span(ctx, ctx->ReadBuffer, readWidth, x, y, rgba);
390 }
391 else {
392 GLuint index[MAX_WIDTH];
393 (*swrast->Driver.ReadCI32Span)(ctx, readWidth, x, y, index);
394 if (ctx->Pixel.IndexShift != 0 || ctx->Pixel.IndexOffset !=0 ) {
395 _mesa_map_ci(ctx, readWidth, index);
396 }
397 _mesa_map_ci_to_rgba_chan(ctx, readWidth, index, rgba);
398 }
399 _mesa_pack_rgba_span(ctx, readWidth, (const GLchan (*)[4]) rgba,
400 GL_RGBA, GL_FLOAT, dest, &_mesa_native_packing,
401 transferOps & IMAGE_PRE_CONVOLUTION_BITS);
402 dest += width * 4;
403 }
404
405 /* do convolution */
406 if (ctx->Pixel.Convolution2DEnabled) {
407 _mesa_convolve_2d_image(ctx, &readWidth, &height, tmpImage, convImage);
408 }
409 else {
410 ASSERT(ctx->Pixel.Separable2DEnabled);
411 _mesa_convolve_sep_image(ctx, &readWidth, &height, tmpImage, convImage);
412 }
413 FREE(tmpImage);
414
415 /* finish transfer ops and pack the resulting image */
416 src = convImage;
417 for (row = 0; row < height; row++) {
418 GLvoid *dest;
419 dest = _mesa_image_address(packing, pixels, readWidth, height,
420 format, type, 0, row, 0);
421 _mesa_pack_float_rgba_span(ctx, readWidth,
422 (const GLfloat (*)[4]) src,
423 format, type, dest, packing,
424 transferOps & IMAGE_POST_CONVOLUTION_BITS);
425 src += readWidth * 4;
426 }
427 }
428 else {
429 /* no convolution */
430 GLint row;
431 for (row = 0; row < height; row++, y++) {
432 GLchan rgba[MAX_WIDTH][4];
433 GLvoid *dst;
434 if (ctx->Visual.rgbMode) {
435 _mesa_read_rgba_span(ctx, ctx->ReadBuffer, readWidth, x, y, rgba);
436 }
437 else {
438 GLuint index[MAX_WIDTH];
439 (*swrast->Driver.ReadCI32Span)(ctx, readWidth, x, y, index);
440 if (ctx->Pixel.IndexShift != 0 || ctx->Pixel.IndexOffset != 0) {
441 _mesa_map_ci(ctx, readWidth, index);
442 }
443 _mesa_map_ci_to_rgba_chan(ctx, readWidth, index, rgba);
444 }
445 dst = _mesa_image_address(packing, pixels, width, height,
446 format, type, 0, row, 0);
447 if (ctx->Visual.redBits < CHAN_BITS ||
448 ctx->Visual.greenBits < CHAN_BITS ||
449 ctx->Visual.blueBits < CHAN_BITS) {
450 /* Requantize the color values into floating point and go from
451 * there. This fixes conformance failures with 16-bit color
452 * buffers, for example.
453 */
454 DEFMARRAY(GLfloat, rgbaf, MAX_WIDTH, 4); /* mac 32k limitation */
455 CHECKARRAY(rgbaf, return); /* mac 32k limitation */
456 _mesa_chan_to_float_span(ctx, readWidth,
457 (CONST GLchan (*)[4]) rgba, rgbaf);
458 _mesa_pack_float_rgba_span(ctx, readWidth,
459 (CONST GLfloat (*)[4]) rgbaf,
460 format, type, dst, packing,
461 ctx->_ImageTransferState);
462 UNDEFARRAY(rgbaf); /* mac 32k limitation */
463 }
464 else {
465 /* GLubytes are fine */
466 _mesa_pack_rgba_span(ctx, readWidth, (CONST GLchan (*)[4]) rgba,
467 format, type, dst, packing,
468 ctx->_ImageTransferState);
469 }
470 }
471 }
472
473 (*swrast->Driver.SetReadBuffer)(ctx, ctx->DrawBuffer, ctx->Color.DriverDrawBuffer);
474 }
475
476
477
478 void
479 _swrast_ReadPixels( GLcontext *ctx,
480 GLint x, GLint y, GLsizei width, GLsizei height,
481 GLenum format, GLenum type,
482 const struct gl_pixelstore_attrib *pack,
483 GLvoid *pixels )
484 {
485 SWcontext *swrast = SWRAST_CONTEXT(ctx);
486 (void) pack;
487
488 if (swrast->NewState)
489 _swrast_validate_derived( ctx );
490
491 RENDER_START(swrast,ctx);
492
493 switch (format) {
494 case GL_COLOR_INDEX:
495 read_index_pixels(ctx, x, y, width, height, type, pixels, &ctx->Pack);
496 break;
497 case GL_STENCIL_INDEX:
498 read_stencil_pixels(ctx, x,y, width,height, type, pixels, &ctx->Pack);
499 break;
500 case GL_DEPTH_COMPONENT:
501 read_depth_pixels(ctx, x, y, width, height, type, pixels, &ctx->Pack);
502 break;
503 case GL_RED:
504 case GL_GREEN:
505 case GL_BLUE:
506 case GL_ALPHA:
507 case GL_RGB:
508 case GL_LUMINANCE:
509 case GL_LUMINANCE_ALPHA:
510 case GL_RGBA:
511 case GL_BGR:
512 case GL_BGRA:
513 case GL_ABGR_EXT:
514 read_rgba_pixels(ctx, x, y, width, height,
515 format, type, pixels, &ctx->Pack);
516 break;
517 default:
518 _mesa_error( ctx, GL_INVALID_ENUM, "glReadPixels(format)" );
519 }
520
521 RENDER_FINISH(swrast,ctx);
522 }