1// Aseprite Document Library
2// Copyright (c) 2001-2015 David Capello
3//
4// This file is released under the terms of the MIT license.
5// Read LICENSE.txt for more information.
6
7#ifndef DOC_PRIMITIVES_FAST_H_INCLUDED
8#define DOC_PRIMITIVES_FAST_H_INCLUDED
9#pragma once
10
11#include "doc/color.h"
12
13namespace doc {
14 class Image;
15 template<typename ImageTraits> class ImageImpl;
16
17 template<class Traits>
18 inline typename Traits::address_t get_pixel_address_fast(const Image* image, int x, int y) {
19 ASSERT(x >= 0 && x < image->width());
20 ASSERT(y >= 0 && y < image->height());
21
22 return (((ImageImpl<Traits>*)image)->address(x, y));
23 }
24
25 template<class Traits>
26 inline typename Traits::pixel_t get_pixel_fast(const Image* image, int x, int y) {
27 ASSERT(x >= 0 && x < image->width());
28 ASSERT(y >= 0 && y < image->height());
29
30 return *(((ImageImpl<Traits>*)image)->address(x, y));
31 }
32
33 template<class Traits>
34 inline void put_pixel_fast(Image* image, int x, int y, typename Traits::pixel_t color) {
35 ASSERT(x >= 0 && x < image->width());
36 ASSERT(y >= 0 && y < image->height());
37
38 *(((ImageImpl<Traits>*)image)->address(x, y)) = color;
39 }
40
41 //////////////////////////////////////////////////////////////////////
42 // Bitmap specialization
43
44 template<>
45 inline BitmapTraits::pixel_t get_pixel_fast<BitmapTraits>(const Image* image, int x, int y) {
46 ASSERT(x >= 0 && x < image->width());
47 ASSERT(y >= 0 && y < image->height());
48
49 return (*image->getPixelAddress(x, y)) & (1 << (x % 8)) ? 1: 0;
50 }
51
52 template<>
53 inline void put_pixel_fast<BitmapTraits>(Image* image, int x, int y, BitmapTraits::pixel_t color) {
54 ASSERT(x >= 0 && x < image->width());
55 ASSERT(y >= 0 && y < image->height());
56
57 if (color)
58 *image->getPixelAddress(x, y) |= (1 << (x % 8));
59 else
60 *image->getPixelAddress(x, y) &= ~(1 << (x % 8));
61 }
62
63} // namespace doc
64
65#endif
66