define __KERNEL_STRICT_NAMES to avoid inclusion of kernel types on systems that carry...
[cake.git] / rom / graphics / bitmapscale.c
blob5de91c70b103740eb3578eb91266497e5dc596a8
1 /*
2 Copyright © 1995-2007, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc: Graphics function BitMapScale()
6 Lang: english
7 */
8 #include <aros/debug.h>
9 #include <graphics/scale.h>
10 #include <proto/exec.h>
11 #include <proto/oop.h>
12 #include "graphics_intern.h"
13 #include "gfxfuncsupport.h"
14 #include "objcache.h"
16 VOID HIDD_BM_BitMapScale(OOP_Object *, OOP_Object *, OOP_Object *, struct BitScaleArgs *, OOP_Object *);
18 /*****************************************************************************
20 NAME */
21 #include <proto/graphics.h>
23 AROS_LH1(void, BitMapScale,
25 /* SYNOPSIS */
26 AROS_LHA(struct BitScaleArgs *, bitScaleArgs, A0),
28 /* LOCATION */
29 struct GfxBase *, GfxBase, 113, Graphics)
31 /* FUNCTION
32 Scale a source bit map to a destination bit map other than
33 the source bit map.
35 INPUTS
36 Pass a BitScaleArgs structure filled with the following arguments
37 to this function:
38 bsa_SrcX, bsa_SrcY - upper left coordinate in source bitmap
39 bsa_SrcWidth, bsa_SrcHeight - Width and Height or source bitmap
40 bsa_DestX, bsa_DestY - upper left coordinate in destination
41 bitmap
42 bsa_DestWidth, bsa_DestHeight - this function will set these
43 values. Use the bsa_???Factor for scaling
44 bsa_XSrcFactor:bsa_XDestFactor - Set these to get approximately
45 the same ratio as bsa_XSrcWidth:bsa_XDestWidth, but
46 usually not exactly the same number.
47 bsa_YSrcFactor:bsa_YDestFactor - Set these to get approximately
48 the same ratio as bsa_YSrcWidth:YDestWidth, but
49 usually not exactly the same number.
50 bsa_SrcBitMap - pointer to source bitmap to be scaled
51 bsa_DestBitMap - pointer to destination bitmap which will
52 will hold the scaled bitmap. Make sure it's
53 big enough!
54 bsa_Flags - reserved for future use. Set it to zero!
55 bsa_XDDA, bsa_YDDA - for future use.
56 bsa_Reserved1, bsa_Reserved2 - for future use.
58 RESULT
59 bsa_DestWidth and bsa_DestHeight will be set by this function
61 NOTES
62 - Overlapping source and destination bitmaps are not supported
63 - Make sure that you provide enough memory for the destination
64 bitmap to hold the result
65 - In the destination bitmap only the area where the scaled
66 source bitmap is put into is changed. A frame of the old
67 bitmap is left.
69 EXAMPLE
71 BUGS
73 SEE ALSO
74 ScalerDiv(), graphics/scale.h
76 INTERNALS
78 HISTORY
80 *****************************************************************************/
82 AROS_LIBFUNC_INIT
84 if (bitScaleArgs->bsa_SrcBitMap->pad != 0 ||
85 bitScaleArgs->bsa_DestBitMap->pad != 0 ||
86 bitScaleArgs->bsa_SrcBitMap->Flags & BMF_AROS_HIDD ||
87 bitScaleArgs->bsa_DestBitMap->Flags & BMF_AROS_HIDD)
89 ULONG srcflags = 0;
90 ULONG dstflags = 0;
92 BOOL src_colmap_set = FALSE;
93 BOOL dst_colmap_set = FALSE;
94 BOOL success = TRUE;
95 BOOL colmaps_ok = TRUE;
97 OOP_Object *srcbm_obj;
98 OOP_Object *dstbm_obj;
99 OOP_Object *tmp_gc;
102 srcbm_obj = OBTAIN_HIDD_BM(bitScaleArgs->bsa_SrcBitMap);
103 dstbm_obj = OBTAIN_HIDD_BM(bitScaleArgs->bsa_DestBitMap);
104 tmp_gc = obtain_cache_object(SDD(GfxBase)->gc_cache, GfxBase);
106 /* We must lock any HIDD_BM_SetColorMap calls */
107 LOCK_BLIT
108 if (srcbm_obj && dstbm_obj && tmp_gc)
110 /* Try to get a CLUT for the bitmaps */
111 if (IS_HIDD_BM(bitScaleArgs->bsa_SrcBitMap)) {
112 if (NULL != HIDD_BM_COLMAP(bitScaleArgs->bsa_SrcBitMap))
113 srcflags |= FLG_HASCOLMAP;
114 srcflags |= GET_COLMOD_FLAGS(bitScaleArgs->bsa_SrcBitMap);
115 } else {
116 /* Amiga BM */
117 srcflags |= FLG_PALETTE;
120 if (IS_HIDD_BM(bitScaleArgs->bsa_DestBitMap)) {
121 if (NULL != HIDD_BM_COLMAP(bitScaleArgs->bsa_DestBitMap))
122 dstflags |= FLG_HASCOLMAP;
123 dstflags |= GET_COLMOD_FLAGS(bitScaleArgs->bsa_DestBitMap);
124 } else {
125 /* Amiga BM */
126 dstflags |= FLG_PALETTE;
130 if ( (srcflags == FLG_PALETTE || srcflags == FLG_STATICPALETTE)) {
131 /* palettized with no colmap. Neew to get a colmap from dest*/
132 if (dstflags == FLG_TRUECOLOR) {
134 D(bug("!!! NO WAY GETTING PALETTE FOR src IN BltBitMap\n"));
135 colmaps_ok = FALSE;
136 success = FALSE;
138 } else if (dstflags == (FLG_TRUECOLOR | FLG_HASCOLMAP)) {
140 /* Use the dest colmap for src */
141 HIDD_BM_SetColorMap(srcbm_obj, HIDD_BM_COLMAP(bitScaleArgs->bsa_DestBitMap));
146 if ( (dstflags == FLG_PALETTE || dstflags == FLG_STATICPALETTE)) {
147 /* palettized with no pixtab. Nees to get a pixtab from dest*/
148 if (srcflags == FLG_TRUECOLOR) {
149 D(bug("!!! NO WAY GETTING PALETTE FOR dst IN BltBitMap\n"));
150 colmaps_ok = FALSE;
151 success = FALSE;
153 } else if (srcflags == (FLG_TRUECOLOR | FLG_HASCOLMAP)) {
155 /* Use the src colmap for dst */
156 HIDD_BM_SetColorMap(dstbm_obj, HIDD_BM_COLMAP(bitScaleArgs->bsa_SrcBitMap));
158 dst_colmap_set = TRUE;
162 if (success && colmaps_ok)
164 HIDDT_DrawMode old_drmd;
165 struct TagItem cbtags[] = {
166 { aHidd_GC_DrawMode, vHidd_GC_DrawMode_Copy },
167 { TAG_DONE, 0 }
170 OOP_GetAttr(tmp_gc, aHidd_GC_DrawMode, &old_drmd);
172 OOP_SetAttrs(tmp_gc, cbtags);
174 bitScaleArgs->bsa_DestWidth = ScalerDiv(bitScaleArgs->bsa_SrcWidth,
175 bitScaleArgs->bsa_XDestFactor,
176 bitScaleArgs->bsa_XSrcFactor);
178 bitScaleArgs->bsa_DestHeight = ScalerDiv(bitScaleArgs->bsa_SrcHeight,
179 bitScaleArgs->bsa_YDestFactor,
180 bitScaleArgs->bsa_YSrcFactor);
182 HIDD_BM_BitMapScale(SDD(GfxBase)->pointerbm
183 , srcbm_obj
184 , dstbm_obj
185 , bitScaleArgs
186 , tmp_gc
188 HIDD_BM_UpdateRect(dstbm_obj, bitScaleArgs->bsa_DestX, bitScaleArgs->bsa_DestY, bitScaleArgs->bsa_DestWidth, bitScaleArgs->bsa_DestHeight);
190 cbtags[0].ti_Data = old_drmd;
191 OOP_SetAttrs(tmp_gc, cbtags);
192 } /* if () */
194 if (src_colmap_set)
195 HIDD_BM_SetColorMap(srcbm_obj, NULL);
196 if (dst_colmap_set)
197 HIDD_BM_SetColorMap(dstbm_obj, NULL);
200 if (dstbm_obj)
201 RELEASE_HIDD_BM(dstbm_obj, bitScaleArgs->bsa_DestBitMap);
203 if (srcbm_obj)
204 RELEASE_HIDD_BM(srcbm_obj, bitScaleArgs->bsa_SrcBitMap);
206 if (tmp_gc)
207 release_cache_object(SDD(GfxBase)->gc_cache, tmp_gc, GfxBase);
209 ULOCK_BLIT
212 else
215 * Algorithm for plain Amiga bitmaps.
218 * Unfortunately it's not possible to use 16/32 bit copying on bitmaps with this
219 * algorithm as there might be an odd number of bits per line in a bitmap and
220 * this creates problems when accessing the 2nd, 4th and so on line.
224 #define DEF_USIZE ULONG
225 #define DEF_SIZE LONG
226 #define DEF_NUMBITSMINUS1 31
227 #define DEF_MASK 31
228 #define DEF_READMASK 0x80000000
229 #define DEF_SHIFTY 2
230 #define DEF_SHIFTX 5
233 /* The following lines are necessary for BYTE copying and have to be used right
234 * now!!
237 #define DEF_USIZE UBYTE
238 #define DEF_SIZE BYTE
239 #define DEF_NUMBITSMINUS1 7
240 #define DEF_ANDMASK 7
241 #define DEF_READMASK 0x80
242 #define DEF_SHIFTY 0
243 #define DEF_SHIFTX 3
245 UWORD * LinePattern;
246 bitScaleArgs -> bsa_DestWidth = ScalerDiv(bitScaleArgs -> bsa_SrcWidth,
247 bitScaleArgs -> bsa_XDestFactor,
248 bitScaleArgs -> bsa_XSrcFactor);
250 bitScaleArgs -> bsa_DestHeight= ScalerDiv(bitScaleArgs -> bsa_SrcHeight,
251 bitScaleArgs -> bsa_YDestFactor,
252 bitScaleArgs -> bsa_YSrcFactor);
254 /* first of all lets allocate DestHeight words of memory so we can
255 precalculate which original line goes to which destination lines */
257 if (NULL ==(LinePattern = (UWORD *) AllocMem(sizeof(UWORD)*bitScaleArgs ->bsa_DestHeight, 0)))
258 return;
261 UWORD DestHeight = bitScaleArgs -> bsa_DestHeight;
262 UWORD ys = bitScaleArgs -> bsa_SrcY;
263 ULONG count = 0;
264 ULONG dyd = bitScaleArgs -> bsa_DestHeight;
265 ULONG dys = bitScaleArgs -> bsa_SrcHeight;
266 LONG accuys = dyd;
267 LONG accuyd = - (dys >> 1);
268 while (count < DestHeight)
270 accuyd += dys;
271 while (accuyd > accuys )
273 ys++;
274 accuys += dyd;
276 LinePattern[count] = ys;
277 count++;
283 /* now let's go for the real thing: scaling */
285 UWORD DestWidth = bitScaleArgs -> bsa_DestWidth + bitScaleArgs -> bsa_DestX;
286 ULONG xs = bitScaleArgs -> bsa_SrcX;
287 ULONG count = bitScaleArgs -> bsa_DestX;
288 ULONG dxd = bitScaleArgs -> bsa_DestWidth;
289 ULONG dxs = bitScaleArgs -> bsa_SrcWidth;
290 LONG accuxs = dxd;
291 LONG accuxd = - (dxs >> 1);
292 DEF_USIZE ReadMask;
293 ULONG possible_columns, columncounter;
294 ULONG this_x;
296 while (count < DestWidth)
298 accuxd += dxs;
299 while (accuxd > accuxs )
301 xs++;
302 accuxs += dxd;
305 /* instead of copying column by column we can *maybe* even
306 copy more than one column at a time - we'll have to see */
308 if ((count & DEF_ANDMASK) > (xs & DEF_ANDMASK))
309 possible_columns = DEF_NUMBITSMINUS1 - (count & DEF_ANDMASK);
310 else
311 possible_columns = DEF_NUMBITSMINUS1 - (xs & DEF_ANDMASK);
313 columncounter = 1; /* one row, that's for sure!*/
314 this_x = xs; /* in counter we find the x-coord of the current source pixels */
316 LONG accuxd_tmp = accuxd;
317 LONG accuxs_tmp = accuxs;
318 ULONG next_x = xs;
319 ULONG count2 = count + 1;
321 while (possible_columns > 0 && count2 < DestWidth)
323 /* where's the next x-source-coordinate going to be? */
324 accuxd_tmp += dxs;
325 while (accuxd_tmp > accuxs_tmp )
327 next_x++;
328 accuxs_tmp += dxd;
331 if (this_x + 1 == next_x)
333 /* it's the immediately following coordinate */
334 columncounter++;
335 this_x++;
336 count2++;
338 else
340 /* we're copying more than on column then we have to change
341 * accuxd and accuxs
343 if (columncounter != 1)
345 accuxd = accuxd_tmp;
346 accuxs = accuxs_tmp;
348 break; /* the next column is not the neighbouring one */
351 /* determine how many more columns we can copy */
352 possible_columns--;
353 } /* while */
357 /* let's generate a mask that's columncounter bits wide */
358 ReadMask = DEF_READMASK;
359 ReadMask = (DEF_SIZE)ReadMask >> (columncounter - 1);
360 /* let's adjust this mask to the correct position */
361 ReadMask = ReadMask >> (xs & DEF_ANDMASK);
362 /* The leftmost set bit is xs & DEF_MASK away from the highest bit */
364 /* now that we have generated the read-mask we can copy all the columns
365 * that need copying in all bitmaps.
369 ULONG i,y;
370 ULONG ind;
371 LONG preshift = (xs & DEF_ANDMASK) - (count & DEF_ANDMASK);
372 ULONG shift;
373 ULONG AndMask;
374 struct BitMap * SrcBitMap = bitScaleArgs -> bsa_SrcBitMap;
375 struct BitMap * DestBitMap = bitScaleArgs -> bsa_DestBitMap;
377 if (preshift > 0)
379 shift = preshift;
380 AndMask = (ReadMask << shift) ^ (DEF_SIZE)(-1);
382 else
384 shift = -preshift;
385 AndMask = (ReadMask >> shift) ^ (DEF_SIZE)(-1);
388 /* treat all the Bitmaps after another */
389 for (i = 0; (i < DestBitMap -> Depth) && (i < SrcBitMap -> Depth); i++)
391 for (y = 0; y < bitScaleArgs -> bsa_DestHeight; y++)
393 DEF_USIZE CopyData;
394 ind = LinePattern[y] * (SrcBitMap -> BytesPerRow >> DEF_SHIFTY) + /* y-Coord */
395 (xs >> DEF_SHIFTX); /* x-Coord */
396 CopyData = ((DEF_USIZE *)SrcBitMap -> Planes[i])[ind];
397 CopyData = CopyData & ReadMask;
399 if (preshift > 0)
400 CopyData = CopyData << shift;
401 else
402 CopyData = CopyData >> shift;
404 /* ind correctly calculates the destination Address for the CopyData */
405 ind = y * ((bitScaleArgs -> bsa_DestY + DestBitMap -> BytesPerRow) >> DEF_SHIFTY) + /* y-Coord */
406 (count >> DEF_SHIFTX); /* x-Coord */
407 /* Leave a previous picture in the bitmap untouched except for in the
408 * area where the scaled picture goes into
410 ((DEF_USIZE *)DestBitMap ->Planes[i])[ind] =
411 (((DEF_USIZE *)DestBitMap ->Planes[i])[ind] & AndMask) | CopyData;
413 kprintf("Dest: %x\n\n",(LONG)((DEF_USIZE *)DestBitMap ->Planes[i])[ind]);
415 } /* for () */
416 } /* for () */
418 xs = this_x;
420 /* go to next x-coordinate */
421 count += columncounter;
422 } /* while */
425 /* let's get rid of the allocated memory */
426 FreeMem(LinePattern, sizeof(UWORD) * bitScaleArgs -> bsa_DestHeight);
428 #undef DEF_USIZE
429 #undef DEF_SIZE
430 #undef DEF_NUMBITSMINUS1
431 #undef DEF_ANDMASK
432 #undef DEF_READMASK
433 #undef DEF_SHIFTY
434 #undef DEF_SHIFTX
437 AROS_LIBFUNC_EXIT
438 } /* BitMapScale */