-
Notifications
You must be signed in to change notification settings - Fork 3
/
icnssave.c
185 lines (156 loc) · 5.2 KB
/
icnssave.c
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#include <stdio.h>
#include <libgimp/gimp.h>
#include "icnsdata.h"
#include "main.h"
#define icns_debug g_print
guchar *icns_get_alpha (guint width, guint height, guchar *pixels)
{
// Assuming RGBA
guchar *alpha;
guint npixels;
npixels = width * height;
alpha = g_new (guchar, npixels);
if (alpha)
{
guint i;
for (i = 0; i < npixels; i++)
alpha[i] = pixels[i * 4 + 3];
}
return alpha;
}
/*
* Each color channel (red, green, blue) is treated as a single line of pixels.
* Runs of same-value pixels are stored as (count - 3) | 128 followed by the value.
* Runs of different-value pixels are stored as (count - 1) followed by the series of values;
*/
guchar *icns_compress (guint width, guint height, guchar *rgba, guint *out_size)
{
const guint npixels = width * height;
const guint max_size = width * height * 3 * (130 / 128) + 1; // ?
const guint min_run = 3; /* Shorter run must be stored as uncompressed */
const guint max_run = 130; /* Longest same-value run that can be stored */
const guint min_raw = 1;
const guint max_raw = 128; /* Longest run of non-matching pixels */
guint i, j, size, channel, run, marker;
guchar *out_data;
guchar *run_length;
run_length = g_new (guchar, npixels);
if (!run_length)
{
g_warning ("icns_compress: couldn't allocate run count buffer (%d bytes)", npixels);
return NULL;
}
out_data = g_new (guchar, max_size);
if (!out_data)
{
g_warning ("icns_compress: couldn't allocate output buffer (%d bytes)", max_size);
g_free (run_length);
return NULL;
}
size = 0;
/* For some reason 128x128 icons have an extra 4 bytes at the start */
if (width == 128 && height == 128)
{
out_data[size++] = 0;
out_data[size++] = 0;
out_data[size++] = 0;
out_data[size++] = 0;
}
for (channel = 0; channel < 3; channel++)
{
icns_debug ("Channel %d at output %d\n", channel, size);
/* Count all run lengths */
for (i = 0; i < npixels; i++)
{
for (run = 1; run < max_run && run + i - 1 < npixels ; run++)
if (rgba[i * 4 + channel] != rgba[(i + run) * 4 + channel])
break;
run_length[i] = run;
}
for (i = 0; i < npixels; i++)
{
icns_debug ("[%6d] ", i);
if (run_length[i] >= min_run)
{
/* Compressable! Store and skip ahead */
icns_debug ("Compressed run of %d pixels at %d\n", run_length[i], size);
out_data[size++] = (run_length[i] - min_run) | 0x80;
out_data[size++] = rgba[i * 4 + channel];
i += run_length[i] - 1;
}
else
{
/* Too short: stuff together as many as you can in a raw run */
marker = size++;
run = 0;
while (run < max_raw && i < npixels && run_length[i] < min_run)
{
for (j = 0; j < run_length[i]; j++)
{
out_data[size++] = rgba[(i + j) * 4 + channel];
run++;
}
i += run_length[i];
}
out_data[marker] = run - min_raw;
icns_debug ("Uncompressed run of %d pixels at %d\n", run, marker);
i--; /* Overcorrected */
}
}
}
g_free (run_length);
*out_size = size;
return out_data;
}
GimpPDBStatusType
SaveICNS (const gchar *file_name,
gint32 image_ID)
{
// Horrible awful temporary hack
ResourceHeader icnsHeader;
ResourceHeader it32Header;
ResourceHeader t8mkHeader;
guchar *compData, *alphaData;
FILE *outfile;
guchar *pixels;
gint dataSize;
if (gimp_image_base_type (image_ID) != GIMP_RGB)
{
if (! gimp_image_convert_rgb (image_ID))
return GIMP_PDB_EXECUTION_ERROR;
}
gint *layers;
gint nlayers;
GimpPixelRgn region;
layers = gimp_image_get_layers (image_ID, &nlayers);
GimpDrawable *drawable = gimp_drawable_get (layers[0]);
pixels = g_new (guchar, 128*128*4);
gimp_pixel_rgn_init (®ion, drawable, 0, 0, 128, 128, TRUE, FALSE);
gimp_pixel_rgn_get_rect (®ion, pixels, 0, 0, 128, 128);
compData = icns_compress (128, 128, pixels, &dataSize);
alphaData = icns_get_alpha (128, 128, pixels);
it32Header.type = GUINT32_TO_BE ('it32');
it32Header.size = GUINT32_TO_BE (sizeof (ResourceHeader) + dataSize);
t8mkHeader.type = GUINT32_TO_BE ('t8mk');
t8mkHeader.size = GUINT32_TO_BE (sizeof (ResourceHeader) + 128*128);
icnsHeader.type = GUINT32_TO_BE ('icns');
icnsHeader.size = dataSize + 128*128 + 3 * sizeof (ResourceHeader);
outfile = fopen (file_name, "wb");
if (outfile)
{
fwrite (&icnsHeader, 1, sizeof (ResourceHeader), outfile);
fwrite (&it32Header, 1, sizeof (ResourceHeader), outfile);
fwrite (compData, 1, dataSize, outfile);
fwrite (&t8mkHeader, 1, sizeof (ResourceHeader), outfile);
fwrite (alphaData, 1, 128*128, outfile);
fclose (outfile);
}
else
{
g_warning ("SaveICNS: couldn't open output file");
}
g_free (pixels);
g_free (compData);
g_free (alphaData);
return GIMP_PDB_SUCCESS;
}