Skip to content

Instantly share code, notes, and snippets.

@adamzaninovich
Created September 22, 2017 18:35
Show Gist options
  • Save adamzaninovich/fb3de271af26885cbae3d2851140c62c to your computer and use it in GitHub Desktop.
Save adamzaninovich/fb3de271af26885cbae3d2851140c62c to your computer and use it in GitHub Desktop.
defmodule PNG do
defstruct [:length, :width, :height, :bit_depth, :color_type,
:compression_method, :filter_method, :interlace_method,
:crc, :chunks]
def new(len, width, height, bit_depth, color_type, compression_method,
filter_method, interlace_method, crc, chunks) do
%__MODULE__{
length: len,
width: width,
height: height,
bit_depth: bit_depth,
color_type: color_type,
compression_method: compression_method,
filter_method: filter_method,
interlace_method: interlace_method,
crc: crc,
chunks: chunks
}
end
def parse_png(<<0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A,
len :: size(32),
"IHDR",
width :: size(32),
height :: size(32),
bit_depth,
color_type,
compression_method,
filter_method,
interlace_method,
crc :: size(32),
chunks :: binary>>) do
new(len, width, height, bit_depth, color_type, compression_method,
filter_method, interlace_method, crc, chunks)
end
def to_binary(%__MODULE__{} = png) do
<<0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A,
png.length :: size(32),
"IHDR",
png.width :: size(32),
png.height :: size(32),
png.bit_depth,
png.color_type,
png.compression_method,
png.filter_method,
png.interlace_method,
png.crc :: size(32),
png.chunks :: binary>>
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment