forked from zigimg/zigimg
/
image.zig
243 lines (190 loc) · 7.77 KB
/
image.zig
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
const AllImageFormats = @import("formats/all.zig");
const Allocator = std.mem.Allocator;
const Color = color.Color;
const ColorStorage = color.ColorStorage;
const FormatInterface = @import("format_interface.zig").FormatInterface;
const PixelFormat = @import("pixel_format.zig").PixelFormat;
const color = @import("color.zig");
const errors = @import("errors.zig");
const io = std.io;
const std = @import("std");
const stream_source = @import("./stream_source.zig");
const StreamSource = stream_source.StreamSource;
pub const ImageFormat = enum {
Bmp,
Pbm,
Pcx,
Pgm,
Png,
Ppm,
Raw,
Tga,
};
pub const ImageReader = StreamSource.Reader;
pub const ImageSeekStream = StreamSource.SeekableStream;
pub const ImageWriterStream = StreamSource.Writer;
pub const ImageEncoderOptions = AllImageFormats.ImageEncoderOptions;
pub const ImageSaveInfo = struct {
width: usize,
height: usize,
encoder_options: ImageEncoderOptions,
};
pub const ImageInfo = struct {
width: usize = 0,
height: usize = 0,
pixel_format: PixelFormat = undefined,
};
/// Format-independant image
pub const Image = struct {
allocator: *Allocator = undefined,
width: usize = 0,
height: usize = 0,
pixels: ?ColorStorage = null,
pixel_format: PixelFormat = undefined,
image_format: ImageFormat = undefined,
const Self = @This();
const FormatInteraceFnType = fn () FormatInterface;
const allInterfaceFuncs = comptime blk: {
const allFormatDecls = std.meta.declarations(AllImageFormats);
var result: [allFormatDecls.len]FormatInteraceFnType = undefined;
var index: usize = 0;
for (allFormatDecls) |decl| {
switch (decl.data) {
.Type => |entryType| {
const entryTypeInfo = @typeInfo(entryType);
if (entryTypeInfo == .Struct) {
for (entryTypeInfo.Struct.decls) |structEntry| {
if (std.mem.eql(u8, structEntry.name, "formatInterface")) {
result[index] = @field(entryType, structEntry.name);
index += 1;
break;
}
}
}
},
else => {},
}
}
break :blk result[0..index];
};
pub fn init(allocator: *Allocator) Self {
return Self{
.allocator = allocator,
};
}
pub fn deinit(self: Self) void {
if (self.pixels) |pixels| {
pixels.deinit(self.allocator);
}
}
pub fn fromFilePath(allocator: *Allocator, file_path: []const u8) !Self {
const cwd = std.fs.cwd();
var resolvedPath = try std.fs.path.resolve(allocator, &[_][]const u8{file_path});
defer allocator.free(resolvedPath);
var file = try cwd.openFile(resolvedPath, .{});
defer file.close();
return fromFile(allocator, &file);
}
pub fn fromFile(allocator: *Allocator, file: *std.fs.File) !Self {
var result = init(allocator);
var stream_source = io.StreamSource{ .file = file.* };
try result.internalRead(allocator, stream_source.reader(), stream_source.seekableStream());
return result;
}
pub fn fromMemory(allocator: *Allocator, buffer: []const u8) !Image {
var result = init(allocator);
var fbs_stream: stream_source.FixedBufferStreamSource([]const u8) = undefined;
fbs_stream.init(buffer);
//var stream_source = io.StreamSource{ .const_buffer = io.fixedBufferStream(buffer) };
try result.internalRead(allocator, fbs_stream.stream_source.reader(), fbs_stream.stream_source.seekableStream());
return result;
}
pub fn create(allocator: *Allocator, width: usize, height: usize, pixel_format: PixelFormat) !Self {
var result = Self{
.allocator = allocator,
.width = width,
.height = height,
.pixel_format = pixel_format,
.image_format = .Raw,
.pixels = try ColorStorage.init(allocator, pixel_format, width * height),
};
return result;
}
pub fn writeToFilePath(self: Self, file_path: []const u8, image_format: ImageFormat, encoder_options: ImageEncoderOptions) !void {
if (self.pixels == null) {
return error.NoPixelData;
}
const cwd = std.fs.cwd();
var resolved_path = try std.fs.path.resolve(self.allocator, &[_][]const u8{file_path});
defer self.allocator.free(resolved_path);
var file = try cwd.createFile(resolved_path, .{});
defer file.close();
try self.writeToFile(&file, image_format, encoder_options);
}
pub fn writeToFile(self: Self, file: *std.fs.File, image_format: ImageFormat, encoder_options: ImageEncoderOptions) !void {
if (self.pixels == null) {
return error.NoPixelData;
}
var image_save_info = ImageSaveInfo{
.width = self.width,
.height = self.height,
.encoder_options = encoder_options,
};
var format_interface = try findImageInterfaceFromImageFormat(image_format);
var stream_source = io.StreamSource{ .file = file.* };
if (self.pixels) |pixels| {
try format_interface.writeForImage(self.allocator, stream_source.writer(), stream_source.seekableStream(), pixels, image_save_info);
}
}
pub fn writeToMemory(self: Self, write_buffer: []u8, image_format: ImageFormat, encoder_options: ImageEncoderOptions) ![]u8 {
if (self.pixels == null) {
return error.NoPixelData;
}
var image_save_info = ImageSaveInfo{
.width = self.width,
.height = self.height,
.encoder_options = encoder_options,
};
var format_interface = try findImageInterfaceFromImageFormat(image_format);
var stream_source = io.StreamSource{ .buffer = std.io.fixedBufferStream(write_buffer) };
if (self.pixels) |pixels| {
try format_interface.writeForImage(self.allocator, stream_source.writer(), stream_source.seekableStream(), pixels, image_save_info);
}
return stream_source.buffer.getWritten();
}
pub fn iterator(self: Self) color.ColorStorageIterator {
if (self.pixels) |*pixels| {
return color.ColorStorageIterator.init(pixels);
}
return color.ColorStorageIterator.initNull();
}
fn internalRead(self: *Self, allocator: *Allocator, reader: ImageReader, seekStream: ImageSeekStream) !void {
var formatInterface = try findImageInterfaceFromStream(reader, seekStream);
self.image_format = formatInterface.format();
try seekStream.seekTo(0);
const imageInfo = try formatInterface.readForImage(allocator, reader, seekStream, &self.pixels);
self.width = imageInfo.width;
self.height = imageInfo.height;
self.pixel_format = imageInfo.pixel_format;
}
fn findImageInterfaceFromStream(reader: ImageReader, seekStream: ImageSeekStream) !FormatInterface {
for (allInterfaceFuncs) |intefaceFn| {
const formatInterface = intefaceFn();
try seekStream.seekTo(0);
const found = try formatInterface.formatDetect(reader, seekStream);
if (found) {
return formatInterface;
}
}
return errors.ImageFormatInvalid;
}
fn findImageInterfaceFromImageFormat(image_format: ImageFormat) !FormatInterface {
for (allInterfaceFuncs) |intefaceFn| {
const formatInterface = intefaceFn();
if (formatInterface.format() == image_format) {
return formatInterface;
}
}
return errors.ImageFormatInvalid;
}
};