wip prep commit in lieu of gfx subsystem update changes.
[AROS.git] / workbench / libs / cgfx / readpixelarray.c
blob11c43519b8d1b8975b05740323f3c19a2d79afda
1 /*
2 Copyright © 1995-2017, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc:
6 Lang: english
7 */
9 #include <hidd/gfx.h>
10 #include <aros/debug.h>
12 #include "cybergraphics_intern.h"
13 #include "gfxfuncsupport.h"
15 struct render_data
17 UBYTE *array;
18 HIDDT_StdPixFmt pixfmt;
19 ULONG modulo;
20 ULONG bppix;
23 static ULONG RenderHook(struct render_data *data, LONG srcx, LONG srcy,
24 OOP_Object *dstbm_obj, OOP_Object *dst_gc, struct Rectangle *rect,
25 struct GfxBase *GfxBase);
27 /*****************************************************************************
29 NAME */
30 #include <proto/cybergraphics.h>
32 AROS_LH10(ULONG, ReadPixelArray,
34 /* SYNOPSIS */
35 AROS_LHA(APTR , dst , A0),
36 AROS_LHA(UWORD , destx , D0),
37 AROS_LHA(UWORD , desty , D1),
38 AROS_LHA(UWORD , dstmod , D2),
39 AROS_LHA(struct RastPort *, rp , A1),
40 AROS_LHA(UWORD , srcx , D3),
41 AROS_LHA(UWORD , srcy , D4),
42 AROS_LHA(UWORD , width , D5),
43 AROS_LHA(UWORD , height , D6),
44 AROS_LHA(UBYTE , dstformat , D7),
46 /* LOCATION */
47 struct Library *, CyberGfxBase, 20, Cybergraphics)
49 /* FUNCTION
50 Copies a rectangular portion of a RastPort to a block of raw pixel
51 values.
53 INPUTS
54 dst - pointer to the pixel values.
55 destx, desty - top-lefthand corner of portion of destination rectangle
56 to write to (in pixels).
57 dstmod - the number of bytes in each row of the destination rectangle.
58 rp - the RastPort to read.
59 srcx, srcy - top-lefthand corner of portion of source RastPort to
60 read (in pixels).
61 width, height - size of the area to copy (in pixels).
62 dstformat - the format of the destination pixels. The following format
63 types are supported:
64 RECTFMT_RGB
65 RECTFMT_RGBA
66 RECTFMT_ARGB
67 RECTFMT_RAW
68 RECTFMT_RGB15
69 RECTFMT_BGR15
70 RECTFMT_RGB15PC
71 RECTFMT_BGR15PC
72 RECTFMT_RGB16
73 RECTFMT_BGR16
74 RECTFMT_RGB16PC
75 RECTFMT_BGR16PC
76 RECTFMT_RGB24
77 RECTFMT_BGR24
78 RECTFMT_0RGB32
79 RECTFMT_BGR032
80 RECTFMT_RGB032
81 RECTFMT_0BGR32
82 RECTFMT_ARGB32
83 RECTFMT_BGRA32
84 RECTFMT_RGBA32
85 RECTFMT_ABGR32
87 RESULT
88 count - number of pixels read.
90 NOTES
91 See WritePixelArray() for descriptions of pixel formats. Where a
92 RastPort does not support an alpha channel, destination alpha values
93 will be set to zero.
95 EXAMPLE
97 BUGS
99 SEE ALSO
101 INTERNALS
103 *****************************************************************************/
105 AROS_LIBFUNC_INIT
107 ULONG start_offset;
108 IPTR bppix;
109 LONG pixread = 0;
110 BYTE old_drmd;
111 struct Rectangle rr;
112 struct render_data data;
114 if (width == 0 || height == 0)
115 return 0;
117 /* Filter out unsupported modes */
118 if (dstformat == RECTFMT_LUT8 || dstformat == RECTFMT_GREY8)
120 D(bug("RECTFMT_LUT8 and RECTFMT_GREY8 are not supported "
121 "by ReadPixelArray()\n"));
122 return 0;
125 /* This is cybergraphx. We only work wih HIDD bitmaps */
126 if (!IS_HIDD_BM(rp->BitMap))
128 D(bug("!!!!! Trying to use CGFX call on non-hidd bitmap "
129 "in ReadPixelArray() !!!\n"));
130 return 0;
133 /* Preserve old drawmode */
134 old_drmd = rp->DrawMode;
135 SetDrMd(rp, JAM2);
137 bppix = GetRectFmtBytesPerPixel(dstformat, rp, CyberGfxBase);
138 start_offset = ((ULONG)desty) * dstmod + destx * bppix;
139 data.array = ((UBYTE *)dst) + start_offset;
140 data.pixfmt = GetHIDDRectFmt(dstformat, rp, CyberGfxBase);
141 data.modulo = dstmod;
142 data.bppix = bppix;
144 rr.MinX = srcx;
145 rr.MinY = srcy;
146 rr.MaxX = srcx + width - 1;
147 rr.MaxY = srcy + height - 1;
149 pixread = DoRenderFunc(rp, NULL, &rr, RenderHook, &data, FALSE);
151 /* restore old drawmode */
152 SetDrMd(rp, old_drmd);
154 return pixread;
156 AROS_LIBFUNC_EXIT
157 } /* ReadPixelArray */
159 static ULONG RenderHook(struct render_data *data, LONG srcx, LONG srcy,
160 OOP_Object *dstbm_obj, OOP_Object *dst_gc,
161 struct Rectangle *rect, struct GfxBase *GfxBase)
163 ULONG width = rect->MaxX - rect->MinX + 1;
164 ULONG height = rect->MaxY - rect->MinY + 1;
165 UBYTE *array = data->array + data->modulo * srcy + data->bppix * srcx;
167 HIDD_BM_GetImage(dstbm_obj, array, data->modulo,
168 rect->MinX, rect->MinY, width, height, data->pixfmt);
170 return width * height;