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