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 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
|
///////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2014, Industrial Light & Magic, a division of Lucas
// Digital Ltd. LLC
//
// All rights reserved.
//
// 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
// from this software without specific prior written permission.
//
// 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.
//
///////////////////////////////////////////////////////////////////////////
//----------------------------------------------------------------------------
//
// class FlatImageChannel
//
//----------------------------------------------------------------------------
#include "ImfSampleCountChannel.h"
#include "ImfDeepImageLevel.h"
#include "ImfImage.h"
#include <Iex.h>
using namespace IMATH_NAMESPACE;
using namespace IEX_NAMESPACE;
using namespace std;
OPENEXR_IMF_INTERNAL_NAMESPACE_SOURCE_ENTER
namespace {
unsigned int
roundListSizeUp (unsigned int n)
{
//
// Calculate the number of samples to be allocated for a sample list
// with n entries by rounding n up to the next power of two.
//
if (n == 0)
return 0;
unsigned int s = 1;
while (s < n)
s <<= 1;
return s;
}
size_t
roundBufferSizeUp (size_t n)
{
//
// Calculate the number of samples to be allocated for n samples.
// Leave some extra space for new samples that may be added later.
//
return n + n / 2;
}
} // namespace
SampleCountChannel::SampleCountChannel (DeepImageLevel& level):
ImageChannel (level, 1, 1, false),
_numSamples (0),
_base (0),
_sampleListSizes (0),
_sampleListPositions (0),
_totalNumSamples (0),
_totalSamplesOccupied (0),
_sampleBufferSize (0)
{
resize();
}
SampleCountChannel::~SampleCountChannel ()
{
delete [] _numSamples;
delete [] _sampleListSizes;
delete [] _sampleListPositions;
}
PixelType
SampleCountChannel::pixelType () const
{
return UINT;
}
Slice
SampleCountChannel::slice () const
{
return Slice (UINT, // type
(char *) _base, // base
sizeof (unsigned int), // xStride
pixelsPerRow() * sizeof (unsigned int), // yStride
xSampling(),
ySampling());
}
DeepImageLevel &
SampleCountChannel::deepLevel ()
{
return static_cast <DeepImageLevel &> (level());
}
const DeepImageLevel &
SampleCountChannel::deepLevel () const
{
return static_cast <const DeepImageLevel &> (level());
}
void
SampleCountChannel::set (int x, int y, unsigned int newNumSamples)
{
//
// Set the number of samples for pixel (x,y) to newNumSamples.
// Compute the position of the pixel in the the various
// arrays that describe it.
//
size_t i = (_base + y * pixelsPerRow() + x) - _numSamples;
if (newNumSamples <= _numSamples[i])
{
//
// The number of samples for the pixel becomes smaller.
// Save the new number of samples.
//
_totalNumSamples -= _numSamples[i] - newNumSamples;
_numSamples[i] = newNumSamples;
return;
}
if (newNumSamples <= _sampleListSizes[i])
{
//
// The number of samples for the pixel becomes larger, but the new
// number of samples still fits into the space that has been allocated
// for the sample list. Set the new samples at the end of the list to
// zero.
//
deepLevel().setSamplesToZero (i,
_numSamples[i],
newNumSamples);
_totalNumSamples += newNumSamples - _numSamples[i];
_numSamples[i] = newNumSamples;
return;
}
int newSampleListSize = roundListSizeUp (newNumSamples);
if (_totalSamplesOccupied + newSampleListSize <= _sampleBufferSize)
{
//
// The number of samples in the pixel no longer fits into the
// space that has been allocated for the sample list, but there
// is space available at the end of the sample buffer. Allocate
// space for a new list at the end of the sample buffer, and move
// the sample list from its old location to its new, larger place.
//
deepLevel().moveSampleList
(i, _numSamples[i], newNumSamples, _totalSamplesOccupied);
_sampleListPositions[i] = _totalSamplesOccupied;
_totalSamplesOccupied += newSampleListSize;
_totalNumSamples += newNumSamples - _numSamples[i];
_numSamples[i] = newNumSamples;
return;
}
//
// The new number of samples no longer fits into the space that has
// been allocated for the sample list, and there is not enough room
// at the end of the sample buffer for a new, larger sample list.
// Allocate an entirely new sample buffer, and move all existing
// sample lists into it.
//
unsigned int * oldNumSamples = 0;
size_t * oldSampleListPositions = 0;
try
{
_totalNumSamples += newNumSamples - _numSamples[i];
oldNumSamples = _numSamples;
_numSamples = new unsigned int [numPixels()];
resetBasePointer();
oldSampleListPositions = _sampleListPositions;
_sampleListPositions = new size_t [numPixels()];
_totalSamplesOccupied = 0;
for (size_t j = 0; j < numPixels(); ++j)
{
if (j == i)
_numSamples[j] = newNumSamples;
else
_numSamples[j] = oldNumSamples[j];
_sampleListPositions[j] = _totalSamplesOccupied;
_sampleListSizes[j] = roundListSizeUp (_numSamples[j]);
_totalSamplesOccupied += _sampleListSizes[j];
}
_sampleBufferSize = roundBufferSizeUp (_totalSamplesOccupied);
deepLevel().moveSamplesToNewBuffer (oldNumSamples,
_numSamples,
_sampleListPositions);
delete [] oldNumSamples;
delete [] oldSampleListPositions;
}
catch (...)
{
delete [] oldNumSamples;
delete [] oldSampleListPositions;
level().image().resize (Box2i (V2i (0, 0), V2i (-1, -1)));
throw;
}
}
void
SampleCountChannel::set (int r, unsigned int newNumSamples[])
{
int x = level().dataWindow().min.x;
int y = r + level().dataWindow().min.x;
for (int i = 0; i < pixelsPerRow(); ++i, ++x)
set (x, y, newNumSamples[i]);
}
void
SampleCountChannel::clear ()
{
try
{
for (size_t i = 0; i < numPixels(); ++i)
{
_numSamples[i] = 0;
_sampleListSizes[i] = 0;
_sampleListPositions[i] = 0;
}
_totalNumSamples = 0;
_totalSamplesOccupied = 0;
_sampleBufferSize = roundBufferSizeUp (_totalSamplesOccupied);
deepLevel().initializeSampleLists();
}
catch (...)
{
level().image().resize (Box2i (V2i (0, 0), V2i (-1, -1)));
throw;
}
}
unsigned int *
SampleCountChannel::beginEdit ()
{
return _numSamples;
}
void
SampleCountChannel::endEdit ()
{
try
{
_totalNumSamples = 0;
_totalSamplesOccupied = 0;
for (size_t i = 0; i < numPixels(); ++i)
{
_sampleListSizes[i] = roundListSizeUp (_numSamples[i]);
_sampleListPositions[i] = _totalSamplesOccupied;
_totalNumSamples += _numSamples[i];
_totalSamplesOccupied += _sampleListSizes[i];
}
_sampleBufferSize = roundBufferSizeUp (_totalSamplesOccupied);
deepLevel().initializeSampleLists();
}
catch (...)
{
level().image().resize (Box2i (V2i (0, 0), V2i (-1, -1)));
throw;
}
}
void
SampleCountChannel::resize ()
{
ImageChannel::resize();
delete [] _numSamples;
delete [] _sampleListSizes;
delete [] _sampleListPositions;
_numSamples = 0; // set to 0 to prevent double
_sampleListSizes = 0; // deletion in case of an exception
_sampleListPositions = 0;
_numSamples = new unsigned int [numPixels()];
_sampleListSizes = new unsigned int [numPixels()];
_sampleListPositions = new size_t [numPixels()];
resetBasePointer();
for (size_t i = 0; i < numPixels(); ++i)
{
_numSamples[i] = 0;
_sampleListSizes[i] = 0;
_sampleListPositions[i] = 0;
}
_totalNumSamples = 0;
_totalSamplesOccupied = 0;
_sampleBufferSize = roundBufferSizeUp (_totalSamplesOccupied);
}
void
SampleCountChannel::resetBasePointer ()
{
_base = _numSamples -
level().dataWindow().min.y * pixelsPerRow() -
level().dataWindow().min.x;
}
OPENEXR_IMF_INTERNAL_NAMESPACE_SOURCE_EXIT
|