Skip to content

Commit

Permalink
Make some changes to support systems without an OS
Browse files Browse the repository at this point in the history
  • Loading branch information
leroycep committed Mar 7, 2021
1 parent dca0b36 commit 5686ab6
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/formats/pcx.zig
Expand Up @@ -247,7 +247,7 @@ pub const PCX = struct {
x += 1;
}
},
else => std.debug.panic("{} pixel format not supported yet!", .{@tagName(pixels)}),
else => unreachable, //std.debug.panic("{} pixel format not supported yet!", .{@tagName(pixels)}),
}
}

Expand Down
7 changes: 5 additions & 2 deletions src/formats/png.zig
Expand Up @@ -15,6 +15,7 @@ const std = @import("std");
const utils = @import("../utils.zig");
const zlib = @import("../compression/zlib.zig");
const deflate = @import("../compression/deflate.zig");
const stream_source = @import("../stream_source.zig");

const PNGMagicHeader = "\x89PNG\x0D\x0A\x1A\x0A";

Expand Down Expand Up @@ -68,8 +69,10 @@ pub const IHDR = packed struct {
pub fn deinit(self: Self, allocator: *Allocator) void {}

pub fn read(self: *Self, allocator: *Allocator, read_buffer: []u8) !bool {
var stream = std.io.StreamSource{ .buffer = std.io.fixedBufferStream(read_buffer) };
self.* = try utils.readStructBig(stream.reader(), Self);
var fbs_stream: stream_source.FixedBufferStreamSource([]const u8) = undefined;
fbs_stream.init(read_buffer);
//var stream = std.io.StreamSource{ .buffer = std.io.fixedBufferStream(read_buffer) };
self.* = try utils.readStructBig(fbs_stream.stream_source.reader(), Self);
return true;
}
};
Expand Down
14 changes: 9 additions & 5 deletions src/image.zig
Expand Up @@ -8,6 +8,8 @@ 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,
Expand All @@ -20,9 +22,9 @@ pub const ImageFormat = enum {
Tga,
};

pub const ImageReader = io.StreamSource.Reader;
pub const ImageSeekStream = io.StreamSource.SeekableStream;
pub const ImageWriterStream = io.StreamSource.Writer;
pub const ImageReader = StreamSource.Reader;
pub const ImageSeekStream = StreamSource.SeekableStream;
pub const ImageWriterStream = StreamSource.Writer;

pub const ImageEncoderOptions = AllImageFormats.ImageEncoderOptions;

Expand Down Expand Up @@ -112,9 +114,11 @@ pub const Image = struct {
pub fn fromMemory(allocator: *Allocator, buffer: []const u8) !Image {
var result = init(allocator);

var stream_source = io.StreamSource{ .const_buffer = io.fixedBufferStream(buffer) };
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, stream_source.reader(), stream_source.seekableStream());
try result.internalRead(allocator, fbs_stream.stream_source.reader(), fbs_stream.stream_source.seekableStream());

return result;
}
Expand Down
141 changes: 141 additions & 0 deletions src/stream_source.zig
@@ -0,0 +1,141 @@
// SPDX-License-Identifier: MIT
// Copyright (c) 2015-2021 Zig Contributors
// This file is part of [zig](https://ziglang.org/), which is MIT licensed.
// The MIT license requires this copyright notice to be included in all copies
// and substantial portions of the software.
const std = @import("std");
const io = std.io;
const testing = std.testing;

/// Provides `io.Reader`, `io.Writer`, and `io.SeekableStream` for in-memory buffers as
/// well as files.
/// For memory sources, if the supplied byte buffer is const, then `io.Writer` is not available.
/// The error set of the stream functions is the error set of the corresponding file functions.
pub const StreamSource = struct {
read: fn (self: *StreamSource, dest: []u8) ReadError!usize,
write: fn (self: *StreamSource, bytes: []const u8) WriteError!usize = writeNotSupported,
seekTo: fn (self: *StreamSource, pos: u64) SeekError!void,
seekBy: fn (self: *StreamSource, amt: i64) SeekError!void,
getEndPos: fn (self: *StreamSource) GetSeekPosError!u64,
getPos: fn (self: *StreamSource) GetSeekPosError!u64,

pub const ReadError = std.fs.File.ReadError;
pub const WriteError = std.fs.File.WriteError;
pub const SeekError = std.fs.File.SeekError;
pub const GetSeekPosError = std.fs.File.GetPosError;

pub const Reader = io.Reader(*StreamSource, ReadError, read);
pub const Writer = io.Writer(*StreamSource, WriteError, write);
pub const SeekableStream = io.SeekableStream(
*StreamSource,
SeekError,
GetSeekPosError,
seekTo,
seekBy,
getPos,
getEndPos,
);

pub fn read(self: *StreamSource, dest: []u8) ReadError!usize {
return self.read(self, dest);
}

pub fn write(self: *StreamSource, bytes: []const u8) WriteError!usize {
return self.write(self, bytes);
}

pub fn seekTo(self: *StreamSource, pos: u64) SeekError!void {
return self.seekTo(self, pos);
}

pub fn seekBy(self: *StreamSource, amt: i64) SeekError!void {
return self.seekBy(self, amt);
}

pub fn getEndPos(self: *StreamSource) GetSeekPosError!u64 {
return self.getEndPos(
self,
);
}

pub fn getPos(self: *StreamSource) GetSeekPosError!u64 {
return self.getPos(
self,
);
}

fn writeNotSupported(self: *StreamSource, bytes: []const u8) WriteError!usize {
return error.AccessDenied;
}

pub fn reader(self: *StreamSource) Reader {
return .{ .context = self };
}

pub fn writer(self: *StreamSource) Writer {
return .{ .context = self };
}

pub fn seekableStream(self: *StreamSource) SeekableStream {
return .{ .context = self };
}
};

