Skip to content
This repository has been archived by the owner on Sep 14, 2022. It is now read-only.

Commit

Permalink
Port file I/O to new file classes
Browse files Browse the repository at this point in the history
  • Loading branch information
cfeck committed Mar 9, 2012
1 parent 1f59d7e commit e66d7f0
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 124 deletions.
2 changes: 1 addition & 1 deletion decoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class ImageDecoder : public BitDecoder<Code>
memcpy(dCount, staticdCount, sizeof(dCount));
}

void decodeImagePixels(Image<> &im) {
void decodeImagePixels(Image<> &im) __attribute__((always_inline)) {
unsigned char *p = (unsigned char *) im.data();
const int bpr = bpp * im.width();
im.setSamplesPerLine(bpr);
Expand Down
2 changes: 1 addition & 1 deletion encoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class ImageEncoder : public BitEncoder<Code>
memcpy(dCount, staticdCount, sizeof(dCount));
}

void encodeImagePixels(const Image<> &im) {
void encodeImagePixels(const Image<> &im) __attribute__((always_inline)) {
const int bpr = im.samplesPerLine();
const unsigned char *p = im.data();
int size = im.width() * im.height();
Expand Down
86 changes: 42 additions & 44 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -1,70 +1,56 @@
#include <unistd.h>
#include <fcntl.h>
#include <cstdio>
#include <cstdlib>
#include <cstddef>
#include <cstring>

#include <sys/stat.h>

#include "libiz.h"
#include "encoder.h"
#include "decoder.h"
#include "portableimage.h"

using namespace IZ;
#include "file.h"

static void decodeIZ(const char *infilename, const char *outfilename)
{
PortableImage pi;
int infd = ::open(infilename, O_RDONLY);
if (infd < 0) {
InputFile infile(infilename);
if (!infile.isReadable()) {
perror("Cannot open input file");
exit(EXIT_FAILURE);
}
int outfd = ::open(outfilename, O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (outfd < 0) {
OutputFile outfile(outfilename);
if (!outfile.isWritable()) {
perror("Cannot open output file");
exit(EXIT_FAILURE);
}
struct stat sb;
fstat(infd, &sb);
int insize = sb.st_size;
unsigned char *src = new unsigned char[insize + 17];
int pos = 0;
int remaining = insize;
while (remaining > 0) {
int bytesRead = ::read(infd, src + pos, remaining);
if (bytesRead < 0)
break;
remaining -= bytesRead;
pos += bytesRead;
}
if (remaining > 0) {
perror("Cannot read input file");
IZ::initDecodeTable();
IZ::ImageDecoder<3> ic;
ic.begin(infile.data());
ic.decodeImageSize(pi);
pi.setComponents(3);
const unsigned int dataSize = pi.width() * pi.height() * pi.components();
unsigned char *dest = outfile.prepareData(dataSize + 33);
if (!dest) {
perror("Cannot write output file");
exit(EXIT_FAILURE);
}
initDecodeTable();
pi.setComponents(3);
decodeImage(pi, src);
pi.write(outfd);
::close(infd);
::close(outfd);
delete[] src;
pi.writeHeader(dest);
ic.decodeImagePixels(pi);
outfile.commitData(dest, pi.data() - dest + dataSize);
}

static void encodeIZ(const char *infilename, const char *outfilename)
{
PortableImage pi;
int infd = ::open(infilename, O_RDONLY);
if (infd < 0) {
InputFile infile(infilename);
if (!infile.isReadable()) {
perror("Cannot open input file");
exit(EXIT_FAILURE);
}
int outfd = ::open(outfilename, O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (outfd < 0) {
OutputFile outfile(outfilename);
if (!outfile.isWritable()) {
perror("Cannot open output file");
exit(EXIT_FAILURE);
}
if (!pi.read(infd)) {
if (!pi.readHeader(infile.data())) {
fprintf(stderr, "Cannot handle input file, only 24 bit PPM files supported.\n");
exit(EXIT_FAILURE);
}
Expand All @@ -76,12 +62,24 @@ static void encodeIZ(const char *infilename, const char *outfilename)
fprintf(stderr, "Cannot handle image size %d x %d, limit is 16384 x 16384.\n", pi.width(), pi.height());
exit(EXIT_FAILURE);
}
unsigned char *dest = new unsigned char[pi.height() * pi.width() * 4 + 33];
initEncodeTable();
::write(outfd, dest, encodeImage(pi, dest) - dest);
::close(infd);
::close(outfd);
delete[] dest;
unsigned char *dest = outfile.prepareData(pi.height() * pi.width() * 4 + 33);
if (!dest) {
perror("Cannot write output file");
exit(EXIT_FAILURE);
}
#if 0
IZ::initEncodeTable();
IZ::ImageEncoder<3> ic;
ic.begin(dest);
ic.encodeImageSize(pi);
ic.encodeImagePixels(pi);
unsigned char *destEnd = ic.end();
outfile.commitData(dest, destEnd - dest);
#else
IZ::initEncodeTable();
unsigned char *destEnd = encodeImage(pi, dest);
outfile.commitData(dest, destEnd - dest);
#endif
}

int main(int argc, char *argv[])
Expand Down
2 changes: 1 addition & 1 deletion pixel.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class Pixel
p[C2] = c[2];
}

void predict(const U *p, int bpp, int bpr, int (*predictor)(int, int, int)) {
void predict(const U *p, int bpp, int bpr, int (*predictor)(int, int, int)) __attribute__((always_inline)) {
c[0] = predictor(p[C0 - bpp], p[C0 - bpr], p[C0 - bpp - bpr]);
c[1] = predictor(p[C1 - bpp], p[C1 - bpr], p[C1 - bpp - bpr]);
c[2] = predictor(p[C2 - bpp], p[C2 - bpr], p[C2 - bpp - bpr]);
Expand Down
73 changes: 3 additions & 70 deletions portableimage.cpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,5 @@
#ifdef HAVE_CONFIG_H
#include "iz_config.h"
#endif

#include "portableimage.h"

#if defined(HAVE_MMAP)
#include <sys/mman.h>
#endif

#include <sys/stat.h>
#include <unistd.h>

#include <cstdlib>

static unsigned char *writeValue(unsigned char *p, unsigned char whiteSpace, int value)
{
unsigned int v = value;
Expand Down Expand Up @@ -68,7 +55,10 @@ unsigned char *PortableImage::writeHeader(unsigned char *p)
p = writeValue(p, ' ', m_height);
p = writeValue(p, '\n', 255);

setSamplesPerLine(m_components * m_width);

*p++ = '\n';
m_data = p;
return p;
}

Expand Down Expand Up @@ -104,67 +94,10 @@ bool PortableImage::readHeader(const unsigned char *p)

PortableImage::PortableImage()
: m_components(0)
#if defined(HAVE_MMAP)
, m_buffer(MAP_FAILED)
#else
, m_buffer(0)
#endif
{
}

PortableImage::~PortableImage()
{
#if defined(HAVE_MMAP)
if (m_buffer != MAP_FAILED)
munmap(m_buffer, m_bufferSize);
#else
free(m_buffer);
#endif
}

bool PortableImage::read(int fd)
{
#if defined(HAVE_MMAP)
struct stat sb;
fstat(fd, &sb);
m_bufferSize = sb.st_size;
m_buffer = mmap(0, m_bufferSize, PROT_READ/* | PROT_WRITE*/, MAP_PRIVATE, fd, 0);
if (m_buffer == MAP_FAILED || !readHeader((unsigned char *) m_buffer)) {
m_components = 0;
return false;
}
return true;
#else
struct stat sb;
fstat(fd, &sb);
m_bufferSize = sb.st_size;
m_buffer = malloc(m_bufferSize);
int pos = 0;
int remaining = m_bufferSize;
while (remaining > 0) {
int bytesRead = ::read(fd, (char *) m_buffer + pos, remaining);
if (bytesRead < 0)
break;
remaining -= bytesRead;
pos += bytesRead;
}
if (remaining > 0 || !readHeader((unsigned char *) m_buffer)) {
m_components = 0;
return false;
}
return true;
#endif
}

bool PortableImage::write(int fd)
{
unsigned char buf[32];
int size = writeHeader(buf) - buf;
if (::write(fd, buf, size) != size)
return false;
size = m_components * m_width * m_height;
if (::write(fd, m_data, size) != size)
return false;
return true;
}

7 changes: 0 additions & 7 deletions portableimage.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,15 @@ class PortableImage : public IZ::Image<>
~PortableImage();

public:
bool read(int fd);
bool write(int fd);

int components() const { return m_components; }
void setComponents(int components) { m_components = components; }

private:
bool readHeader(const unsigned char *data);
unsigned char *writeHeader(unsigned char *data);

private:
int m_components;
int m_maxVal;
void *m_buffer;
int m_bufferSize;
int m_flags;
};

#endif

0 comments on commit e66d7f0

Please sign in to comment.