merge internal branch:

- some support for floating point images.
- Charles Bloom extrapolation filter.
- misc fixes.
This commit is contained in:
castano
2007-12-03 09:14:19 +00:00
parent 2e41727f81
commit 4373aa758b
16 changed files with 3175 additions and 2959 deletions

View File

@ -44,6 +44,11 @@ IF(TIFF_FOUND)
INCLUDE_DIRECTORIES(${TIFF_INCLUDE_DIR})
ENDIF(TIFF_FOUND)
IF(OPENEXR_FOUND)
SET(LIBS ${LIBS} ${OPENEXR_LIBRARIES})
INCLUDE_DIRECTORIES(${OPENEXR_INCLUDE_PATHS})
ENDIF(OPENEXR_FOUND)
# targets
ADD_DEFINITIONS(-DNVIMAGE_EXPORTS)

View File

@ -15,15 +15,29 @@ using namespace nv;
namespace
{
static int round(float f)
static int iround(float f)
{
return int(f);
}
static int ifloor(float f)
{
return int(floor(f));
}
static float frac(float f)
{
return f - floor(f);
}
static int mirror(int x, int w)
{
x = fabs(x);
while (x >= w) {
x = 2 * w - x - 2;
}
return x;
}
}
@ -161,7 +175,7 @@ void FloatImage::normalize(uint base_component)
for(uint i = 0; i < size; i++) {
Vector3 normal(xChannel[i], yChannel[i], zChannel[i]);
normal = normalizeSafe(normal, Vector3(zero));
normal = normalizeSafe(normal, Vector3(zero), 0.0f);
xChannel[i] = normal.x();
yChannel[i] = normal.y();
@ -226,72 +240,42 @@ void FloatImage::exponentiate(uint base_component, uint num, float power)
}
}
#if 0
float FloatImage::nearest(float x, float y, int c, WrapMode wm) const
float FloatImage::sampleNearest(const float x, const float y, const int c, const WrapMode wm) const
{
if( wm == WrapMode_Clamp ) return nearest_clamp(x, y, c);
/*if( wm == WrapMode_Repeat )*/ return nearest_repeat(x, y, c);
//if( wm == WrapMode_Mirror ) return nearest_mirror(x, y, c);
if( wm == WrapMode_Clamp ) return sampleNearestClamp(x, y, c);
else if( wm == WrapMode_Repeat ) return sampleNearestRepeat(x, y, c);
else /*if( wm == WrapMode_Mirror )*/ return sampleNearestMirror(x, y, c);
}
float FloatImage::nearest_clamp(int x, int y, const int c) const
float FloatImage::sampleLinear(const float x, const float y, const int c, const WrapMode wm) const
{
const int w = m_width;
const int h = m_height;
int ix = ::clamp(x, 0, w-1);
int iy = ::clamp(y, 0, h-1);
if( wm == WrapMode_Clamp ) return sampleLinearClamp(x, y, c);
else if( wm == WrapMode_Repeat ) return sampleLinearRepeat(x, y, c);
else /*if( wm == WrapMode_Mirror )*/ return sampleLinearMirror(x, y, c);
}
float FloatImage::sampleNearestClamp(const float x, const float y, const int c) const
{
int ix = ::clamp(iround(x * m_width), 0, m_width-1);
int iy = ::clamp(iround(y * m_height), 0, m_height-1);
return pixel(ix, iy, c);
}
float FloatImage::nearest_repeat(int x, int y, const int c) const
float FloatImage::sampleNearestRepeat(const float x, const float y, const int c) const
{
const int w = m_width;
const int h = m_height;
int ix = x % w;
int iy = y % h;
return pixel(ix, iy, c);
}
#endif
float FloatImage::nearest(float x, float y, int c, WrapMode wm) const
{
if( wm == WrapMode_Clamp ) return nearest_clamp(x, y, c);
/*if( wm == WrapMode_Repeat )*/ return nearest_repeat(x, y, c);
//if( wm == WrapMode_Mirror ) return nearest_mirror(x, y, c);
}
float FloatImage::linear(float x, float y, int c, WrapMode wm) const
{
if( wm == WrapMode_Clamp ) return linear_clamp(x, y, c);
/*if( wm == WrapMode_Repeat )*/ return linear_repeat(x, y, c);
//if( wm == WrapMode_Mirror ) return linear_mirror(x, y, c);
}
float FloatImage::nearest_clamp(float x, float y, const int c) const
{
const int w = m_width;
const int h = m_height;
int ix = ::clamp(round(x * w), 0, w-1);
int iy = ::clamp(round(y * h), 0, h-1);
int ix = iround(frac(x) * m_width);
int iy = iround(frac(y) * m_height);
return pixel(ix, iy, c);
}
float FloatImage::nearest_repeat(float x, float y, const int c) const
float FloatImage::sampleNearestMirror(const float x, const float y, const int c) const
{
const int w = m_width;
const int h = m_height;
int ix = round(frac(x) * w);
int iy = round(frac(y) * h);
int ix = mirror(iround(x * m_width), m_width);
int iy = mirror(iround(y * m_height), m_height);
return pixel(ix, iy, c);
}
float FloatImage::nearest_mirror(float x, float y, const int c) const
{
// @@ TBD
return 0.0f;
}
float FloatImage::linear_clamp(float x, float y, const int c) const
float FloatImage::sampleLinearClamp(float x, float y, const int c) const
{
const int w = m_width;
const int h = m_height;
@ -302,10 +286,10 @@ float FloatImage::linear_clamp(float x, float y, const int c) const
const float fracX = frac(x);
const float fracY = frac(y);
const int ix0 = ::clamp(round(x), 0, w-1);
const int iy0 = ::clamp(round(y), 0, h-1);
const int ix1 = ::clamp(round(x)+1, 0, w-1);
const int iy1 = ::clamp(round(y)+1, 0, h-1);
const int ix0 = ::clamp(ifloor(x), 0, w-1);
const int iy0 = ::clamp(ifloor(y), 0, h-1);
const int ix1 = ::clamp(ifloor(x)+1, 0, w-1);
const int iy1 = ::clamp(ifloor(y)+1, 0, h-1);
float f1 = pixel(ix0, iy0, c);
float f2 = pixel(ix1, iy0, c);
@ -318,7 +302,7 @@ float FloatImage::linear_clamp(float x, float y, const int c) const
return lerp(i1, i2, fracY);
}
float FloatImage::linear_repeat(float x, float y, int c) const
float FloatImage::sampleLinearRepeat(float x, float y, int c) const
{
const int w = m_width;
const int h = m_height;
@ -326,10 +310,10 @@ float FloatImage::linear_repeat(float x, float y, int c) const
const float fracX = frac(x * w);
const float fracY = frac(y * h);
int ix0 = round(frac(x) * w);
int iy0 = round(frac(y) * h);
int ix1 = round(frac(x + 1.0f/w) * w);
int iy1 = round(frac(y + 1.0f/h) * h);
int ix0 = ifloor(frac(x) * w);
int iy0 = ifloor(frac(y) * h);
int ix1 = ifloor(frac(x + 1.0f/w) * w);
int iy1 = ifloor(frac(y + 1.0f/h) * h);
float f1 = pixel(ix0, iy0, c);
float f2 = pixel(ix1, iy0, c);
@ -342,10 +326,31 @@ float FloatImage::linear_repeat(float x, float y, int c) const
return lerp(i1, i2, fracY);
}
float FloatImage::linear_mirror(float x, float y, int c) const
float FloatImage::sampleLinearMirror(float x, float y, int c) const
{
// @@ TBD
return 0.0f;
const int w = m_width;
const int h = m_height;
x *= w;
y *= h;
const float fracX = frac(x);
const float fracY = frac(y);
int ix0 = mirror(x, w);
int iy0 = mirror(y, h);
int ix1 = mirror(x + 1, w);
int iy1 = mirror(y + 1, h);
float f1 = pixel(ix0, iy0, c);
float f2 = pixel(ix1, iy0, c);
float f3 = pixel(ix0, iy1, c);
float f4 = pixel(ix1, iy1, c);
float i1 = lerp(f1, f2, fracX);
float i2 = lerp(f3, f4, fracX);
return lerp(i1, i2, fracY);
}

