// Copyright (c) Meta Platforms, Inc. and affiliates.
// All rights reserved.
//
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree.

// Extension-level glue for the stable-ABI image_stable extension -- the stable
// counterpart of common.cpp. Holds pieces that belong to the extension rather
// than to any single operator, and grows as more ops migrate.

#include "common_stable.h"

#include <torch/headeronly/util/Exception.h>

// If we are in a Windows environment, we need to define
// initialization functions for the image_stable extension.
#if !defined(MOBILE) && defined(_WIN32)
void* PyInit_image_stable(void) {
  return nullptr;
}
#endif // !defined(MOBILE) && defined(_WIN32)

namespace vision {
namespace image {

void validate_encoded_data(const torch::stable::Tensor& encoded_data) {
  STD_TORCH_CHECK(
      encoded_data.is_contiguous(), "Input tensor must be contiguous.");
  STD_TORCH_CHECK(
      encoded_data.scalar_type() == torch::headeronly::ScalarType::Byte,
      "Input tensor must have uint8 data type, got ",
      torch::headeronly::toString(encoded_data.scalar_type()));
  STD_TORCH_CHECK(
      encoded_data.dim() == 1 && encoded_data.numel() > 0,
      "Input tensor must be 1-dimensional and non-empty, got ",
      encoded_data.dim(),
      " dims  and ",
      encoded_data.numel(),
      " numels.");
}

} // namespace image
} // namespace vision
