Debian Codegraph @

nx-X11/extras/Mesa/src/mesa/swrast/s_readpix.c

raw
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.4.1
4 *
5 * Copyright (C) 1999-2005 Brian Paul All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * 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 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25
26 #include "glheader.h"
27 #include "bufferobj.h"
28 #include "colormac.h"
29 #include "convolve.h"
30 #include "context.h"
31 #include "feedback.h"
32 #include "image.h"
33 #include "macros.h"
34 #include "imports.h"
35 #include "pixel.h"
36
37 #include "s_context.h"
38 #include "s_depth.h"
39 #include "s_span.h"
40 #include "s_stencil.h"
41
42
43
44 /*
45 * Read a block of color index pixels.
46 */
47 static void
48 read_index_pixels( GLcontext *ctx,
49 GLint x, GLint y,
50 GLsizei width, GLsizei height,
51 GLenum type, GLvoid *pixels,
52 const struct gl_pixelstore_attrib *packing )
53 {
54 struct gl_renderbuffer *rb = ctx->ReadBuffer->_ColorReadBuffer;
55 /*
56 SWcontext *swrast = SWRAST_CONTEXT(ctx);
57 */
58 GLint i, readWidth;
59
60 /* error checking */
61 if (ctx->Visual.rgbMode) {
62 _mesa_error( ctx, GL_INVALID_OPERATION, "glReadPixels" );
63 return;
64 }
65
66 if (type != GL_BYTE &&
67 type != GL_UNSIGNED_BYTE &&
68 type != GL_SHORT &&
69 type != GL_UNSIGNED_SHORT &&
70 type != GL_INT &&
71 type != GL_UNSIGNED_INT &&
72 type != GL_FLOAT) {
73 _mesa_error( ctx, GL_INVALID_OPERATION, "glReadPixels(index type)");
74 return;
75 }
76
77 _swrast_use_read_buffer(ctx);
78
79 /* XXX: width should never be > MAX_WIDTH since we did clipping earlier */
80 readWidth = (width > MAX_WIDTH) ? MAX_WIDTH : width;
81
82 /* process image row by row */
83 for (i = 0; i < height; i++) {
84 GLuint index[MAX_WIDTH];
85 GLvoid *dest;
86 ASSERT(rb->DataType == GL_UNSIGNED_INT);
87 rb->GetRow(ctx, rb, readWidth, x, y + i, index);
88
89 dest = _mesa_image_address2d(packing, pixels, width, height,
90 GL_COLOR_INDEX, type, i, 0);
91
92 _mesa_pack_index_span(ctx, readWidth, type, dest, index,
93 &ctx->Pack, ctx->_ImageTransferState);
94 }
95
96 _swrast_use_draw_buffer(ctx);
97 }
98
99
100
101 /**
102 * Read pixels for format=GL_DEPTH_COMPONENT.
103 */
104 static void
105 read_depth_pixels( GLcontext *ctx,
106 GLint x, GLint y,
107 GLsizei width, GLsizei height,
108 GLenum type, GLvoid *pixels,
109 const struct gl_pixelstore_attrib *packing )
110 {
111 struct gl_renderbuffer *rb
112 = ctx->ReadBuffer->Attachment[BUFFER_DEPTH].Renderbuffer;
113 GLint readWidth;
114 GLboolean bias_or_scale;
115
116 /* Error checking */
117 if (ctx->ReadBuffer->Visual.depthBits <= 0 || !rb) {
118 /* No depth buffer */
119 _mesa_error( ctx, GL_INVALID_OPERATION, "glReadPixels" );
120 return;
121 }
122
123 if (type != GL_BYTE &&
124 type != GL_UNSIGNED_BYTE &&
125 type != GL_SHORT &&
126 type != GL_UNSIGNED_SHORT &&
127 type != GL_INT &&
128 type != GL_UNSIGNED_INT &&
129 type != GL_FLOAT) {
130 _mesa_error( ctx, GL_INVALID_OPERATION, "glReadPixels(depth type)");
131 return;
132 }
133
134 /* XXX: width should never be > MAX_WIDTH since we did clipping earlier */
135 readWidth = (width > MAX_WIDTH) ? MAX_WIDTH : width;
136
137 bias_or_scale = ctx->Pixel.DepthBias!=0.0 || ctx->Pixel.DepthScale!=1.0;
138
139 if (type==GL_UNSIGNED_SHORT && ctx->ReadBuffer->Visual.depthBits == 16
140 && !bias_or_scale && !packing->SwapBytes) {
141 /* Special case: directly read 16-bit unsigned depth values. */
142 GLint j;
143 for (j=0;j<height;j++,y++) {
144 GLdepth depth[MAX_WIDTH];
145 GLushort *dst = (GLushort*) _mesa_image_address2d(packing, pixels,
146 width, height, GL_DEPTH_COMPONENT, type, j, 0);
147 GLint i;
148 _swrast_read_depth_span(ctx, rb, width, x, y, depth);
149 for (i = 0; i < width; i++)
150 dst[i] = depth[i];
151 }
152 }
153 else if (type==GL_UNSIGNED_INT && ctx->ReadBuffer->Visual.depthBits == 32
154 && !bias_or_scale && !packing->SwapBytes) {
155 /* Special case: directly read 32-bit unsigned depth values. */
156 GLint j;
157 for (j=0;j<height;j++,y++) {
158 GLdepth *dst = (GLdepth *) _mesa_image_address2d(packing, pixels,
159 width, height, GL_DEPTH_COMPONENT, type, j, 0);
160 _swrast_read_depth_span(ctx, rb, width, x, y, dst);
161 }
162 }
163 else {
164 /* General case (slower) */
165 GLint j;
166 for (j=0;j<height;j++,y++) {
167 GLfloat depth[MAX_WIDTH];
168 GLvoid *dest;
169
170 _swrast_read_depth_span_float(ctx, rb, readWidth, x, y, depth);
171
172 dest = _mesa_image_address2d(packing, pixels, width, height,
173 GL_DEPTH_COMPONENT, type, j, 0);
174
175 _mesa_pack_depth_span(ctx, readWidth, (GLdepth *) dest, type,
176 depth, packing);
177 }
178 }
179 }
180
181
182 /**
183 * Read pixels for format=GL_STENCIL_INDEX.
184 */
185 static void
186 read_stencil_pixels( GLcontext *ctx,
187 GLint x, GLint y,
188 GLsizei width, GLsizei height,
189 GLenum type, GLvoid *pixels,
190 const struct gl_pixelstore_attrib *packing )
191 {
192 struct gl_renderbuffer *rb
193 = ctx->ReadBuffer->Attachment[BUFFER_STENCIL].Renderbuffer;
194 GLint j, readWidth;
195
196 if (type != GL_BYTE &&
197 type != GL_UNSIGNED_BYTE &&
198 type != GL_SHORT &&
199 type != GL_UNSIGNED_SHORT &&
200 type != GL_INT &&
201 type != GL_UNSIGNED_INT &&
202 type != GL_FLOAT &&
203 type != GL_BITMAP) {
204 _mesa_error( ctx, GL_INVALID_OPERATION, "glReadPixels(stencil type)");
205 return;
206 }
207
208 if (ctx->ReadBuffer->Visual.stencilBits <= 0 || !rb) {
209 /* No stencil buffer */
210 _mesa_error( ctx, GL_INVALID_OPERATION, "glReadPixels" );
211 return;
212 }
213
214 /* XXX: width should never be > MAX_WIDTH since we did clipping earlier */
215 readWidth = (width > MAX_WIDTH) ? MAX_WIDTH : width;
216
217 /* process image row by row */
218 for (j=0;j<height;j++,y++) {
219 GLvoid *dest;
220 GLstencil stencil[MAX_WIDTH];
221
222 _swrast_read_stencil_span(ctx, rb, readWidth, x, y, stencil);
223
224 dest = _mesa_image_address2d(packing, pixels, width, height,
225 GL_STENCIL_INDEX, type, j, 0);
226
227 _mesa_pack_stencil_span(ctx, readWidth, type, dest, stencil, packing);
228 }
229 }
230
231
232
233 /**
234 * Optimized glReadPixels for particular pixel formats:
235 * GL_UNSIGNED_BYTE, GL_RGBA
236 * when pixel scaling, biasing and mapping are disabled.
237 */
238 static GLboolean
239 read_fast_rgba_pixels( GLcontext *ctx,
240 GLint x, GLint y,
241 GLsizei width, GLsizei height,
242 GLenum format, GLenum type,
243 GLvoid *pixels,
244 const struct gl_pixelstore_attrib *packing )
245 {
246 struct gl_renderbuffer *rb = ctx->ReadBuffer->_ColorReadBuffer;
247 /* can't do scale, bias, mapping, etc */
248 if (ctx->_ImageTransferState)
249 return GL_FALSE;
250
251 /* can't do fancy pixel packing */
252 if (packing->Alignment != 1 || packing->SwapBytes || packing->LsbFirst)
253 return GL_FALSE;
254
255 {
256 GLint srcX = x;
257 GLint srcY = y;
258 GLint readWidth = width; /* actual width read */
259 GLint readHeight = height; /* actual height read */
260 GLint skipPixels = packing->SkipPixels;
261 GLint skipRows = packing->SkipRows;
262 GLint rowLength;
263
264 if (packing->RowLength > 0)
265 rowLength = packing->RowLength;
266 else
267 rowLength = width;
268
269 /*
270 * Ready to read!
271 * The window region at (destX, destY) of size (readWidth, readHeight)
272 * will be read back.
273 * We'll write pixel data to buffer pointed to by "pixels" but we'll
274 * skip "skipRows" rows and skip "skipPixels" pixels/row.
275 */
276 #if CHAN_BITS == 8
277 if (format == GL_RGBA && type == GL_UNSIGNED_BYTE) {
278 #elif CHAN_BITS == 16
279 if (format == GL_RGBA && type == GL_UNSIGNED_SHORT) {
280 #else
281 if (0) {
282 #endif
283 GLchan *dest = (GLchan *) pixels
284 + (skipRows * rowLength + skipPixels) * 4;
285 GLint row;
286
287 if (packing->Invert) {
288 /* start at top and go down */
289 dest += (readHeight - 1) * rowLength * 4;
290 rowLength = -rowLength;
291 }
292
293 ASSERT(rb->GetRow);
294 for (row=0; row<readHeight; row++) {
295 rb->GetRow(ctx, rb, readWidth, srcX, srcY, dest);
296 dest += rowLength * 4;
297 srcY++;
298 }
299 return GL_TRUE;
300 }
301 else {
302 /* can't do this format/type combination */
303 return GL_FALSE;
304 }
305 }
306 }
307
308
309
310 /*
311 * Read R, G, B, A, RGB, L, or LA pixels.
312 */
313 static void
314 read_rgba_pixels( GLcontext *ctx,
315 GLint x, GLint y,
316 GLsizei width, GLsizei height,
317 GLenum format, GLenum type, GLvoid *pixels,
318 const struct gl_pixelstore_attrib *packing )
319 {
320 struct gl_renderbuffer *rb = ctx->ReadBuffer->_ColorReadBuffer;
321 GLint readWidth;
322
323 if (!rb) {
324 /* No readbuffer is OK with GL_EXT_framebuffer_object */
325 return;
326 }
327
328 /* do error checking on pixel type, format was already checked by caller */
329 switch (type) {
330 case GL_UNSIGNED_BYTE:
331 case GL_BYTE:
332 case GL_UNSIGNED_SHORT:
333 case GL_SHORT:
334 case GL_UNSIGNED_INT:
335 case GL_INT:
336 case GL_FLOAT:
337 case GL_UNSIGNED_BYTE_3_3_2:
338 case GL_UNSIGNED_BYTE_2_3_3_REV:
339 case GL_UNSIGNED_SHORT_5_6_5:
340 case GL_UNSIGNED_SHORT_5_6_5_REV:
341 case GL_UNSIGNED_SHORT_4_4_4_4:
342 case GL_UNSIGNED_SHORT_4_4_4_4_REV:
343 case GL_UNSIGNED_SHORT_5_5_5_1:
344 case GL_UNSIGNED_SHORT_1_5_5_5_REV:
345 case GL_UNSIGNED_INT_8_8_8_8:
346 case GL_UNSIGNED_INT_8_8_8_8_REV:
347 case GL_UNSIGNED_INT_10_10_10_2:
348 case GL_UNSIGNED_INT_2_10_10_10_REV:
349 /* valid pixel type */
350 break;
351 case GL_HALF_FLOAT_ARB:
352 if (!ctx->Extensions.ARB_half_float_pixel) {
353 _mesa_error( ctx, GL_INVALID_ENUM, "glReadPixels(type)" );
354 return;
355 }
356 break;
357 default:
358 _mesa_error( ctx, GL_INVALID_ENUM, "glReadPixels(type)" );
359 return;
360 }
361
362 if (!_mesa_is_legal_format_and_type(ctx, format, type) ||
363 format == GL_INTENSITY) {
364 _mesa_error(ctx, GL_INVALID_OPERATION, "glReadPixels(format or type)");
365 return;
366 }
367
368 _swrast_use_read_buffer(ctx);
369
370 /* Try optimized path first */
371 if (read_fast_rgba_pixels( ctx, x, y, width, height,
372 format, type, pixels, packing )) {
373
374 _swrast_use_draw_buffer(ctx);
375 return; /* done! */
376 }
377
378 /* XXX: width should never be > MAX_WIDTH since we did clipping earlier */
379 readWidth = (width > MAX_WIDTH) ? MAX_WIDTH : width;
380
381
382 if (ctx->Pixel.Convolution2DEnabled || ctx->Pixel.Separable2DEnabled) {
383 const GLuint transferOps = ctx->_ImageTransferState;
384 GLfloat *dest, *src, *tmpImage, *convImage;
385 GLint row;
386
387 tmpImage = (GLfloat *) _mesa_malloc(width * height * 4 * sizeof(GLfloat));
388 if (!tmpImage) {
389 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glReadPixels");
390 return;
391 }
392 convImage = (GLfloat *) _mesa_malloc(width * height * 4 * sizeof(GLfloat));
393 if (!convImage) {
394 _mesa_free(tmpImage);
395 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glReadPixels");
396 return;
397 }
398
399 /* read full RGBA, FLOAT image */
400 dest = tmpImage;
401 for (row = 0; row < height; row++, y++) {
402 GLchan rgba[MAX_WIDTH][4];
403 if (ctx->Visual.rgbMode) {
404 _swrast_read_rgba_span(ctx, rb, readWidth, x, y, rgba);
405 }
406 else {
407 GLuint index[MAX_WIDTH];
408 ASSERT(rb->DataType == GL_UNSIGNED_INT);
409 rb->GetRow(ctx, rb, readWidth, x, y, index);
410 if (ctx->Pixel.IndexShift != 0 || ctx->Pixel.IndexOffset !=0 ) {
411 _mesa_map_ci(ctx, readWidth, index);
412 }
413 _mesa_map_ci_to_rgba_chan(ctx, readWidth, index, rgba);
414 }
415 _mesa_pack_rgba_span_chan(ctx, readWidth, (const GLchan (*)[4]) rgba,
416 GL_RGBA, GL_FLOAT, dest, &ctx->DefaultPacking,
417 transferOps & IMAGE_PRE_CONVOLUTION_BITS);
418 dest += width * 4;
419 }
420
421 /* do convolution */
422 if (ctx->Pixel.Convolution2DEnabled) {
423 _mesa_convolve_2d_image(ctx, &readWidth, &height, tmpImage, convImage);
424 }
425 else {
426 ASSERT(ctx->Pixel.Separable2DEnabled);
427 _mesa_convolve_sep_image(ctx, &readWidth, &height, tmpImage, convImage);
428 }
429 _mesa_free(tmpImage);
430
431 /* finish transfer ops and pack the resulting image */
432 src = convImage;
433 for (row = 0; row < height; row++) {
434 GLvoid *dest;
435 dest = _mesa_image_address2d(packing, pixels, readWidth, height,
436 format, type, row, 0);
437 _mesa_pack_rgba_span_float(ctx, readWidth,
438 (const GLfloat (*)[4]) src,
439 format, type, dest, packing,
440 transferOps & IMAGE_POST_CONVOLUTION_BITS);
441 src += readWidth * 4;
442 }
443 }
444 else {
445 /* no convolution */
446 GLint row;
447 for (row = 0; row < height; row++, y++) {
448 GLchan rgba[MAX_WIDTH][4];
449 GLvoid *dst;
450 if (ctx->Visual.rgbMode) {
451 _swrast_read_rgba_span(ctx, rb, readWidth, x, y, rgba);
452 }
453 else {
454 GLuint index[MAX_WIDTH];
455 ASSERT(rb->DataType == GL_UNSIGNED_INT);
456 rb->GetRow(ctx, rb, readWidth, x, y, index);
457 if (ctx->Pixel.IndexShift != 0 || ctx->Pixel.IndexOffset != 0) {
458 _mesa_map_ci(ctx, readWidth, index);
459 }
460 _mesa_map_ci_to_rgba_chan(ctx, readWidth, index, rgba);
461 }
462 dst = _mesa_image_address2d(packing, pixels, width, height,
463 format, type, row, 0);
464 if (ctx->Visual.redBits < CHAN_BITS ||
465 ctx->Visual.greenBits < CHAN_BITS ||
466 ctx->Visual.blueBits < CHAN_BITS) {
467 /* Requantize the color values into floating point and go from
468 * there. This fixes conformance failures with 16-bit color
469 * buffers, for example.
470 */
471 DEFMARRAY(GLfloat, rgbaf, MAX_WIDTH, 4); /* mac 32k limitation */
472 CHECKARRAY(rgbaf, return); /* mac 32k limitation */
473 _mesa_chan_to_float_span(ctx, readWidth,
474 (CONST GLchan (*)[4]) rgba, rgbaf);
475 _mesa_pack_rgba_span_float(ctx, readWidth,
476 (CONST GLfloat (*)[4]) rgbaf,
477 format, type, dst, packing,
478 ctx->_ImageTransferState);
479 UNDEFARRAY(rgbaf); /* mac 32k limitation */
480 }
481 else {
482 /* GLubytes are fine */
483 _mesa_pack_rgba_span_chan(ctx, readWidth, (CONST GLchan (*)[4]) rgba,
484 format, type, dst, packing,
485 ctx->_ImageTransferState);
486 }
487 }
488 }
489
490 _swrast_use_draw_buffer(ctx);
491 }
492
493
494 /**
495 * Software fallback routine for ctx->Driver.ReadPixels().
496 * We wind up using the swrast->ReadSpan() routines to do the job.
497 */
498 void
499 _swrast_ReadPixels( GLcontext *ctx,
500 GLint x, GLint y, GLsizei width, GLsizei height,
501 GLenum format, GLenum type,
502 const struct gl_pixelstore_attrib *packing,
503 GLvoid *pixels )
504 {
505 SWcontext *swrast = SWRAST_CONTEXT(ctx);
506 struct gl_pixelstore_attrib clippedPacking;
507
508 if (swrast->NewState)
509 _swrast_validate_derived( ctx );
510
511 /* Do all needed clipping here, so that we can forget about it later */
512 clippedPacking = *packing;
513 if (clippedPacking.RowLength == 0) {
514 clippedPacking.RowLength = width;
515 }
516 if (!_mesa_clip_readpixels(ctx, &x, &y, &width, &height,
517 &clippedPacking.SkipPixels,
518 &clippedPacking.SkipRows)) {
519 /* The ReadPixels region is totally outside the window bounds */
520 return;
521 }
522
523 if (clippedPacking.BufferObj->Name) {
524 /* pack into PBO */
525 GLubyte *buf;
526 if (!_mesa_validate_pbo_access(2, &clippedPacking, width, height, 1,
527 format, type, pixels)) {
528 _mesa_error(ctx, GL_INVALID_OPERATION,
529 "glReadPixels(invalid PBO access)");
530 return;
531 }
532 buf = (GLubyte *) ctx->Driver.MapBuffer(ctx, GL_PIXEL_PACK_BUFFER_EXT,
533 GL_WRITE_ONLY_ARB,
534 clippedPacking.BufferObj);
535 if (!buf) {
536 /* buffer is already mapped - that's an error */
537 _mesa_error(ctx, GL_INVALID_OPERATION, "glReadPixels(PBO is mapped)");
538 return;
539 }
540 pixels = ADD_POINTERS(buf, pixels);
541 }
542
543 RENDER_START(swrast, ctx);
544
545 switch (format) {
546 case GL_COLOR_INDEX:
547 read_index_pixels(ctx, x, y, width, height, type, pixels,
548 &clippedPacking);
549 break;
550 case GL_STENCIL_INDEX:
551 read_stencil_pixels(ctx, x,y, width,height, type, pixels,
552 &clippedPacking);
553 break;
554 case GL_DEPTH_COMPONENT:
555 read_depth_pixels(ctx, x, y, width, height, type, pixels,
556 &clippedPacking);
557 break;
558 case GL_RED:
559 case GL_GREEN:
560 case GL_BLUE:
561 case GL_ALPHA:
562 case GL_RGB:
563 case GL_LUMINANCE:
564 case GL_LUMINANCE_ALPHA:
565 case GL_RGBA:
566 case GL_BGR:
567 case GL_BGRA:
568 case GL_ABGR_EXT:
569 read_rgba_pixels(ctx, x, y, width, height,
570 format, type, pixels, &clippedPacking);
571 break;
572 default:
573 _mesa_error( ctx, GL_INVALID_ENUM, "glReadPixels(format)" );
574 /* don't return yet, clean-up */
575 }
576
577 RENDER_FINISH(swrast, ctx);
578
579 if (clippedPacking.BufferObj->Name) {
580 /* done with PBO so unmap it now */
581 ctx->Driver.UnmapBuffer(ctx, GL_PIXEL_PACK_BUFFER_EXT,
582 clippedPacking.BufferObj);
583 }
584 }