View File

@ -97,18 +97,16 @@ public:
void setPixel(float f, uint idx);
float pixel(uint idx) const;
float nearest(int x, int y, int c, WrapMode wm) const;
float sampleNearest(float x, float y, int c, WrapMode wm) const;
float sampleLinear(float x, float y, int c, WrapMode wm) const;
float nearest(float x, float y, int c, WrapMode wm) const;
float linear(float x, float y, int c, WrapMode wm) const;
float sampleNearestClamp(float x, float y, int c) const;
float sampleNearestRepeat(float x, float y, int c) const;
float sampleNearestMirror(float x, float y, int c) const;
float nearest_clamp(float x, float y, int c) const;
float nearest_repeat(float x, float y, int c) const;
float nearest_mirror(float x, float y, int c) const;
float linear_clamp(float x, float y, int c) const;
float linear_repeat(float x, float y, int c) const;
float linear_mirror(float x, float y, int c) const;
float sampleLinearClamp(float x, float y, int c) const;
float sampleLinearRepeat(float x, float y, int c) const;
float sampleLinearMirror(float x, float y, int c) const;
//@}
public:

View File

@ -1,6 +1,7 @@
// This code is in the public domain -- castanyo@yahoo.es
#include <nvcore/Containers.h>
#include <nvcore/Ptr.h>
#include <nvmath/nvmath.h>
@ -11,7 +12,7 @@ using namespace nv;
// This is a variation of Sapiro's inpainting method.
void nv::fillExtrapolateOnce(FloatImage * img, BitMap * bmap)
void nv::fillExtrapolate(int passCount, FloatImage * img, BitMap * bmap)
{
nvCheck(img != NULL);
nvCheck(bmap != NULL);
@ -23,10 +24,12 @@ void nv::fillExtrapolateOnce(FloatImage * img, BitMap * bmap)
nvCheck(bmap->width() == uint(w));
nvCheck(bmap->height() == uint(h));
BitMap * newbmap = new BitMap(w, h);
for(int c = 0; c < count; c++) {
AutoPtr<BitMap> newbmap(new BitMap(w, h));
for(int p = 0; p < passCount; p++)
{
for(int c = 0; c < count; c++)
{
float * channel = img->channel(c);
for(int y = 0; y < h; y++) {
@ -88,18 +91,6 @@ void nv::fillExtrapolateOnce(FloatImage * img, BitMap * bmap)
// Update the bit mask.
swap(*newbmap, *bmap);
}
void nv::fillExtrapolateNTimes(FloatImage * img, BitMap * bmap, int n)
{
nvCheck(img != NULL);
nvCheck(bmap != NULL);
nvCheck(n > 0);
for(int i = 0; i < n; i++)
{
fillExtrapolateOnce(img, bmap);
}
}
@ -134,7 +125,7 @@ namespace {
} // namespace
// Voronoi filling using EDT-4
void nv::fillVoronoi(FloatImage * img, const BitMap & bmap)
void nv::fillVoronoi(FloatImage * img, const BitMap * bmap)
{
nvCheck(img != NULL);
@ -142,8 +133,8 @@ void nv::fillVoronoi(FloatImage * img, const BitMap & bmap)
const int h = img->height();
const int count = img->componentNum();
nvCheck(bmap.width() == uint(w));
nvCheck(bmap.height() == uint(h));
nvCheck(bmap->width() == uint(w));
nvCheck(bmap->height() == uint(h));
Array<Neighbor> edm;
edm.resize(w * h);
@ -154,7 +145,7 @@ void nv::fillVoronoi(FloatImage * img, const BitMap & bmap)
// Init edm.
for( y = 0; y < h; y++ ) {
for( x = 0; x < w; x++ ) {
if( bmap.bitAt(x, y) ) {
if( bmap->bitAt(x, y) ) {
edm[y * w + x].x = x;
edm[y * w + x].y = y;
edm[y * w + x].d = 0;
@ -229,7 +220,7 @@ void nv::fillVoronoi(FloatImage * img, const BitMap & bmap)
}
void nv::fillBlur(FloatImage * img, const BitMap & bmap)
void nv::fillBlur(FloatImage * img, const BitMap * bmap)
{
nvCheck(img != NULL);
@ -306,7 +297,7 @@ static bool downsample(const FloatImage * src, const BitMap * srcMask, const Flo
}
// This is the filter used in the Lumigraph paper. The Unreal engine uses something similar.
void nv::fillPullPush(FloatImage * img, const BitMap & bmap)
void nv::fillPullPush(FloatImage * img, const BitMap * bmap)
{
nvCheck(img != NULL);
@ -320,7 +311,7 @@ void nv::fillPullPush(FloatImage * img, const BitMap & bmap)
Array<const BitMap *> mipmapMasks(num);
mipmaps.append(img);
mipmapMasks.append(&bmap);
mipmapMasks.append(bmap);
const FloatImage * current;
const BitMap * currentMask;
@ -336,16 +327,22 @@ void nv::fillPullPush(FloatImage * img, const BitMap & bmap)
for(uint y = 0; y < h; y++) {
for(uint x = 0; x < w; x++) {
uint sx = x;
uint sy = y;
int sx = x;
int sy = y;
//float sx = x;
//float sy = y;
const uint levelCount = mipmaps.count();
for(uint l = 0; l < levelCount; l++) {
for (uint l = 0; l < levelCount; l++)
{
//const float fx = sx / mipmaps[l]->width();
//const float fy = sy / mipmaps[l]->height();
if (mipmapMasks[l]->bitAt(sx, sy))
{
// Sample mipmaps[l](sx, sy) and copy to img(x, y)
for(uint c = 0; c < count; c++) {
//img->setPixel(mipmaps[l]->linear_clamp(fx, fy, c), x, y, c);
img->setPixel(mipmaps[l]->pixel(sx, sy, c), x, y, c);
}
break;
@ -357,20 +354,20 @@ void nv::fillPullPush(FloatImage * img, const BitMap & bmap)
}
}
// Don't delete the original image and mask.
mipmaps[0] = NULL;
mipmapMasks[0] = NULL;
// Delete the mipmaps.
deleteAll(mipmaps);
deleteAll(mipmapMasks);
}
/*
void nv::fillSeamFix(FloatImage * img, const BitMap & bmap)
{
}
*/
#if 0 // Code below is under the BPL license.
/**
This Code is from Charles Bloom:
DoPixelSeamFix
10-20-02
@ -386,7 +383,7 @@ Note that I'm working on normals, but I treat them just as 3 scalars and normali
at the end. To be more correct, I would work on the surface of a sphere, but that
just seems like way too much work.
**/
*/
struct LocalPixels
{
@ -395,11 +392,11 @@ struct LocalPixels
// index [y][x]
bool fill[5][5];
float data[5][5];
mutable float result;
mutable float weight;
bool Quad3SubH(gVec4 * pQ,int row) const
bool Quad3SubH(float * pQ, int row) const
{
const bool * pFill = fill[row];
const float * pDat = data[row];
@ -426,7 +423,7 @@ struct LocalPixels
}
// improve result with a horizontal quad in row 1 and/or
bool Quad3SubV(gVec4 * pQ,int col) const
bool Quad3SubV(float * pQ, int col) const
{
if ( fill[1][col] && fill[2][col] && fill[3][col] )
{
@ -449,13 +446,13 @@ struct LocalPixels
return false;
}
bool Quad3H(gVec4 * pQ) const
bool Quad3H(float * pQ) const
{
if (!Quad3SubH(pQ,1))
{
return Quad3SubH(pQ,3);
}
gVec4 q(0,0,0,0); // initializer not needed, just make it shut up
float q = 0.0f; // initializer not needed, just make it shut up
if (Quad3SubH(&q, 3))
{
// got q and pQ
@ -464,13 +461,13 @@ struct LocalPixels
return true;
}
bool Quad3V(gVec4 * pQ) const
bool Quad3V(float * pQ) const
{
if (!Quad3SubV(pQ, 1))
{
return Quad3SubV(pQ, 3);
}
gVec4 q(0,0,0,0); // initializer not needed, just make it shut up
float q = 0.0f; // initializer not needed, just make it shut up
if (Quad3SubV(&q, 3))
{
// got q and pQ
@ -482,7 +479,7 @@ struct LocalPixels
// a common want is [1] - ([0]+[2])*0.5f ;
// so use -0.5f*Quad
bool TryQuads() const
bool tryQuads() const
{
bool res = false;
@ -490,7 +487,7 @@ struct LocalPixels
if ( fill[2][1] && fill[2][3] )
{
// got horizontal straddle
gVec4 q;
float q;
if ( Quad3H(&q) )
{
result += (data[2][1] + data[2][3] - q) * 0.5f;
@ -501,7 +498,7 @@ struct LocalPixels
if ( fill[1][2] && fill[3][2] )
{
// got vertical straddle
gVec4 q;
float q;
if ( Quad3V(&q) )
{
result += (data[1][2] + data[3][2] - q) * 0.5f;
@ -514,7 +511,7 @@ struct LocalPixels
if ( fill[2][0] && fill[2][1] )
{
// got left-side pair
gVec4 q;
float q;
if ( Quad3H(&q) )
{
result += data[2][1]*2.f - data[2][0] + q;
@ -525,7 +522,7 @@ struct LocalPixels
if ( fill[2][3] && fill[2][4] )
{
// got right-side pair
gVec4 q;
float q;
if ( Quad3H(&q) )
{
result += data[2][3]*2.f - data[2][4] + q;
@ -536,7 +533,7 @@ struct LocalPixels
if ( fill[0][2] && fill[1][2] )
{
// got left-side pair
gVec4 q;
float q;
if ( Quad3V(&q) )
{
result += data[1][2]*2.f - data[0][2] + q;
@ -547,7 +544,7 @@ struct LocalPixels
if ( fill[3][2] && fill[4][2] )
{
// got right-side pair
gVec4 q;
float q;
if ( Quad3V(&q) )
{
result += data[3][2]*2.f - data[4][2] + q;
@ -558,7 +555,7 @@ struct LocalPixels
return res;
}
bool TryPlanar() const
bool tryPlanar() const
{
// four cases :
const int indices[] =
@ -580,26 +577,26 @@ struct LocalPixels
continue;
result += data[ I[0] ][ I[1] ] + data[ I[2] ][ I[3] ] - data[ I[4] ][ I[5] ];
weight += 1.f;
weight += 1.0f;
res = true;
}
return res;
}
bool TryTwos() const
bool tryTwos() const
{
bool res = false;
if (fill[2][1] && fill[2][3])
{
result += (data[2][1] + data[2][3]) * 0.5f;
weight += 1.f;
weight += 1.0f;
res = true;
}
if (fill[1][2] && fill[3][2])
{
result += (data[1][2] + data[3][2]) * 0.5f;
weight += 1.f;
weight += 1.0f;
res = true;
}
@ -619,64 +616,83 @@ struct LocalPixels
if (!fill[ I[2] ][ I[3] ])
continue;
result += data[ I[0] ][ I[1] ]*2.f - data[ I[2] ][ I[3] ];
weight += 1.f;
result += data[ I[0] ][ I[1] ]*2.0f - data[ I[2] ][ I[3] ];
weight += 1.0f;
res = true;
}
return res;
}
bool DoLocalPixelFill() const
bool doLocalPixelFill() const
{
result = gVec4::zero;
weight = 0.f;
result = 0.0f;
weight = 0.0f;
if ( TryQuads() )
if (tryQuads()) {
return true;
if ( TryPlanar() )
return true;
return TryTwos();
}
}; // LocalPixels -----------------------------------------------
void gNormalMap::DoPixelSeamFix()
{
gLog::Printf("gNormalMap::DoPixelSeamFix..");
const int desiredTicks = 30;
const int heightPerTick = NUM_SEAMFIX_PASSES * m_height / desiredTicks;
int tick = 0;
for(int pass=0;pass<NUM_SEAMFIX_PASSES;pass++)
{
for(int yb=0;yb<m_height;yb++)
{
gVec4 * pRow = m_normals + m_width * yb;
const EState * pStateRow = m_states + m_width * yb;
for(int xb=0;xb<m_width;xb++)
{
if ( pStateRow[xb] != eNull && pStateRow[xb] != eEdge )
{
ASSERT( ! IsNull(pRow[xb]) );
continue; // it's got a pixel
if (tryPlanar()) {
return true;
}
return tryTwos();
}
}; // struct LocalPixels
// This is a cubic extrapolation filter from Charles Bloom (DoPixelSeamFix).
void nv::fillCubicExtrapolate(int passCount, FloatImage * img, BitMap * bmap, int coverageIndex /*= -1*/)
{
nvCheck(passCount > 0);
nvCheck(img != NULL);
nvCheck(bmap != NULL);
const int w = img->width();
const int h = img->height();
const int count = img->componentNum();
nvCheck(bmap->width() == uint(w));
nvCheck(bmap->height() == uint(h));
AutoPtr<BitMap> newbmap( new BitMap(w, h) );
float * coverageChannel = NULL;
if (coverageIndex != -1)
{
coverageChannel = img->channel(coverageIndex);
}
int firstChannel = -1;
for (int p = 0; p < passCount; p++)
{
for (int c = 0; c < count; c++)
{
if (c == coverageIndex) continue;
if (firstChannel == -1) firstChannel = c;
float * channel = img->channel(c);
for (int yb = 0; yb < h; yb++) {
for (int xb = 0; xb < w; xb++) {
if (bmap->bitAt(xb, yb)) {
// Not a hole.
newbmap->setBitAt(xb, yb);
continue;
}
// can be non-null, if it wasn't actually inside any tri,
// but got the anti-aliased edge effect of a tri
// replace edge pixels with seam-fix here
//ASSERT( IsNull(pRow[xb]) );
// make the local neighborhood:
int numFill = 0;
LocalPixels lp;
for (int ny = 0; ny < 5; ny++)
{
int y = (yb + ny - 2);
if ( y < 0 || y >= m_height )
if ( y < 0 || y >= h )
{
// out of range
for(int i = 0; i < 5; i++)
@ -685,67 +701,53 @@ void gNormalMap::DoPixelSeamFix()
}
continue;
}
gVec4 * pRow = m_normals + m_width * y;
const EState * pStateRow = m_states + m_width * y;
for (int nx = 0; nx < 5; nx++)
{
int x = (xb + nx - 2);
if ( x < 0 || x >= m_width )
if (x < 0 || x >= w)
{
lp.fill[ny][nx] = false;
}
else if ( pStateRow[x] == eNull || pStateRow[x] == eEdge )
else
{
int idx = img->index(x, y);
if (!bmap->bitAt(idx))
{
lp.fill[ny][nx] = false;
}
else
{
lp.fill[ny][nx] = true;
lp.data[ny][nx] = pRow[x];
lp.data[ny][nx] = channel[idx];
numFill++;
}
}
}
}
// need at least 3 to do anything decent
if (numFill < 2)
continue;
ASSERT(lp.fill[2][2] == false);
if ( lp.DoLocalPixelFill() )
{
if ( lp.result.MutableVec3().NormalizeSafe() )
{
pRow[xb] = lp.result;
pRow[xb][3] /= lp.weight;
}
}
}
nvDebugCheck(lp.fill[2][2] == false);
if ( ++tick == heightPerTick )
if (lp.doLocalPixelFill())
{
tick = 0;
gLog::Printf(".");
}
}
const int idx = img->index(xb, yb);
channel[idx] = lp.result / lp.weight;
// now run back over and stamp anything that's not null as being ok
for(int y=0;y<m_height;y++)
if (c == firstChannel)
{
const gVec4 * pRow = m_normals + m_width * y;
EState * pStateRow = m_states + m_width * y;
for(int x=0;x<m_width;x++)
{
if ( ( pStateRow[x] == eNull || pStateRow[x] == eEdge ) && ! IsNull(pRow[x]) )
{
pStateRow[x] = eSeamFixed;
//coverageChannel[idx] /= lp.weight; // @@ Not sure what this was for, coverageChannel[idx] is always zero.
newbmap->setBitAt(xb, yb);
}
}
}
}
}
gLog::Printf("done\n");
// Update the bit mask.
swap(*newbmap, *bmap);
}
}
#endif // 0

View File

@ -84,12 +84,12 @@ namespace nv
};
NVIMAGE_API void fillVoronoi(FloatImage * img, const BitMap & bmap);
NVIMAGE_API void fillBlur(FloatImage * img, const BitMap & bmap);
NVIMAGE_API void fillPullPush(FloatImage * img, const BitMap & bmap);
NVIMAGE_API void fillVoronoi(FloatImage * img, const BitMap * bmap);
NVIMAGE_API void fillBlur(FloatImage * img, const BitMap * bmap);
NVIMAGE_API void fillPullPush(FloatImage * img, const BitMap * bmap);
NVIMAGE_API void fillExtrapolateOnce(FloatImage * img, BitMap * bmap);
NVIMAGE_API void fillExtrapolateNTimes(FloatImage * img, BitMap * bmap, int n);
NVIMAGE_API void fillExtrapolate(int passCount, FloatImage * img, BitMap * bmap);
NVIMAGE_API void fillCubicExtrapolate(int passCount, FloatImage * img, BitMap * bmap, int coverageIndex = -1);
} // nv namespace

View File

@ -4,6 +4,8 @@
#include <nvcore/Containers.h>
#include <nvcore/StrLib.h>
#include <nvcore/StdStream.h>
#include <nvcore/Tokenizer.h>
#include <nvcore/TextWriter.h>
#include <nvmath/Color.h>
@ -29,11 +31,13 @@ extern "C" {
# include <tiffio.h>
#endif
#if defined(HAVE_EXR)
# include <ImfRgbaFile.h>
# include <ImfInputFile.h> // ???
#if defined(HAVE_OPENEXR)
# include <ImfIO.h>
# include <ImathBox.h>
# include <ImfChannelList.h>
# include <ImfInputFile.h>
# include <ImfOutputFile.h>
# include <ImfArray.h>
using namespace Imf;
#endif
using namespace nv;
@ -55,23 +59,28 @@ namespace {
} // namespace
Image * nv::ImageIO::load(const char * name)
Image * nv::ImageIO::load(const char * fileName)
{
StdInputStream stream(name);
nvDebugCheck(fileName != NULL);
StdInputStream stream(fileName);
if (stream.isError()) {
return false;
return NULL;
}
return load(name, stream);
return ImageIO::load(fileName, stream);
}
Image * nv::ImageIO::load(const char * name, Stream & s)
Image * nv::ImageIO::load(const char * fileName, Stream & s)
{
const char * extension = Path::extension(name);
nvDebugCheck(fileName != NULL);
nvDebugCheck(s.isLoading());
const char * extension = Path::extension(fileName);
if (strCaseCmp(extension, ".tga") == 0) {
return loadTGA(s);
return ImageIO::loadTGA(s);
}
#if defined(HAVE_JPEG)
if (strCaseCmp(extension, ".jpg") == 0 || strCaseCmp(extension, ".jpeg") == 0) {
@ -90,36 +99,113 @@ Image * nv::ImageIO::load(const char * name, Stream & s)
return NULL;
}
NVIMAGE_API FloatImage * nv::ImageIO::loadFloat(const char * name)
bool nv::ImageIO::save(const char * fileName, Stream & s, Image * img)
{
StdInputStream stream(name);
nvDebugCheck(fileName != NULL);
nvDebugCheck(s.isSaving());
nvDebugCheck(img != NULL);
const char * extension = Path::extension(fileName);
if (strCaseCmp(extension, ".tga") == 0) {
return ImageIO::saveTGA(s, img);
}
return false;
}
bool nv::ImageIO::save(const char * fileName, Image * img)
{
nvDebugCheck(fileName != NULL);
nvDebugCheck(img != NULL);
StdOutputStream stream(fileName);
if (stream.isError())
{
return false;
}
return ImageIO::save(fileName, stream, img);
}
FloatImage * nv::ImageIO::loadFloat(const char * fileName)
{
nvDebugCheck(fileName != NULL);
StdInputStream stream(fileName);
if (stream.isError()) {
return false;
}
return loadFloat(name, stream);
return loadFloat(fileName, stream);
}
NVIMAGE_API FloatImage * nv::ImageIO::loadFloat(const char * name, Stream & s)
FloatImage * nv::ImageIO::loadFloat(const char * fileName, Stream & s)
{
const char * extension = Path::extension(name);
nvDebugCheck(fileName != NULL);
const char * extension = Path::extension(fileName);
#if defined(HAVE_TIFF)
if (strCaseCmp(extension, ".tif") == 0 || strCaseCmp(extension, ".tiff") == 0) {
return loadFloatTIFF(name, s);
return loadFloatTIFF(fileName, s);
}
#endif
#if defined(HAVE_EXR)
#if defined(HAVE_OPENEXR)
if (strCaseCmp(extension, ".exr") == 0) {
return loadFloatEXR(name, s);
return loadFloatEXR(fileName, s);
}
#endif
if (strCaseCmp(extension, ".pfm") == 0) {
return loadFloatPFM(fileName, s);
}
return NULL;
}
bool nv::ImageIO::saveFloat(const char * fileName, const FloatImage * fimage, uint base_component, uint num_components)
{
const char * extension = Path::extension(fileName);
#if defined(HAVE_OPENEXR)
if (strCaseCmp(extension, ".exr") == 0)
{
return ImageIO::saveFloatEXR(fileName, fimage, base_component, num_components);
}
#endif
#if defined(HAVE_TIFF)
if (strCaseCmp(extension, ".tif") == 0 || strCaseCmp(extension, ".tiff") == 0)
{
return ImageIO::saveFloatTIFF(fileName, fimage, base_component, num_components);
}
#endif
if (strCaseCmp(extension, ".pfm") == 0)
{
// return ImageIO::saveFloatPFM(fileName, fimage, base_component, num_components);
}
if (num_components == 3 || num_components == 4)
{
AutoPtr<Image> image(fimage->createImage(base_component, num_components));
nvCheck(image != NULL);
if (num_components == 4)
{
image->setFormat(Image::Format_ARGB);
}
return ImageIO::save(fileName, image.ptr());
}
return false;
}
/// Load TGA image.
Image * nv::ImageIO::loadTGA(Stream & s)
{
@ -912,7 +998,7 @@ bool nv::ImageIO::saveFloatTIFF(const char * fileName, const FloatImage * fimage
{
nvCheck(fileName != NULL);
nvCheck(fimage != NULL);
nvCheck(fimage->componentNum() <= base_component + num_components);
nvCheck(base_component + num_components <= fimage->componentNum());
const int iW = fimage->width();
const int iH = fimage->height();
@ -937,6 +1023,11 @@ bool nv::ImageIO::saveFloatTIFF(const char * fileName, const FloatImage * fimage
TIFFSetField(image, TIFFTAG_ROWSPERSTRIP, rowsperstrip);
TIFFSetField(image, TIFFTAG_COMPRESSION, COMPRESSION_PACKBITS);
if (num_components == 3)
{
// Set this so that it can be visualized with pfstools.
TIFFSetField(image, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
}
TIFFSetField(image, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
TIFFSetField(image, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
@ -963,14 +1054,14 @@ bool nv::ImageIO::saveFloatTIFF(const char * fileName, const FloatImage * fimage
#endif
#if defined(HAVE_EXR)
#if defined(HAVE_OPENEXR)
namespace
{
class ExrStream : public Imf::IStream
{
public:
ExrStream(Stream & s) : m_stream(s)
ExrStream(const char * name, Stream & s) : Imf::IStream(name), m_stream(s)
{
nvDebugCheck(s.isLoading());
}
@ -987,12 +1078,12 @@ namespace
return m_stream.isAtEnd();
}
virtual Int64 tellg()
virtual Imf::Int64 tellg()
{
return m_stream.tell();
}
virtual void seekg(Int64 pos)
virtual void seekg(Imf::Int64 pos)
{
m_stream.seek(pos);
}
@ -1010,12 +1101,13 @@ namespace
FloatImage * nv::ImageIO::loadFloatEXR(const char * fileName, Stream & s)
{
nvCheck(s.isLoading());
nvCheck(!s.isError());
ExrStream stream(s);
RgbaInputFile inputFile(stream);
ExrStream stream(fileName, s);
Imf::InputFile inputFile(stream);
Box2i box = inputFile.dataWindow();
Imath::Box2i box = inputFile.header().dataWindow();
int width = box.max.x - box.min.y + 1;
int height = box.max.x - box.min.y + 1;
@ -1024,7 +1116,7 @@ FloatImage * nv::ImageIO::loadFloatEXR(const char * fileName, Stream & s)
// Count channels.
uint channelCount= 0;
for (ChannelList::ConstIterator it = channels.begin(); it != channels.end(); ++it)
for (Imf::ChannelList::ConstIterator it = channels.begin(); it != channels.end(); ++it)
{
channelCount++;
}
@ -1034,11 +1126,11 @@ FloatImage * nv::ImageIO::loadFloatEXR(const char * fileName, Stream & s)
fimage->allocate(channelCount, width, height);
// Describe image's layout with a framebuffer.
FrameBuffer frameBuffer;
Imf::FrameBuffer frameBuffer;
uint i = 0;
for (ChannelList::ConstIterator it = channels.begin(); it != channels.end(); ++it, ++i)
for (Imf::ChannelList::ConstIterator it = channels.begin(); it != channels.end(); ++it, ++i)
{
frameBuffer.insert(it.name(), Slice(FLOAT, fimage->channel(i), sizeof(float), sizeof(float) * width));
frameBuffer.insert(it.name(), Imf::Slice(Imf::FLOAT, (char *)fimage->channel(i), sizeof(float), sizeof(float) * width));
}
// Read it.
@ -1048,22 +1140,11 @@ FloatImage * nv::ImageIO::loadFloatEXR(const char * fileName, Stream & s)
return fimage.release();
}
FloatImage * nv::ImageIO::loadFloatEXR(const char * fileName)
{
StdInputStream stream(fileName);
if (stream.isError()) {
return false;
}
return loadFloatExr(fileName, stream);
}
bool nv::ImageIO::saveFloatEXR(const char * fileName, const FloatImage * fimage, uint base_component, uint num_components)
{
nvCheck(fileName != NULL);
nvCheck(fimage != NULL);
nvCheck(fimage->componentNum() <= base_component + num_components);
nvCheck(base_component + num_components <= fimage->componentNum());
nvCheck(num_components > 0 && num_components <= 4);
const int w = fimage->width();
@ -1071,29 +1152,146 @@ bool nv::ImageIO::saveFloatEXR(const char * fileName, const FloatImage * fimage,
const char * channelNames[] = {"R", "G", "B", "A"};
Header header (width, height);
Imf::Header header (w, h);
for (uint c = 0; c < num_components; c++)
{
header.channels().insert(channelNames[c], Channel(FLOAT));
header.channels().insert(channelNames[c], Imf::Channel(Imf::FLOAT));
}
OutputFile file(fileName, header);
FrameBuffer frameBuffer;
Imf::OutputFile file(fileName, header);
Imf::FrameBuffer frameBuffer;
for (uint c = 0; c < num_components; c++)
{
const char * channel = (char *) fimage->channel(base_component + c);
frameBuffer.insert(channelNames[c], Slice(FLOAT, channel, sizeof(float), sizeof(float) * w));
char * channel = (char *) fimage->channel(base_component + c);
frameBuffer.insert(channelNames[c], Imf::Slice(Imf::FLOAT, channel, sizeof(float), sizeof(float) * w));
}
file.setFrameBuffer(frameBuffer);
file.writePixels(height);
file.writePixels(h);
return false;
return true;
}
#endif // defined(HAVE_EXR)
#endif // defined(HAVE_OPENEXR)
FloatImage * nv::ImageIO::loadFloatPFM(const char * fileName, Stream & s)
{
nvCheck(s.isLoading());
nvCheck(!s.isError());
Tokenizer parser(&s);
parser.nextToken();
bool grayscale;
if (parser.token() == "PF")
{
grayscale = false;
}
else if (parser.token() == "Pf")
{
grayscale = true;
}
else
{
// Invalid file.
return NULL;
}
parser.nextLine();
int width = parser.token().toInt(); parser.nextToken();
int height = parser.token().toInt();
parser.nextLine();
float scaleFactor = parser.token().toFloat();
if (scaleFactor >= 0)
{
s.setByteOrder(Stream::BigEndian);
}
else
{
s.setByteOrder(Stream::LittleEndian);
}
scaleFactor = fabsf(scaleFactor);
// Allocate image.
AutoPtr<FloatImage> fimage(new FloatImage());
if (grayscale)
{
fimage->allocate(1, width, height);
float * channel = fimage->channel(0);
for (int i = 0; i < width * height; i++)
{
s << channel[i];
}
}
else
{
fimage->allocate(3, width, height);
float * rchannel = fimage->channel(0);
float * gchannel = fimage->channel(1);
float * bchannel = fimage->channel(2);
for (int i = 0; i < width * height; i++)
{
s << rchannel[i] << gchannel[i] << bchannel[i];
}
}
return fimage.release();
}
bool nv::ImageIO::saveFloatPFM(const char * fileName, const FloatImage * fimage, uint base_component, uint num_components)
{
nvCheck(fileName != NULL);
nvCheck(fimage != NULL);
nvCheck(fimage->componentNum() <= base_component + num_components);
nvCheck(num_components == 1 || num_components == 3);
StdOutputStream stream(fileName);
TextWriter writer(&stream);
if (num_components == 1) writer.write("Pf\n");
else /*if (num_components == 3)*/ writer.write("PF\n");
int w = fimage->width();
int h = fimage->height();
writer.write("%d %d\n", w, h);
writer.write("%f\n", -1.0f); // little endian with 1.0 scale.
if (num_components == 1)
{
float * channel = const_cast<float *>(fimage->channel(0));
for (int i = 0; i < w * h; i++)
{
stream << channel[i];
}
}
else
{
float * rchannel = const_cast<float *>(fimage->channel(0));
float * gchannel = const_cast<float *>(fimage->channel(1));
float * bchannel = const_cast<float *>(fimage->channel(2));
for (int i = 0; i < w * h; i++)
{
stream << rchannel[i] << gchannel[i] << bchannel[i];
}
}
return true;
}
#if 0

View File

@ -15,9 +15,14 @@ namespace nv
{
NVIMAGE_API Image * load(const char * fileName);
NVIMAGE_API Image * load(const char * fileName, Stream & s);
NVIMAGE_API FloatImage * loadFloat(const char * fileName);
NVIMAGE_API FloatImage * loadFloat(const char * fileName, Stream & s);
NVIMAGE_API bool save(const char * fileName, Stream & s, Image * img);
NVIMAGE_API bool save(const char * fileName, Image * img);
NVIMAGE_API bool saveFloat(const char * fileName, const FloatImage * fimage, uint base_component, uint num_components);
NVIMAGE_API Image * loadTGA(Stream & s);
NVIMAGE_API bool saveTGA(Stream & s, const Image * img);
@ -37,12 +42,15 @@ namespace nv
NVIMAGE_API bool saveFloatTIFF(const char * fileName, const FloatImage * fimage, uint base_component, uint num_components);
#endif
#if defined(HAVE_EXR)
#if defined(HAVE_OPENEXR)
NVIMAGE_API FloatImage * loadFloatEXR(const char * fileName, Stream & s);
NVIMAGE_API bool saveFloatEXR(const char * fileName, const FloatImage * fimage, uint base_component, uint num_components);
#endif
NVIMAGE_API FloatImage * loadFloatPFM(const char * fileName, Stream & s);
NVIMAGE_API bool saveFloatPFM(const char * fileName, const FloatImage * fimage, uint base_component, uint num_components);
} // ImageIO namespace
} // nv namespace