Ejemplo n.º 1
0
void	
OutputFile::writePixels (int numScanLines)
{
    try
    {
        Lock lock (*_data);

	if (_data->slices.size() == 0)
	    throw Iex::ArgExc ("No frame buffer specified "
			       "as pixel data source.");

        //
        // Maintain two iterators:
        //     nextWriteBuffer: next linebuffer to be written to the file
        //     nextCompressBuffer: next linebuffer to compress
        //

        int first = (_data->currentScanLine - _data->minY) /
                         _data->linesInBuffer;

        int nextWriteBuffer = first;
        int nextCompressBuffer;
        int stop;
        int step;
        int scanLineMin;
        int scanLineMax;

        {
            //
            // Create a task group for all line buffer tasks. When the
            // taskgroup goes out of scope, the destructor waits until
	    // all tasks are complete.
            //
            
            TaskGroup taskGroup;
            
            //
            // Determine the range of lineBuffers that intersect the scan
	    // line range.  Then add the initial compression tasks to the
	    // thread pool.  We always add in at least one task but the
	    // individual task might not do anything if numScanLines == 0.
            //
    
            if (_data->lineOrder == INCREASING_Y)
            {
                int last = (_data->currentScanLine + (numScanLines - 1) -
                            _data->minY) / _data->linesInBuffer;
    
                scanLineMin = _data->currentScanLine;
                scanLineMax = _data->currentScanLine + numScanLines - 1;
    
                int numTasks = max (min ((int)_data->lineBuffers.size(),
                                         last - first + 1),
				    1);

                for (int i = 0; i < numTasks; i++)
		{
                    ThreadPool::addGlobalTask
                        (new LineBufferTask (&taskGroup, _data, first + i,
                                             scanLineMin, scanLineMax));
		}
    
                nextCompressBuffer = first + numTasks;
                stop = last + 1;
                step = 1;
            }
            else
            {
                int last = (_data->currentScanLine - (numScanLines - 1) -
                            _data->minY) / _data->linesInBuffer;
    
                scanLineMax = _data->currentScanLine;
                scanLineMin = _data->currentScanLine - numScanLines + 1;
    
                int numTasks = max (min ((int)_data->lineBuffers.size(),
                                         first - last + 1),
				    1);

                for (int i = 0; i < numTasks; i++)
		{
                    ThreadPool::addGlobalTask
                        (new LineBufferTask (&taskGroup, _data, first - i,
                                             scanLineMin, scanLineMax));
		}
    
                nextCompressBuffer = first - numTasks;
                stop = last - 1;
                step = -1;
            }
            
            while (true)
            {
                if (_data->missingScanLines <= 0)
                {
                    throw Iex::ArgExc ("Tried to write more scan lines "
                                       "than specified by the data window.");
                }
    
		//
                // Wait until the next line buffer is ready to be written
		//

                LineBuffer *writeBuffer =
		    _data->getLineBuffer (nextWriteBuffer);

                writeBuffer->wait();
                
                int numLines = writeBuffer->scanLineMax - 
                               writeBuffer->scanLineMin + 1;

                _data->missingScanLines -= numLines;
    
		//
                // If the line buffer is only partially full, then it is
		// not complete and we cannot write it to disk yet.
		//

                if (writeBuffer->partiallyFull)
                {
                    _data->currentScanLine = _data->currentScanLine +
                                             step * numLines;
                    writeBuffer->post();
    
                    return;
                }
    
		//
                // Write the line buffer
		//

                writePixelData (_data, writeBuffer);
                nextWriteBuffer += step;

                _data->currentScanLine = _data->currentScanLine +
                                         step * numLines;
    
                #ifdef DEBUG
    
                    assert (_data->currentScanLine ==
                            ((_data->lineOrder == INCREASING_Y) ?
                             writeBuffer->scanLineMax + 1:
                             writeBuffer->scanLineMin - 1));
    
                #endif
                
		//
                // Release the lock on the line buffer
		//

                writeBuffer->post();
                
		//
                // If this was the last line buffer in the scanline range
		//

                if (nextWriteBuffer == stop)
                    break;
    
		//
                // If there are no more line buffers to compress,
                // then only continue to write out remaining lineBuffers
		//

                if (nextCompressBuffer == stop)
                    continue;
    
		//
                // Add nextCompressBuffer as a compression task
		//

                ThreadPool::addGlobalTask
                    (new LineBufferTask (&taskGroup, _data, nextCompressBuffer,
                                         scanLineMin, scanLineMax));
                
		//
                // Update the next line buffer we need to compress
		//

                nextCompressBuffer += step;
            }
        
	    //
            // Finish all tasks
	    //
        }
        
	//
	// Exeption handling:
	//
	// LineBufferTask::execute() may have encountered exceptions, but
	// those exceptions occurred in another thread, not in the thread
	// that is executing this call to OutputFile::writePixels().
	// LineBufferTask::execute() has caught all exceptions and stored
	// the exceptions' what() strings in the line buffers.
	// Now we check if any line buffer contains a stored exception; if
	// this is the case then we re-throw the exception in this thread.
	// (It is possible that multiple line buffers contain stored
	// exceptions.  We re-throw the first exception we find and
	// ignore all others.)
	//

	const string *exception = 0;

        for (int i = 0; i < _data->lineBuffers.size(); ++i)
	{
            LineBuffer *lineBuffer = _data->lineBuffers[i];

	    if (lineBuffer->hasException && !exception)
		exception = &lineBuffer->exception;

	    lineBuffer->hasException = false;
	}

	if (exception)
	    throw Iex::IoExc (*exception);
    }
    catch (Iex::BaseExc &e)
    {
	REPLACE_EXC (e, "Failed to write pixel data to image "
		        "file \"" << fileName() << "\". " << e);
	throw;
    }
}