2 Copyright © 1995-2013, The AROS Development Team. All rights reserved.
5 Desc: Graphics function BitMapScale()
9 #include <aros/debug.h>
10 #include <graphics/scale.h>
11 #include <proto/exec.h>
12 #include <proto/oop.h>
14 #include "graphics_intern.h"
15 #include "gfxfuncsupport.h"
18 /*****************************************************************************
21 #include <proto/graphics.h>
23 AROS_LH1(void, BitMapScale
,
26 AROS_LHA(struct BitScaleArgs
*, bitScaleArgs
, A0
),
29 struct GfxBase
*, GfxBase
, 113, Graphics
)
32 Scale a source bit map to a destination bit map other than
36 Pass a BitScaleArgs structure filled with the following arguments
38 bsa_SrcX, bsa_SrcY - upper left coordinate in source bitmap
39 bsa_SrcWidth, bsa_SrcHeight - Width and Height of source bitmap
40 bsa_DestX, bsa_DestY - upper left coordinate in destination
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_SrcWidth:bsa_DestWidth, but
46 usually not exactly the same number.
47 bsa_YSrcFactor:bsa_YDestFactor - Set these to get approximately
48 the same ratio as bsa_SrcHeight:DestHeight, 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 hold the scaled bitmap. Make sure it's
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.
59 bsa_DestWidth and bsa_DestHeight will be set by this function
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
74 ScalerDiv(), graphics/scale.h
80 *****************************************************************************/
84 if (IS_HIDD_BM(bitScaleArgs
->bsa_SrcBitMap
) ||
85 IS_HIDD_BM(bitScaleArgs
->bsa_DestBitMap
))
90 BOOL src_colmap_set
= FALSE
;
91 BOOL dst_colmap_set
= FALSE
;
93 BOOL colmaps_ok
= TRUE
;
95 OOP_Object
*srcbm_obj
;
96 OOP_Object
*dstbm_obj
;
99 srcbm_obj
= OBTAIN_HIDD_BM(bitScaleArgs
->bsa_SrcBitMap
);
100 dstbm_obj
= OBTAIN_HIDD_BM(bitScaleArgs
->bsa_DestBitMap
);
101 tmp_gc
= obtain_cache_object(CDD(GfxBase
)->gc_cache
, GfxBase
);
103 /* We must lock any HIDD_BM_SetColorMap calls */
105 if (srcbm_obj
&& dstbm_obj
&& tmp_gc
)
107 /* Try to get a CLUT for the bitmaps */
108 if (IS_HIDD_BM(bitScaleArgs
->bsa_SrcBitMap
)) {
109 if (NULL
!= HIDD_BM_COLMAP(bitScaleArgs
->bsa_SrcBitMap
))
110 srcflags
|= FLG_HASCOLMAP
;
111 srcflags
|= GET_COLMOD_FLAGS(bitScaleArgs
->bsa_SrcBitMap
);
114 srcflags
|= FLG_PALETTE
;
117 if (IS_HIDD_BM(bitScaleArgs
->bsa_DestBitMap
)) {
118 if (NULL
!= HIDD_BM_COLMAP(bitScaleArgs
->bsa_DestBitMap
))
119 dstflags
|= FLG_HASCOLMAP
;
120 dstflags
|= GET_COLMOD_FLAGS(bitScaleArgs
->bsa_DestBitMap
);
123 dstflags
|= FLG_PALETTE
;
127 if ( (srcflags
== FLG_PALETTE
|| srcflags
== FLG_STATICPALETTE
)) {
128 /* palettized with no colmap. Need to get a colmap from dest */
129 if (dstflags
== FLG_TRUECOLOR
) {
131 D(bug("!!! NO WAY GETTING PALETTE FOR src IN BltBitMap\n"));
135 } else if (dstflags
== (FLG_TRUECOLOR
| FLG_HASCOLMAP
)) {
137 /* Use the dest colmap for src */
138 HIDD_BM_SetColorMap(srcbm_obj
, HIDD_BM_COLMAP(bitScaleArgs
->bsa_DestBitMap
));
143 if ( (dstflags
== FLG_PALETTE
|| dstflags
== FLG_STATICPALETTE
)) {
144 /* palettized with no pixtab. Need to get a pixtab from dest */
145 if (srcflags
== FLG_TRUECOLOR
) {
146 D(bug("!!! NO WAY GETTING PALETTE FOR dst IN BltBitMap\n"));
150 } else if (srcflags
== (FLG_TRUECOLOR
| FLG_HASCOLMAP
)) {
152 /* Use the src colmap for dst */
153 HIDD_BM_SetColorMap(dstbm_obj
, HIDD_BM_COLMAP(bitScaleArgs
->bsa_SrcBitMap
));
155 dst_colmap_set
= TRUE
;
159 if (success
&& colmaps_ok
)
161 struct monitor_driverdata
*driver
, *dst_driver
;
163 HIDDT_DrawMode old_drmd
;
164 struct TagItem cbtags
[] = {
165 { aHidd_GC_DrawMode
, vHidd_GC_DrawMode_Copy
},
169 OOP_GetAttr(tmp_gc
, aHidd_GC_DrawMode
, &old_drmd
);
170 OOP_SetAttrs(tmp_gc
, cbtags
);
172 bitScaleArgs
->bsa_DestWidth
= ScalerDiv(bitScaleArgs
->bsa_SrcWidth
,
173 bitScaleArgs
->bsa_XDestFactor
,
174 bitScaleArgs
->bsa_XSrcFactor
);
176 bitScaleArgs
->bsa_DestHeight
= ScalerDiv(bitScaleArgs
->bsa_SrcHeight
,
177 bitScaleArgs
->bsa_YDestFactor
,
178 bitScaleArgs
->bsa_YSrcFactor
);
181 * Select a driver to call. The same as in BltBitMap(), but select
182 * bitmap object instead of driver object.
184 driver
= GET_BM_DRIVERDATA(bitScaleArgs
->bsa_SrcBitMap
);
185 dst_driver
= GET_BM_DRIVERDATA(bitScaleArgs
->bsa_DestBitMap
);
187 if (driver
== (struct monitor_driverdata
*)CDD(GfxBase
))
189 else if (dst_driver
->flags
& DF_UseFakeGfx
)
194 HIDD_BM_BitMapScale(bm_obj
200 update_bitmap(bitScaleArgs
->bsa_DestBitMap
, dstbm_obj
,
201 bitScaleArgs
->bsa_DestX
, bitScaleArgs
->bsa_DestY
,
202 bitScaleArgs
->bsa_DestWidth
, bitScaleArgs
->bsa_DestHeight
,
205 cbtags
[0].ti_Data
= old_drmd
;
206 OOP_SetAttrs(tmp_gc
, cbtags
);
210 HIDD_BM_SetColorMap(srcbm_obj
, NULL
);
212 HIDD_BM_SetColorMap(dstbm_obj
, NULL
);
216 RELEASE_HIDD_BM(dstbm_obj
, bitScaleArgs
->bsa_DestBitMap
);
219 RELEASE_HIDD_BM(srcbm_obj
, bitScaleArgs
->bsa_SrcBitMap
);
222 release_cache_object(CDD(GfxBase
)->gc_cache
, tmp_gc
, GfxBase
);
230 * Algorithm for plain Amiga bitmaps.
233 * Unfortunately it's not possible to use 16/32 bit copying on bitmaps with this
234 * algorithm as there might be an odd number of bits per line in a bitmap and
235 * this creates problems when accessing the 2nd, 4th and so on line.
239 #define DEF_USIZE ULONG
240 #define DEF_SIZE LONG
241 #define DEF_NUMBITSMINUS1 31
243 #define DEF_READMASK 0x80000000
248 /* The following lines are necessary for BYTE copying and have to be used right
252 #define DEF_USIZE UBYTE
253 #define DEF_SIZE BYTE
254 #define DEF_NUMBITSMINUS1 7
255 #define DEF_ANDMASK 7
256 #define DEF_READMASK 0x80
261 bitScaleArgs
-> bsa_DestWidth
= ScalerDiv(bitScaleArgs
-> bsa_SrcWidth
,
262 bitScaleArgs
-> bsa_XDestFactor
,
263 bitScaleArgs
-> bsa_XSrcFactor
);
265 bitScaleArgs
-> bsa_DestHeight
= ScalerDiv(bitScaleArgs
-> bsa_SrcHeight
,
266 bitScaleArgs
-> bsa_YDestFactor
,
267 bitScaleArgs
-> bsa_YSrcFactor
);
269 /* first of all lets allocate DestHeight words of memory so we can
270 precalculate which original line goes to which destination lines */
272 if (NULL
==(LinePattern
= (UWORD
*) AllocMem(sizeof(UWORD
)*bitScaleArgs
->bsa_DestHeight
, 0)))
276 UWORD DestHeight
= bitScaleArgs
-> bsa_DestHeight
;
277 UWORD ys
= bitScaleArgs
-> bsa_SrcY
;
279 ULONG dyd
= bitScaleArgs
-> bsa_DestHeight
;
280 ULONG dys
= bitScaleArgs
-> bsa_SrcHeight
;
282 LONG accuyd
= - (dys
>> 1);
283 while (count
< DestHeight
)
286 while (accuyd
> accuys
)
291 LinePattern
[count
] = ys
;
298 /* now let's go for the real thing: scaling */
300 UWORD DestWidth
= bitScaleArgs
-> bsa_DestWidth
+ bitScaleArgs
-> bsa_DestX
;
301 ULONG xs
= bitScaleArgs
-> bsa_SrcX
;
302 ULONG count
= bitScaleArgs
-> bsa_DestX
;
303 ULONG dxd
= bitScaleArgs
-> bsa_DestWidth
;
304 ULONG dxs
= bitScaleArgs
-> bsa_SrcWidth
;
306 LONG accuxd
= - (dxs
>> 1);
308 ULONG possible_columns
, columncounter
;
311 while (count
< DestWidth
)
314 while (accuxd
> accuxs
)
320 /* instead of copying column by column we can *maybe* even
321 copy more than one column at a time - we'll have to see */
323 if ((count
& DEF_ANDMASK
) > (xs
& DEF_ANDMASK
))
324 possible_columns
= DEF_NUMBITSMINUS1
- (count
& DEF_ANDMASK
);
326 possible_columns
= DEF_NUMBITSMINUS1
- (xs
& DEF_ANDMASK
);
328 columncounter
= 1; /* one row, that's for sure!*/
329 this_x
= xs
; /* in counter we find the x-coord of the current source pixels */
331 LONG accuxd_tmp
= accuxd
;
332 LONG accuxs_tmp
= accuxs
;
334 ULONG count2
= count
+ 1;
336 while (possible_columns
> 0 && count2
< DestWidth
)
338 /* where's the next x-source-coordinate going to be? */
340 while (accuxd_tmp
> accuxs_tmp
)
346 if (this_x
+ 1 == next_x
)
348 /* it's the immediately following coordinate */
355 /* we're copying more than on column then we have to change
358 if (columncounter
!= 1)
363 break; /* the next column is not the neighbouring one */
366 /* determine how many more columns we can copy */
372 /* let's generate a mask that's columncounter bits wide */
373 ReadMask
= DEF_READMASK
;
374 ReadMask
= (DEF_SIZE
)ReadMask
>> (columncounter
- 1);
375 /* let's adjust this mask to the correct position */
376 ReadMask
= ReadMask
>> (xs
& DEF_ANDMASK
);
377 /* The leftmost set bit is xs & DEF_MASK away from the highest bit */
379 /* now that we have generated the read-mask we can copy all the columns
380 * that need copying in all bitmaps.
386 LONG preshift
= (xs
& DEF_ANDMASK
) - (count
& DEF_ANDMASK
);
389 struct BitMap
* SrcBitMap
= bitScaleArgs
-> bsa_SrcBitMap
;
390 struct BitMap
* DestBitMap
= bitScaleArgs
-> bsa_DestBitMap
;
395 AndMask
= (ReadMask
<< shift
) ^ (DEF_SIZE
)(-1);
400 AndMask
= (ReadMask
>> shift
) ^ (DEF_SIZE
)(-1);
403 /* treat all the Bitmaps after another */
404 for (i
= 0; (i
< DestBitMap
-> Depth
) && (i
< SrcBitMap
-> Depth
); i
++)
406 for (y
= 0; y
< bitScaleArgs
-> bsa_DestHeight
; y
++)
409 ind
= LinePattern
[y
] * (SrcBitMap
-> BytesPerRow
>> DEF_SHIFTY
) + /* y-Coord */
410 (xs
>> DEF_SHIFTX
); /* x-Coord */
411 CopyData
= ((DEF_USIZE
*)SrcBitMap
-> Planes
[i
])[ind
];
412 CopyData
= CopyData
& ReadMask
;
415 CopyData
= CopyData
<< shift
;
417 CopyData
= CopyData
>> shift
;
419 /* ind correctly calculates the destination Address for the CopyData */
420 ind
= y
* ((bitScaleArgs
-> bsa_DestY
+ DestBitMap
-> BytesPerRow
) >> DEF_SHIFTY
) + /* y-Coord */
421 (count
>> DEF_SHIFTX
); /* x-Coord */
422 /* Leave a previous picture in the bitmap untouched except for in the
423 * area where the scaled picture goes into
425 ((DEF_USIZE
*)DestBitMap
->Planes
[i
])[ind
] =
426 (((DEF_USIZE
*)DestBitMap
->Planes
[i
])[ind
] & AndMask
) | CopyData
;
428 kprintf("Dest: %x\n\n",(LONG)((DEF_USIZE *)DestBitMap ->Planes[i])[ind]);
435 /* go to next x-coordinate */
436 count
+= columncounter
;
440 /* let's get rid of the allocated memory */
441 FreeMem(LinePattern
, sizeof(UWORD
) * bitScaleArgs
-> bsa_DestHeight
);
445 #undef DEF_NUMBITSMINUS1