1// Aseprite Document Library
2// Copyright (c) 2018-2020 Igara Studio S.A.
3// Copyright (c) 2001-2016 David Capello
4//
5// This file is released under the terms of the MIT license.
6// Read LICENSE.txt for more information.
7
8#ifdef HAVE_CONFIG_H
9#include "config.h"
10#endif
11
12#include "doc/image.h"
13
14#include "doc/algo.h"
15#include "doc/brush.h"
16#include "doc/image_impl.h"
17#include "doc/palette.h"
18#include "doc/primitives.h"
19#include "doc/rgbmap.h"
20
21namespace doc {
22
23Image::Image(const ImageSpec& spec)
24 : Object(ObjectType::Image)
25 , m_spec(spec)
26{
27}
28
29Image::~Image()
30{
31}
32
33int Image::getMemSize() const
34{
35 return sizeof(Image) + getRowStrideSize()*height();
36}
37
38int Image::getRowStrideSize() const
39{
40 return getRowStrideSize(width());
41}
42
43int Image::getRowStrideSize(int pixels_per_row) const
44{
45 return calculate_rowstride_bytes(pixelFormat(), pixels_per_row);
46}
47
48// static
49Image* Image::create(PixelFormat format, int width, int height,
50 const ImageBufferPtr& buffer)
51{
52 return Image::create(ImageSpec((ColorMode)format, width, height, 0), buffer);
53}
54
55// static
56Image* Image::create(const ImageSpec& spec,
57 const ImageBufferPtr& buffer)
58{
59 ASSERT(spec.width() >= 1 && spec.height() >= 1);
60 if (spec.width() < 1 || spec.height() < 1)
61 return nullptr;
62
63 switch (spec.colorMode()) {
64 case ColorMode::RGB: return new ImageImpl<RgbTraits>(spec, buffer);
65 case ColorMode::GRAYSCALE: return new ImageImpl<GrayscaleTraits>(spec, buffer);
66 case ColorMode::INDEXED: return new ImageImpl<IndexedTraits>(spec, buffer);
67 case ColorMode::BITMAP: return new ImageImpl<BitmapTraits>(spec, buffer);
68 case ColorMode::TILEMAP: return new ImageImpl<TilemapTraits>(spec, buffer);
69 }
70 return nullptr;
71}
72
73// static
74Image* Image::createCopy(const Image* image, const ImageBufferPtr& buffer)
75{
76 ASSERT(image);
77 return crop_image(image, 0, 0, image->width(), image->height(),
78 image->maskColor(), buffer);
79}
80
81} // namespace doc
82