MatOfInt4.mm 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. //
  2. // MatOfInt4.mm
  3. //
  4. // Created by Giles Payne on 2019/12/27.
  5. //
  6. #import "MatOfInt4.h"
  7. #import "Range.h"
  8. #import "CvType.h"
  9. #import "ArrayUtil.h"
  10. @implementation MatOfInt4
  11. static const int _depth = CV_32S;
  12. static const int _channels = 4;
  13. #ifdef __cplusplus
  14. - (instancetype)initWithNativeMat:(cv::Mat*)nativeMat {
  15. self = [super initWithNativeMat:nativeMat];
  16. if (self && ![self empty] && [self checkVector:_channels depth:_depth] < 0) {
  17. @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"Incompatible Mat" userInfo:nil];
  18. }
  19. return self;
  20. }
  21. #endif
  22. - (instancetype)initWithMat:(Mat*)mat {
  23. self = [super initWithMat:mat rowRange:[Range all]];
  24. if (self && ![self empty] && [self checkVector:_channels depth:_depth] < 0) {
  25. @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"Incompatible Mat" userInfo:nil];
  26. }
  27. return self;
  28. }
  29. - (instancetype)initWithArray:(NSArray<NSNumber*>*)array {
  30. self = [super init];
  31. if (self) {
  32. [self fromArray:array];
  33. }
  34. return self;
  35. }
  36. - (void)alloc:(int)elemNumber {
  37. if (elemNumber>0) {
  38. [super create:elemNumber cols:1 type:[CvType makeType:_depth channels:_channels]];
  39. }
  40. }
  41. - (void)fromArray:(NSArray<NSNumber*>*)array {
  42. [self alloc:(int)array.count / _channels];
  43. [self put:0 col:0 data:array];
  44. }
  45. - (NSArray<NSNumber*>*)toArray {
  46. int length = [self length];
  47. NSMutableArray<NSNumber*>* data = createArrayWithSize(length, @0.0);
  48. [self get:0 col:0 data:data];
  49. return data;
  50. }
  51. - (int)length {
  52. int num = [self checkVector:_channels depth:_depth];
  53. if (num < 0) {
  54. @throw [NSException exceptionWithName:NSInternalInconsistencyException reason:@"Incompatible Mat" userInfo:nil];
  55. }
  56. return num * _channels;
  57. }
  58. @end