[319] | 1 | // Copyright (C) 2011-2014 ChaosForge Ltd
|
---|
[13] | 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 |
|
---|
[228] | 50 | void image::fill( region r, uint8 value, int stride )
|
---|
| 51 | {
|
---|
| 52 | if ( stride == 0 ) stride = r.size.x * static_cast<sint32>( m_depth );
|
---|
| 53 |
|
---|
| 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 );
|
---|
| 56 |
|
---|
| 57 | for( int i = 0; i < r.size.y; ++i )
|
---|
| 58 | {
|
---|
| 59 | // TODO: test
|
---|
| 60 | std::fill( m_data + bpos + bline * i, m_data + bpos + bline * i + stride, value );
|
---|
| 61 | }
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 |
|
---|
[121] | 65 | void image::set_region( region r, const uint8 * data, int stride )
|
---|
[13] | 66 | {
|
---|
[121] | 67 | if ( stride == 0 ) stride = r.size.x * static_cast<sint32>( m_depth );
|
---|
[90] | 68 |
|
---|
[121] | 69 | sint32 bpos = (r.pos.y*m_size.x + r.pos.x ) * static_cast<sint32>( m_depth );
|
---|
| 70 | sint32 bline = m_size.x*static_cast<sint32>( m_depth );
|
---|
[13] | 71 |
|
---|
[26] | 72 | for( int i = 0; i < r.size.y; ++i )
|
---|
| 73 | {
|
---|
[90] | 74 | // TODO: test if same as old:
|
---|
| 75 | // memcpy( m_data+((r.pos.y+i)*m_size.x + r.pos.x ) * m_depth,
|
---|
| 76 | // data + (i*stride), r.size.x * m_depth );
|
---|
| 77 | std::copy( data + i*stride, data + (i+1)*stride, m_data + bpos + bline * i );
|
---|
[26] | 78 | }
|
---|
[13] | 79 | }
|
---|
| 80 |
|
---|
[228] | 81 | void image::set_region( region r, const image_data* idata )
|
---|
| 82 | {
|
---|
| 83 | if ( idata->get_depth() == m_depth )
|
---|
| 84 | {
|
---|
| 85 | set_region( r, idata->get_data() );
|
---|
| 86 | return;
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | fill( r, 255 );
|
---|
| 90 |
|
---|
| 91 | sint32 bpos = (r.pos.y*m_size.x + r.pos.x ) * static_cast<sint32>( m_depth );
|
---|
| 92 | sint32 bline = m_size.x*static_cast<sint32>( m_depth );
|
---|
| 93 |
|
---|
| 94 | const uint8* data = idata->get_data();
|
---|
| 95 | sint32 depth = idata->get_depth();
|
---|
| 96 | sint32 dstride = r.size.x * static_cast<sint32>( depth );
|
---|
| 97 |
|
---|
| 98 | for( int y = 0; y < r.size.y; ++y )
|
---|
| 99 | {
|
---|
| 100 | sint32 pos = bpos + bline * y;
|
---|
| 101 | for( int x = 0; x < r.size.x; ++x )
|
---|
| 102 | {
|
---|
| 103 | sint32 xy = pos + x * m_depth;
|
---|
| 104 | for( int e = 0; e < depth; ++e )
|
---|
| 105 | {
|
---|
| 106 | m_data[ xy + e ] = data[ y*dstride + x * depth + e ];
|
---|
| 107 | }
|
---|
| 108 | }
|
---|
| 109 | }
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 |
|
---|
[13] | 113 | image::~image()
|
---|
| 114 | {
|
---|
| 115 | delete[] m_data;
|
---|
| 116 | }
|
---|
| 117 |
|
---|
[21] | 118 |
|
---|