File size: 6,355 Bytes
9913017 | 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 | ///////////////////////////////////////////////////////////////////////////
//
// NAME
// Convert.cpp -- convert between image types, copy images, select bands
//
// DESCRIPTION
// See Convert.h for a full description
//
// SEE ALSO
// Convert.h full description
//
// Copyright � Richard Szeliski, 2001.
// See Copyright.h for more details
//
///////////////////////////////////////////////////////////////////////////
#include "Image.h"
#include "Error.h"
#include "Convert.h"
//
// Type conversion utilities
//
template <class T1, class T2>
extern void ScaleAndOffsetLine(T1* src, T2* dst, int n,
float scale, float offset,
T2 minVal, T2 maxVal)
{
// This routine does NOT round values when converting from float to int
const bool scaleOffset = (scale != 1.0f) || (offset != 0.0f);
const bool clip = (minVal < maxVal);
if (scaleOffset)
for (int i = 0; i < n; i++)
{
float val = src[i] * scale + offset;
if (clip)
val = __min(__max(val, minVal), maxVal);
dst[i] = (T2) val;
}
else if (clip)
for (int i = 0; i < n; i++)
{
dst[i] = (T2) __min(__max(src[i], minVal), maxVal);
}
else if (typeid(T1) == typeid(T2))
memcpy(dst, src, n*sizeof(T2));
else
for (int i = 0; i < n; i++)
{
dst[i] = (T2) src[i];
}
}
template <class T1, class T2>
extern void ScaleAndOffset(CImageOf<T1>& src, CImageOf<T2>& dst, float scale, float offset)
{
// Convert between images of same shape but diffent types,
// and optionally scale and/or offset the pixel values
CShape sShape = src.Shape();
CShape dShape = dst.Shape();
// Make sure the shapes (ignoring bands) are compatible
if (sShape != dShape)
dst.ReAllocate(sShape);
// Determine if clipping is required
T2 minVal = dst.MinVal();
T2 maxVal = dst.MaxVal();
// if (minVal <= src.MinVal() && maxVal >= src.MaxVal())
// changed DS 4/2003 - take into consideration scale and offset
// removed DS 1/2004 - doesn't work for negative scales - safer to always clip
//if (src.MinVal() * scale + offset >= minVal &&
// src.MaxVal() * scale + offset <= maxVal)
// minVal = maxVal = 0;
// Process each row
for (int y = 0; y < sShape.height; y++)
{
int n = sShape.width * sShape.nBands;
ScaleAndOffsetLine(&src.Pixel(0, y, 0), &dst.Pixel(0, y, 0),
n, scale, offset, minVal, maxVal);
}
}
template <class T>
extern CImageOf<T> ConvertToRGBA(CImageOf<T> src)
{
// Check if already RGBA
CShape sShape = src.Shape();
if (sShape.nBands == 4 && src.alphaChannel == 3)
return src;
// Make sure the source is a gray image
if (sShape.nBands != 1)
throw CError("ConvertToRGBA: can only convert from 1-band (gray) image");
// Allocate the new image
CShape dShape(sShape.width, sShape.height, 4);
CImageOf<T> dst(dShape);
// Process each row
int aC = dst.alphaChannel;
for (int y = 0; y < sShape.height; y++)
{
T* srcP = &src.Pixel(0, y, 0);
T* dstP = &dst.Pixel(0, y, 0);
for (int x = 0; x < sShape.width; x++, srcP++)
for (int b = 0; b < dShape.nBands; b++, dstP++)
*dstP = (b == aC) ? 255 : *srcP;
}
return dst;
}
template <class T>
extern CImageOf<T> ConvertToGray(CImageOf<T> src)
{
// Check if already gray
CShape sShape = src.Shape();
if (sShape.nBands == 1)
return src;
// Make sure the source is a color image
if (sShape.nBands != 4 || src.alphaChannel != 3)
throw CError("ConvertToGray: can only convert from 4-band (RGBA) image");
// Allocate the new image
CShape dShape(sShape.width, sShape.height, 1);
CImageOf<T> dst(dShape);
// Process each row
T minVal = dst.MinVal();
T maxVal = dst.MaxVal();
for (int y = 0; y < sShape.height; y++)
{
T* srcP = &src.Pixel(0, y, 0);
T* dstP = &dst.Pixel(0, y, 0);
for (int x = 0; x < sShape.width; x++, srcP += 4, dstP++)
{
RGBA<T>& p = *(RGBA<T> *) srcP;
// OLD FORMULA: float Y = (float)(0.212671 * p.R + 0.715160 * p.G + 0.072169 * p.B);
// Changed to commonly used formula 6/4/07 DS
float Y = (float)(0.299 * p.R + 0.587 * p.G + 0.114 * p.B);
*dstP = (T) __min(maxVal, __max(minVal, Y));
}
}
return dst;
}
template <class T>
extern void BandSelect(CImageOf<T>& src, CImageOf<T>& dst, int sBand, int dBand)
{
// Convert between images of same type but different # of bands
CShape sShape = src.Shape();
CShape dShape = dst.Shape();
// Make sure the shapes (ignoring bands) are compatible
if (! sShape.SameIgnoringNBands(dShape) || dShape.nBands == 0)
{
dShape.width = sShape.width;
dShape.height = sShape.height;
dShape.nBands = (dShape.nBands) ? dShape.nBands : 1;
dst.ReAllocate(dShape);
}
// Check the bands are valid
int sB = sShape.nBands;
int dB = dShape.nBands;
if (sBand < 0 || sBand >= sB)
throw CError("BandSelect: source band %d is invalid", sBand);
if (dBand < 0 || dBand >= dB)
throw CError("BandSelect: destination band %d is invalid", dBand);
// Process each row
for (int y = 0; y < sShape.height; y++)
{
T* srcP = &src.Pixel(0, y, 0);
T* dstP = &dst.Pixel(0, y, 0);
for (int x = 0; x < sShape.width; x++, srcP += sB, dstP += dB)
dstP[dBand] = srcP[sBand];
}
}
//
// Force instantiation for the types we care about (uchar, int, float)
//
template <class T1>
void CopyPixelsInstantiate(CImageOf<T1> s1)
{
CByteImage b2;
CIntImage i2;
CFloatImage f2;
CopyPixels(s1, b2);
CopyPixels(s1, i2);
CopyPixels(s1, f2);
}
template <class T>
int InstantiateConvert(CImageOf<T> src)
{
CopyPixelsInstantiate(src);
CImageOf<T> r1 = ConvertToRGBA(src);
CImageOf<T> r2 = ConvertToGray(src);
BandSelect(r1, r2, 0, 0);
return (int)r1.Pixel(0, 0, 0);
}
void InstantiateAllConverts(void)
{
InstantiateConvert(CByteImage());
InstantiateConvert(CIntImage());
InstantiateConvert(CFloatImage());
}
|