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}) INCLUDE_DIRECTORIES(${TIFF_INCLUDE_DIR})
ENDIF(TIFF_FOUND) ENDIF(TIFF_FOUND)
IF(OPENEXR_FOUND)
SET(LIBS ${LIBS} ${OPENEXR_LIBRARIES})
INCLUDE_DIRECTORIES(${OPENEXR_INCLUDE_PATHS})
ENDIF(OPENEXR_FOUND)
# targets # targets
ADD_DEFINITIONS(-DNVIMAGE_EXPORTS) ADD_DEFINITIONS(-DNVIMAGE_EXPORTS)

View File

@ -15,15 +15,29 @@ using namespace nv;
namespace namespace
{ {
static int round(float f) static int iround(float f)
{ {
return int(f); return int(f);
} }
static int ifloor(float f)
{
return int(floor(f));
}
static float frac(float f) static float frac(float f)
{ {
return f - floor(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++) { for(uint i = 0; i < size; i++) {
Vector3 normal(xChannel[i], yChannel[i], zChannel[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(); xChannel[i] = normal.x();
yChannel[i] = normal.y(); yChannel[i] = normal.y();
@ -226,72 +240,42 @@ void FloatImage::exponentiate(uint base_component, uint num, float power)
} }
} }
#if 0 float FloatImage::sampleNearest(const float x, const float y, const int c, const WrapMode wm) const
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_Clamp ) return sampleNearestClamp(x, y, c);
/*if( wm == WrapMode_Repeat )*/ return nearest_repeat(x, y, c); else if( wm == WrapMode_Repeat ) return sampleNearestRepeat(x, y, c);
//if( wm == WrapMode_Mirror ) return nearest_mirror(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; if( wm == WrapMode_Clamp ) return sampleLinearClamp(x, y, c);
const int h = m_height; else if( wm == WrapMode_Repeat ) return sampleLinearRepeat(x, y, c);
int ix = ::clamp(x, 0, w-1); else /*if( wm == WrapMode_Mirror )*/ return sampleLinearMirror(x, y, c);
int iy = ::clamp(y, 0, h-1); }
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); 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; int ix = iround(frac(x) * m_width);
const int h = m_height; int iy = iround(frac(y) * 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);
return pixel(ix, iy, c); 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; int ix = mirror(iround(x * m_width), m_width);
const int h = m_height; int iy = mirror(iround(y * m_height), m_height);
int ix = round(frac(x) * w);
int iy = round(frac(y) * h);
return pixel(ix, iy, c); return pixel(ix, iy, c);
} }
float FloatImage::nearest_mirror(float x, float y, const int c) const float FloatImage::sampleLinearClamp(float x, float y, const int c) const
{
// @@ TBD
return 0.0f;
}
float FloatImage::linear_clamp(float x, float y, const int c) const
{ {
const int w = m_width; const int w = m_width;
const int h = m_height; 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 fracX = frac(x);
const float fracY = frac(y); const float fracY = frac(y);
const int ix0 = ::clamp(round(x), 0, w-1); const int ix0 = ::clamp(ifloor(x), 0, w-1);
const int iy0 = ::clamp(round(y), 0, h-1); const int iy0 = ::clamp(ifloor(y), 0, h-1);
const int ix1 = ::clamp(round(x)+1, 0, w-1); const int ix1 = ::clamp(ifloor(x)+1, 0, w-1);
const int iy1 = ::clamp(round(y)+1, 0, h-1); const int iy1 = ::clamp(ifloor(y)+1, 0, h-1);
float f1 = pixel(ix0, iy0, c); float f1 = pixel(ix0, iy0, c);
float f2 = pixel(ix1, 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); 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 w = m_width;
const int h = m_height; 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 fracX = frac(x * w);
const float fracY = frac(y * h); const float fracY = frac(y * h);
int ix0 = round(frac(x) * w); int ix0 = ifloor(frac(x) * w);
int iy0 = round(frac(y) * h); int iy0 = ifloor(frac(y) * h);
int ix1 = round(frac(x + 1.0f/w) * w); int ix1 = ifloor(frac(x + 1.0f/w) * w);
int iy1 = round(frac(y + 1.0f/h) * h); int iy1 = ifloor(frac(y + 1.0f/h) * h);
float f1 = pixel(ix0, iy0, c); float f1 = pixel(ix0, iy0, c);
float f2 = pixel(ix1, 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); 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 const int w = m_width;
return 0.0f; 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); void setPixel(float f, uint idx);
float pixel(uint idx) const; 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 sampleNearestClamp(float x, float y, int c) const;
float linear(float x, float y, int c, WrapMode wm) 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 sampleLinearClamp(float x, float y, int c) const;
float nearest_repeat(float x, float y, int c) const; float sampleLinearRepeat(float x, float y, int c) const;
float nearest_mirror(float x, float y, int c) const; float sampleLinearMirror(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;
//@} //@}
public: public:

View File

@ -1,6 +1,7 @@
// This code is in the public domain -- castanyo@yahoo.es // This code is in the public domain -- castanyo@yahoo.es
#include <nvcore/Containers.h> #include <nvcore/Containers.h>
#include <nvcore/Ptr.h>
#include <nvmath/nvmath.h> #include <nvmath/nvmath.h>
@ -11,7 +12,7 @@ using namespace nv;
// This is a variation of Sapiro's inpainting method. // 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(img != NULL);
nvCheck(bmap != NULL); nvCheck(bmap != NULL);
@ -23,83 +24,73 @@ void nv::fillExtrapolateOnce(FloatImage * img, BitMap * bmap)
nvCheck(bmap->width() == uint(w)); nvCheck(bmap->width() == uint(w));
nvCheck(bmap->height() == uint(h)); nvCheck(bmap->height() == uint(h));
BitMap * newbmap = new BitMap(w, h); AutoPtr<BitMap> newbmap(new BitMap(w, h));
for(int c = 0; c < count; c++) { for(int p = 0; p < passCount; p++)
{
for(int c = 0; c < count; c++)
{
float * channel = img->channel(c);
float * channel = img->channel(c); for(int y = 0; y < h; y++) {
for(int x = 0; x < w; x++) {
for(int y = 0; y < h; y++) { if (bmap->bitAt(x, y)) {
for(int x = 0; x < w; x++) { // Not a hole.
newbmap->setBitAt(x, y);
if (bmap->bitAt(x, y)) { continue;
// Not a hole.
newbmap->setBitAt(x, y);
continue;
}
const bool west = bmap->bitAt(img->indexClamp(x-1, y));
const bool east = bmap->bitAt(img->indexClamp(x+1, y));
const bool north = bmap->bitAt(img->indexClamp(x, y-1));
const bool south = bmap->bitAt(img->indexClamp(x, y+1));
const bool northwest = bmap->bitAt(img->indexClamp(x-1, y-1));
const bool northeast = bmap->bitAt(img->indexClamp(x+1, y-1));
const bool southwest = bmap->bitAt(img->indexClamp(x-1, y+1));
const bool southeast = bmap->bitAt(img->indexClamp(x+1, y+1));
int num = west + east + north + south + northwest + northeast + southwest + southeast;
if (num != 0) {
float average = 0.0f;
if (num == 3 && west && northwest && southwest) {
average = channel[img->indexClamp(x-1, y)];
}
else if (num == 3 && east && northeast && southeast) {
average = channel[img->indexClamp(x+1, y)];
}
else if (num == 3 && north && northwest && northeast) {
average = channel[img->indexClamp(x, y-1)];
}
else if (num == 3 && south && southwest && southeast) {
average = channel[img->indexClamp(x, y+1)];
}
else {
float total = 0.0f;
if (west) { average += 1 * channel[img->indexClamp(x-1, y)]; total += 1; }
if (east) { average += 1 * channel[img->indexClamp(x+1, y)]; total += 1; }
if (north) { average += 1 * channel[img->indexClamp(x, y-1)]; total += 1; }
if (south) { average += 1 * channel[img->indexClamp(x, y+1)]; total += 1; }
if (northwest) { average += channel[img->indexClamp(x-1, y-1)]; ++total; }
if (northeast) { average += channel[img->indexClamp(x+1, y-1)]; ++total; }
if (southwest) { average += channel[img->indexClamp(x-1, y+1)]; ++total; }
if (southeast) { average += channel[img->indexClamp(x+1, y+1)]; ++total; }
average /= total;
} }
channel[img->indexClamp(x, y)] = average; const bool west = bmap->bitAt(img->indexClamp(x-1, y));
newbmap->setBitAt(x, y); const bool east = bmap->bitAt(img->indexClamp(x+1, y));
const bool north = bmap->bitAt(img->indexClamp(x, y-1));
const bool south = bmap->bitAt(img->indexClamp(x, y+1));
const bool northwest = bmap->bitAt(img->indexClamp(x-1, y-1));
const bool northeast = bmap->bitAt(img->indexClamp(x+1, y-1));
const bool southwest = bmap->bitAt(img->indexClamp(x-1, y+1));
const bool southeast = bmap->bitAt(img->indexClamp(x+1, y+1));
int num = west + east + north + south + northwest + northeast + southwest + southeast;
if (num != 0) {
float average = 0.0f;
if (num == 3 && west && northwest && southwest) {
average = channel[img->indexClamp(x-1, y)];
}
else if (num == 3 && east && northeast && southeast) {
average = channel[img->indexClamp(x+1, y)];
}
else if (num == 3 && north && northwest && northeast) {
average = channel[img->indexClamp(x, y-1)];
}
else if (num == 3 && south && southwest && southeast) {
average = channel[img->indexClamp(x, y+1)];
}
else {
float total = 0.0f;
if (west) { average += 1 * channel[img->indexClamp(x-1, y)]; total += 1; }
if (east) { average += 1 * channel[img->indexClamp(x+1, y)]; total += 1; }
if (north) { average += 1 * channel[img->indexClamp(x, y-1)]; total += 1; }
if (south) { average += 1 * channel[img->indexClamp(x, y+1)]; total += 1; }
if (northwest) { average += channel[img->indexClamp(x-1, y-1)]; ++total; }
if (northeast) { average += channel[img->indexClamp(x+1, y-1)]; ++total; }
if (southwest) { average += channel[img->indexClamp(x-1, y+1)]; ++total; }
if (southeast) { average += channel[img->indexClamp(x+1, y+1)]; ++total; }
average /= total;
}
channel[img->indexClamp(x, y)] = average;
newbmap->setBitAt(x, y);
}
} }
} }
} }
}
// Update the bit mask. // Update the bit mask.
swap(*newbmap, *bmap); 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 } // namespace
// Voronoi filling using EDT-4 // Voronoi filling using EDT-4
void nv::fillVoronoi(FloatImage * img, const BitMap & bmap) void nv::fillVoronoi(FloatImage * img, const BitMap * bmap)
{ {
nvCheck(img != NULL); nvCheck(img != NULL);
@ -142,8 +133,8 @@ void nv::fillVoronoi(FloatImage * img, const BitMap & bmap)
const int h = img->height(); const int h = img->height();
const int count = img->componentNum(); const int count = img->componentNum();
nvCheck(bmap.width() == uint(w)); nvCheck(bmap->width() == uint(w));
nvCheck(bmap.height() == uint(h)); nvCheck(bmap->height() == uint(h));
Array<Neighbor> edm; Array<Neighbor> edm;
edm.resize(w * h); edm.resize(w * h);
@ -154,7 +145,7 @@ void nv::fillVoronoi(FloatImage * img, const BitMap & bmap)
// Init edm. // Init edm.
for( y = 0; y < h; y++ ) { for( y = 0; y < h; y++ ) {
for( x = 0; x < w; x++ ) { 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].x = x;
edm[y * w + x].y = y; edm[y * w + x].y = y;
edm[y * w + x].d = 0; 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); 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. // 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); nvCheck(img != NULL);
@ -320,7 +311,7 @@ void nv::fillPullPush(FloatImage * img, const BitMap & bmap)
Array<const BitMap *> mipmapMasks(num); Array<const BitMap *> mipmapMasks(num);
mipmaps.append(img); mipmaps.append(img);
mipmapMasks.append(&bmap); mipmapMasks.append(bmap);
const FloatImage * current; const FloatImage * current;
const BitMap * currentMask; const BitMap * currentMask;
@ -336,16 +327,22 @@ void nv::fillPullPush(FloatImage * img, const BitMap & bmap)
for(uint y = 0; y < h; y++) { for(uint y = 0; y < h; y++) {
for(uint x = 0; x < w; x++) { for(uint x = 0; x < w; x++) {
uint sx = x; int sx = x;
uint sy = y; int sy = y;
//float sx = x;
//float sy = y;
const uint levelCount = mipmaps.count(); 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)) if (mipmapMasks[l]->bitAt(sx, sy))
{ {
// Sample mipmaps[l](sx, sy) and copy to img(x, y) // Sample mipmaps[l](sx, sy) and copy to img(x, y)
for(uint c = 0; c < count; c++) { 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); img->setPixel(mipmaps[l]->pixel(sx, sy, c), x, y, c);
} }
break; 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(mipmaps);
deleteAll(mipmapMasks); 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 DoPixelSeamFix
10-20-02 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 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. just seems like way too much work.
**/ */
struct LocalPixels struct LocalPixels
{ {
@ -395,11 +392,11 @@ struct LocalPixels
// index [y][x] // index [y][x]
bool fill[5][5]; bool fill[5][5];
float data[5][5]; float data[5][5];
mutable float result; mutable float result;
mutable float weight; mutable float weight;
bool Quad3SubH(float * pQ, int row) const
bool Quad3SubH(gVec4 * pQ,int row) const
{ {
const bool * pFill = fill[row]; const bool * pFill = fill[row];
const float * pDat = data[row]; const float * pDat = data[row];
@ -426,7 +423,7 @@ struct LocalPixels
} }
// improve result with a horizontal quad in row 1 and/or // 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] ) if ( fill[1][col] && fill[2][col] && fill[3][col] )
{ {
@ -449,14 +446,14 @@ struct LocalPixels
return false; return false;
} }
bool Quad3H(gVec4 * pQ) const bool Quad3H(float * pQ) const
{ {
if ( ! Quad3SubH(pQ,1) ) if (!Quad3SubH(pQ,1))
{ {
return Quad3SubH(pQ,3); 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) ) if (Quad3SubH(&q, 3))
{ {
// got q and pQ // got q and pQ
*pQ = (*pQ+q)*0.5f; *pQ = (*pQ+q)*0.5f;
@ -464,17 +461,17 @@ struct LocalPixels
return true; return true;
} }
bool Quad3V(gVec4 * pQ) const bool Quad3V(float * pQ) const
{ {
if ( ! Quad3SubV(pQ,1) ) if (!Quad3SubV(pQ, 1))
{ {
return Quad3SubV(pQ,3); 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) ) if (Quad3SubV(&q, 3))
{ {
// got q and pQ // got q and pQ
*pQ = (*pQ+q)*0.5f; *pQ = (*pQ + q) * 0.5f;
} }
return true; return true;
} }
@ -482,7 +479,7 @@ struct LocalPixels
// a common want is [1] - ([0]+[2])*0.5f ; // a common want is [1] - ([0]+[2])*0.5f ;
// so use -0.5f*Quad // so use -0.5f*Quad
bool TryQuads() const bool tryQuads() const
{ {
bool res = false; bool res = false;
@ -490,7 +487,7 @@ struct LocalPixels
if ( fill[2][1] && fill[2][3] ) if ( fill[2][1] && fill[2][3] )
{ {
// got horizontal straddle // got horizontal straddle
gVec4 q; float q;
if ( Quad3H(&q) ) if ( Quad3H(&q) )
{ {
result += (data[2][1] + data[2][3] - q) * 0.5f; result += (data[2][1] + data[2][3] - q) * 0.5f;
@ -501,7 +498,7 @@ struct LocalPixels
if ( fill[1][2] && fill[3][2] ) if ( fill[1][2] && fill[3][2] )
{ {
// got vertical straddle // got vertical straddle
gVec4 q; float q;
if ( Quad3V(&q) ) if ( Quad3V(&q) )
{ {
result += (data[1][2] + data[3][2] - q) * 0.5f; result += (data[1][2] + data[3][2] - q) * 0.5f;
@ -514,7 +511,7 @@ struct LocalPixels
if ( fill[2][0] && fill[2][1] ) if ( fill[2][0] && fill[2][1] )
{ {
// got left-side pair // got left-side pair
gVec4 q; float q;
if ( Quad3H(&q) ) if ( Quad3H(&q) )
{ {
result += data[2][1]*2.f - data[2][0] + q; result += data[2][1]*2.f - data[2][0] + q;
@ -525,7 +522,7 @@ struct LocalPixels
if ( fill[2][3] && fill[2][4] ) if ( fill[2][3] && fill[2][4] )
{ {
// got right-side pair // got right-side pair
gVec4 q; float q;
if ( Quad3H(&q) ) if ( Quad3H(&q) )
{ {
result += data[2][3]*2.f - data[2][4] + q; result += data[2][3]*2.f - data[2][4] + q;
@ -536,7 +533,7 @@ struct LocalPixels
if ( fill[0][2] && fill[1][2] ) if ( fill[0][2] && fill[1][2] )
{ {
// got left-side pair // got left-side pair
gVec4 q; float q;
if ( Quad3V(&q) ) if ( Quad3V(&q) )
{ {
result += data[1][2]*2.f - data[0][2] + q; result += data[1][2]*2.f - data[0][2] + q;
@ -547,7 +544,7 @@ struct LocalPixels
if ( fill[3][2] && fill[4][2] ) if ( fill[3][2] && fill[4][2] )
{ {
// got right-side pair // got right-side pair
gVec4 q; float q;
if ( Quad3V(&q) ) if ( Quad3V(&q) )
{ {
result += data[3][2]*2.f - data[4][2] + q; result += data[3][2]*2.f - data[4][2] + q;
@ -558,7 +555,7 @@ struct LocalPixels
return res; return res;
} }
bool TryPlanar() const bool tryPlanar() const
{ {
// four cases : // four cases :
const int indices[] = const int indices[] =
@ -569,37 +566,37 @@ struct LocalPixels
2,3, 3,2, 3,3 2,3, 3,2, 3,3
}; };
bool res = false; bool res = false;
for(int i=0;i<4;i++) for (int i = 0; i < 4; i++)
{ {
const int * I = indices + i*6; const int * I = indices + i*6;
if ( ! fill[ I[0] ][ I[1] ] ) if (!fill[ I[0] ][ I[1] ])
continue; continue;
if ( ! fill[ I[2] ][ I[3] ] ) if (!fill[ I[2] ][ I[3] ])
continue; continue;
if ( ! fill[ I[4] ][ I[5] ] ) if (!fill[ I[4] ][ I[5] ])
continue; continue;
result += data[ I[0] ][ I[1] ] + data[ I[2] ][ I[3] ] - data[ I[4] ][ I[5] ]; result += data[ I[0] ][ I[1] ] + data[ I[2] ][ I[3] ] - data[ I[4] ][ I[5] ];
weight += 1.f; weight += 1.0f;
res = true; res = true;
} }
return res; return res;
} }
bool TryTwos() const bool tryTwos() const
{ {
bool res = false; bool res = false;
if ( fill[2][1] && fill[2][3] ) if (fill[2][1] && fill[2][3])
{ {
result += (data[2][1] + data[2][3]) * 0.5f; result += (data[2][1] + data[2][3]) * 0.5f;
weight += 1.f; weight += 1.0f;
res = true; res = true;
} }
if ( fill[1][2] && fill[3][2] ) if (fill[1][2] && fill[3][2])
{ {
result += (data[1][2] + data[3][2]) * 0.5f; result += (data[1][2] + data[3][2]) * 0.5f;
weight += 1.f; weight += 1.0f;
res = true; res = true;
} }
@ -611,141 +608,146 @@ struct LocalPixels
1,2, 0,2, 1,2, 0,2,
3,2, 4,2, 3,2, 4,2,
}; };
for(int i=0;i<4;i++) for (int i = 0; i < 4; i++)
{ {
const int * I = indices + i*4; const int * I = indices + i*4;
if ( ! fill[ I[0] ][ I[1] ] ) if (!fill[ I[0] ][ I[1] ])
continue; continue;
if ( ! fill[ I[2] ][ I[3] ] ) if (!fill[ I[2] ][ I[3] ])
continue; continue;
result += data[ I[0] ][ I[1] ]*2.f - data[ I[2] ][ I[3] ]; result += data[ I[0] ][ I[1] ]*2.0f - data[ I[2] ][ I[3] ];
weight += 1.f; weight += 1.0f;
res = true; res = true;
} }
return res; return res;
} }
bool doLocalPixelFill() const
bool DoLocalPixelFill() const
{ {
result = gVec4::zero; result = 0.0f;
weight = 0.f; weight = 0.0f;
if ( TryQuads() ) if (tryQuads()) {
return true; return true;
}
if ( TryPlanar() ) if (tryPlanar()) {
return true; return true;
}
return TryTwos(); return tryTwos();
} }
}; // LocalPixels ----------------------------------------------- }; // struct LocalPixels
void gNormalMap::DoPixelSeamFix()
// This is a cubic extrapolation filter from Charles Bloom (DoPixelSeamFix).
void nv::fillCubicExtrapolate(int passCount, FloatImage * img, BitMap * bmap, int coverageIndex /*= -1*/)
{ {
gLog::Printf("gNormalMap::DoPixelSeamFix.."); nvCheck(passCount > 0);
nvCheck(img != NULL);
nvCheck(bmap != NULL);
const int desiredTicks = 30; const int w = img->width();
const int heightPerTick = NUM_SEAMFIX_PASSES * m_height / desiredTicks; const int h = img->height();
int tick = 0; const int count = img->componentNum();
for(int pass=0;pass<NUM_SEAMFIX_PASSES;pass++) nvCheck(bmap->width() == uint(w));
nvCheck(bmap->height() == uint(h));
AutoPtr<BitMap> newbmap( new BitMap(w, h) );
float * coverageChannel = NULL;
if (coverageIndex != -1)
{ {
for(int yb=0;yb<m_height;yb++) coverageChannel = img->channel(coverageIndex);
{ }
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
}
// 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 firstChannel = -1;
int numFill = 0;
LocalPixels lp; for (int p = 0; p < passCount; p++)
for(int ny=0;ny<5;ny++) {
{ for (int c = 0; c < count; c++)
int y = (yb + ny - 2); {
if ( y < 0 || y >= m_height ) if (c == coverageIndex) continue;
{ if (firstChannel == -1) firstChannel = c;
// out of range
for(int i=0;i<5;i++) float * channel = img->channel(c);
{
lp.fill[ny][i] = false; 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; continue;
} }
gVec4 * pRow = m_normals + m_width * y;
const EState * pStateRow = m_states + m_width * y; int numFill = 0;
for(int nx=0;nx<5;nx++)
LocalPixels lp;
for (int ny = 0; ny < 5; ny++)
{ {
int x = (xb + nx - 2); int y = (yb + ny - 2);
if ( x < 0 || x >= m_width ) if ( y < 0 || y >= h )
{ {
lp.fill[ny][nx] = false; // out of range
for(int i = 0; i < 5; i++)
{
lp.fill[ny][i] = false;
}
continue;
} }
else if ( pStateRow[x] == eNull || pStateRow[x] == eEdge )
for (int nx = 0; nx < 5; nx++)
{ {
lp.fill[ny][nx] = false; int x = (xb + nx - 2);
if (x < 0 || x >= w)
{
lp.fill[ny][nx] = false;
}
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] = channel[idx];
numFill++;
}
}
} }
else }
// need at least 3 to do anything decent
if (numFill < 2)
continue;
nvDebugCheck(lp.fill[2][2] == false);
if (lp.doLocalPixelFill())
{
const int idx = img->index(xb, yb);
channel[idx] = lp.result / lp.weight;
if (c == firstChannel)
{ {
lp.fill[ny][nx] = true; //coverageChannel[idx] /= lp.weight; // @@ Not sure what this was for, coverageChannel[idx] is always zero.
lp.data[ny][nx] = pRow[x]; newbmap->setBitAt(xb, yb);
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;
}
}
}
if ( ++tick == heightPerTick )
{
tick = 0;
gLog::Printf(".");
} }
} }
// now run back over and stamp anything that's not null as being ok // Update the bit mask.
swap(*newbmap, *bmap);
for(int y=0;y<m_height;y++)
{
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;
}
}
}
} }
gLog::Printf("done\n");
} }
#endif // 0

View File

@ -84,12 +84,12 @@ namespace nv
}; };
NVIMAGE_API void fillVoronoi(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 fillBlur(FloatImage * img, const BitMap * bmap);
NVIMAGE_API void fillPullPush(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 fillExtrapolate(int passCount, FloatImage * img, BitMap * bmap);
NVIMAGE_API void fillExtrapolateNTimes(FloatImage * img, BitMap * bmap, int n); NVIMAGE_API void fillCubicExtrapolate(int passCount, FloatImage * img, BitMap * bmap, int coverageIndex = -1);
} // nv namespace } // nv namespace

View File

@ -4,6 +4,8 @@
#include <nvcore/Containers.h> #include <nvcore/Containers.h>
#include <nvcore/StrLib.h> #include <nvcore/StrLib.h>
#include <nvcore/StdStream.h> #include <nvcore/StdStream.h>
#include <nvcore/Tokenizer.h>
#include <nvcore/TextWriter.h>
#include <nvmath/Color.h> #include <nvmath/Color.h>
@ -29,11 +31,13 @@ extern "C" {
# include <tiffio.h> # include <tiffio.h>
#endif #endif
#if defined(HAVE_EXR) #if defined(HAVE_OPENEXR)
# include <ImfRgbaFile.h> # include <ImfIO.h>
# include <ImfInputFile.h> // ??? # include <ImathBox.h>
# include <ImfChannelList.h>
# include <ImfInputFile.h>
# include <ImfOutputFile.h>
# include <ImfArray.h> # include <ImfArray.h>
using namespace Imf;
#endif #endif
using namespace nv; using namespace nv;
@ -55,23 +59,28 @@ namespace {
} // 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()) { 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) { if (strCaseCmp(extension, ".tga") == 0) {
return loadTGA(s); return ImageIO::loadTGA(s);
} }
#if defined(HAVE_JPEG) #if defined(HAVE_JPEG)
if (strCaseCmp(extension, ".jpg") == 0 || strCaseCmp(extension, ".jpeg") == 0) { if (strCaseCmp(extension, ".jpg") == 0 || strCaseCmp(extension, ".jpeg") == 0) {
@ -90,36 +99,113 @@ Image * nv::ImageIO::load(const char * name, Stream & s)
return NULL; 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()) { if (stream.isError()) {
return false; 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 defined(HAVE_TIFF)
if (strCaseCmp(extension, ".tif") == 0 || strCaseCmp(extension, ".tiff") == 0) { if (strCaseCmp(extension, ".tif") == 0 || strCaseCmp(extension, ".tiff") == 0) {
return loadFloatTIFF(name, s); return loadFloatTIFF(fileName, s);
} }
#endif #endif
#if defined(HAVE_EXR) #if defined(HAVE_OPENEXR)
if (strCaseCmp(extension, ".exr") == 0) { if (strCaseCmp(extension, ".exr") == 0) {
return loadFloatEXR(name, s); return loadFloatEXR(fileName, s);
} }
#endif #endif
if (strCaseCmp(extension, ".pfm") == 0) {
return loadFloatPFM(fileName, s);
}
return NULL; 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. /// Load TGA image.
Image * nv::ImageIO::loadTGA(Stream & s) Image * nv::ImageIO::loadTGA(Stream & s)
{ {
@ -912,7 +998,7 @@ bool nv::ImageIO::saveFloatTIFF(const char * fileName, const FloatImage * fimage
{ {
nvCheck(fileName != NULL); nvCheck(fileName != NULL);
nvCheck(fimage != 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 iW = fimage->width();
const int iH = fimage->height(); 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_ROWSPERSTRIP, rowsperstrip);
TIFFSetField(image, TIFFTAG_COMPRESSION, COMPRESSION_PACKBITS); 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_ORIENTATION, ORIENTATION_TOPLEFT);
TIFFSetField(image, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG); TIFFSetField(image, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
@ -963,14 +1054,14 @@ bool nv::ImageIO::saveFloatTIFF(const char * fileName, const FloatImage * fimage
#endif #endif
#if defined(HAVE_EXR) #if defined(HAVE_OPENEXR)
namespace namespace
{ {
class ExrStream : public Imf::IStream class ExrStream : public Imf::IStream
{ {
public: public:
ExrStream(Stream & s) : m_stream(s) ExrStream(const char * name, Stream & s) : Imf::IStream(name), m_stream(s)
{ {
nvDebugCheck(s.isLoading()); nvDebugCheck(s.isLoading());
} }
@ -987,12 +1078,12 @@ namespace
return m_stream.isAtEnd(); return m_stream.isAtEnd();
} }
virtual Int64 tellg() virtual Imf::Int64 tellg()
{ {
return m_stream.tell(); return m_stream.tell();
} }
virtual void seekg(Int64 pos) virtual void seekg(Imf::Int64 pos)
{ {
m_stream.seek(pos); m_stream.seek(pos);
} }
@ -1010,12 +1101,13 @@ namespace
FloatImage * nv::ImageIO::loadFloatEXR(const char * fileName, Stream & s) FloatImage * nv::ImageIO::loadFloatEXR(const char * fileName, Stream & s)
{ {
nvCheck(s.isLoading());
nvCheck(!s.isError()); nvCheck(!s.isError());
ExrStream stream(s); ExrStream stream(fileName, s);
RgbaInputFile inputFile(stream); Imf::InputFile inputFile(stream);
Box2i box = inputFile.dataWindow(); Imath::Box2i box = inputFile.header().dataWindow();
int width = box.max.x - box.min.y + 1; int width = box.max.x - box.min.y + 1;
int height = 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. // Count channels.
uint channelCount= 0; 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++; channelCount++;
} }
@ -1034,11 +1126,11 @@ FloatImage * nv::ImageIO::loadFloatEXR(const char * fileName, Stream & s)
fimage->allocate(channelCount, width, height); fimage->allocate(channelCount, width, height);
// Describe image's layout with a framebuffer. // Describe image's layout with a framebuffer.
FrameBuffer frameBuffer; Imf::FrameBuffer frameBuffer;
uint i = 0; 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. // Read it.
@ -1048,22 +1140,11 @@ FloatImage * nv::ImageIO::loadFloatEXR(const char * fileName, Stream & s)
return fimage.release(); 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) bool nv::ImageIO::saveFloatEXR(const char * fileName, const FloatImage * fimage, uint base_component, uint num_components)
{ {
nvCheck(fileName != NULL); nvCheck(fileName != NULL);
nvCheck(fimage != 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); nvCheck(num_components > 0 && num_components <= 4);
const int w = fimage->width(); 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"}; const char * channelNames[] = {"R", "G", "B", "A"};
Header header (width, height); Imf::Header header (w, h);
for (uint c = 0; c < num_components; c++) 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); Imf::OutputFile file(fileName, header);
FrameBuffer frameBuffer; Imf::FrameBuffer frameBuffer;
for (uint c = 0; c < num_components; c++) for (uint c = 0; c < num_components; c++)
{ {
const char * channel = (char *) fimage->channel(base_component + c); char * channel = (char *) fimage->channel(base_component + c);
frameBuffer.insert(channelNames[c], Slice(FLOAT, channel, sizeof(float), sizeof(float) * w)); frameBuffer.insert(channelNames[c], Imf::Slice(Imf::FLOAT, channel, sizeof(float), sizeof(float) * w));
} }
file.setFrameBuffer(frameBuffer); 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 #if 0

View File

@ -15,9 +15,14 @@ namespace nv
{ {
NVIMAGE_API Image * load(const char * fileName); NVIMAGE_API Image * load(const char * fileName);
NVIMAGE_API Image * load(const char * fileName, Stream & s); NVIMAGE_API Image * load(const char * fileName, Stream & s);
NVIMAGE_API FloatImage * loadFloat(const char * fileName); NVIMAGE_API FloatImage * loadFloat(const char * fileName);
NVIMAGE_API FloatImage * loadFloat(const char * fileName, Stream & s); 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 Image * loadTGA(Stream & s);
NVIMAGE_API bool saveTGA(Stream & s, const Image * img); 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); NVIMAGE_API bool saveFloatTIFF(const char * fileName, const FloatImage * fimage, uint base_component, uint num_components);
#endif #endif
#if defined(HAVE_EXR) #if defined(HAVE_OPENEXR)
NVIMAGE_API FloatImage * loadFloatEXR(const char * fileName, Stream & s); 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); NVIMAGE_API bool saveFloatEXR(const char * fileName, const FloatImage * fimage, uint base_component, uint num_components);
#endif #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 } // ImageIO namespace
} // nv namespace } // nv namespace