summaryrefslogtreecommitdiff
path: root/generic/Image.cpp
blob: 1270a6a5a191eb67fd0f35812718d973060526f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include "Image.h"
#ifdef HAVE_CONFIG_H
#include "config.h"
#else
#define _(x) x
#endif

Image::Image(unsigned int ax, unsigned int ay) : x(ax), y(ay), img((Color *) malloc(x * y * sizeof(Color))) {
    Fill();
}

Image::~Image() {
    free((void *)img);
}

bool Image::CanWrite() {
    return false;
}

String Image::GetName() {
    return String(_("Image ")) + x + "x" + y;
}

void Image::Fill(Color c) {
    for (unsigned int i = 0; i < x * y; i++) {
	img[i] = c;
    }
}

Color Image::GetPixel(unsigned int px, unsigned int py) {
    if ((px >= x) || (py >= y)) {
	return Color(0, 0, 0, 0);
    }
    
    return img[x * py + px];
}

void Image::SetPixel(unsigned int px, unsigned int py, Color c) {
    if ((px >= x) || (py >= y)) {
	return;
    }

    img[x * py + px] = c;
}

#ifndef WORDS_BIGENDIAN
#define WORDS_BIGENDIAN 0
#endif

bool Image::Prepare(unsigned int f) {
    if (GetSize()) return false;
    
    switch (f) {
	case FORMAT_TGA_BASIC:
	    TGAHeader Header;
	    TGAFooter Footer;
	    
	    Header.IDLength = 0;
	    Header.ColorMapType = 0;
	    Header.ImageType = 2;
	    Header.CM_FirstEntry = 0;
	    Header.CM_Length = 0;
	    Header.CM_EntrySize = 0;
	    Header.IS_XOrigin = 0;
	    Header.IS_YOrigin = 0;
	    Header.IS_Width = WORDS_BIGENDIAN ? ((x & 0xff) << 8) | ((x & 0xff00) >> 8) : x;
	    Header.IS_Height = WORDS_BIGENDIAN ? ((y & 0xff) << 8) | ((y & 0xff00) >> 8) : y;
	    Header.IS_Depth = 32;
	    Header.IS_Descriptor = 0x20;

	    Footer.ExtOffset = 0;
	    Footer.DevOffset = 0;
	    strcpy(Footer.Sig, "TRUEVISION-XFILE.");
	    
	    write(&Header, sizeof(Header));
	    write(img, x * y * sizeof(Color));
	    write(&Footer, sizeof(Footer));
	    
	    return true;
	    
	    break;
	default:
	    return false;
    }
}