// 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)

#include <assert.h>
#include <stdlib.h>
#include "./rescaler.h"

//------------------------------------------------------------------------------

#define RFIX 30
#define MULT_FIX(x, y) (((int64_t)(x) * (y) + (1 << (RFIX - 1))) >> RFIX)

void WebPRescalerInit(WebPRescaler* const wrk, int src_width, int src_height,
                      uint8_t* const dst, int dst_width, int dst_height,
                      int dst_stride, int num_channels, int x_add, int x_sub,
                      int y_add, int y_sub, int32_t* const work) {
  wrk->x_expand = (src_width < dst_width);
  wrk->src_width = src_width;
  wrk->src_height = src_height;
  wrk->dst_width = dst_width;
  wrk->dst_height = dst_height;
  wrk->dst = dst;
  wrk->dst_stride = dst_stride;
  wrk->num_channels = num_channels;
  // for 'x_expand', we use bilinear interpolation
  wrk->x_add = wrk->x_expand ? (x_sub - 1) : x_add - x_sub;
  wrk->x_sub = wrk->x_expand ? (x_add - 1) : x_sub;
  wrk->y_accum = y_add;
  wrk->y_add = y_add;
  wrk->y_sub = y_sub;
  wrk->fx_scale = (1 << RFIX) / x_sub;
  wrk->fy_scale = (1 << RFIX) / y_sub;
  wrk->fxy_scale = wrk->x_expand ?
      ((int64_t)dst_height << RFIX) / (x_sub * src_height) :
      ((int64_t)dst_height << RFIX) / (x_add * src_height);
  wrk->irow = work;
  wrk->frow = work + num_channels * dst_width;
}

void WebPRescalerImportRow(WebPRescaler* const wrk,
                           const uint8_t* const src, int channel) {
  const int x_stride = wrk->num_channels;
  const int x_out_max = wrk->dst_width * wrk->num_channels;
  int x_in = channel;
  int x_out;
  int accum = 0;
  if (!wrk->x_expand) {
    int sum = 0;
    for (x_out = channel; x_out < x_out_max; x_out += x_stride) {
      accum += wrk->x_add;
      for (; accum > 0; accum -= wrk->x_sub) {
        sum += src[x_in];
        x_in += x_stride;
      }
      {        // Emit next horizontal pixel.
        const int32_t base = src[x_in];
        const int32_t frac = base * (-accum);
        x_in += x_stride;
        wrk->frow[x_out] = (sum + base) * wrk->x_sub - frac;
        // fresh fractional start for next pixel
        sum = (int)MULT_FIX(frac, wrk->fx_scale);
      }
    }
  } else {        // simple bilinear interpolation
    int left = src[channel], right = src[channel];
    for (x_out = channel; x_out < x_out_max; x_out += x_stride) {
      if (accum < 0) {
        left = right;
        x_in += x_stride;
        right = src[x_in];
        accum += wrk->x_add;
      }
      wrk->frow[x_out] = right * wrk->x_add + (left - right) * accum;
      accum -= wrk->x_sub;
    }
  }
  // Accumulate the new row's contribution
  for (x_out = channel; x_out < x_out_max; x_out += x_stride) {
    wrk->irow[x_out] += wrk->frow[x_out];
  }
}

