Repository: google/mediapipe
File: mediapipe/tasks/ios/vision/core/sources/MPPMask.mm (Objective-C++)

Code before:
// Copyright 2023 The MediaPipe Authors.
//
// 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
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// 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.

#import "mediapipe/tasks/ios/vision/core/sources/MPPMask.h"
#import "mediapipe/tasks/ios/common/sources/MPPCommon.h"
#import "mediapipe/tasks/ios/common/utils/sources/MPPCommonUtils.h"

namespace {
template <typename T>
T *allocateDataPtr(std::unique_ptr<T[]> &data, size_t length) {
  data = std::unique_ptr<T[]>(new T[length]);
  return data.get();
}

template <typename T>
void copyData(const T *destination, const T *source, size_t length) {
  memcpy((void *)destination, source, length * sizeof(T));
}
}  // namespace

@interface MPPMask () {
  const UInt8 *_uint8Data;
  const float *_float32Data;
  std::unique_ptr<UInt8[]> _allocatedUInt8Data;
  std::unique_ptr<float[]> _allocatedFloat32Data;
}
@end

@implementation MPPMask

- (nullable instancetype)initWithWidth:(NSInteger)width
                                height:(NSInteger)height
                              dataType:(MPPMaskDataType)dataType
                                 error:(NSError **)error {
  if (dataType < MPPMaskDataTypeUInt8 || dataType > MPPMaskDataTypeFloat32) {
    [MPPCommonUtils createCustomError:error

Review comment (bug):
Maybe I am misunderstanding this, but I don't quite follow how this works.

- This is passing in `data` by reference.
- It is then re-assigning data to a new unique pointer, but the original data is left untouched (?)
- It then returns a pointer into the data array, which will be invalidated immediately since the function goes out of scope.

My understanding might be totally wrong but either way, I would prefer if we inlined this method in the places that its is called.

Code after:
// Copyright 2023 The MediaPipe Authors.
//
// 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
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// 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.

#import "mediapipe/tasks/ios/vision/core/sources/MPPMask.h"
#import "mediapipe/tasks/ios/common/sources/MPPCommon.h"
#import "mediapipe/tasks/ios/common/utils/sources/MPPCommonUtils.h"

@interface MPPMask () {
  const UInt8 *_uint8Data;
  const float *_float32Data;
  std::unique_ptr<UInt8[]> _uint8DataPtr;
  std::unique_ptr<float[]> _float32DataPtr;
}
@end

@implementation MPPMask

- (nullable instancetype)initWithWidth:(NSInteger)width
                                height:(NSInteger)height
                              dataType:(MPPMaskDataType)dataType
                                 error:(NSError **)error {
  if (dataType < MPPMaskDataTypeUInt8 || dataType > MPPMaskDataTypeFloat32) {
    [MPPCommonUtils createCustomError:error
                             withCode:MPPTasksErrorCodeInvalidArgumentError
                          description:@"Invalid value for data type."];
    return nil;
  }

  self = [super init];
  if (self) {
    _width = width;
    _height = height;
    _dataType = dataType;
  }
  return self;
}