1c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
2c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// found in the LICENSE file.
4c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
5c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#ifndef UI_GFX_IMAGE_IMAGE_FAMILY_H_
6c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#define UI_GFX_IMAGE_IMAGE_FAMILY_H_
7c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
8c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include <iterator>
9c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include <map>
10c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include <utility>
11c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#include "ui/gfx/gfx_export.h"
13c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "ui/gfx/image/image.h"
14c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
15c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)namespace gfx {
16c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)class ImageSkia;
17c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)class Size;
18c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
19c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// A collection of images at different sizes. The images should be different
20c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// representations of the same basic concept (for example, an icon) at various
21c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// sizes and (optionally) aspect ratios. A method is provided for finding the
22c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// most appropriate image to fit in a given rectangle.
23c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)//
24c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// NOTE: This is not appropriate for storing an image at a single logical pixel
25c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// size, with high-DPI bitmap versions; use an Image or ImageSkia for that. Each
26c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// image in an ImageFamily should have a different logical size (and may also
27c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// include high-DPI representations).
284e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)class GFX_EXPORT ImageFamily {
29c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) private:
30424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // An <aspect ratio, DIP width> pair.
31424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // A 0x0 image has aspect ratio 1.0. 0xN and Nx0 images are treated as 0x0.
32424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  struct MapKey : std::pair<float, int> {
33424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    MapKey(float aspect, int width)
34424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)        : std::pair<float, int>(aspect, width) {}
35424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
36424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    float aspect() const { return first; }
37424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
38424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    int width() const { return second; }
39424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  };
40c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
41c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) public:
42c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Type for iterating over all images in the family, in order.
43c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Dereferencing this iterator returns a gfx::Image.
444e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  class GFX_EXPORT const_iterator :
45c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    std::iterator<std::bidirectional_iterator_tag, const gfx::Image> {
46c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)   public:
47c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const_iterator();
48c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
49c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const_iterator(const const_iterator& other);
50c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
51a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    ~const_iterator();
52a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
53c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const_iterator& operator++() {
54c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      ++map_iterator_;
55c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      return *this;
56c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    }
57c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
58c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const_iterator operator++(int /*unused*/) {
59c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      const_iterator result(*this);
60c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      ++(*this);
61c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      return result;
62c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    }
63c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
64c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const_iterator& operator--() {
65c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      --map_iterator_;
66c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      return *this;
67c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    }
68c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
69c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const_iterator operator--(int /*unused*/) {
70c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      const_iterator result(*this);
71c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      --(*this);
72c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      return result;
73c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    }
74c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
75c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    bool operator==(const const_iterator& other) const {
76c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      return map_iterator_ == other.map_iterator_;
77c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    }
78c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
79c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    bool operator!=(const const_iterator& other) const {
80c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      return map_iterator_ != other.map_iterator_;
81c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    }
82c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
83c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const gfx::Image& operator*() const {
84c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      return map_iterator_->second;
85c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    }
86c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
87c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const gfx::Image* operator->() const {
88c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      return &**this;
89c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    }
90c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
91c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)   private:
92c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    friend class ImageFamily;
93c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
94c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    explicit const_iterator(
95c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        const std::map<MapKey, gfx::Image>::const_iterator& other);
96c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
97c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    std::map<MapKey, gfx::Image>::const_iterator map_iterator_;
98c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  };
99c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
100c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  ImageFamily();
101c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  ~ImageFamily();
102c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
103c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Gets an iterator to the first image.
104c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  const_iterator begin() const { return const_iterator(map_.begin()); }
105c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Gets an iterator to one after the last image.
106c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  const_iterator end() const { return const_iterator(map_.end()); }
107c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
108c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Determines whether the image family has no images in it.
109c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  bool empty() const { return map_.empty(); }
110c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
111c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Removes all images from the family.
112c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  void clear() { return map_.clear(); }
113c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
114c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Adds an image to the family. If another image is already present at the
115c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // same size, it will be overwritten.
116c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  void Add(const gfx::Image& image);
117c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
118c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Adds an image to the family. If another image is already present at the
119c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // same size, it will be overwritten.
120c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  void Add(const gfx::ImageSkia& image_skia);
121c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
122c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Gets the best image to use in a rectangle of |width|x|height|.
123c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Gets an image at the same aspect ratio as |width|:|height|, if possible, or
124c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // if not, the closest aspect ratio. Among images of that aspect ratio,
125c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // returns the smallest image with both its width and height bigger or equal
126c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // to the requested size. If none exists, returns the largest image of that
127c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // aspect ratio. If there are no images in the family, returns NULL.
128c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  const gfx::Image* GetBest(int width, int height) const;
129c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
130c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Gets the best image to use in a rectangle of |size|.
131c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Gets an image at the same aspect ratio as |size.width()|:|size.height()|,
132c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // if possible, or if not, the closest aspect ratio. Among images of that
133c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // aspect ratio, returns the smallest image with both its width and height
134c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // bigger or equal to the requested size. If none exists, returns the largest
135c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // image of that aspect ratio. If there are no images in the family, returns
136c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // NULL.
137c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  const gfx::Image* GetBest(const gfx::Size& size) const;
138c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
139c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) private:
140c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Find the closest aspect ratio in the map to |desired_aspect|.
141c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Ties are broken by the thinner aspect.
142c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // |map_| must not be empty. |desired_aspect| must be > 0.0.
143c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  float GetClosestAspect(float desired_aspect) const;
144c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
145c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Gets an image with aspect ratio |aspect|, at the best size for |width|.
146c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Returns the smallest image of aspect ratio |aspect| with its width bigger
147c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // or equal to |width|. If none exists, returns the largest image of aspect
148c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // ratio |aspect|. Behavior is undefined if there is not at least one image in
149c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // |map_| of aspect ratio |aspect|.
150c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  const gfx::Image* GetWithExactAspect(float aspect, int width) const;
151c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
152c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Map from (aspect ratio, width) to image.
153c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  std::map<MapKey, gfx::Image> map_;
154c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)};
155c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
156c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}  // namespace gfx
157c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
158c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#endif  // UI_GFX_IMAGE_IMAGE_FAMILY_H_
159