| 1 |
//---------------------------------------------------------------------------- |
| 2 |
// Anti-Grain Geometry - Version 2.4 |
| 3 |
// Copyright (C) 2002-2005 Maxim Shemanarev (http://www.antigrain.com) |
| 4 |
// |
| 5 |
// Permission to copy, use, modify, sell and distribute this software |
| 6 |
// is granted provided this copyright notice appears in all copies. |
| 7 |
// This software is provided "as is" without express or implied |
| 8 |
// warranty, and with no claim as to its suitability for any purpose. |
| 9 |
// |
| 10 |
//---------------------------------------------------------------------------- |
| 11 |
// Contact: [email protected] |
| 12 |
// [email protected] |
| 13 |
// http://www.antigrain.com |
| 14 |
//---------------------------------------------------------------------------- |
| 15 |
// |
| 16 |
// Conversion from one colorspace/pixel format to another |
| 17 |
// |
| 18 |
//---------------------------------------------------------------------------- |
| 19 |
|
| 20 |
#ifndef AGG_COLOR_CONV_INCLUDED |
| 21 |
#define AGG_COLOR_CONV_INCLUDED |
| 22 |
|
| 23 |
#include <string.h> |
| 24 |
#include "agg_basics.h" |
| 25 |
#include "agg_rendering_buffer.h" |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
namespace agg |
| 31 |
{ |
| 32 |
|
| 33 |
//--------------------------------------------------------------color_conv |
| 34 |
template<class RenBuf, class CopyRow> |
| 35 |
void color_conv(RenBuf* dst, const RenBuf* src, CopyRow copy_row_functor) |
| 36 |
{ |
| 37 |
unsigned width = src->width(); |
| 38 |
unsigned height = src->height(); |
| 39 |
|
| 40 |
if(dst->width() < width) width = dst->width(); |
| 41 |
if(dst->height() < height) height = dst->height(); |
| 42 |
|
| 43 |
if(width) |
| 44 |
{ |
| 45 |
unsigned y; |
| 46 |
for(y = 0; y < height; y++) |
| 47 |
{ |
| 48 |
copy_row_functor(dst->row_ptr(0, y, width), |
| 49 |
src->row_ptr(y), |
| 50 |
width); |
| 51 |
} |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
//---------------------------------------------------------color_conv_row |
| 57 |
template<class CopyRow> |
| 58 |
void color_conv_row(int8u* dst, |
| 59 |
const int8u* src, |
| 60 |
unsigned width, |
| 61 |
CopyRow copy_row_functor) |
| 62 |
{ |
| 63 |
copy_row_functor(dst, src, width); |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
//---------------------------------------------------------color_conv_same |
| 68 |
template<int BPP> class color_conv_same |
| 69 |
{ |
| 70 |
public: |
| 71 |
void operator () (int8u* dst, |
| 72 |
const int8u* src, |
| 73 |
unsigned width) const |
| 74 |
{ |
| 75 |
memmove(dst, src, width*BPP); |
| 76 |
} |
| 77 |
}; |
| 78 |
|
| 79 |
|
| 80 |
// Generic pixel converter. |
| 81 |
template<class DstFormat, class SrcFormat> |
| 82 |
struct conv_pixel |
| 83 |
{ |
| 84 |
void operator()(void* dst, const void* src) const |
| 85 |
{ |
| 86 |
// Read a pixel from the source format and write it to the destination format. |
| 87 |
DstFormat::write_plain_color(dst, SrcFormat::read_plain_color(src)); |
| 88 |
} |
| 89 |
}; |
| 90 |
|
| 91 |
// Generic row converter. Uses conv_pixel to convert individual pixels. |
| 92 |
template<class DstFormat, class SrcFormat> |
| 93 |
struct conv_row |
| 94 |
{ |
| 95 |
void operator()(void* dst, const void* src, unsigned width) const |
| 96 |
{ |
| 97 |
conv_pixel<DstFormat, SrcFormat> conv; |
| 98 |
do |
| 99 |
{ |
| 100 |
conv(dst, src); |
| 101 |
dst = (int8u*)dst + DstFormat::pix_width; |
| 102 |
src = (int8u*)src + SrcFormat::pix_width; |
| 103 |
} |
| 104 |
while (--width); |
| 105 |
} |
| 106 |
}; |
| 107 |
|
| 108 |
// Specialization for case where source and destination formats are identical. |
| 109 |
template<class Format> |
| 110 |
struct conv_row<Format, Format> |
| 111 |
{ |
| 112 |
void operator()(void* dst, const void* src, unsigned width) const |
| 113 |
{ |
| 114 |
memmove(dst, src, width * Format::pix_width); |
| 115 |
} |
| 116 |
}; |
| 117 |
|
| 118 |
// Top-level conversion function, converts one pixel format to any other. |
| 119 |
template<class DstFormat, class SrcFormat, class RenBuf> |
| 120 |
void convert(RenBuf* dst, const RenBuf* src) |
| 121 |
{ |
| 122 |
color_conv(dst, src, conv_row<DstFormat, SrcFormat>()); |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
#endif |