github.com/cellofellow/gopkg@v0.0.0-20140722061823-eec0544a62ad/image/webp/libwebp/src/enc/backward_references.h (about) 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 // Author: Jyrki Alakuijala (jyrki@google.com) 11 // 12 13 #ifndef WEBP_ENC_BACKWARD_REFERENCES_H_ 14 #define WEBP_ENC_BACKWARD_REFERENCES_H_ 15 16 #include <assert.h> 17 #include <stdlib.h> 18 #include "../webp/types.h" 19 #include "../webp/format_constants.h" 20 21 #ifdef __cplusplus 22 extern "C" { 23 #endif 24 25 // The spec allows 11, we use 9 bits to reduce memory consumption in encoding. 26 // Having 9 instead of 11 only removes about 0.25 % of compression density. 27 #define MAX_COLOR_CACHE_BITS 9 28 29 // Max ever number of codes we'll use: 30 #define PIX_OR_COPY_CODES_MAX \ 31 (NUM_LITERAL_CODES + NUM_LENGTH_CODES + (1 << MAX_COLOR_CACHE_BITS)) 32 33 // ----------------------------------------------------------------------------- 34 // PixOrCopy 35 36 enum Mode { 37 kLiteral, 38 kCacheIdx, 39 kCopy, 40 kNone 41 }; 42 43 typedef struct { 44 // mode as uint8_t to make the memory layout to be exactly 8 bytes. 45 uint8_t mode; 46 uint16_t len; 47 uint32_t argb_or_distance; 48 } PixOrCopy; 49 50 static WEBP_INLINE PixOrCopy PixOrCopyCreateCopy(uint32_t distance, 51 uint16_t len) { 52 PixOrCopy retval; 53 retval.mode = kCopy; 54 retval.argb_or_distance = distance; 55 retval.len = len; 56 return retval; 57 } 58 59 static WEBP_INLINE PixOrCopy PixOrCopyCreateCacheIdx(int idx) { 60 PixOrCopy retval; 61 assert(idx >= 0); 62 assert(idx < (1 << MAX_COLOR_CACHE_BITS)); 63 retval.mode = kCacheIdx; 64 retval.argb_or_distance = idx; 65 retval.len = 1; 66 return retval; 67 } 68 69 static WEBP_INLINE PixOrCopy PixOrCopyCreateLiteral(uint32_t argb) { 70 PixOrCopy retval; 71 retval.mode = kLiteral; 72 retval.argb_or_distance = argb; 73 retval.len = 1; 74 return retval; 75 } 76 77 static WEBP_INLINE int PixOrCopyIsLiteral(const PixOrCopy* const p) { 78 return (p->mode == kLiteral); 79 } 80 81 static WEBP_INLINE int PixOrCopyIsCacheIdx(const PixOrCopy* const p) { 82 return (p->mode == kCacheIdx); 83 } 84 85 static WEBP_INLINE int PixOrCopyIsCopy(const PixOrCopy* const p) { 86 return (p->mode == kCopy); 87 } 88 89 static WEBP_INLINE uint32_t PixOrCopyLiteral(const PixOrCopy* const p, 90 int component) { 91 assert(p->mode == kLiteral); 92 return (p->argb_or_distance >> (component * 8)) & 0xff; 93 } 94 95 static WEBP_INLINE uint32_t PixOrCopyLength(const PixOrCopy* const p) { 96 return p->len; 97 } 98 99 static WEBP_INLINE uint32_t PixOrCopyArgb(const PixOrCopy* const p) { 100 assert(p->mode == kLiteral); 101 return p->argb_or_distance; 102 } 103 104 static WEBP_INLINE uint32_t PixOrCopyCacheIdx(const PixOrCopy* const p) { 105 assert(p->mode == kCacheIdx); 106 assert(p->argb_or_distance < (1U << MAX_COLOR_CACHE_BITS)); 107 return p->argb_or_distance; 108 } 109 110 static WEBP_INLINE uint32_t PixOrCopyDistance(const PixOrCopy* const p) { 111 assert(p->mode == kCopy); 112 return p->argb_or_distance; 113 } 114 115 // ----------------------------------------------------------------------------- 116 // VP8LBackwardRefs 117 118 typedef struct { 119 PixOrCopy* refs; 120 int size; // currently used 121 int max_size; // maximum capacity 122 } VP8LBackwardRefs; 123 124 // Initialize the object. Must be called first. 'refs' can be NULL. 125 void VP8LInitBackwardRefs(VP8LBackwardRefs* const refs); 126 127 // Release memory and re-initialize the object. 'refs' can be NULL. 128 void VP8LClearBackwardRefs(VP8LBackwardRefs* const refs); 129 130 // Allocate 'max_size' references. Returns false in case of memory error. 131 int VP8LBackwardRefsAlloc(VP8LBackwardRefs* const refs, int max_size); 132 133 // ----------------------------------------------------------------------------- 134 // Main entry points 135 136 // Evaluates best possible backward references for specified quality. 137 // Further optimize for 2D locality if use_2d_locality flag is set. 138 int VP8LGetBackwardReferences(int width, int height, 139 const uint32_t* const argb, 140 int quality, int cache_bits, int use_2d_locality, 141 VP8LBackwardRefs* const best); 142 143 // Produce an estimate for a good color cache size for the image. 144 int VP8LCalculateEstimateForCacheSize(const uint32_t* const argb, 145 int xsize, int ysize, 146 int* const best_cache_bits); 147 148 #ifdef __cplusplus 149 } 150 #endif 151 152 #endif // WEBP_ENC_BACKWARD_REFERENCES_H_