@@ -83,6 +83,24 @@ OIIO::TypeDesc extractStringCharPointers( const std::vector<T> &strings, std::ve
83
83
return type;
84
84
}
85
85
86
+ template <typename T>
87
+ DataPtr extractScalarData ( const OIIO::ParamValue &value )
88
+ {
89
+ const T *data = static_cast <const T *>( value.data () );
90
+ if ( value.type ().arraylen == 0 )
91
+ {
92
+ return new TypedData<T>( *data );
93
+ }
94
+ else if ( value.type ().arraylen > 0 )
95
+ {
96
+ using DataType = TypedData<std::vector<T>>;
97
+ typename DataType::Ptr result = new DataType;
98
+ result->writable ().insert ( result->writable ().end (), data, data + value.type ().arraylen );
99
+ return result;
100
+ }
101
+ return nullptr ;
102
+ }
103
+
86
104
} // namespace
87
105
88
106
namespace IECoreImage
@@ -566,15 +584,15 @@ IECore::DataPtr data( const OIIO::ParamValue &value )
566
584
{
567
585
if ( type.aggregate == TypeDesc::SCALAR )
568
586
{
569
- return new CharData ( * static_cast < const char * >( value. data () ) );
587
+ return extractScalarData< char >( value );
570
588
}
571
589
return nullptr ;
572
590
}
573
591
case TypeDesc::UCHAR :
574
592
{
575
593
if ( type.aggregate == TypeDesc::SCALAR )
576
594
{
577
- return new UCharData ( * static_cast < const unsigned char * >( value. data () ) );
595
+ return extractScalarData< unsigned char >( value );
578
596
}
579
597
return nullptr ;
580
598
}
@@ -606,30 +624,30 @@ IECore::DataPtr data( const OIIO::ParamValue &value )
606
624
{
607
625
if ( type.aggregate == TypeDesc::SCALAR )
608
626
{
609
- return new UShortData ( * static_cast < const unsigned short * >( value. data () ) );
627
+ return extractScalarData< unsigned short >( value );
610
628
}
611
629
return nullptr ;
612
630
}
613
631
case TypeDesc::SHORT :
614
632
{
615
633
if ( type.aggregate == TypeDesc::SCALAR )
616
634
{
617
- return new ShortData ( * static_cast < const short * >( value. data () ) );
635
+ return extractScalarData< short >( value );
618
636
}
619
637
return nullptr ;
620
638
}
621
639
case TypeDesc::UINT :
622
640
{
623
641
if ( type.aggregate == TypeDesc::SCALAR )
624
642
{
625
- const unsigned *typedData = static_cast <const unsigned *>( value.data () );
626
643
if ( type.vecsemantics == TypeDesc::TIMECODE )
627
644
{
645
+ const unsigned *typedData = static_cast <const unsigned *>( value.data () );
628
646
return new TimeCodeData ( Imf::TimeCode ( typedData[0 ], typedData[1 ] ) );
629
647
}
630
648
else
631
649
{
632
- return new UIntData ( *typedData );
650
+ return extractScalarData< unsigned int >( value );
633
651
}
634
652
}
635
653
return nullptr ;
@@ -641,17 +659,7 @@ IECore::DataPtr data( const OIIO::ParamValue &value )
641
659
{
642
660
case TypeDesc::SCALAR :
643
661
{
644
- if ( !type.arraylen )
645
- {
646
- return new IntData ( *typedData );
647
- }
648
- else
649
- {
650
- IntVectorDataPtr vectorData = new IntVectorData ();
651
- vectorData->writable ().resize ( type.arraylen );
652
- std::copy ( &typedData[0 ], &typedData[type.arraylen ], vectorData->writable ().begin () );
653
- return vectorData;
654
- }
662
+ return extractScalarData<int >( value );
655
663
}
656
664
case TypeDesc::VEC2 :
657
665
{
@@ -696,7 +704,7 @@ IECore::DataPtr data( const OIIO::ParamValue &value )
696
704
{
697
705
if ( type.aggregate == TypeDesc::SCALAR )
698
706
{
699
- return new HalfData ( * static_cast < const half * >( value. data () ) );
707
+ return extractScalarData< half>( value );
700
708
}
701
709
return nullptr ;
702
710
}
@@ -707,17 +715,7 @@ IECore::DataPtr data( const OIIO::ParamValue &value )
707
715
{
708
716
case TypeDesc::SCALAR :
709
717
{
710
- if ( !type.arraylen )
711
- {
712
- return new FloatData ( *typedData );
713
- }
714
- else
715
- {
716
- FloatVectorDataPtr vectorData = new FloatVectorData ();
717
- vectorData->writable ().resize ( type.arraylen );
718
- std::copy ( &typedData[0 ], &typedData[type.arraylen ], vectorData->writable ().begin () );
719
- return vectorData;
720
- }
718
+ return extractScalarData<float >( value );
721
719
}
722
720
case TypeDesc::VEC2 :
723
721
{
@@ -764,17 +762,7 @@ IECore::DataPtr data( const OIIO::ParamValue &value )
764
762
{
765
763
case TypeDesc::SCALAR :
766
764
{
767
- if ( !type.arraylen )
768
- {
769
- return new DoubleData ( *typedData );
770
- }
771
- else
772
- {
773
- DoubleVectorDataPtr vectorData = new DoubleVectorData ();
774
- vectorData->writable ().resize ( type.arraylen );
775
- std::copy ( &typedData[0 ], &typedData[type.arraylen ], vectorData->writable ().begin () );
776
- return vectorData;
777
- }
765
+ return extractScalarData<double >( value );
778
766
}
779
767
case TypeDesc::VEC2 :
780
768
{
0 commit comments