1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714
|
// ==========================================================
// Bitmap rotation by means of 3 shears.
//
// Design and implementation by
// - Herv Drolon (drolon@infonie.fr)
// - Thorsten Radde (support@IdealSoftware.com)
//
// This file is part of FreeImage 3
//
// COVERED CODE IS PROVIDED UNDER THIS LICENSE ON AN "AS IS" BASIS, WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, WITHOUT LIMITATION, WARRANTIES
// THAT THE COVERED CODE IS FREE OF DEFECTS, MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE
// OR NON-INFRINGING. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE COVERED
// CODE IS WITH YOU. SHOULD ANY COVERED CODE PROVE DEFECTIVE IN ANY RESPECT, YOU (NOT
// THE INITIAL DEVELOPER OR ANY OTHER CONTRIBUTOR) ASSUME THE COST OF ANY NECESSARY
// SERVICING, REPAIR OR CORRECTION. THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL
// PART OF THIS LICENSE. NO USE OF ANY COVERED CODE IS AUTHORIZED HEREUNDER EXCEPT UNDER
// THIS DISCLAIMER.
//
// Use at your own risk!
// ==========================================================
/*
============================================================
References :
[1] Paeth A., A Fast Algorithm for General Raster Rotation.
Graphics Gems, p. 179, Andrew Glassner editor, Academic Press, 1990.
[2] Yariv E., High quality image rotation (rotate by shear).
[Online] http://www.codeproject.com/bitmap/rotatebyshear.asp
[3] Treskunov A., Fast and high quality true-color bitmap rotation function.
[Online] http://anton.treskunov.net/Software/doc/fast_and_high_quality_true_color_bitmap_rotation_function.html
============================================================
*/
#include "FreeImage.h"
#include "Utilities.h"
#define ROTATE_PI double (3.1415926535897932384626433832795)
#define RBLOCK 64 // image blocks of RBLOCK*RBLOCK pixels
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Prototypes definition
static void HorizontalSkew(FIBITMAP *src, FIBITMAP *dst, int row, int iOffset, double dWeight);
static void VerticalSkew(FIBITMAP *src, FIBITMAP *dst, int col, int iOffset, double dWeight);
static FIBITMAP* Rotate90(FIBITMAP *src);
static FIBITMAP* Rotate180(FIBITMAP *src);
static FIBITMAP* Rotate270(FIBITMAP *src);
static FIBITMAP* Rotate45(FIBITMAP *src, double dAngle);
static FIBITMAP* RotateAny(FIBITMAP *src, double dAngle);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
Skews a row horizontally (with filtered weights).
Limited to 45 degree skewing only. Filters two adjacent pixels.
@param src Pointer to source image to rotate
@param dst Pointer to destination image
@param row Row index
@param iOffset Skew offset
@param dWeight Relative weight of right pixel
*/
static void
HorizontalSkew(FIBITMAP *src, FIBITMAP *dst, int row, int iOffset, BYTE Weight) {
int i, j;
int iXPos;
int src_width = FreeImage_GetWidth(src);
int dst_width = FreeImage_GetWidth(dst);
switch(FreeImage_GetBPP(src)) {
case 8:
case 24:
case 32:
{
BYTE pxlSrc[4], pxlLeft[4], pxlOldLeft[4]; // 4 = 32-bit max
// calculate the number of bytes per pixel (1 for 8-bit, 3 for 24-bit or 4 for 32-bit)
int bytespp = FreeImage_GetLine(src) / FreeImage_GetWidth(src);
BYTE *src_bits = FreeImage_GetScanLine(src, row);
BYTE *dst_bits = FreeImage_GetScanLine(dst, row);
// fill gap left of skew with background
if(iOffset > 0) {
memset(dst_bits, 0, iOffset * bytespp);
}
memset(&pxlOldLeft[0], 0, bytespp);
for(i = 0; i < src_width; i++) {
// loop through row pixels
memcpy(&pxlSrc[0], src_bits, bytespp);
// calculate weights
for(j = 0; j < bytespp; j++) {
pxlLeft[j] = BYTE(((WORD)pxlSrc[j] * Weight) / 256);
}
// check boundaries
iXPos = i + iOffset;
if((iXPos >= 0) && (iXPos < dst_width)) {
// update left over on source
for(j = 0; j < bytespp; j++) {
pxlSrc[j] = pxlSrc[j] - (pxlLeft[j] - pxlOldLeft[j]);
}
memcpy(&dst_bits[iXPos*bytespp], &pxlSrc[0], bytespp);
}
// save leftover for next pixel in scan
memcpy(&pxlOldLeft[0], &pxlLeft[0], bytespp);
// next pixel in scan
src_bits += bytespp;
}
// go to rightmost point of skew
iXPos = src_width + iOffset;
if(iXPos < dst_width) {
dst_bits = FreeImage_GetScanLine(dst, row) + iXPos * bytespp;
// If still in image bounds, put leftovers there
memcpy(dst_bits, &pxlOldLeft[0], bytespp);
// clear to the right of the skewed line with background
dst_bits += bytespp;
memset(dst_bits, 0, bytespp * (dst_width - iXPos - 1));
}
}
break;
}
}
/**
Skews a column vertically (with filtered weights).
Limited to 45 degree skewing only. Filters two adjacent pixels.
@param src Pointer to source image to rotate
@param dst Pointer to destination image
@param col Column index
@param iOffset Skew offset
@param dWeight Relative weight of upper pixel
*/
static void
VerticalSkew(FIBITMAP *src, FIBITMAP *dst, int col, int iOffset, BYTE Weight) {
int i, j, iYPos;
int src_height = FreeImage_GetHeight(src);
int dst_height = FreeImage_GetHeight(dst);
switch(FreeImage_GetBPP(src)) {
case 8:
case 24:
case 32:
{
BYTE pxlSrc[4], pxlLeft[4], pxlOldLeft[4]; // 4 = 32-bit max
// calculate the number of bytes per pixel (1 for 8-bit, 3 for 24-bit or 4 for 32-bit)
int bytespp = FreeImage_GetLine(src) / FreeImage_GetWidth(src);
unsigned src_pitch = FreeImage_GetPitch(src);
unsigned dst_pitch = FreeImage_GetPitch(dst);
unsigned index = col * bytespp;
BYTE *src_bits = FreeImage_GetBits(src) + index;
BYTE *dst_bits = FreeImage_GetBits(dst) + index;
// fill gap above skew with background
if(iOffset > 0) {
for(i = 0; i < iOffset; i++) {
memset(dst_bits, 0, bytespp);
dst_bits += dst_pitch;
}
}
memset(&pxlOldLeft[0], 0, bytespp);
for(i = 0; i < src_height; i++) {
// loop through column pixels
memcpy(&pxlSrc[0], src_bits, bytespp);
// calculate weights
for(j = 0; j < bytespp; j++) {
pxlLeft[j] = BYTE(((WORD)pxlSrc[j] * Weight) / 256);
}
// check boundaries
iYPos = i + iOffset;
if((iYPos >= 0) && (iYPos < dst_height)) {
// update left over on source
for(j = 0; j < bytespp; j++) {
pxlSrc[j] = pxlSrc[j] - (pxlLeft[j] - pxlOldLeft[j]);
}
dst_bits = FreeImage_GetScanLine(dst, iYPos) + index;
memcpy(dst_bits, &pxlSrc[0], bytespp);
}
// save leftover for next pixel in scan
memcpy(&pxlOldLeft[0], &pxlLeft[0], bytespp);
// next pixel in scan
src_bits += src_pitch;
}
// go to bottom point of skew
iYPos = src_height + iOffset;
if(iYPos < dst_height) {
dst_bits = FreeImage_GetScanLine(dst, iYPos) + index;
// if still in image bounds, put leftovers there
memcpy(dst_bits, &pxlOldLeft[0], bytespp);
// clear below skewed line with background
while(++iYPos < dst_height) {
dst_bits += dst_pitch;
memset(dst_bits, 0, bytespp);
}
}
}
break;
}
}
/**
Rotates an image by 90 degrees (counter clockwise).
Precise rotation, no filters required.<br>
Code adapted from CxImage (http://www.xdp.it/cximage.htm)
@param src Pointer to source image to rotate
@return Returns a pointer to a newly allocated rotated image if successful, returns NULL otherwise
*/
static FIBITMAP*
Rotate90(FIBITMAP *src) {
int x, y, y2;
int bpp = FreeImage_GetBPP(src);
int src_width = FreeImage_GetWidth(src);
int src_height = FreeImage_GetHeight(src);
int dst_width = src_height;
int dst_height = src_width;
// allocate and clear dst image
FIBITMAP *dst = FreeImage_Allocate(dst_width, dst_height, bpp);
if(NULL == dst) return NULL;
// get src and dst scan width
int src_pitch = FreeImage_GetPitch(src);
int dst_pitch = FreeImage_GetPitch(dst);
if(bpp == 1) {
// speedy rotate for BW images
BYTE *sbits, *dbits, *dbitsmax, bitpos, *nrow, *srcdisp;
div_t div_r;
BYTE *bsrc = FreeImage_GetBits(src);
BYTE *bdest = FreeImage_GetBits(dst);
dbitsmax = bdest + dst_height * dst_pitch - 1;
for(y = 0; y < src_height; y++) {
// figure out the column we are going to be copying to
div_r = div(y, 8);
// set bit pos of src column byte
bitpos = (BYTE)(128 >> div_r.rem);
srcdisp = bsrc + y * src_pitch;
for (x = 0; x < src_pitch; x++) {
// get source bits
sbits = srcdisp + x;
// get destination column
nrow = bdest + (dst_height - 1 - (x * 8)) * dst_pitch + div_r.quot;
for (int z = 0; z < 8; z++) {
// get destination byte
dbits = nrow - z * dst_pitch;
if ((dbits < bdest) || (dbits > dbitsmax)) break;
if (*sbits & (128 >> z)) *dbits |= bitpos;
}
}
}
}
else if((bpp == 8) || (bpp == 24) || (bpp == 32)) {
// anything other than BW :
// This optimized version of rotation rotates image by smaller blocks. It is quite
// a bit faster than obvious algorithm, because it produces much less CPU cache misses.
// This optimization can be tuned by changing block size (RBLOCK). 96 is good value for current
// CPUs (tested on Athlon XP and Celeron D). Larger value (if CPU has enough cache) will increase
// speed somehow, but once you drop out of CPU's cache, things will slow down drastically.
// For older CPUs with less cache, lower value would yield better results.
int xs, ys; // x-segment and y-segment
BYTE *bsrc = FreeImage_GetBits(src); // source pixels
BYTE *bdest = FreeImage_GetBits(dst); // destination pixels
// calculate the number of bytes per pixel (1 for 8-bit, 3 for 24-bit or 4 for 32-bit)
int bytespp = FreeImage_GetLine(src) / FreeImage_GetWidth(src);
for(xs = 0; xs < dst_width; xs += RBLOCK) { // for all image blocks of RBLOCK*RBLOCK pixels
for(ys = 0; ys < dst_height; ys += RBLOCK) {
for(y = ys; y < MIN(dst_height, ys + RBLOCK); y++) { // do rotation
y2 = dst_height - y - 1;
// point to src pixel at (y2, xs)
BYTE *src_bits = bsrc + (xs * src_pitch) + (y2 * bytespp);
// point to dst pixel at (xs, y)
BYTE *dst_bits = bdest + (y * dst_pitch) + (xs * bytespp);
for (x = xs; x < MIN(dst_width, xs + RBLOCK); x++) {
// dst.SetPixel(x, y, src.GetPixel(y2, x));
for(int j = 0; j < bytespp; j++) {
dst_bits[j] = src_bits[j];
}
dst_bits += bytespp;
src_bits += src_pitch;
}
}
}
}
}
return dst;
}
/**
Rotates an image by 180 degrees (counter clockwise).
Precise rotation, no filters required.
@param src Pointer to source image to rotate
@return Returns a pointer to a newly allocated rotated image if successful, returns NULL otherwise
*/
static FIBITMAP*
Rotate180(FIBITMAP *src) {
int x, y, k, pos;
int bpp = FreeImage_GetBPP(src);
int src_width = FreeImage_GetWidth(src);
int src_height = FreeImage_GetHeight(src);
int dst_width = src_width;
int dst_height = src_height;
FIBITMAP *dst = FreeImage_Allocate(dst_width, dst_height, bpp);
if(NULL == dst) return NULL;
if(bpp == 1) {
for(int y = 0; y < src_height; y++) {
BYTE *src_bits = FreeImage_GetScanLine(src, y);
BYTE *dst_bits = FreeImage_GetScanLine(dst, dst_height - y - 1);
for(int x = 0; x < src_width; x++) {
// get bit at (x, y)
k = (src_bits[x >> 3] & (0x80 >> (x & 0x07))) != 0;
// set bit at (dst_width - x - 1, dst_height - y - 1)
pos = dst_width - x - 1;
k ? dst_bits[pos >> 3] |= (0x80 >> (pos & 0x7)) : dst_bits[pos >> 3] &= (0xFF7F >> (pos & 0x7));
}
}
}
else if((bpp == 8) || (bpp == 24) || (bpp == 32)) {
// Calculate the number of bytes per pixel (1 for 8-bit, 3 for 24-bit or 4 for 32-bit)
int bytespp = FreeImage_GetLine(src) / FreeImage_GetWidth(src);
for(y = 0; y < src_height; y++) {
BYTE *src_bits = FreeImage_GetScanLine(src, y);
BYTE *dst_bits = FreeImage_GetScanLine(dst, dst_height - y - 1) + (dst_width - 1) * bytespp;
for(x = 0; x < src_width; x++) {
// get pixel at (x, y)
// set pixel at (dst_width - x - 1, dst_height - y - 1)
for(k = 0; k < bytespp; k++) {
dst_bits[k] = src_bits[k];
}
src_bits += bytespp;
dst_bits -= bytespp;
}
}
}
return dst;
}
/**
Rotates an image by 270 degrees (counter clockwise).
Precise rotation, no filters required.<br>
Code adapted from CxImage (http://www.xdp.it/cximage.htm)
@param src Pointer to source image to rotate
@return Returns a pointer to a newly allocated rotated image if successful, returns NULL otherwise
*/
static FIBITMAP*
Rotate270(FIBITMAP *src) {
int x, x2, y, dlineup;
int bpp = FreeImage_GetBPP(src);
int src_width = FreeImage_GetWidth(src);
int src_height = FreeImage_GetHeight(src);
int dst_width = src_height;
int dst_height = src_width;
// allocate and clear dst image
FIBITMAP *dst = FreeImage_Allocate(dst_width, dst_height, bpp);
if(NULL == dst) return NULL;
// get src and dst scan width
int src_pitch = FreeImage_GetPitch(src);
int dst_pitch = FreeImage_GetPitch(dst);
if(bpp == 1) {
// speedy rotate for BW images
BYTE *sbits, *dbits, *dbitsmax, bitpos, *nrow, *srcdisp;
div_t div_r;
BYTE *bsrc = FreeImage_GetBits(src);
BYTE *bdest = FreeImage_GetBits(dst);
dbitsmax = bdest + dst_height * dst_pitch - 1;
dlineup = 8 * dst_pitch - dst_width;
for(y = 0; y < src_height; y++) {
// figure out the column we are going to be copying to
div_r = div(y + dlineup, 8);
// set bit pos of src column byte
bitpos = (BYTE)(1 << div_r.rem);
srcdisp = bsrc + y * src_pitch;
for (x = 0; x < src_pitch; x++) {
// get source bits
sbits = srcdisp + x;
// get destination column
nrow = bdest + (x * 8) * dst_pitch + dst_pitch - 1 - div_r.quot;
for (int z = 0; z < 8; z++) {
// get destination byte
dbits = nrow + z * dst_pitch;
if ((dbits < bdest) || (dbits > dbitsmax)) break;
if (*sbits & (128 >> z)) *dbits |= bitpos;
}
}
}
}
else if((bpp == 8) || (bpp == 24) || (bpp == 32)) {
// anything other than BW :
// This optimized version of rotation rotates image by smaller blocks. It is quite
// a bit faster than obvious algorithm, because it produces much less CPU cache misses.
// This optimization can be tuned by changing block size (RBLOCK). 96 is good value for current
// CPUs (tested on Athlon XP and Celeron D). Larger value (if CPU has enough cache) will increase
// speed somehow, but once you drop out of CPU's cache, things will slow down drastically.
// For older CPUs with less cache, lower value would yield better results.
int xs, ys; // x-segment and y-segment
BYTE *bsrc = FreeImage_GetBits(src); // source pixels
BYTE *bdest = FreeImage_GetBits(dst); // destination pixels
// Calculate the number of bytes per pixel (1 for 8-bit, 3 for 24-bit or 4 for 32-bit)
int bytespp = FreeImage_GetLine(src) / FreeImage_GetWidth(src);
for(xs = 0; xs < dst_width; xs += RBLOCK) { // for all image blocks of RBLOCK*RBLOCK pixels
for(ys = 0; ys < dst_height; ys += RBLOCK) {
for(x = xs; x < MIN(dst_width, xs + RBLOCK); x++) { // do rotation
x2 = dst_width - x - 1;
// point to src pixel at (ys, x2)
BYTE *src_bits = bsrc + (x2 * src_pitch) + (ys * bytespp);
// point to dst pixel at (x, ys)
BYTE *dst_bits = bdest + (ys * dst_pitch) + (x * bytespp);
for (y = ys; y < MIN(dst_height, ys + RBLOCK); y++) {
// dst.SetPixel(x, y, src.GetPixel(y, x2));
for(int j = 0; j < bytespp; j++) {
dst_bits[j] = src_bits[j];
}
src_bits += bytespp;
dst_bits += dst_pitch;
}
}
}
}
}
return dst;
}
/**
Rotates an image by a given degree in range [-45 .. +45] (counter clockwise)
using the 3-shear technique.
@param src Pointer to source image to rotate
@param dAngle Rotation angle
@return Returns a pointer to a newly allocated rotated image if successful, returns NULL otherwise
*/
static FIBITMAP*
Rotate45(FIBITMAP *src, double dAngle) {
int u;
int bpp = FreeImage_GetBPP(src);
double dRadAngle = dAngle * ROTATE_PI / double(180); // Angle in radians
double dSinE = sin(dRadAngle);
double dTan = tan(dRadAngle / 2);
int src_width = FreeImage_GetWidth(src);
int src_height = FreeImage_GetHeight(src);
// Calc first shear (horizontal) destination image dimensions
int width_1 = src_width + int((double)src_height * fabs(dTan) + 0.5);
int height_1 = src_height;
/******* Perform 1st shear (horizontal) ******/
// Allocate image for 1st shear
FIBITMAP *dst1 = FreeImage_Allocate(width_1, height_1, bpp);
if(NULL == dst1) {
return NULL;
}
for(u = 0; u < height_1; u++) {
double dShear;
if(dTan >= 0) {
// Positive angle
dShear = (double(u) + 0.5) * dTan;
}
else {
// Negative angle
dShear = (double(int(u) - height_1) + 0.5) * dTan;
}
int iShear = int(floor(dShear));
HorizontalSkew(src, dst1, u, iShear, BYTE(255 * (dShear - double(iShear)) + 1));
}
/******* Perform 2nd shear (vertical) ******/
// Calc 2nd shear (vertical) destination image dimensions
int width_2 = width_1;
int height_2 = int((double)src_width * fabs(dSinE) + (double)src_height * cos(dRadAngle) + 0.5) + 1;
// Allocate image for 2nd shear
FIBITMAP *dst2 = FreeImage_Allocate(width_2, height_2, bpp);
if(NULL == dst2) {
FreeImage_Unload(dst1);
return NULL;
}
double dOffset; // Variable skew offset
if(dSinE > 0) {
// Positive angle
dOffset = double(src_width - 1) * dSinE;
}
else {
// Negative angle
dOffset = -dSinE * double (src_width - width_2);
}
for(u = 0; u < width_2; u++, dOffset -= dSinE) {
int iShear = int(floor(dOffset));
VerticalSkew(dst1, dst2, u, iShear, BYTE(255 * (dOffset - double(iShear)) + 1));
}
/******* Perform 3rd shear (horizontal) ******/
// Free result of 1st shear
FreeImage_Unload(dst1);
// Calc 3rd shear (horizontal) destination image dimensions
int width_3 = int(double(src_height) * fabs(dSinE) + double(src_width) * cos(dRadAngle) + 0.5) + 1;
int height_3 = height_2;
// Allocate image for 3rd shear
FIBITMAP *dst3 = FreeImage_Allocate(width_3, height_3, bpp);
if(NULL == dst3) {
FreeImage_Unload(dst2);
return NULL;
}
if(dSinE >= 0) {
// Positive angle
dOffset = double(src_width - 1) * dSinE * -dTan;
}
else {
// Negative angle
dOffset = dTan * (double(src_width - 1) * -dSinE + double(1 - height_3));
}
for(u = 0; u < height_3; u++, dOffset += dTan) {
int iShear = int(floor(dOffset));
HorizontalSkew(dst2, dst3, u, iShear, BYTE(255 * (dOffset - double (iShear)) + 1));
}
// Free result of 2nd shear
FreeImage_Unload(dst2);
// Return result of 3rd shear
return dst3;
}
/**
Rotates a 1-, 8-, 24- or 32-bit image by a given angle (given in degree).
Angle is unlimited, except for 1-bit images (limited to integer multiples of 90 degree).
3-shears technique is used.
@param src Pointer to source image to rotate
@param dAngle Rotation angle
@return Returns a pointer to a newly allocated rotated image if successful, returns NULL otherwise
*/
static FIBITMAP*
RotateAny(FIBITMAP *src, double dAngle) {
if(NULL == src) {
return NULL;
}
FIBITMAP *image = src;
while(dAngle >= 360) {
// Bring angle to range of (-INF .. 360)
dAngle -= 360;
}
while(dAngle < 0) {
// Bring angle to range of [0 .. 360)
dAngle += 360;
}
if((dAngle > 45) && (dAngle <= 135)) {
// Angle in (45 .. 135]
// Rotate image by 90 degrees into temporary image,
// so it requires only an extra rotation angle
// of -45 .. +45 to complete rotation.
image = Rotate90(src);
dAngle -= 90;
}
else if((dAngle > 135) && (dAngle <= 225)) {
// Angle in (135 .. 225]
// Rotate image by 180 degrees into temporary image,
// so it requires only an extra rotation angle
// of -45 .. +45 to complete rotation.
image = Rotate180(src);
dAngle -= 180;
}
else if((dAngle > 225) && (dAngle <= 315)) {
// Angle in (225 .. 315]
// Rotate image by 270 degrees into temporary image,
// so it requires only an extra rotation angle
// of -45 .. +45 to complete rotation.
image = Rotate270(src);
dAngle -= 270;
}
// If we got here, angle is in (-45 .. +45]
if(NULL == image) {
// Failed to allocate middle image
return NULL;
}
if(0 == dAngle) {
if(image == src) {
// Nothing to do ...
return FreeImage_Clone(src);
} else {
// No more rotation needed
return image;
}
}
else {
// Perform last rotation
FIBITMAP *dst = Rotate45(image, dAngle);
if(src != image) {
// Middle image was required, free it now.
FreeImage_Unload(image);
}
return dst;
}
}
// ==========================================================
FIBITMAP *DLL_CALLCONV
FreeImage_RotateClassic(FIBITMAP *dib, double angle) {
if(!dib) return NULL;
if(0 == angle) {
return FreeImage_Clone(dib);
}
// DIB are stored upside down ...
angle *= -1;
try {
int bpp = FreeImage_GetBPP(dib);
if(bpp == 1) {
// only rotate for integer multiples of 90 degree
if(fmod(angle, 90) != 0)
return NULL;
// perform the rotation
FIBITMAP *dst = RotateAny(dib, angle);
if(!dst) throw(1);
// build a greyscale palette
RGBQUAD *dst_pal = FreeImage_GetPalette(dst);
if(FreeImage_GetColorType(dib) == FIC_MINISBLACK) {
dst_pal[0].rgbRed = dst_pal[0].rgbGreen = dst_pal[0].rgbBlue = 0;
dst_pal[1].rgbRed = dst_pal[1].rgbGreen = dst_pal[1].rgbBlue = 255;
} else {
dst_pal[0].rgbRed = dst_pal[0].rgbGreen = dst_pal[0].rgbBlue = 255;
dst_pal[1].rgbRed = dst_pal[1].rgbGreen = dst_pal[1].rgbBlue = 0;
}
return dst;
}
if((bpp == 8) || (bpp == 24) || (bpp == 32)) {
FIBITMAP *dst = RotateAny(dib, angle);
if(!dst) throw(1);
if(bpp == 8) {
// copy original palette to rotated bitmap
RGBQUAD *src_pal = FreeImage_GetPalette(dib);
RGBQUAD *dst_pal = FreeImage_GetPalette(dst);
memcpy(&dst_pal[0], &src_pal[0], 256 * sizeof(RGBQUAD));
}
return dst;
}
} catch(int) {
return NULL;
}
return NULL;
}
|