3 * Copyright 1999-2000 Y.Takagi. All Rights Reserved.
4 * Copyright 2005 Michael Pfeiffer. All Rights Reserved.
5 * - Rewrote get_valid_rect from scratch.
13 class BoundsCalculator
16 bool getValidRect(BBitmap
*bitmap
, RECT
*rect
);
30 INLINE
bool isEmpty(const rgb_color
*pixel
);
32 INLINE
bool isRowEmpty(const rgb_color
*row
);
34 INLINE
const uchar
*getRow(int x
, int y
);
40 INLINE
void updateLeftBound(const rgb_color
*row
);
41 INLINE
void updateRightBound(const rgb_color
*row
);
46 BoundsCalculator::isEmpty(const rgb_color
*pixel
)
48 return pixel
->red
== 0xff && pixel
->green
== 0xff && pixel
->blue
== 0xff;
53 BoundsCalculator::isRowEmpty(const rgb_color
*row
)
55 for (int x
= 0; x
< fWidth
; x
++) {
66 BoundsCalculator::getRow(int x
, int y
)
68 return fBits
+ x
+ fBPR
* y
;
73 BoundsCalculator::getTop()
75 const uchar
* row
= getRow(fLeft
, fTop
);
78 for (top
= fTop
; top
<= fBottom
; top
++) {
79 if (!isRowEmpty((const rgb_color
*)row
)) {
90 BoundsCalculator::getBottom()
92 const uchar
*row
= getRow(fLeft
, fBottom
);
95 for (bottom
= fBottom
; bottom
>= fTop
; bottom
--) {
96 if (!isRowEmpty((const rgb_color
*)row
)) {
107 BoundsCalculator::updateLeftBound(const rgb_color
*row
)
109 for (int x
= fLeft
; x
< fLeftBound
; x
++) {
120 BoundsCalculator::updateRightBound(const rgb_color
*row
)
123 for (int x
= fRight
; x
> fRightBound
; x
--) {
133 // returns false if the bitmap is empty or has wrong color space.
135 BoundsCalculator::getValidRect(BBitmap
*bitmap
, RECT
*rect
)
138 kRectIsInvalid
= false,
139 kRectIsEmpty
= false,
143 switch (bitmap
->ColorSpace()) {
148 return kRectIsInvalid
;
152 // initialize member variables
153 fBits
= (uchar
*)bitmap
->Bits();
154 fBPR
= bitmap
->BytesPerRow();
157 fRight
= rect
->right
;
159 fBottom
= rect
->bottom
;
161 fWidth
= fRight
- fLeft
+ 1;
165 if (fTop
> fBottom
) {
170 fBottom
= getBottom();
172 // calculate left and right bounds
173 fLeftBound
= fRight
+ 1;
174 fRightBound
= fLeft
- 1;
176 const uchar
*row
= getRow(fLeft
, fTop
);
177 for (int y
= fTop
; y
<= fBottom
; y
++) {
178 updateLeftBound((const rgb_color
*)row
);
179 updateRightBound((const rgb_color
*)row
);
180 if (fLeft
== fLeftBound
&& fRight
== fRightBound
) {
186 // return bounds in rectangle
187 rect
->left
= fLeftBound
;
188 rect
->right
= fRightBound
;
190 rect
->bottom
= fBottom
;
196 bool get_valid_rect(BBitmap
*a_bitmap
, RECT
*rc
)
198 BoundsCalculator calculator
;
199 return calculator
.getValidRect(a_bitmap
, rc
);
203 int color_space2pixel_depth(color_space cs
)
208 case B_GRAY1
: /* Y0[0],Y1[0],Y2[0],Y3[0],Y4[0],Y5[0],Y6[0],Y7[0] */
211 case B_GRAY8
: /* Y[7:0] */
212 case B_CMAP8
: /* D[7:0] */
215 case B_RGB15
: /* G[2:0],B[4:0] -[0],R[4:0],G[4:3] */
216 case B_RGB15_BIG
: /* -[0],R[4:0],G[4:3] G[2:0],B[4:0] */
219 case B_RGB32
: /* B[7:0] G[7:0] R[7:0] -[7:0] */
220 case B_RGB32_BIG
: /* -[7:0] R[7:0] G[7:0] B[7:0] */