Files
libwebp/src/utils/rescaler_utils.h
Arman Hasanzadeh 1fdd4ef501 Add fbounds-safety annotations for WebPRescaler.
Reasoning:

The `irow` and `frow` pointers in `WebPRescaler`
(src/utils/rescaler_utils.h:49) were annotated with
`WEBP_COUNTED_BY(dst_width * num_channels)`. This is based on their
initialization in `WebPRescalerInit` (src/utils/rescaler_utils.c:82-83)
where they are assigned parts of the `work` buffer, whose total size
is `2 * dst_width * num_channels`. The `work` parameter in
`WebPRescalerInit` (src/utils/rescaler_utils.h:58,
src/utils/rescaler_utils.c:33) was also annotated accordingly.

To satisfy the side-by-side assignment requirement for external bounds,
assignments to `rescaler->irow` and `rescaler->frow` in
`WebPRescalerInit` were moved closer to the assignments of
`dst_width` and `num_channels` (src/utils/rescaler_utils.c:50-53).
Since `work` have bound information, `WEBP_UNSAFE_MEMSET` has
been changed to `memset`.

In `WebPRescalerImport` (src/utils/rescaler_utils.c:140-150), where
`irow` and `frow` are swapped, self-assignments for `dst_width` and
`num_channels` were added side-by-side with the pointer assignments.
Additionally, `WEBP_UNSAFE_FORGE_BIDI_INDEXABLE` was used for the
pointer assignments to handle the `WEBP_ASSUME_UNSAFE_INDEXABLE_ABI`
setting used during testing.

Bug: 432511821
Change-Id: If716fb79a06dee9e807eff060806daf038810523
2025-08-20 17:56:26 -07:00

107 lines
4.2 KiB
C

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