1#include "image.h"
2
3#include <game/mapitems.h>
4
5CEditorImage::CEditorImage(CEditor *pEditor) :
6 m_AutoMapper(pEditor)
7{
8 Init(pEditor);
9 m_Texture.Invalidate();
10}
11
12CEditorImage::~CEditorImage()
13{
14 Graphics()->UnloadTexture(pIndex: &m_Texture);
15 free(ptr: m_pData);
16 m_pData = nullptr;
17}
18
19void CEditorImage::Init(CEditor *pEditor)
20{
21 CEditorComponent::Init(pEditor);
22 RegisterSubComponent(Component&: m_AutoMapper);
23 InitSubComponents();
24}
25
26void CEditorImage::AnalyseTileFlags()
27{
28 mem_zero(block: m_aTileFlags, size: sizeof(m_aTileFlags));
29
30 size_t tw = m_Width / 16; // tilesizes
31 size_t th = m_Height / 16;
32 if(tw == th && m_Format == CImageInfo::FORMAT_RGBA)
33 {
34 int TileId = 0;
35 for(size_t ty = 0; ty < 16; ty++)
36 for(size_t tx = 0; tx < 16; tx++, TileId++)
37 {
38 bool Opaque = true;
39 for(size_t x = 0; x < tw; x++)
40 for(size_t y = 0; y < th; y++)
41 {
42 size_t p = (ty * tw + y) * m_Width + tx * tw + x;
43 if(m_pData[p * 4 + 3] < 250)
44 {
45 Opaque = false;
46 break;
47 }
48 }
49
50 if(Opaque)
51 m_aTileFlags[TileId] |= TILEFLAG_OPAQUE;
52 }
53 }
54}
55
56bool CEditorImage::DataEquals(const CEditorImage &Other) const
57{
58 // If height, width or pixel size don't match, then data cannot be equal
59 const size_t ImgPixelSize = PixelSize();
60
61 if(Other.m_Height != m_Height || Other.m_Width != m_Width || Other.PixelSize() != ImgPixelSize)
62 return false;
63
64 const auto &&GetPixel = [&](uint8_t *pData, int x, int y, size_t p) -> uint8_t {
65 return pData[x * ImgPixelSize + (m_Width * ImgPixelSize * y) + p];
66 };
67
68 // Look through every pixel and check if there are any difference
69 for(size_t y = 0; y < m_Height; y += ImgPixelSize)
70 {
71 for(size_t x = 0; x < m_Width; x += ImgPixelSize)
72 {
73 for(size_t p = 0; p < ImgPixelSize; p++)
74 if(GetPixel(m_pData, x, y, p) != GetPixel(Other.m_pData, x, y, p))
75 return false;
76 }
77 }
78
79 return true;
80}
81