// Copyright 2014 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. // ----------------------------------------------------------------------------- // // Near-lossless image preprocessing adjusts pixel values to help // compressibility with a guarantee of maximum deviation between original and // resulting pixel values. // // Author: Jyrki Alakuijala (jyrki@google.com) // Converted to C by Aleksander Kramarz (akramarz@google.com)
// Quantizes the value up or down to a multiple of 1<<bits (or to 255), // choosing the closer one, resolving ties using bankers' rounding. static uint32_t FindClosestDiscretized(uint32_t a, int bits) { const uint32_t mask = (1u << bits) - 1; const uint32_t biased = a + (mask >> 1) + ((a >> bits) & 1);
assert(bits > 0); if (biased > 0xff) return 0xff; return biased & ~mask;
}
// Applies FindClosestDiscretized to all channels of pixel. static uint32_t ClosestDiscretizedArgb(uint32_t a, int bits) { return
(FindClosestDiscretized(a >> 24, bits) << 24) |
(FindClosestDiscretized((a >> 16) & 0xff, bits) << 16) |
(FindClosestDiscretized((a >> 8) & 0xff, bits) << 8) |
(FindClosestDiscretized(a & 0xff, bits));
}
// Checks if distance between corresponding channel values of pixels a and b // is within the given limit. staticint IsNear(uint32_t a, uint32_t b, int limit) { int k; for (k = 0; k < 4; ++k) { constint delta =
(int)((a >> (k * 8)) & 0xff) - (int)((b >> (k * 8)) & 0xff); if (delta >= limit || delta <= -limit) { return 0;
}
} return 1;
}
staticint IsSmooth(const uint32_t* const prev_row, const uint32_t* const curr_row, const uint32_t* const next_row, int ix, int limit) { // Check that all pixels in 4-connected neighborhood are smooth. return (IsNear(curr_row[ix], curr_row[ix - 1], limit) &&
IsNear(curr_row[ix], curr_row[ix + 1], limit) &&
IsNear(curr_row[ix], prev_row[ix], limit) &&
IsNear(curr_row[ix], next_row[ix], limit));
}
// Adjusts pixel values of image with given maximum error. staticvoid NearLossless(int xsize, int ysize, const uint32_t* argb_src, int stride, int limit_bits, uint32_t* copy_buffer,
uint32_t* argb_dst) { int x, y; constint limit = 1 << limit_bits;
uint32_t* prev_row = copy_buffer;
uint32_t* curr_row = prev_row + xsize;
uint32_t* next_row = curr_row + xsize;
memcpy(curr_row, argb_src, xsize * sizeof(argb_src[0]));
memcpy(next_row, argb_src + stride, xsize * sizeof(argb_src[0]));
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.