| OLD | NEW |
| (Empty) |
| 1 // Copyright 2012 Google Inc. All Rights Reserved. | |
| 2 // | |
| 3 // Use of this source code is governed by a BSD-style license | |
| 4 // that can be found in the COPYING file in the root of the source | |
| 5 // tree. An additional intellectual property rights grant can be found | |
| 6 // in the file PATENTS. All contributing project authors may | |
| 7 // be found in the AUTHORS file in the root of the source tree. | |
| 8 // ----------------------------------------------------------------------------- | |
| 9 // | |
| 10 // Rescaling functions | |
| 11 // | |
| 12 // Author: Skal (pascal.massimino@gmail.com) | |
| 13 | |
| 14 #include <assert.h> | |
| 15 #include <stdlib.h> | |
| 16 #include <string.h> | |
| 17 #include "../dsp/dsp.h" | |
| 18 #include "./rescaler.h" | |
| 19 | |
| 20 //------------------------------------------------------------------------------ | |
| 21 | |
| 22 void WebPRescalerInit(WebPRescaler* const wrk, int src_width, int src_height, | |
| 23 uint8_t* const dst, | |
| 24 int dst_width, int dst_height, int dst_stride, | |
| 25 int num_channels, rescaler_t* const work) { | |
| 26 const int x_add = src_width, x_sub = dst_width; | |
| 27 const int y_add = src_height, y_sub = dst_height; | |
| 28 wrk->x_expand = (src_width < dst_width); | |
| 29 wrk->y_expand = (src_height < dst_height); | |
| 30 wrk->src_width = src_width; | |
| 31 wrk->src_height = src_height; | |
| 32 wrk->dst_width = dst_width; | |
| 33 wrk->dst_height = dst_height; | |
| 34 wrk->src_y = 0; | |
| 35 wrk->dst_y = 0; | |
| 36 wrk->dst = dst; | |
| 37 wrk->dst_stride = dst_stride; | |
| 38 wrk->num_channels = num_channels; | |
| 39 | |
| 40 // for 'x_expand', we use bilinear interpolation | |
| 41 wrk->x_add = wrk->x_expand ? (x_sub - 1) : x_add; | |
| 42 wrk->x_sub = wrk->x_expand ? (x_add - 1) : x_sub; | |
| 43 if (!wrk->x_expand) { // fx_scale is not used otherwise | |
| 44 wrk->fx_scale = WEBP_RESCALER_FRAC(1, wrk->x_sub); | |
| 45 } | |
| 46 // vertical scaling parameters | |
| 47 wrk->y_add = wrk->y_expand ? y_add - 1 : y_add; | |
| 48 wrk->y_sub = wrk->y_expand ? y_sub - 1 : y_sub; | |
| 49 wrk->y_accum = wrk->y_expand ? wrk->y_sub : wrk->y_add; | |
| 50 if (!wrk->y_expand) { | |
| 51 // This is WEBP_RESCALER_FRAC(dst_height, x_add * y_add) without the cast. | |
| 52 // Its value is <= WEBP_RESCALER_ONE, because dst_height <= wrk->y_add, and | |
| 53 // wrk->x_add >= 1; | |
| 54 const uint64_t ratio = | |
| 55 (uint64_t)dst_height * WEBP_RESCALER_ONE / (wrk->x_add * wrk->y_add); | |
| 56 if (ratio != (uint32_t)ratio) { | |
| 57 // When ratio == WEBP_RESCALER_ONE, we can't represent the ratio with the | |
| 58 // current fixed-point precision. This happens when src_height == | |
| 59 // wrk->y_add (which == src_height), and wrk->x_add == 1. | |
| 60 // => We special-case fxy_scale = 0, in WebPRescalerExportRow(). | |
| 61 wrk->fxy_scale = 0; | |
| 62 } else { | |
| 63 wrk->fxy_scale = (uint32_t)ratio; | |
| 64 } | |
| 65 wrk->fy_scale = WEBP_RESCALER_FRAC(1, wrk->y_sub); | |
| 66 } else { | |
| 67 wrk->fy_scale = WEBP_RESCALER_FRAC(1, wrk->x_add); | |
| 68 // wrk->fxy_scale is unused here. | |
| 69 } | |
| 70 wrk->irow = work; | |
| 71 wrk->frow = work + num_channels * dst_width; | |
| 72 memset(work, 0, 2 * dst_width * num_channels * sizeof(*work)); | |
| 73 | |
| 74 WebPRescalerDspInit(); | |
| 75 } | |
| 76 | |
| 77 int WebPRescalerGetScaledDimensions(int src_width, int src_height, | |
| 78 int* const scaled_width, | |
| 79 int* const scaled_height) { | |
| 80 assert(scaled_width != NULL); | |
| 81 assert(scaled_height != NULL); | |
| 82 { | |
| 83 int width = *scaled_width; | |
| 84 int height = *scaled_height; | |
| 85 | |
| 86 // if width is unspecified, scale original proportionally to height ratio. | |
| 87 if (width == 0) { | |
| 88 width = (src_width * height + src_height / 2) / src_height; | |
| 89 } | |
| 90 // if height is unspecified, scale original proportionally to width ratio. | |
| 91 if (height == 0) { | |
| 92 height = (src_height * width + src_width / 2) / src_width; | |
| 93 } | |
| 94 // Check if the overall dimensions still make sense. | |
| 95 if (width <= 0 || height <= 0) { | |
| 96 return 0; | |
| 97 } | |
| 98 | |
| 99 *scaled_width = width; | |
| 100 *scaled_height = height; | |
| 101 return 1; | |
| 102 } | |
| 103 } | |
| 104 | |
| 105 //------------------------------------------------------------------------------ | |
| 106 // all-in-one calls | |
| 107 | |
| 108 int WebPRescaleNeededLines(const WebPRescaler* const wrk, int max_num_lines) { | |
| 109 const int num_lines = (wrk->y_accum + wrk->y_sub - 1) / wrk->y_sub; | |
| 110 return (num_lines > max_num_lines) ? max_num_lines : num_lines; | |
| 111 } | |
| 112 | |
| 113 int WebPRescalerImport(WebPRescaler* const wrk, int num_lines, | |
| 114 const uint8_t* src, int src_stride) { | |
| 115 int total_imported = 0; | |
| 116 while (total_imported < num_lines && !WebPRescalerHasPendingOutput(wrk)) { | |
| 117 if (wrk->y_expand) { | |
| 118 rescaler_t* const tmp = wrk->irow; | |
| 119 wrk->irow = wrk->frow; | |
| 120 wrk->frow = tmp; | |
| 121 } | |
| 122 WebPRescalerImportRow(wrk, src); | |
| 123 if (!wrk->y_expand) { // Accumulate the contribution of the new row. | |
| 124 int x; | |
| 125 for (x = 0; x < wrk->num_channels * wrk->dst_width; ++x) { | |
| 126 wrk->irow[x] += wrk->frow[x]; | |
| 127 } | |
| 128 } | |
| 129 ++wrk->src_y; | |
| 130 src += src_stride; | |
| 131 ++total_imported; | |
| 132 wrk->y_accum -= wrk->y_sub; | |
| 133 } | |
| 134 return total_imported; | |
| 135 } | |
| 136 | |
| 137 int WebPRescalerExport(WebPRescaler* const rescaler) { | |
| 138 int total_exported = 0; | |
| 139 while (WebPRescalerHasPendingOutput(rescaler)) { | |
| 140 WebPRescalerExportRow(rescaler); | |
| 141 ++total_exported; | |
| 142 } | |
| 143 return total_exported; | |
| 144 } | |
| 145 | |
| 146 //------------------------------------------------------------------------------ | |
| OLD | NEW |