1 // Copyright 2016 The Draco Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 #ifndef DRACO_COMPRESSION_ATTRIBUTES_PREDICTION_SCHEMES_PREDICTION_SCHEME_DELTA_ENCODER_H_
16 #define DRACO_COMPRESSION_ATTRIBUTES_PREDICTION_SCHEMES_PREDICTION_SCHEME_DELTA_ENCODER_H_
17 
18 #include "draco/compression/attributes/prediction_schemes/prediction_scheme_encoder.h"
19 
20 namespace draco {
21 
22 // Basic prediction scheme based on computing backward differences between
23 // stored attribute values (also known as delta-coding). Usually works better
24 // than the reference point prediction scheme, because nearby values are often
25 // encoded next to each other.
26 template <typename DataTypeT, class TransformT>
27 class PredictionSchemeDeltaEncoder
28     : public PredictionSchemeEncoder<DataTypeT, TransformT> {
29  public:
30   using CorrType =
31       typename PredictionSchemeEncoder<DataTypeT, TransformT>::CorrType;
32   // Initialized the prediction scheme.
PredictionSchemeDeltaEncoder(const PointAttribute * attribute)33   explicit PredictionSchemeDeltaEncoder(const PointAttribute *attribute)
34       : PredictionSchemeEncoder<DataTypeT, TransformT>(attribute) {}
PredictionSchemeDeltaEncoder(const PointAttribute * attribute,const TransformT & transform)35   PredictionSchemeDeltaEncoder(const PointAttribute *attribute,
36                                const TransformT &transform)
37       : PredictionSchemeEncoder<DataTypeT, TransformT>(attribute, transform) {}
38 
39   bool ComputeCorrectionValues(
40       const DataTypeT *in_data, CorrType *out_corr, int size,
41       int num_components, const PointIndex *entry_to_point_id_map) override;
GetPredictionMethod()42   PredictionSchemeMethod GetPredictionMethod() const override {
43     return PREDICTION_DIFFERENCE;
44   }
IsInitialized()45   bool IsInitialized() const override { return true; }
46 };
47 
48 template <typename DataTypeT, class TransformT>
49 bool PredictionSchemeDeltaEncoder<
ComputeCorrectionValues(const DataTypeT * in_data,CorrType * out_corr,int size,int num_components,const PointIndex *)50     DataTypeT, TransformT>::ComputeCorrectionValues(const DataTypeT *in_data,
51                                                     CorrType *out_corr,
52                                                     int size,
53                                                     int num_components,
54                                                     const PointIndex *) {
55   this->transform().Init(in_data, size, num_components);
56   // Encode data from the back using D(i) = D(i) - D(i - 1).
57   for (int i = size - num_components; i > 0; i -= num_components) {
58     this->transform().ComputeCorrection(
59         in_data + i, in_data + i - num_components, out_corr + i);
60   }
61   // Encode correction for the first element.
62   std::unique_ptr<DataTypeT[]> zero_vals(new DataTypeT[num_components]());
63   this->transform().ComputeCorrection(in_data, zero_vals.get(), out_corr);
64   return true;
65 }
66 
67 }  // namespace draco
68 
69 #endif  // DRACO_COMPRESSION_ATTRIBUTES_PREDICTION_SCHEMES_PREDICTION_SCHEME_DELTA_ENCODER_H_
70