libcruft-util/image.cpp

212 lines
5.0 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
//-----------------------------------------------------------------------------
2015-10-20 16:53:32 +11:00
template <size_t C, typename T>
buffer<C,T>::buffer (util::extentu<2> _size):
2015-10-19 17:43:48 +11:00
m_size (_size),
m_stride (C, C * _size.w),
2015-10-20 16:53:32 +11:00
m_data (std::make_unique<T[]> (_size.area () * C))
{ ; }
2015-05-26 16:25:21 +10:00
2015-02-04 15:59:06 +11:00
//-----------------------------------------------------------------------------
2015-10-20 16:53:32 +11:00
template <size_t C, typename T>
2015-02-04 15:59:06 +11:00
template <typename U>
2015-10-20 16:53:32 +11:00
buffer<C,U>
buffer<C,T>::alloc (void) const
2015-02-04 15:59:06 +11:00
{
2015-10-20 16:53:32 +11:00
return buffer<C,U> (m_size);
2015-02-04 15:59:06 +11:00
}
//-----------------------------------------------------------------------------
template <typename T, typename U>
static U
rescale (T v)
{
return v * sizeof (U) / sizeof (T);
}
//-----------------------------------------------------------------------------
2015-10-20 16:53:32 +11:00
template <size_t C, typename T>
2015-02-04 15:59:06 +11:00
template <typename U>
2015-10-20 16:53:32 +11:00
util::image::buffer<C,U>
util::image::buffer<C,T>::clone (void) const
2015-02-04 15:59:06 +11:00
{
auto out = alloc<U> ();
2015-10-20 16:53:32 +11:00
auto func = renormalise<T,U>;
std::transform (begin (), end (), out.begin (), func);
2015-02-04 15:59:06 +11:00
return out;
}
///////////////////////////////////////////////////////////////////////////////
2015-10-20 16:53:32 +11:00
template <size_t C, typename T>
const T&
2015-10-20 16:53:32 +11:00
buffer<C,T>::operator[] (point<2,size_t> p) const
{
2015-10-20 16:53:32 +11:00
CHECK (util::all (p < extent ()));
2015-07-23 21:18:15 +10:00
return begin ()[offset (p)];
}
//-----------------------------------------------------------------------------
2015-10-20 16:53:32 +11:00
template <size_t C, typename T>
T&
2015-10-20 16:53:32 +11:00
buffer<C,T>::operator[] (point<2,size_t> p)
{
2015-10-20 16:53:32 +11:00
CHECK (util::all (p < extent ()));
2015-07-23 21:18:15 +10:00
return begin ()[offset (p)];
}
//-----------------------------------------------------------------------------
2015-10-20 16:53:32 +11:00
template <size_t C, typename T>
const T&
2015-10-20 16:53:32 +11:00
buffer<C,T>::operator[] (size_t idx) const
{
2015-10-19 17:43:48 +11:00
CHECK_LT (idx, size ());
return begin ()[idx];
}
//-----------------------------------------------------------------------------
2015-10-20 16:53:32 +11:00
template <size_t C, typename T>
T&
2015-10-20 16:53:32 +11:00
buffer<C,T>::operator[] (size_t idx)
{
2015-10-19 17:43:48 +11:00
CHECK_LT (idx, size ());
return begin ()[idx];
}
///////////////////////////////////////////////////////////////////////////////
2015-10-20 16:53:32 +11:00
template <size_t C, typename T>
2015-09-21 12:35:44 +10:00
T*
2015-10-20 16:53:32 +11:00
buffer<C,T>::data (void)
{
return begin ();
}
//-----------------------------------------------------------------------------
2015-10-20 16:53:32 +11:00
template <size_t C, typename T>
2015-09-21 12:35:44 +10:00
T*
2015-10-20 16:53:32 +11:00
buffer<C,T>::begin (void)
{
return m_data.get ();
}
//-----------------------------------------------------------------------------
2015-10-20 16:53:32 +11:00
template <size_t C, typename T>
2015-09-21 12:35:44 +10:00
T*
2015-10-20 16:53:32 +11:00
buffer<C,T>::end (void)
{
2015-10-19 17:43:48 +11:00
return begin () + m_size.back () * m_stride.back ();
}
//-----------------------------------------------------------------------------
2015-10-20 16:53:32 +11:00
template <size_t C, typename T>
2015-09-21 12:35:44 +10:00
const T*
2015-10-20 16:53:32 +11:00
buffer<C,T>::begin (void) const
2015-09-21 12:35:44 +10:00
{
return cbegin ();
}
//-----------------------------------------------------------------------------
2015-10-20 16:53:32 +11:00
template <size_t C, typename T>
2015-09-21 12:35:44 +10:00
const T*
2015-10-20 16:53:32 +11:00
buffer<C,T>::end (void) const
2015-09-21 12:35:44 +10:00
{
return cend ();
}
//-----------------------------------------------------------------------------
2015-10-20 16:53:32 +11:00
template <size_t C, typename T>
2015-09-21 12:35:44 +10:00
const T*
2015-10-20 16:53:32 +11:00
buffer<C,T>::data (void) const
{
return begin ();
}
//-----------------------------------------------------------------------------
2015-10-20 16:53:32 +11:00
template <size_t C, typename T>
2015-09-21 12:35:44 +10:00
const T*
2015-10-20 16:53:32 +11:00
buffer<C,T>::cbegin (void) const
{
return m_data.get ();
}
//-----------------------------------------------------------------------------
2015-10-20 16:53:32 +11:00
template <size_t C, typename T>
2015-09-21 12:35:44 +10:00
const T*
2015-10-20 16:53:32 +11:00
buffer<C,T>::cend (void) const
{
2015-10-19 17:43:48 +11:00
return cbegin () + m_size.back () * m_stride.back ();
}
2015-09-21 12:35:44 +10:00
///////////////////////////////////////////////////////////////////////////////
2015-08-18 00:00:35 +10:00
2015-10-20 16:53:32 +11:00
#define INSTANTIATE_C_T_U(C,T,U) \
template util::image::buffer<C,U> util::image::buffer<C,T>::alloc (void) const; \
template util::image::buffer<C,U> util::image::buffer<C,T>::clone (void) const; \
template util::image::buffer<C,U> util::image::buffer<C,T>::cast (void) const;
#define INSTANTIATE_C_T(C,T) \
template struct util::image::buffer<C,T>; \
INSTANTIATE_C_T_U(C,T,uint8_t) \
INSTANTIATE_C_T_U(C,T,uint16_t) \
INSTANTIATE_C_T_U(C,T,uint32_t) \
2015-10-29 10:49:43 +11:00
INSTANTIATE_C_T_U(C,T,uint64_t) \
2015-10-20 16:53:32 +11:00
INSTANTIATE_C_T_U(C,T,float) \
INSTANTIATE_C_T_U(C,T,double)
#define INSTANTIATE_C(C) \
INSTANTIATE_C_T(C,uint8_t) \
INSTANTIATE_C_T(C,uint16_t) \
INSTANTIATE_C_T(C,uint32_t) \
2015-10-29 10:49:43 +11:00
INSTANTIATE_C_T(C,uint64_t) \
2015-10-20 16:53:32 +11:00
INSTANTIATE_C_T(C,float) \
INSTANTIATE_C_T(C,double)
INSTANTIATE_C(1)
INSTANTIATE_C(2)
INSTANTIATE_C(3)
INSTANTIATE_C(4)