CopyPixel.h
1/* Copyright (C) 2010-2016 Marc Boris Duerner
2 Copyright (C) 2006-2010 by Aloysius Indrayanto
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 As a special exception, you may use this file as part of a free
10 software library without restriction. Specifically, if other files
11 instantiate templates or use macros or inline functions from this
12 file, or you compile this file and link it with other files to
13 produce an executable, this file does not by itself cause the
14 resulting executable to be covered by the GNU General Public
15 License. This exception does not however invalidate any other
16 reasons why the executable file might be covered by the GNU Library
17 General Public License.
18
19 This library is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 Lesser General Public License for more details.
23
24 You should have received a copy of the GNU Lesser General Public
25 License along with this library; if not, write to the Free Software
26 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
27 02110-1301 USA
28*/
29
30#ifndef PT_GFX_COPYPIXEL_H
31#define PT_GFX_COPYPIXEL_H
32
33#include <Pt/Gfx/Api.h>
34#include <Pt/Gfx/ConvertColor.h>
35#include <Pt/Gfx/ImageTraits.h>
36#include <Pt/TypeTraits.h>
37
38#include <algorithm>
39#include <cstddef>
40
41namespace Pt {
42
43namespace Gfx {
44
46// PixelConverter
48
49template <typename Fmt1, typename Fmt2>
50struct PixelConverter
51{
52 template <typename P1, typename P2>
53 static void convert(const P1& from, P2& to)
54 {
55 typedef typename P2::ColorType ColorType;
56
57 to = convertColor<ColorType>( from.getColor() );
58 }
59
60 template <typename P1, typename P2>
61 static void convert(const P1& from, P2& to, std::size_t length)
62 {
63 typedef typename P1::ColorType ColorT1;
64 typedef typename P2::ColorType ColorT2;
65
66 convertImpl(from, to, length, IsSame<ColorT1, ColorT2>());
67 }
68
69 template <typename P1, typename P2>
70 static void convertImpl(const P1& from, P2& to, std::size_t length, TrueType)
71 {
72 typedef typename P1::ColorType ColorType;
73
74 const std::size_t bufsize = 64;
75 ColorType colors[bufsize];
76
77 P1 f(from);
78 P2 t(to);
79
80 while(length > 0)
81 {
82 std::size_t n = std::min(length, bufsize);
83
84 f.getColors(colors, n);
85 t.assign(colors, n);
86
87 f.advance(n);
88 t.advance(n);
89
90 length -= n;
91 }
92 }
93
94 template <typename P1, typename P2>
95 static void convertImpl(const P1& from, P2& to, std::size_t length, FalseType)
96 {
97 typedef typename P1::ColorType ColorT1;
98 typedef typename P2::ColorType ColorT2;
99
100 const std::size_t bufsize = 64;
101 ColorT1 fromColors[bufsize];
102 ColorT2 toColors[bufsize];
103
104 P1 f(from);
105 P2 t(to);
106
107 while(length > 0)
108 {
109 std::size_t n = std::min(length, bufsize);
110
111 f.getColors(fromColors, n);
112 convertColors(toColors, fromColors, n);
113 t.assign(toColors, n);
114
115 f.advance(n);
116 t.advance(n);
117
118 length -= n;
119 }
120 }
121};
122
123template <typename Fmt>
124struct PixelConverter<Fmt, Fmt>
125{
126 template <typename P1, typename P2>
127 static void convert(const P1& from, P2& to)
128 {
129 to.assign(from);
130 }
131
132 template <typename P1, typename P2>
133 static void convert(const P1& from, P2& to, std::size_t length)
134 {
135 to.assign(from, length);
136 }
137};
138
140// copy pixels
142
145template <typename P1, typename P2>
146void copyPixel(const P1& from, P2& to)
147{
148 typedef typename P1::FormatType Fmt1;
149 typedef typename P2::FormatType Fmt2;
150
151 PixelConverter<Fmt1, Fmt2>::convert(from, to);
152}
153
156template <typename P1, typename P2>
157void copyPixel(const P1& from, P2& to, std::size_t length)
158{
159 typedef typename P1::FormatType Fmt1;
160 typedef typename P2::FormatType Fmt2;
161
162 PixelConverter<Fmt1, Fmt2>::convert(from, to, length);
163}
164
165} // namespace Gfx
166
167} // namespace Pt
168
169#endif
Graphics and imaging services.
Definition Api-Argb32Image.h:12
void copyPixel(const P1 &from, P2 &to)
Copies a single pixel.
Definition CopyPixel.h:146
Core module.
Definition Allocator.h:33