pub fn FixedBufferStreamSource(comptime Buffer: type) type {
return struct {
fbs: io.FixedBufferStream(Buffer),
stream_source: StreamSource = StreamSource{
.read = read,
.write = write,
.seekTo = seekTo,
.seekBy = seekBy,
.getEndPos = getEndPos,
.getPos = getPos,
},

pub const ReadError = StreamSource.ReadError;
pub const WriteError = StreamSource.WriteError;
pub const SeekError = StreamSource.SeekError;
pub const GetSeekPosError = StreamSource.GetSeekPosError;

pub fn init(this: *@This(), buffer: Buffer) void {
this.* = .{
.fbs = io.fixedBufferStream(buffer),
};
}

fn read(stream_source: *StreamSource, dest: []u8) ReadError!usize {
const this = @fieldParentPtr(@This(), "stream_source", stream_source);
return this.fbs.read(dest);
}

fn write(stream_source: *StreamSource, bytes: []const u8) WriteError!usize {
const this = @fieldParentPtr(@This(), "stream_source", stream_source);
switch (Buffer) {
[]u8 => return this.fbs.write(bytes),
[]const u8 => return error.AccessDenied,
else => @compileError("Type not supported in FixedBufferStreamSource: " ++ @typeName(Buffer)),
}
}

fn seekTo(stream_source: *StreamSource, pos: u64) SeekError!void {
const this = @fieldParentPtr(@This(), "stream_source", stream_source);
return this.fbs.seekTo(pos);
}

fn seekBy(stream_source: *StreamSource, amt: i64) SeekError!void {
const this = @fieldParentPtr(@This(), "stream_source", stream_source);
return this.fbs.seekBy(amt);
}

fn getEndPos(stream_source: *StreamSource) GetSeekPosError!u64 {
const this = @fieldParentPtr(@This(), "stream_source", stream_source);
return this.fbs.getEndPos();
}

fn getPos(stream_source: *StreamSource) GetSeekPosError!u64 {
const this = @fieldParentPtr(@This(), "stream_source", stream_source);
return this.fbs.getPos();
}
};
}
8 changes: 5 additions & 3 deletions src/utils.zig
Expand Up @@ -2,6 +2,7 @@ const builtin = std.builtin;
const std = @import("std");
const io = std.io;
const meta = std.meta;
const image = @import("image.zig");

pub fn toMagicNumberNative(magic: []const u8) u32 {
var result: u32 = 0;
Expand Down Expand Up @@ -29,11 +30,11 @@ pub const toMagicNumberLittle = switch (builtin.endian) {
builtin.Endian.Big => toMagicNumberForeign,
};

pub fn readStructNative(reader: io.StreamSource.Reader, comptime T: type) !T {
pub fn readStructNative(reader: image.ImageReader, comptime T: type) !T {
return try reader.readStruct(T);
}

pub fn readStructForeign(reader: io.StreamSource.Reader, comptime T: type) !T {
pub fn readStructForeign(reader: image.ImageReader, comptime T: type) !T {
comptime std.debug.assert(@typeInfo(T).Struct.layout != builtin.TypeInfo.ContainerLayout.Auto);

var result: T = undefined;
Expand All @@ -53,7 +54,8 @@ pub fn readStructForeign(reader: io.StreamSource.Reader, comptime T: type) !T {
});
},
else => {
std.debug.panic("Add support for type {} in readStructForeign", .{@typeName(entry.field_type)});
unreachable;
//@panic("Add support for type {} in readStructForeign");//, .{@typeName(entry.field_type)});
},
}
}
Expand Down

0 comments on commit 5686ab6

Please sign in to comment.