ImfTiledMisc.cpp 6.59 KB
Newer Older
1 2 3 4
///////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2004, Industrial Light & Magic, a division of Lucas
// Digital Ltd. LLC
5
//
6
// All rights reserved.
7
//
8 9 10 11 12 13 14 15 16 17 18
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
// *       Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// *       Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// *       Neither the name of Industrial Light & Magic nor the names of
// its contributors may be used to endorse or promote products derived
19 20
// from this software without specific prior written permission.
//
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
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
///////////////////////////////////////////////////////////////////////////


//-----------------------------------------------------------------------------
//
//	Miscellaneous stuff related to tiled files
//
//-----------------------------------------------------------------------------

#include <ImfTiledMisc.h>
#include "Iex.h"
#include <ImfMisc.h>
#include <ImfChannelList.h>
46
#include <algorithm> // for std::max()
47 48 49 50 51 52 53 54 55 56 57 58


namespace Imf {

using Imath::Box2i;
using Imath::V2i;


int
levelSize (int min, int max, int l, LevelRoundingMode rmode)
{
    if (l < 0)
59
    throw Iex::ArgExc ("Argument not in valid range.");
60 61 62 63 64 65

    int a = max - min + 1;
    int b = (1 << l);
    int size = a / b;

    if (rmode == ROUND_UP && size * b < a)
66
    size += 1;
67 68 69 70 71 72 73

    return std::max (size, 1);
}


Box2i
dataWindowForLevel (const TileDescription &tileDesc,
74 75 76
            int minX, int maxX,
            int minY, int maxY,
            int lx, int ly)
77 78 79 80
{
    V2i levelMin = V2i (minX, minY);

    V2i levelMax = levelMin +
81 82
           V2i (levelSize (minX, maxX, lx, tileDesc.roundingMode) - 1,
            levelSize (minY, maxY, ly, tileDesc.roundingMode) - 1);
83 84 85 86 87 88 89

    return Box2i(levelMin, levelMax);
}


Box2i
dataWindowForTile (const TileDescription &tileDesc,
90 91 92 93
           int minX, int maxX,
           int minY, int maxY,
           int dx, int dy,
           int lx, int ly)
94 95
{
    V2i tileMin = V2i (minX + dx * tileDesc.xSize,
96
               minY + dy * tileDesc.ySize);
97 98 99 100

    V2i tileMax = tileMin + V2i (tileDesc.xSize - 1, tileDesc.ySize - 1);

    V2i levelMax = dataWindowForLevel
101
               (tileDesc, minX, maxX, minY, maxY, lx, ly).max;
102 103

    tileMax = V2i (std::min (tileMax[0], levelMax[0]),
104
           std::min (tileMax[1], levelMax[1]));
105 106 107 108 109 110 111 112 113 114 115 116 117

    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();
118 119
     c != channels.end();
     ++c)
120
    {
121
    bytesPerPixel += pixelTypeSize (c.channel().type);
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
    }

    return bytesPerPixel;
}


namespace {

int
floorLog2 (int x)
{
    //
    // For x > 0, floorLog2(y) returns floor(log(x)/log(2)).
    //

    int y = 0;

    while (x > 1)
    {
141 142
    y +=  1;
    x >>= 1;
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
    }

    return y;
}


int
ceilLog2 (int x)
{
    //
    // For x > 0, ceilLog2(y) returns ceil(log(x)/log(2)).
    //

    int y = 0;
    int r = 0;

    while (x > 1)
    {
161 162
    if (x & 1)
        r = 1;
163

164 165
    y +=  1;
    x >>= 1;
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
    }

    return y + r;
}


int
roundLog2 (int x, LevelRoundingMode rmode)
{
    return (rmode == ROUND_DOWN)? floorLog2 (x): ceilLog2 (x);
}


int
calculateNumXLevels (const TileDescription& tileDesc,
181 182
             int minX, int maxX,
             int minY, int maxY)
183 184 185 186 187 188 189
{
    int num = 0;

    switch (tileDesc.mode)
    {
      case ONE_LEVEL:

190 191
    num = 1;
    break;
192 193 194

      case MIPMAP_LEVELS:

195 196 197 198 199
    {
      int w = maxX - minX + 1;
      int h = maxY - minY + 1;
      num = roundLog2 (std::max (w, h), tileDesc.roundingMode) + 1;
    }
200 201 202 203
        break;

      case RIPMAP_LEVELS:

204 205 206 207 208
    {
      int w = maxX - minX + 1;
      num = roundLog2 (w, tileDesc.roundingMode) + 1;
    }
    break;
209 210 211

      default:

212
    throw Iex::ArgExc ("Unknown LevelMode format.");
213 214 215 216 217 218 219 220
    }

    return num;
}


int
calculateNumYLevels (const TileDescription& tileDesc,
221 222
             int minX, int maxX,
             int minY, int maxY)
223 224 225 226 227 228 229
{
    int num = 0;

    switch (tileDesc.mode)
    {
      case ONE_LEVEL:

230 231
    num = 1;
    break;
232 233 234

      case MIPMAP_LEVELS:

235 236 237 238 239
    {
      int w = maxX - minX + 1;
      int h = maxY - minY + 1;
      num = roundLog2 (std::max (w, h), tileDesc.roundingMode) + 1;
    }
240 241 242 243
        break;

      case RIPMAP_LEVELS:

244 245 246 247 248
    {
      int h = maxY - minY + 1;
      num = roundLog2 (h, tileDesc.roundingMode) + 1;
    }
    break;
249 250 251

      default:

252
    throw Iex::ArgExc ("Unknown LevelMode format.");
253 254 255 256 257 258 259 260
    }

    return num;
}


void
calculateNumTiles (int *numTiles,
261 262 263 264
           int numLevels,
           int min, int max,
           int size,
           LevelRoundingMode rmode)
265 266 267
{
    for (int i = 0; i < numLevels; i++)
    {
268
    numTiles[i] = (levelSize (min, max, i, rmode) + size - 1) / size;
269 270 271 272 273 274 275 276
    }
}

} // namespace


void
precalculateTileInfo (const TileDescription& tileDesc,
277 278 279 280
              int minX, int maxX,
              int minY, int maxY,
              int *&numXTiles, int *&numYTiles,
              int &numXLevels, int &numYLevels)
281 282 283
{
    numXLevels = calculateNumXLevels(tileDesc, minX, maxX, minY, maxY);
    numYLevels = calculateNumYLevels(tileDesc, minX, maxX, minY, maxY);
284

285 286 287 288
    numXTiles = new int[numXLevels];
    numYTiles = new int[numYLevels];

    calculateNumTiles (numXTiles,
289 290 291 292
               numXLevels,
               minX, maxX,
               tileDesc.xSize,
               tileDesc.roundingMode);
293 294

    calculateNumTiles (numYTiles,
295 296 297 298
               numYLevels,
               minY, maxY,
               tileDesc.ySize,
               tileDesc.roundingMode);
299 300 301 302
}


} // namespace Imf