libcruft-util/image.cpp

241 lines
5.9 KiB
C++
Raw Normal View History

2012-05-16 15:03:49 +10:00
/*
2015-04-13 18:05:28 +10:00
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
2012-05-16 15:03:49 +10:00
*
2015-04-13 18:05:28 +10:00
* http://www.apache.org/licenses/LICENSE-2.0
2015-02-04 15:59:06 +11:00
*
2015-04-13 18:05:28 +10:00
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
2012-05-16 15:03:49 +10:00
*
2015-02-04 15:59:06 +11:00
* Copyright 2011-2015 Danny Robson <danny@nerdcruft.net>
2012-05-16 15:03:49 +10:00
*/
#include "image.hpp"
#include "debug.hpp"
2012-05-16 15:03:49 +10:00
using util::image::buffer;
2015-05-26 16:25:21 +10:00
//-----------------------------------------------------------------------------
template <typename T>
util::image::buffer<T>::buffer (util::extent2u _size):
buffer<T> (_size.w, _size.h)
{ ; }
2015-02-04 15:59:06 +11:00
//-----------------------------------------------------------------------------
template <typename T>
util::image::buffer<T>::buffer (size_t _w, size_t _h, size_t _s):
w (_w),
h (_h),
s (_s),
m_data (std::make_unique<T[]> (_h * _s))
2015-02-04 15:59:06 +11:00
{
CHECK_NEQ (w * h, 0);
CHECK_GE (s, w);
}
//-----------------------------------------------------------------------------
template <typename T>
util::image::buffer<T>::buffer (size_t _w, size_t _h):
buffer<T> (_w, _h, _w)
{ ; }
//-----------------------------------------------------------------------------
template <typename T>
util::image::buffer<T>::buffer (size_t _w,
size_t _h,
size_t _s,
std::unique_ptr<T[]> &&_data):
w (_w),
h (_h),
s (_s),
m_data (std::move (_data))
2015-02-04 15:59:06 +11:00
{
CHECK_NEQ (w * h, 0);
CHECK_GE (s, w);
}
//-----------------------------------------------------------------------------
template <typename T>
template <typename U>
util::image::buffer<U>
util::image::buffer<T>::alloc (void) const
{
return buffer<U> (w, h, s);
}
//-----------------------------------------------------------------------------
template <typename T, typename U>
static U
rescale (T v)
{
return v * sizeof (U) / sizeof (T);
}
//-----------------------------------------------------------------------------
template <typename T>
template <typename U>
util::image::buffer<U>
util::image::buffer<T>::clone (void) const
{
auto out = alloc<U> ();
std::transform (begin (), end (), out.begin (), renormalise<T,U>);
2015-02-04 15:59:06 +11:00
return out;
}
///////////////////////////////////////////////////////////////////////////////
template <typename T>
const T&
buffer<T>::operator[] (point<2,size_t> p) const
{
CHECK_LT (p.x, w);
CHECK_LT (p.y, h);
2015-07-23 21:18:15 +10:00
return begin ()[offset (p)];
}
//-----------------------------------------------------------------------------
template <typename T>
T&
buffer<T>::operator[] (point<2,size_t> p)
{
CHECK_LT (p.x, w);
CHECK_LT (p.y, h);
2015-07-23 21:18:15 +10:00
return begin ()[offset (p)];
}
//-----------------------------------------------------------------------------
template <typename T>
const T&
buffer<T>::operator[] (size_t idx) const
{
CHECK_LT (idx, h * s);
return begin ()[idx];
}
//-----------------------------------------------------------------------------
template <typename T>
T&
buffer<T>::operator[] (size_t idx)
{
CHECK_LT (idx, h * s);
return begin ()[idx];
}
///////////////////////////////////////////////////////////////////////////////
template <typename T>
2015-09-21 12:35:44 +10:00
T*
buffer<T>::data (void)
{
return begin ();
}
//-----------------------------------------------------------------------------
template <typename T>
2015-09-21 12:35:44 +10:00
T*
buffer<T>::begin (void)
{
return m_data.get ();
}
//-----------------------------------------------------------------------------
template <typename T>
2015-09-21 12:35:44 +10:00
T*
buffer<T>::end (void)
{
return begin () + h * s;
}
//-----------------------------------------------------------------------------
template <typename T>
2015-09-21 12:35:44 +10:00
const T*
buffer<T>::begin (void) const
{
return cbegin ();
}
//-----------------------------------------------------------------------------
template <typename T>
const T*
buffer<T>::end (void) const
{
return cend ();
}
//-----------------------------------------------------------------------------
template <typename T>
const T*
buffer<T>::data (void) const
{
return begin ();
}
//-----------------------------------------------------------------------------
template <typename T>
2015-09-21 12:35:44 +10:00
const T*
buffer<T>::cbegin (void) const
{
return m_data.get ();
}
//-----------------------------------------------------------------------------
template <typename T>
2015-09-21 12:35:44 +10:00
const T*
buffer<T>::cend (void) const
{
2015-09-21 12:35:44 +10:00
return cbegin () + h * s;
}
2015-09-21 12:35:44 +10:00
///////////////////////////////////////////////////////////////////////////////
template struct util::image::buffer<char>;
2015-02-04 15:59:06 +11:00
template struct util::image::buffer<uint8_t>;
2015-07-23 21:18:54 +10:00
template struct util::image::buffer<uint16_t>;
2015-08-18 00:00:35 +10:00
template struct util::image::buffer<uint32_t>;
template struct util::image::buffer< int32_t>;
2015-05-18 14:11:08 +10:00
template struct util::image::buffer<float>;
template struct util::image::buffer<double>;
2015-05-18 14:11:08 +10:00
2015-09-21 12:35:44 +10:00
template util::image::buffer<char> util::image::buffer<char>::alloc (void) const;
2015-02-04 15:59:06 +11:00
template util::image::buffer<uint8_t> util::image::buffer<uint8_t>::alloc (void) const;
template util::image::buffer<uint8_t> util::image::buffer<uint8_t>::clone (void) const;
2015-05-18 14:11:08 +10:00
template util::image::buffer<uint8_t> util::image::buffer<float>::clone (void) const;
template util::image::buffer<uint8_t> util::image::buffer<double>::clone (void) const;
2015-08-18 00:00:35 +10:00
template util::image::buffer<float> util::image::buffer<float>::alloc (void) const;
template util::image::buffer<uint32_t> util::image::buffer<float>::alloc (void) const;
template util::image::buffer< int32_t> util::image::buffer<float>::alloc (void) const;
template util::image::buffer<float> util::image::buffer<float>::clone (void) const;
2015-08-18 00:00:35 +10:00
template util::image::buffer<uint32_t> util::image::buffer<uint32_t>::alloc (void) const;