1 /*
2     RawSpeed - RAW file decoder.
3 
4     Copyright (C) 2017 Roman Lebedev
5 
6     This library is free software; you can redistribute it and/or
7     modify it under the terms of the GNU Lesser General Public
8     License as published by the Free Software Foundation; either
9     version 2 of the License, or (at your option) any later version.
10 
11     This library is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14     Lesser General Public License for more details.
15 
16     You should have received a copy of the GNU Lesser General Public
17     License along with this library; if not, write to the Free Software
18     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20 
21 #include "fuzz/Common.h"
22 #include "common/Point.h"              // for iPoint2D
23 #include "common/RawImage.h"           // for RawImage, RawImageData, RawIm...
24 #include "common/RawspeedException.h"  // for ThrowRSE
25 #include "io/ByteStream.h"             // for ByteStream
26 #include "io/IOException.h"            // for ThrowIOE
27 #include "metadata/ColorFilterArray.h" // for ColorFilterArray, CFAColor
28 #include <cassert>                     // for assert
29 #include <cstdint>                     // for uint32_t
30 #include <limits>                      // for numeric_limits
31 
CreateRawImage(rawspeed::ByteStream & bs)32 rawspeed::RawImage CreateRawImage(rawspeed::ByteStream& bs) {
33   const uint32_t width = bs.getU32();
34   const uint32_t height = bs.getU32();
35   const uint32_t type = bs.getU32();
36   const uint32_t cpp = bs.getU32();
37   const uint32_t isCFA = bs.getU32();
38 
39   if (type != static_cast<uint32_t>(rawspeed::RawImageType::UINT16) &&
40       type != static_cast<uint32_t>(rawspeed::RawImageType::F32))
41     ThrowRSE("Unknown image type: %u", type);
42 
43   rawspeed::RawImage mRaw(
44       rawspeed::RawImage::create(static_cast<rawspeed::RawImageType>(type)));
45 
46   mRaw->dim =
47       rawspeed::iPoint2D(static_cast<rawspeed::iPoint2D::value_type>(width),
48                          static_cast<rawspeed::iPoint2D::value_type>(height));
49   mRaw->setCpp(cpp);
50   mRaw->isCFA = isCFA;
51 
52   return mRaw;
53 }
54 
CreateCFA(rawspeed::ByteStream & bs)55 rawspeed::ColorFilterArray CreateCFA(rawspeed::ByteStream& bs) {
56   const uint32_t cfaWidth = bs.getU32();
57   const uint32_t cfaHeight = bs.getU32();
58 
59   rawspeed::ColorFilterArray cfa;
60 
61   if (cfaHeight &&
62       cfaWidth > std::numeric_limits<decltype(cfaWidth)>::max() / cfaHeight)
63     ThrowIOE("Integer overflow when calculating CFA area");
64   (void)bs.check(cfaWidth * cfaHeight, 4);
65 
66   cfa.setSize(rawspeed::iPoint2D(cfaWidth, cfaHeight));
67 
68   for (auto x = 0U; x < cfaWidth; x++) {
69     for (auto y = 0U; y < cfaHeight; y++) {
70       const uint32_t color = bs.getU32();
71       if (color >= static_cast<uint32_t>(rawspeed::CFAColor::END))
72         ThrowRSE("Unknown color: %u", color);
73 
74       cfa.setColorAt(rawspeed::iPoint2D(x, y),
75                      static_cast<rawspeed::CFAColor>(color));
76     }
77   }
78 
79   return cfa;
80 }
81