This source file includes following definitions.
- levelSize
- dataWindowForLevel
- dataWindowForTile
- calculateBytesPerPixel
- floorLog2
- ceilLog2
- roundLog2
- calculateNumXLevels
- calculateNumYLevels
- calculateNumTiles
- precalculateTileInfo
#include <ImfTiledMisc.h>
#include "Iex.h"
#include <ImfMisc.h>
#include <ImfChannelList.h>
#include <algorithm>
namespace Imf {
using Imath::Box2i;
using Imath::V2i;
int
levelSize (int min, int max, int l, LevelRoundingMode rmode)
{
if (l < 0)
throw Iex::ArgExc ("Argument not in valid range.");
int a = max - min + 1;
int b = (1 << l);
int size = a / b;
if (rmode == ROUND_UP && size * b < a)
size += 1;
return std::max (size, 1);
}
Box2i
dataWindowForLevel (const TileDescription &tileDesc,
int minX, int maxX,
int minY, int maxY,
int lx, int ly)
{
V2i levelMin = V2i (minX, minY);
V2i levelMax = levelMin +
V2i (levelSize (minX, maxX, lx, tileDesc.roundingMode) - 1,
levelSize (minY, maxY, ly, tileDesc.roundingMode) - 1);
return Box2i(levelMin, levelMax);
}
Box2i
dataWindowForTile (const TileDescription &tileDesc,
int minX, int maxX,
int minY, int maxY,
int dx, int dy,
int lx, int ly)
{
V2i tileMin = V2i (minX + dx * tileDesc.xSize,
minY + dy * tileDesc.ySize);
V2i tileMax = tileMin + V2i (tileDesc.xSize - 1, tileDesc.ySize - 1);
V2i levelMax = dataWindowForLevel
(tileDesc, minX, maxX, minY, maxY, lx, ly).max;
tileMax = V2i (std::min (tileMax[0], levelMax[0]),
std::min (tileMax[1], levelMax[1]));
return Box2i (tileMin, tileMax);
}
size_t
calculateBytesPerPixel (const Header &header)
{
const ChannelList &channels = header.channels();
size_t bytesPerPixel = 0;
for (ChannelList::ConstIterator c = channels.begin();
c != channels.end();
++c)
{
bytesPerPixel += pixelTypeSize (c.channel().type);
}
return bytesPerPixel;
}
namespace {
int
floorLog2 (int x)
{
int y = 0;
while (x > 1)
{
y += 1;
x >>= 1;
}
return y;
}
int
ceilLog2 (int x)
{
int y = 0;
int r = 0;
while (x > 1)
{
if (x & 1)
r = 1;
y += 1;
x >>= 1;
}
return y + r;
}
int
roundLog2 (int x, LevelRoundingMode rmode)
{
return (rmode == ROUND_DOWN)? floorLog2 (x): ceilLog2 (x);
}
int
calculateNumXLevels (const TileDescription& tileDesc,
int minX, int maxX,
int minY, int maxY)
{
int num = 0;
switch (tileDesc.mode)
{
case ONE_LEVEL:
num = 1;
break;
case MIPMAP_LEVELS:
{
int w = maxX - minX + 1;
int h = maxY - minY + 1;
num = roundLog2 (std::max (w, h), tileDesc.roundingMode) + 1;
}
break;
case RIPMAP_LEVELS:
{
int w = maxX - minX + 1;
num = roundLog2 (w, tileDesc.roundingMode) + 1;
}
break;
default:
throw Iex::ArgExc ("Unknown LevelMode format.");
}
return num;
}
int
calculateNumYLevels (const TileDescription& tileDesc,
int minX, int maxX,
int minY, int maxY)
{
int num = 0;
switch (tileDesc.mode)
{
case ONE_LEVEL:
num = 1;
break;
case MIPMAP_LEVELS:
{
int w = maxX - minX + 1;
int h = maxY - minY + 1;
num = roundLog2 (std::max (w, h), tileDesc.roundingMode) + 1;
}
break;
case RIPMAP_LEVELS:
{
int h = maxY - minY + 1;
num = roundLog2 (h, tileDesc.roundingMode) + 1;
}
break;
default:
throw Iex::ArgExc ("Unknown LevelMode format.");
}
return num;
}
void
calculateNumTiles (int *numTiles,
int numLevels,
int min, int max,
int size,
LevelRoundingMode rmode)
{
for (int i = 0; i < numLevels; i++)
{
numTiles[i] = (levelSize (min, max, i, rmode) + size - 1) / size;
}
}
}
void
precalculateTileInfo (const TileDescription& tileDesc,
int minX, int maxX,
int minY, int maxY,
int *&numXTiles, int *&numYTiles,
int &numXLevels, int &numYLevels)
{
numXLevels = calculateNumXLevels(tileDesc, minX, maxX, minY, maxY);
numYLevels = calculateNumYLevels(tileDesc, minX, maxX, minY, maxY);
numXTiles = new int[numXLevels];
numYTiles = new int[numYLevels];
calculateNumTiles (numXTiles,
numXLevels,
minX, maxX,
tileDesc.xSize,
tileDesc.roundingMode);
calculateNumTiles (numYTiles,
numYLevels,
minY, maxY,
tileDesc.ySize,
tileDesc.roundingMode);
}
}