Fixed a warning (missing type cast)
[tangerine.git] / rom / graphics / bitmapscale.c
blob97715dbe0827ccc3b1225bd24af111a1a1e72871
1 /*
2 Copyright © 1995-2001, 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 args
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/scales.h
76 INTERNALS
78 HISTORY
80 *****************************************************************************/
82 AROS_LIBFUNC_INIT
83 AROS_LIBBASE_EXT_DECL(struct GfxBase *,GfxBase)
86 if (bitScaleArgs->bsa_SrcBitMap->pad != 0 ||
87 bitScaleArgs->bsa_DestBitMap->pad != 0 ||
88 bitScaleArgs->bsa_SrcBitMap->Flags & BMF_AROS_HIDD ||
89 bitScaleArgs->bsa_DestBitMap->Flags & BMF_AROS_HIDD)
91 ULONG srcflags = 0;
92 ULONG dstflags = 0;
94 BOOL src_colmap_set = FALSE;
95 BOOL dst_colmap_set = FALSE;
96 BOOL success = TRUE;
97 BOOL colmaps_ok = TRUE;
99 OOP_Object *srcbm_obj;
100 OOP_Object *dstbm_obj;
101 OOP_Object *tmp_gc;
104 srcbm_obj = OBTAIN_HIDD_BM(bitScaleArgs->bsa_SrcBitMap);
105 dstbm_obj = OBTAIN_HIDD_BM(bitScaleArgs->bsa_DestBitMap);
106 tmp_gc = obtain_cache_object(SDD(GfxBase)->gc_cache, GfxBase);
108 /* We must lock any HIDD_BM_SetColorMap calls */
109 LOCK_BLIT
110 if (srcbm_obj && dstbm_obj && tmp_gc)
112 /* Try to get a CLUT for the bitmaps */
113 if (IS_HIDD_BM(bitScaleArgs->bsa_SrcBitMap)) {
114 if (NULL != HIDD_BM_COLMAP(bitScaleArgs->bsa_SrcBitMap))
115 srcflags |= FLG_HASCOLMAP;
116 srcflags |= GET_COLMOD_FLAGS(bitScaleArgs->bsa_SrcBitMap);
117 } else {
118 /* Amiga BM */
119 srcflags |= FLG_PALETTE;
122 if (IS_HIDD_BM(bitScaleArgs->bsa_DestBitMap)) {
123 if (NULL != HIDD_BM_COLMAP(bitScaleArgs->bsa_DestBitMap))
124 dstflags |= FLG_HASCOLMAP;
125 dstflags |= GET_COLMOD_FLAGS(bitScaleArgs->bsa_DestBitMap);
126 } else {
127 /* Amiga BM */
128 dstflags |= FLG_PALETTE;
132 if ( (srcflags == FLG_PALETTE || srcflags == FLG_STATICPALETTE)) {
133 /* palettized with no colmap. Neew to get a colmap from dest*/
134 if (dstflags == FLG_TRUECOLOR) {
136 D(bug("!!! NO WAY GETTING PALETTE FOR src IN BltBitMap\n"));
137 colmaps_ok = FALSE;
138 success = FALSE;
140 } else if (dstflags == (FLG_TRUECOLOR | FLG_HASCOLMAP)) {
142 /* Use the dest colmap for src */
143 HIDD_BM_SetColorMap(srcbm_obj, HIDD_BM_COLMAP(bitScaleArgs->bsa_DestBitMap));
148 if ( (dstflags == FLG_PALETTE || dstflags == FLG_STATICPALETTE)) {
149 /* palettized with no pixtab. Nees to get a pixtab from dest*/
150 if (srcflags == FLG_TRUECOLOR) {
151 D(bug("!!! NO WAY GETTING PALETTE FOR dst IN BltBitMap\n"));
152 colmaps_ok = FALSE;
153 success = FALSE;
155 } else if (srcflags == (FLG_TRUECOLOR | FLG_HASCOLMAP)) {
157 /* Use the src colmap for dst */
158 HIDD_BM_SetColorMap(dstbm_obj, HIDD_BM_COLMAP(bitScaleArgs->bsa_SrcBitMap));
160 dst_colmap_set = TRUE;
164 if (TRUE == success && TRUE == colmaps_ok)
166 ULONG old_drmd;
167 struct TagItem cbtags[] = {
168 { aHidd_GC_DrawMode, vHidd_GC_DrawMode_Copy },
169 { TAG_DONE, 0 }
172 OOP_GetAttr(tmp_gc, aHidd_GC_DrawMode, &old_drmd);
174 OOP_SetAttrs(tmp_gc, cbtags);
176 bitScaleArgs->bsa_DestWidth = ScalerDiv(bitScaleArgs->bsa_SrcWidth,
177 bitScaleArgs->bsa_XDestFactor,
178 bitScaleArgs->bsa_XSrcFactor);
180 bitScaleArgs->bsa_DestHeight = ScalerDiv(bitScaleArgs->bsa_SrcHeight,
181 bitScaleArgs->bsa_YDestFactor,
182 bitScaleArgs->bsa_YSrcFactor);
184 HIDD_BM_BitMapScale(SDD(GfxBase)->pointerbm
185 , srcbm_obj
186 , dstbm_obj
187 , bitScaleArgs
188 , tmp_gc
191 cbtags[0].ti_Data = old_drmd;
192 OOP_SetAttrs(tmp_gc, cbtags);
193 } /* if () */
195 if (src_colmap_set)
196 HIDD_BM_SetColorMap(srcbm_obj, NULL);
197 if (dst_colmap_set)
198 HIDD_BM_SetColorMap(dstbm_obj, NULL);
201 if (dstbm_obj)
202 RELEASE_HIDD_BM(dstbm_obj, bitScaleArgs->bsa_DestBitMap);
204 if (srcbm_obj)
205 RELEASE_HIDD_BM(srcbm_obj, bitScaleArgs->bsa_SrcBitMap);
207 if (tmp_gc)
208 release_cache_object(SDD(GfxBase)->gc_cache, tmp_gc, GfxBase);
210 ULOCK_BLIT
213 else
216 * Algorithm for plain Amiga bitmaps.
219 * Unfortunately it's not possible to use 16/32 bit copying on bitmaps with this
220 * algorithm as there might be an odd number of bits per line in a bitmap and
221 * this creates problems when accessing the 2nd, 4th and so on line.
225 #define DEF_USIZE ULONG
226 #define DEF_SIZE LONG
227 #define DEF_NUMBITSMINUS1 31
228 #define DEF_MASK 31
229 #define DEF_READMASK 0x80000000
230 #define DEF_SHIFTY 2
231 #define DEF_SHIFTX 5
234 /* The following lines are necessary for BYTE copying and have to be used right
235 * now!!
238 #define DEF_USIZE UBYTE
239 #define DEF_SIZE BYTE
240 #define DEF_NUMBITSMINUS1 7
241 #define DEF_ANDMASK 7
242 #define DEF_READMASK 0x80
243 #define DEF_SHIFTY 0
244 #define DEF_SHIFTX 3
246 UWORD * LinePattern;
247 bitScaleArgs -> bsa_DestWidth = ScalerDiv(bitScaleArgs -> bsa_SrcWidth,
248 bitScaleArgs -> bsa_XDestFactor,
249 bitScaleArgs -> bsa_XSrcFactor);
251 bitScaleArgs -> bsa_DestHeight= ScalerDiv(bitScaleArgs -> bsa_SrcHeight,
252 bitScaleArgs -> bsa_YDestFactor,
253 bitScaleArgs -> bsa_YSrcFactor);
255 /* first of all lets allocate DestHeight words of memory so we can
256 precalculate which original line goes to which destination lines */
258 if (NULL ==(LinePattern = (UWORD *) AllocMem(sizeof(UWORD)*bitScaleArgs ->bsa_DestHeight, 0)))
259 return;
262 UWORD DestHeight = bitScaleArgs -> bsa_DestHeight;
263 UWORD ys = bitScaleArgs -> bsa_SrcY;
264 ULONG count = 0;
265 ULONG dyd = bitScaleArgs -> bsa_DestHeight;
266 ULONG dys = bitScaleArgs -> bsa_SrcHeight;
267 LONG accuys = dyd;
268 LONG accuyd = - (dys >> 1);
269 while (count < DestHeight)
271 accuyd += dys;
272 while (accuyd > accuys )
274 ys++;
275 accuys += dyd;
277 LinePattern[count] = ys;
278 count++;
284 /* now let's go for the real thing: scaling */
286 UWORD DestWidth = bitScaleArgs -> bsa_DestWidth + bitScaleArgs -> bsa_DestX;
287 ULONG xs = bitScaleArgs -> bsa_SrcX;
288 ULONG count = bitScaleArgs -> bsa_DestX;
289 ULONG dxd = bitScaleArgs -> bsa_DestWidth;
290 ULONG dxs = bitScaleArgs -> bsa_SrcWidth;
291 LONG accuxs = dxd;
292 LONG accuxd = - (dxs >> 1);
293 DEF_USIZE ReadMask;
294 ULONG possible_columns, columncounter;
295 ULONG this_x;
297 while (count < DestWidth)
299 accuxd += dxs;
300 while (accuxd > accuxs )
302 xs++;
303 accuxs += dxd;
306 /* instead of copying column by column we can *maybe* even
307 copy more than one column at a time - we'll have to see */
309 if ((count & DEF_ANDMASK) > (xs & DEF_ANDMASK))
310 possible_columns = DEF_NUMBITSMINUS1 - (count & DEF_ANDMASK);
311 else
312 possible_columns = DEF_NUMBITSMINUS1 - (xs & DEF_ANDMASK);
314 columncounter = 1; /* one row, that's for sure!*/
315 this_x = xs; /* in counter we find the x-coord of the current source pixels */
317 LONG accuxd_tmp = accuxd;
318 LONG accuxs_tmp = accuxs;
319 ULONG next_x = xs;
320 ULONG count2 = count + 1;
322 while (possible_columns > 0 && count2 < DestWidth)
324 /* where's the next x-source-coordinate going to be? */
325 accuxd_tmp += dxs;
326 while (accuxd_tmp > accuxs_tmp )
328 next_x++;
329 accuxs_tmp += dxd;
332 if (this_x + 1 == next_x)
334 /* it's the immediately following coordinate */
335 columncounter++;
336 this_x++;
337 count2++;
339 else
341 /* we're copying more than on column then we have to change
342 * accuxd and accuxs
344 if (columncounter != 1)
346 accuxd = accuxd_tmp;
347 accuxs = accuxs_tmp;
349 break; /* the next column is not the neighbouring one */
352 /* determine how many more columns we can copy */
353 possible_columns--;
354 } /* while */
358 /* let's generate a mask that's columncounter bits wide */
359 ReadMask = DEF_READMASK;
360 ReadMask = (DEF_SIZE)ReadMask >> (columncounter - 1);
361 /* let's adjust this mask to the correct position */
362 ReadMask = ReadMask >> (xs & DEF_ANDMASK);
363 /* The leftmost set bit is xs & DEF_MASK away from the highest bit */
365 /* now that we have generated the read-mask we can copy all the columns
366 * that need copying in all bitmaps.
370 ULONG i,y;
371 ULONG ind;
372 LONG preshift = (xs & DEF_ANDMASK) - (count & DEF_ANDMASK);
373 ULONG shift;
374 ULONG AndMask;
375 struct BitMap * SrcBitMap = bitScaleArgs -> bsa_SrcBitMap;
376 struct BitMap * DestBitMap = bitScaleArgs -> bsa_DestBitMap;
378 if (preshift > 0)
380 shift = preshift;
381 AndMask = (ReadMask << shift) ^ (DEF_SIZE)(-1);
383 else
385 shift = -preshift;
386 AndMask = (ReadMask >> shift) ^ (DEF_SIZE)(-1);
389 /* treat all the Bitmaps after another */
390 for (i = 0; (i < DestBitMap -> Depth) && (i < SrcBitMap -> Depth); i++)
392 for (y = 0; y < bitScaleArgs -> bsa_DestHeight; y++)
394 DEF_USIZE CopyData;
395 ind = LinePattern[y] * (SrcBitMap -> BytesPerRow >> DEF_SHIFTY) + /* y-Coord */
396 (xs >> DEF_SHIFTX); /* x-Coord */
397 CopyData = ((DEF_USIZE *)SrcBitMap -> Planes[i])[ind];
398 CopyData = CopyData & ReadMask;
400 if (preshift > 0)
401 CopyData = CopyData << shift;
402 else
403 CopyData = CopyData >> shift;
405 /* ind correctly calculates the destination Address for the CopyData */
406 ind = y * ((bitScaleArgs -> bsa_DestY + DestBitMap -> BytesPerRow) >> DEF_SHIFTY) + /* y-Coord */
407 (count >> DEF_SHIFTX); /* x-Coord */
408 /* Leave a previous picture in the bitmap untouched except for in the
409 * area where the scaled picture goes into
411 ((DEF_USIZE *)DestBitMap ->Planes[i])[ind] =
412 (((DEF_USIZE *)DestBitMap ->Planes[i])[ind] & AndMask) | CopyData;
414 kprintf("Dest: %x\n\n",(LONG)((DEF_USIZE *)DestBitMap ->Planes[i])[ind]);
416 } /* for () */
417 } /* for () */
419 xs = this_x;
421 /* go to next x-coordinate */
422 count += columncounter;
423 } /* while */
426 /* let's get rid of the allocated memory */
427 FreeMem(LinePattern, sizeof(UWORD) * bitScaleArgs -> bsa_DestHeight);
429 #undef DEF_USIZE
430 #undef DEF_SIZE
431 #undef DEF_NUMBITSMINUS1
432 #undef DEF_ANDMASK
433 #undef DEF_READMASK
434 #undef DEF_SHIFTY
435 #undef DEF_SHIFTX
438 AROS_LIBFUNC_EXIT
439 } /* BitMapScale */