OLD | NEW |
1 // Copyright 2012 Google Inc. All Rights Reserved. | 1 // Copyright 2012 Google Inc. All Rights Reserved. |
2 // | 2 // |
3 // Use of this source code is governed by a BSD-style license | 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 | 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 | 5 // tree. An additional intellectual property rights grant can be found |
6 // in the file PATENTS. All contributing project authors may | 6 // in the file PATENTS. All contributing project authors may |
7 // be found in the AUTHORS file in the root of the source tree. | 7 // be found in the AUTHORS file in the root of the source tree. |
8 // ----------------------------------------------------------------------------- | 8 // ----------------------------------------------------------------------------- |
9 // | 9 // |
10 // Author: Jyrki Alakuijala (jyrki@google.com) | 10 // Author: Jyrki Alakuijala (jyrki@google.com) |
(...skipping 574 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
585 // max_index^2 for the queue size is safe. If you look at | 585 // max_index^2 for the queue size is safe. If you look at |
586 // HistogramCombineGreedy, and imagine that UpdateQueueFront always pushes | 586 // HistogramCombineGreedy, and imagine that UpdateQueueFront always pushes |
587 // data to the queue, you insert at most: | 587 // data to the queue, you insert at most: |
588 // - max_index*(max_index-1)/2 (the first two for loops) | 588 // - max_index*(max_index-1)/2 (the first two for loops) |
589 // - max_index - 1 in the last for loop at the first iteration of the while | 589 // - max_index - 1 in the last for loop at the first iteration of the while |
590 // loop, max_index - 2 at the second iteration ... therefore | 590 // loop, max_index - 2 at the second iteration ... therefore |
591 // max_index*(max_index-1)/2 overall too | 591 // max_index*(max_index-1)/2 overall too |
592 histo_queue->max_size = max_index * max_index; | 592 histo_queue->max_size = max_index * max_index; |
593 // We allocate max_size + 1 because the last element at index "size" is | 593 // We allocate max_size + 1 because the last element at index "size" is |
594 // used as temporary data (and it could be up to max_size). | 594 // used as temporary data (and it could be up to max_size). |
595 histo_queue->queue = WebPSafeMalloc(histo_queue->max_size + 1, | 595 histo_queue->queue = (HistogramPair*)WebPSafeMalloc( |
596 sizeof(*histo_queue->queue)); | 596 histo_queue->max_size + 1, sizeof(*histo_queue->queue)); |
597 return histo_queue->queue != NULL; | 597 return histo_queue->queue != NULL; |
598 } | 598 } |
599 | 599 |
600 static void HistoQueueClear(HistoQueue* const histo_queue) { | 600 static void HistoQueueClear(HistoQueue* const histo_queue) { |
601 assert(histo_queue != NULL); | 601 assert(histo_queue != NULL); |
602 WebPSafeFree(histo_queue->queue); | 602 WebPSafeFree(histo_queue->queue); |
603 } | 603 } |
604 | 604 |
605 static void SwapHistogramPairs(HistogramPair *p1, | 605 static void SwapHistogramPairs(HistogramPair *p1, |
606 HistogramPair *p2) { | 606 HistogramPair *p2) { |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
652 } | 652 } |
653 | 653 |
654 // Combines histograms by continuously choosing the one with the highest cost | 654 // Combines histograms by continuously choosing the one with the highest cost |
655 // reduction. | 655 // reduction. |
656 static int HistogramCombineGreedy(VP8LHistogramSet* const image_histo) { | 656 static int HistogramCombineGreedy(VP8LHistogramSet* const image_histo) { |
657 int ok = 0; | 657 int ok = 0; |
658 int image_histo_size = image_histo->size; | 658 int image_histo_size = image_histo->size; |
659 int i, j; | 659 int i, j; |
660 VP8LHistogram** const histograms = image_histo->histograms; | 660 VP8LHistogram** const histograms = image_histo->histograms; |
661 // Indexes of remaining histograms. | 661 // Indexes of remaining histograms. |
662 int* const clusters = WebPSafeMalloc(image_histo_size, sizeof(*clusters)); | 662 int* const clusters = |
| 663 (int*)WebPSafeMalloc(image_histo_size, sizeof(*clusters)); |
663 // Priority queue of histogram pairs. | 664 // Priority queue of histogram pairs. |
664 HistoQueue histo_queue; | 665 HistoQueue histo_queue; |
665 | 666 |
666 if (!HistoQueueInit(&histo_queue, image_histo_size) || clusters == NULL) { | 667 if (!HistoQueueInit(&histo_queue, image_histo_size) || clusters == NULL) { |
667 goto End; | 668 goto End; |
668 } | 669 } |
669 | 670 |
670 for (i = 0; i < image_histo_size; ++i) { | 671 for (i = 0; i < image_histo_size; ++i) { |
671 // Initialize clusters indexes. | 672 // Initialize clusters indexes. |
672 clusters[i] = i; | 673 clusters[i] = i; |
(...skipping 255 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
928 // Find the optimal map from original histograms to the final ones. | 929 // Find the optimal map from original histograms to the final ones. |
929 HistogramRemap(orig_histo, image_histo, histogram_symbols); | 930 HistogramRemap(orig_histo, image_histo, histogram_symbols); |
930 | 931 |
931 ok = 1; | 932 ok = 1; |
932 | 933 |
933 Error: | 934 Error: |
934 WebPSafeFree(bin_map); | 935 WebPSafeFree(bin_map); |
935 VP8LFreeHistogramSet(orig_histo); | 936 VP8LFreeHistogramSet(orig_histo); |
936 return ok; | 937 return ok; |
937 } | 938 } |
OLD | NEW |