uint8_t* WebPRescalerExportRow(WebPRescaler* const wrk) {
  if (wrk->y_accum <= 0) {
    int x_out = 0;
    uint8_t* const dst = wrk->dst;
    int32_t* const irow = wrk->irow;
    const int32_t* const frow = wrk->frow;
    const int yscale = wrk->fy_scale * (-wrk->y_accum);
    const int x_out_max = wrk->dst_width * wrk->num_channels;

#if defined(__mips__)
    // if wrk->fxy_scale can fit into 32 bits use optimized code,
    // otherwise use C code
    if ((wrk->fxy_scale >> 32) == 0) {
      int temp0, temp1, temp3, temp4, temp5, temp6, temp7, loop_end;
      const int temp2 = (int)(wrk->fxy_scale);
      const int temp8 = x_out_max << 2;
      uint8_t* dst_t = (uint8_t*)dst;
      int32_t* irow_t = (int32_t*)irow;
      const int32_t* frow_t = (const int32_t*)frow;

      __asm__ volatile(
        "addiu    %[temp6],    $zero,       -256          \n\t"
        "addiu    %[temp7],    $zero,       255           \n\t"
        "li       %[temp3],    0x10000                    \n\t"
        "li       %[temp4],    0x8000                     \n\t"
        "addu     %[loop_end], %[frow_t],   %[temp8]      \n\t"
      "1:                                                 \n\t"
        "lw       %[temp0],    0(%[frow_t])               \n\t"
        "mult     %[temp3],    %[temp4]                   \n\t"
        "addiu    %[frow_t],   %[frow_t],   4             \n\t"
        "sll      %[temp0],    %[temp0],    2             \n\t"
        "madd     %[temp0],    %[yscale]                  \n\t"
        "mfhi     %[temp1]                                \n\t"
        "lw       %[temp0],    0(%[irow_t])               \n\t"
        "addiu    %[dst_t],    %[dst_t],    1             \n\t"
        "addiu    %[irow_t],   %[irow_t],   4             \n\t"
        "subu     %[temp0],    %[temp0],    %[temp1]      \n\t"
        "mult     %[temp3],    %[temp4]                   \n\t"
        "sll      %[temp0],    %[temp0],    2             \n\t"
        "madd     %[temp0],    %[temp2]                   \n\t"
        "mfhi     %[temp5]                                \n\t"
        "sw       %[temp1],    -4(%[irow_t])              \n\t"
        "and      %[temp0],    %[temp5],    %[temp6]      \n\t"
        "slti     %[temp1],    %[temp5],    0             \n\t"
        "beqz     %[temp0],    2f                         \n\t"
        "xor      %[temp5],    %[temp5],    %[temp5]      \n\t"
        "movz     %[temp5],    %[temp7],    %[temp1]      \n\t"
      "2:                                                 \n\t"
        "sb       %[temp5],    -1(%[dst_t])               \n\t"
        "bne      %[frow_t],   %[loop_end], 1b            \n\t"

        : [temp0]"=&r"(temp0), [temp1]"=&r"(temp1), [temp3]"=&r"(temp3),
          [temp4]"=&r"(temp4), [temp5]"=&r"(temp5), [temp6]"=&r"(temp6),
          [temp7]"=&r"(temp7), [frow_t]"+r"(frow_t), [irow_t]"+r"(irow_t),
          [dst_t]"+r"(dst_t), [loop_end]"=&r"(loop_end)
        : [temp2]"r"(temp2), [yscale]"r"(yscale), [temp8]"r"(temp8)
        : "memory", "hi", "lo"
      );
      x_out = x_out_max;
    }
#endif  // __mips__
    for (; x_out < x_out_max; ++x_out) {
      const int frac = (int)MULT_FIX(frow[x_out], yscale);
      const int v = (int)MULT_FIX(irow[x_out] - frac, wrk->fxy_scale);
      dst[x_out] = (!(v & ~0xff)) ? v : (v < 0) ? 0 : 255;
      irow[x_out] = frac;   // new fractional start
    }
    wrk->y_accum += wrk->y_add;
    wrk->dst += wrk->dst_stride;
    return dst;
  } else {
    return NULL;
  }
}

#undef MULT_FIX
#undef RFIX

//------------------------------------------------------------------------------
// all-in-one calls

int WebPRescaleNeededLines(const WebPRescaler* const wrk, int max_num_lines) {
  const int num_lines = (wrk->y_accum + wrk->y_sub - 1) / wrk->y_sub;
  return (num_lines > max_num_lines) ? max_num_lines : num_lines;
}

int WebPRescalerImport(WebPRescaler* const wrk, int num_lines,
                       const uint8_t* src, int src_stride) {
  int total_imported = 0;
  while (total_imported < num_lines && wrk->y_accum > 0) {
    int channel;
    for (channel = 0; channel < wrk->num_channels; ++channel) {
      WebPRescalerImportRow(wrk, src, channel);
    }
    src += src_stride;
    ++total_imported;
    wrk->y_accum -= wrk->y_sub;
  }
  return total_imported;
}

int WebPRescalerExport(WebPRescaler* const rescaler) {
  int total_exported = 0;
  while (WebPRescalerHasPendingOutput(rescaler)) {
    WebPRescalerExportRow(rescaler);
    ++total_exported;
  }
  return total_exported;
}

//------------------------------------------------------------------------------