Blame src/utils/rescaler_utils.h

Packit 9c6abc
// Copyright 2012 Google Inc. All Rights Reserved.
Packit 9c6abc
//
Packit 9c6abc
// Use of this source code is governed by a BSD-style license
Packit 9c6abc
// that can be found in the COPYING file in the root of the source
Packit 9c6abc
// tree. An additional intellectual property rights grant can be found
Packit 9c6abc
// in the file PATENTS. All contributing project authors may
Packit 9c6abc
// be found in the AUTHORS file in the root of the source tree.
Packit 9c6abc
// -----------------------------------------------------------------------------
Packit 9c6abc
//
Packit 9c6abc
// Rescaling functions
Packit 9c6abc
//
Packit 9c6abc
// Author: Skal (pascal.massimino@gmail.com)
Packit 9c6abc
Packit 9c6abc
#ifndef WEBP_UTILS_RESCALER_UTILS_H_
Packit 9c6abc
#define WEBP_UTILS_RESCALER_UTILS_H_
Packit 9c6abc
Packit 9c6abc
#ifdef __cplusplus
Packit 9c6abc
extern "C" {
Packit 9c6abc
#endif
Packit 9c6abc
Packit 9c6abc
#include "src/webp/types.h"
Packit 9c6abc
Packit 9c6abc
#define WEBP_RESCALER_RFIX 32   // fixed-point precision for multiplies
Packit 9c6abc
#define WEBP_RESCALER_ONE (1ull << WEBP_RESCALER_RFIX)
Packit 9c6abc
#define WEBP_RESCALER_FRAC(x, y) \
Packit 9c6abc
    ((uint32_t)(((uint64_t)(x) << WEBP_RESCALER_RFIX) / (y)))
Packit 9c6abc
Packit 9c6abc
// Structure used for on-the-fly rescaling
Packit 9c6abc
typedef uint32_t rescaler_t;   // type for side-buffer
Packit 9c6abc
typedef struct WebPRescaler WebPRescaler;
Packit 9c6abc
struct WebPRescaler {
Packit 9c6abc
  int x_expand;               // true if we're expanding in the x direction
Packit 9c6abc
  int y_expand;               // true if we're expanding in the y direction
Packit 9c6abc
  int num_channels;           // bytes to jump between pixels
Packit 9c6abc
  uint32_t fx_scale;          // fixed-point scaling factors
Packit 9c6abc
  uint32_t fy_scale;          // ''
Packit 9c6abc
  uint32_t fxy_scale;         // ''
Packit 9c6abc
  int y_accum;                // vertical accumulator
Packit 9c6abc
  int y_add, y_sub;           // vertical increments
Packit 9c6abc
  int x_add, x_sub;           // horizontal increments
Packit 9c6abc
  int src_width, src_height;  // source dimensions
Packit 9c6abc
  int dst_width, dst_height;  // destination dimensions
Packit 9c6abc
  int src_y, dst_y;           // row counters for input and output
Packit 9c6abc
  uint8_t* dst;
Packit 9c6abc
  int dst_stride;
Packit 9c6abc
  rescaler_t* irow, *frow;    // work buffer
Packit 9c6abc
};
Packit 9c6abc
Packit 9c6abc
// Initialize a rescaler given scratch area 'work' and dimensions of src & dst.
Packit 9c6abc
void WebPRescalerInit(WebPRescaler* const rescaler,
Packit 9c6abc
                      int src_width, int src_height,
Packit 9c6abc
                      uint8_t* const dst,
Packit 9c6abc
                      int dst_width, int dst_height, int dst_stride,
Packit 9c6abc
                      int num_channels,
Packit 9c6abc
                      rescaler_t* const work);
Packit 9c6abc
Packit 9c6abc
// If either 'scaled_width' or 'scaled_height' (but not both) is 0 the value
Packit 9c6abc
// will be calculated preserving the aspect ratio, otherwise the values are
Packit 9c6abc
// left unmodified. Returns true on success, false if either value is 0 after
Packit 9c6abc
// performing the scaling calculation.
Packit 9c6abc
int WebPRescalerGetScaledDimensions(int src_width, int src_height,
Packit 9c6abc
                                    int* const scaled_width,
Packit 9c6abc
                                    int* const scaled_height);
Packit 9c6abc
Packit 9c6abc
// Returns the number of input lines needed next to produce one output line,
Packit 9c6abc
// considering that the maximum available input lines are 'max_num_lines'.
Packit 9c6abc
int WebPRescaleNeededLines(const WebPRescaler* const rescaler,
Packit 9c6abc
                           int max_num_lines);
Packit 9c6abc
Packit 9c6abc
// Import multiple rows over all channels, until at least one row is ready to
Packit 9c6abc
// be exported. Returns the actual number of lines that were imported.
Packit 9c6abc
int WebPRescalerImport(WebPRescaler* const rescaler, int num_rows,
Packit 9c6abc
                       const uint8_t* src, int src_stride);
Packit 9c6abc
Packit 9c6abc
// Export as many rows as possible. Return the numbers of rows written.
Packit 9c6abc
int WebPRescalerExport(WebPRescaler* const rescaler);
Packit 9c6abc
Packit 9c6abc
// Return true if input is finished
Packit 9c6abc
static WEBP_INLINE
Packit 9c6abc
int WebPRescalerInputDone(const WebPRescaler* const rescaler) {
Packit 9c6abc
  return (rescaler->src_y >= rescaler->src_height);
Packit 9c6abc
}
Packit 9c6abc
// Return true if output is finished
Packit 9c6abc
static WEBP_INLINE
Packit 9c6abc
int WebPRescalerOutputDone(const WebPRescaler* const rescaler) {
Packit 9c6abc
  return (rescaler->dst_y >= rescaler->dst_height);
Packit 9c6abc
}
Packit 9c6abc
Packit 9c6abc
// Return true if there are pending output rows ready.
Packit 9c6abc
static WEBP_INLINE
Packit 9c6abc
int WebPRescalerHasPendingOutput(const WebPRescaler* const rescaler) {
Packit 9c6abc
  return !WebPRescalerOutputDone(rescaler) && (rescaler->y_accum <= 0);
Packit 9c6abc
}
Packit 9c6abc
Packit 9c6abc
//------------------------------------------------------------------------------
Packit 9c6abc
Packit 9c6abc
#ifdef __cplusplus
Packit 9c6abc
}    // extern "C"
Packit 9c6abc
#endif
Packit 9c6abc
Packit 9c6abc
#endif  /* WEBP_UTILS_RESCALER_UTILS_H_ */