TuttleOFX  1
ElementReadStream.cpp
Go to the documentation of this file.
00001 // -*- mode: C++; tab-width: 4 -*-
00002 // vi: ts=4
00003 
00004 /*
00005  * Copyright (c) 2009, Patrick A. Palmer.
00006  * All rights reserved.
00007  *
00008  * Redistribution and use in source and binary forms, with or without 
00009  * modification, are permitted provided that the following conditions are met:
00010  *
00011  *   - Redistributions of source code must retain the above copyright notice,
00012  *     this list of conditions and the following disclaimer.
00013  *
00014  *   - Redistributions in binary form must reproduce the above copyright
00015  *     notice, this list of conditions and the following disclaimer in the
00016  *     documentation and/or other materials provided with the distribution.
00017  *
00018  *   - Neither the name of Patrick A. Palmer nor the names of its
00019  *     contributors may be used to endorse or promote products derived from
00020  *     this software without specific prior written permission.
00021  *
00022  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
00023  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
00024  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
00025  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
00026  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
00027  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
00028  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
00029  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
00030  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
00031  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
00032  * POSSIBILITY OF SUCH DAMAGE.
00033  */
00034 
00035 #include "DPX.h"
00036 #include "EndianSwap.h"
00037 #include "ElementReadStream.h"
00038 
00039 
00040 
00041 dpx::ElementReadStream::ElementReadStream(InStream *fd) : fd(fd) 
00042 {
00043 }
00044 
00045 
00046 dpx::ElementReadStream::~ElementReadStream()
00047 {
00048 }
00049 
00050 
00051 void dpx::ElementReadStream::Reset()
00052 {
00053 }
00054 
00055 
00056 bool dpx::ElementReadStream::Read(const dpx::Header &dpxHeader, const int element, const long offset, void * buf, const size_t size)
00057 {
00058         long position = dpxHeader.DataOffset(element) + offset;
00059 
00060         // seek to the memory position                                                           
00061         if (this->fd->Seek(position, InStream::kStart) == false)
00062                 return false;
00063                                 
00064         // read in the data, calculate buffer offset
00065         if (this->fd->Read(buf, size) != size)
00066                 return false;
00067         
00068         // swap the bytes if different byte order       
00069         this->EndianDataCheck(dpxHeader, element, buf, size);
00070 
00071         return true;
00072 }
00073 
00074 
00075 bool dpx::ElementReadStream::ReadDirect(const dpx::Header &dpxHeader, const int element, const long offset, void * buf, const size_t size)
00076 {
00077         long position = dpxHeader.DataOffset(element) + offset;
00078 
00079         // seek to the memory position                                                           
00080         if (this->fd->Seek(position, InStream::kStart) == false)
00081                 return false;
00082                                 
00083         // read in the data, calculate buffer offset
00084         if (this->fd->ReadDirect(buf, size) != size)
00085                 return false;
00086         
00087         // swap the bytes if different byte order       
00088         this->EndianDataCheck(dpxHeader, element, buf, size);
00089 
00090         return true;
00091 }
00092 
00093 
00094 
00095 void dpx::ElementReadStream::EndianDataCheck(const dpx::Header &dpxHeader, const int element, void *buf, const size_t size)
00096 {
00097         if (dpxHeader.RequiresByteSwap())
00098         {
00099                 switch (dpxHeader.BitDepth(element))
00100                 {
00101                 case 8:
00102                         break;
00103                 case 12:
00104                         if (dpxHeader.ImagePacking(element) == dpx::kPacked)
00105                                 dpx::EndianSwapImageBuffer<dpx::kInt>(buf, size / sizeof(U32));
00106                         else
00107                                 dpx::EndianSwapImageBuffer<dpx::kWord>(buf, size / sizeof(U16));
00108                         break;
00109                 case 16:
00110                         dpx::EndianSwapImageBuffer<dpx::kWord>(buf, size / sizeof(U16));
00111                         break;
00112                 default:                // 10-bit, 32-bit, 64-bit
00113                         dpx::EndianSwapImageBuffer<dpx::kInt>(buf, size / sizeof(U32));
00114                 }       
00115         }
00116 }
00117