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
|
// ==========================================================
// Flipping routines
//
// Design and implementation by
// - Floris van den Berg (flvdberg@wxs.nl)
// - Herv Drolon (drolon@infonie.fr)
// - Jim Keir (jimkeir@users.sourceforge.net)
//
// 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!
// ==========================================================
#include "FreeImage.h"
#include "Utilities.h"
/**
Flip the image horizontally along the vertical axis.
@param src Input image to be processed.
@return Returns TRUE if successful, FALSE otherwise.
*/
BOOL DLL_CALLCONV
FreeImage_FlipHorizontal(FIBITMAP *src) {
if (!src) return FALSE;
unsigned line = FreeImage_GetLine(src);
unsigned height = FreeImage_GetHeight(src);
// copy between aligned memories
BYTE *new_bits = (BYTE*)FreeImage_Aligned_Malloc(line * sizeof(BYTE), FIBITMAP_ALIGNMENT);
if (!new_bits) return FALSE;
// mirror the buffer
for (unsigned y = 0; y < height; y++) {
BYTE *bits = FreeImage_GetScanLine(src, y);
memcpy(new_bits, bits, line);
switch (FreeImage_GetBPP(src)) {
case 1 :
{
unsigned width = FreeImage_GetWidth(src);
for(unsigned x = 0; x < width; x++) {
// get pixel at (x, y)
BOOL value = (new_bits[x >> 3] & (0x80 >> (x & 0x07))) != 0;
// set pixel at (new_x, y)
unsigned new_x = width - 1 - x;
value ? bits[new_x >> 3] |= (0x80 >> (new_x & 0x7)) : bits[new_x >> 3] &= (0xff7f >> (new_x & 0x7));
}
break;
}
case 4 :
{
for(unsigned c = 0; c < line; c++) {
bits[c] = new_bits[line - c - 1];
BYTE nibble = (bits[c] & 0xF0) >> 4;
bits[c] = bits[c] << 4;
bits[c] |= nibble;
}
break;
}
case 8:
case 16:
case 24 :
case 32 :
case 48:
case 64:
case 96:
case 128:
{
int bytespp = FreeImage_GetLine(src) / FreeImage_GetWidth(src);
for(unsigned c = 0; c < line; c += bytespp) {
memcpy(bits + c, new_bits + line - c - bytespp, bytespp);
}
break;
}
}
}
FreeImage_Aligned_Free(new_bits);
return TRUE;
}
/**
Flip the image vertically along the horizontal axis.
@param src Input image to be processed.
@return Returns TRUE if successful, FALSE otherwise.
*/
BOOL DLL_CALLCONV
FreeImage_FlipVertical(FIBITMAP *src) {
BYTE *From, *Mid;
if (!src) return FALSE;
// swap the buffer
unsigned pitch = FreeImage_GetPitch(src);
unsigned height = FreeImage_GetHeight(src);
// copy between aligned memories
Mid = (BYTE*)FreeImage_Aligned_Malloc(pitch * sizeof(BYTE), FIBITMAP_ALIGNMENT);
if (!Mid) return FALSE;
From = FreeImage_GetBits(src);
unsigned line_s = 0;
unsigned line_t = (height-1) * pitch;
for(unsigned y = 0; y < height/2; y++) {
memcpy(Mid, From + line_s, pitch);
memcpy(From + line_s, From + line_t, pitch);
memcpy(From + line_t, Mid, pitch);
line_s += pitch;
line_t -= pitch;
}
FreeImage_Aligned_Free(Mid);
return TRUE;
}
|