rawdata.cpp

00001 /*
00002  * libopenraw - rawdata.cpp
00003  *
00004  * Copyright (C) 2007 Hubert Figuiere
00005  *
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2.1 of the License, or (at your option) any later version.
00010  *
00011  * This library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with this library; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
00019  */
00020 
00021 #include <assert.h>
00022 
00023 #include <libopenraw++/rawdata.h>
00024 #include <libopenraw++/rawfile.h>
00025 
00026 namespace OpenRaw {
00027 
00028     class RawData::Private {
00029     public:
00030         RawData *self;
00031         CfaPattern cfa_pattern;
00032         uint32_t compression;
00033         uint8_t *pos;
00034         size_t offset;
00035         size_t row_offset;
00036         uint8_t slice;
00037         uint32_t sliceWidth;
00038         uint32_t sliceOffset;
00039 
00040         std::vector<uint16_t> slices;
00041 
00042         Private(RawData *_self)
00043             :   self(_self), cfa_pattern(OR_CFA_PATTERN_NONE),
00044                 compression(0),
00045                 pos(NULL), offset(0),
00046                 row_offset(0),
00047                 slice(0), sliceWidth(0),
00048                 sliceOffset(0), slices()
00049             {
00050             }
00051         void advance(size_t s);
00052         void nextSlice();
00053         void nextRow();
00054     private:
00055         Private(const Private &);
00056         Private & operator=(const Private &);
00057     };
00058 
00059 
00060     RawData *
00061     RawData::getAndExtractRawData(const char* filename, uint32_t options,
00062                                                                 or_error & err)
00063     {
00064         err = OR_ERROR_NONE;
00065         RawData *rawdata = NULL;
00066 
00067         RawFile *file = RawFile::newRawFile(filename);
00068         if (file) {
00069             rawdata = new RawData();
00070             err = file->getRawData(*rawdata, options);
00071             delete file;
00072         }
00073         else {
00074             err = OR_ERROR_CANT_OPEN; // file error
00075         }
00076         return rawdata;
00077     }
00078 
00079 
00080     RawData::RawData()
00081         : BitmapData(),
00082           d(new RawData::Private(this))
00083     {
00084 
00085     }
00086 
00087 
00088     RawData::~RawData()
00089     {
00090         delete d;
00091     }
00092 
00093 
00094     void RawData::swap(RawData & with)
00095     {
00096         BitmapData::swap(with);
00097         std::swap(this->d, with.d);
00098     }
00099 
00100     void * RawData::allocData(const size_t s)
00101     {
00102         void * p = BitmapData::allocData(s);
00103         d->pos = (uint8_t*)p;
00104         d->offset = 0;
00105         return p;
00106     }
00107 
00108 
00109     void RawData::setDimensions(uint32_t _x, uint32_t _y)
00110     {
00111         BitmapData::setDimensions(_x, _y);
00112         if(d->slices.size()) {
00113             d->sliceWidth = d->slices[0];
00114         }
00115         else {
00116             d->sliceWidth = _x;
00117         }
00118     }
00119 
00120     void RawData::setSlices(const std::vector<uint16_t> & slices)
00121     {
00122         d->slices = slices;
00123         if(slices.size()) {
00124             d->sliceWidth = slices[0];
00125         }
00126         else {
00127             d->sliceWidth = x();
00128         }
00129     }
00130 
00131     void RawData::setCfaPattern(or_cfa_pattern t)
00132     {
00133         d->cfa_pattern = t;
00134     }
00135 
00136     or_cfa_pattern RawData::cfaPattern()
00137     {
00138         return d->cfa_pattern;
00139     }
00140 
00141     void RawData::setCompression(uint32_t t)
00142     {
00143         d->compression = t;
00144     }
00145 
00146     uint32_t RawData::compression()
00147     {
00148         return d->compression;
00149     }
00150 
00151 #if 0
00152     RawData &RawData::append(uint8_t c)
00153     {
00154         assert(d->pos);
00155         assert(d->offset < d->data_size);
00156         *(d->pos) = c;
00157         advance(sizeof(c));
00158         return *this;
00159     }
00160 #endif
00161 
00162     RawData &RawData::append(uint16_t c)
00163     {
00164         assert(d->pos);
00165 //      assert(d->offset < d->data_size);
00166         *(d->pos) = c & 0xff;
00167         *(d->pos + 1) = (c >> 8) & 0xff; 
00168         d->advance(sizeof(c));
00169         return *this;
00170     }
00171     
00172 
00173     void RawData::nextRow()
00174     {
00175         d->nextRow();
00176     }
00177 
00178 
00179     void RawData::Private::nextRow()
00180     {
00181         uint32_t w = self->x() * 2;
00182         uint32_t row = offset / w;
00183         row++;
00184         if(row == self->y()) 
00185         {
00186             // on the last
00187             nextSlice();
00188             row = 0;
00189         }
00190         offset = row * w + sliceOffset * 2;
00191         pos = (uint8_t*)(self->data()) + offset;
00192         row_offset = offset;
00193     }
00194 
00195     void RawData::Private::nextSlice()
00196     {
00197         if(slices.size()) {
00198             sliceOffset += slices[slice];
00199             slice++;
00200             if(slices.size() > slice) {
00201                 sliceWidth = slices[slice];
00202             }
00203             else {
00204                 sliceWidth = 0;
00205             }
00206         }
00207     }
00208     
00209     void RawData::Private::advance(size_t s)
00210     {
00211         if(offset + s - row_offset >= sliceWidth * 2) {
00212             nextRow();
00213         }
00214         else { 
00215             pos += s;
00216             offset += s;
00217         }
00218     }
00219 
00220 }
00221 

Generated on Sat Nov 7 19:23:16 2009 for libopenraw by  doxygen 1.5.8