[13] | 1 | // Copyright (C) 2011 Kornel Kisielewicz
|
---|
| 2 | // This file is part of NV Libraries.
|
---|
| 3 | // For conditions of distribution and use, see copyright notice in nv.hh
|
---|
| 4 |
|
---|
[89] | 5 | #include "nv/gfx/image.hh"
|
---|
[13] | 6 |
|
---|
[90] | 7 | #include <algorithm>
|
---|
[13] | 8 |
|
---|
| 9 | using namespace nv;
|
---|
| 10 |
|
---|
| 11 | image::image( glm::ivec2 size, size_t depth )
|
---|
| 12 | : m_size( size ), m_depth( depth ), m_data( nullptr )
|
---|
| 13 | {
|
---|
[128] | 14 | m_data = new uint8[ static_cast<uint32>( m_size.x * m_size.y ) * m_depth ];
|
---|
[13] | 15 | }
|
---|
| 16 |
|
---|
[90] | 17 | image::image( image_data* data )
|
---|
| 18 | : m_size( data->get_size() ), m_depth( data->get_depth() ), m_data( data->release_data() )
|
---|
| 19 | {
|
---|
| 20 | NV_ASSERT( m_data, "image created from empty image_data!" );
|
---|
| 21 | }
|
---|
| 22 |
|
---|
| 23 |
|
---|
[21] | 24 | image::image( glm::ivec2 size, size_t depth, const uint8 * data, bool reversed )
|
---|
| 25 | : m_size( size ), m_depth( depth ), m_data( nullptr )
|
---|
| 26 | {
|
---|
[121] | 27 | sint32 bsize = m_size.x * m_size.y * static_cast<sint32>( m_depth );
|
---|
| 28 | m_data = new uint8[ bsize ];
|
---|
[21] | 29 |
|
---|
| 30 | if ( reversed )
|
---|
| 31 | {
|
---|
[121] | 32 | sint32 bline = m_size.x * static_cast<sint32>( m_depth );
|
---|
[90] | 33 | for( int i = 0; i < m_size.y; ++i )
|
---|
[21] | 34 | {
|
---|
[90] | 35 | std::copy( data + i * bline, data + (i + 1) * bline, m_data + bsize - ( i + 1 ) * bline );
|
---|
[21] | 36 | }
|
---|
| 37 |
|
---|
| 38 | }
|
---|
| 39 | else
|
---|
| 40 | {
|
---|
[90] | 41 | std::copy( data, data + bsize, m_data );
|
---|
[21] | 42 | }
|
---|
| 43 | }
|
---|
| 44 |
|
---|
[13] | 45 | void image::fill( uint8 value )
|
---|
| 46 | {
|
---|
[121] | 47 | std::fill( m_data, m_data + m_size.x * m_size.y * (int)m_depth, value );
|
---|
[13] | 48 | }
|
---|
| 49 |
|
---|
[121] | 50 | void image::set_region( region r, const uint8 * data, int stride )
|
---|
[13] | 51 | {
|
---|
[121] | 52 | if ( stride == 0 ) stride = r.size.x * static_cast<sint32>( m_depth );
|
---|
[90] | 53 |
|
---|
[121] | 54 | sint32 bpos = (r.pos.y*m_size.x + r.pos.x ) * static_cast<sint32>( m_depth );
|
---|
| 55 | sint32 bline = m_size.x*static_cast<sint32>( m_depth );
|
---|
[13] | 56 |
|
---|
[26] | 57 | for( int i = 0; i < r.size.y; ++i )
|
---|
| 58 | {
|
---|
[90] | 59 | // TODO: test if same as old:
|
---|
| 60 | // memcpy( m_data+((r.pos.y+i)*m_size.x + r.pos.x ) * m_depth,
|
---|
| 61 | // data + (i*stride), r.size.x * m_depth );
|
---|
| 62 | std::copy( data + i*stride, data + (i+1)*stride, m_data + bpos + bline * i );
|
---|
[26] | 63 | }
|
---|
[13] | 64 | }
|
---|
| 65 |
|
---|
| 66 | image::~image()
|
---|
| 67 | {
|
---|
| 68 | delete[] m_data;
|
---|
| 69 | }
|
---|
| 70 |
|
---|
[21] | 71 |
|
---|