[core] Remove unused

This commit is contained in:
John Firebaugh
2015-11-23 16:35:32 -08:00
parent 36081f6486
commit 6ff3ef55f8
6 changed files with 42 additions and 63 deletions

View File

@@ -29,9 +29,7 @@ struct ImageReader : private noncopyable
{
virtual unsigned width() const=0;
virtual unsigned height() const=0;
virtual bool hasAlpha() const=0;
virtual bool premultipliedAlpha() const=0;
virtual void read(unsigned x, unsigned y, unsigned width, unsigned height, uint8_t* image)=0;
virtual std::unique_ptr<uint8_t[]> read()=0;
virtual ~ImageReader() {}
};

View File

@@ -55,9 +55,7 @@ public:
~JpegReader();
unsigned width() const;
unsigned height() const;
inline bool hasAlpha() const { return false; }
inline bool premultipliedAlpha() const { return true; }
void read(unsigned x, unsigned y, unsigned w, unsigned h, uint8_t* image);
std::unique_ptr<uint8_t[]> read();
private:
void init();
static void on_error(j_common_ptr cinfo);

View File

@@ -53,9 +53,7 @@ public:
~PngReader();
unsigned width() const;
unsigned height() const;
inline bool hasAlpha() const { return has_alpha_; }
bool premultipliedAlpha() const { return true; } // png_set_alpha_mode(png, PNG_ALPHA_PREMULTIPLIED, 2.2)
void read(unsigned x,unsigned y, unsigned width, unsigned height, uint8_t* image);
std::unique_ptr<uint8_t[]> read();
private:
void init();
static void png_read_data(png_structp png_ptr, png_bytep data, png_size_t length);

View File

@@ -81,8 +81,7 @@ Image::Image(std::string const& data)
auto reader = getImageReader(reinterpret_cast<const uint8_t*>(data.c_str()), data.size());
width = reader->width();
height = reader->height();
img = std::make_unique<uint8_t[]>(width * height * 4);
reader->read(0, 0, width, height, img.get());
img = reader->read();
}
catch (ImageReaderException const& ex)
{

View File

@@ -145,8 +145,10 @@ unsigned JpegReader<T>::height() const
}
template <typename T>
void JpegReader<T>::read(unsigned x0, unsigned y0, unsigned w, unsigned h, uint8_t* image)
std::unique_ptr<uint8_t[]> JpegReader<T>::read()
{
std::unique_ptr<uint8_t[]> image = std::make_unique<uint8_t[]>(width_ * height_ * 4);
stream_.clear();
stream_.seekg(0, std::ios_base::beg);
@@ -167,19 +169,16 @@ void JpegReader<T>::read(unsigned x0, unsigned y0, unsigned w, unsigned h, uint8
row_stride = cinfo.output_width * cinfo.output_components;
buffer = (*cinfo.mem->alloc_sarray) ((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1);
w = std::min(w,width_ - x0);
h = std::min(h,height_ - y0);
const std::unique_ptr<unsigned int[]> out_row(new unsigned int[w]);
const std::unique_ptr<unsigned int[]> out_row(new unsigned int[width_]);
unsigned row = 0;
while (cinfo.output_scanline < cinfo.output_height)
{
jpeg_read_scanlines(&cinfo, buffer, 1);
if (row >= y0 && row < y0 + h)
if (row < height_)
{
for (unsigned int x = 0; x < w; ++x)
for (unsigned int x = 0; x < width_; ++x)
{
unsigned col = x + x0;
unsigned col = x;
r = buffer[0][cinfo.output_components * col];
if (cinfo.output_components > 2)
{
@@ -191,11 +190,13 @@ void JpegReader<T>::read(unsigned x0, unsigned y0, unsigned w, unsigned h, uint8
}
out_row[x] = (0xff << 24) | (b << 16) | (g << 8) | r;
}
std::copy((char*)out_row.get(), (char*)out_row.get() + w*4, image + (row - y0)*width_*4);
std::copy((char*)out_row.get(), (char*)out_row.get() + width_*4, image.get() + row*width_*4);
}
++row;
}
jpeg_finish_decompress(&cinfo);
return image;
}
template class JpegReader<boost::iostreams::array_source>;

View File

@@ -115,8 +115,10 @@ unsigned PngReader<T>::height() const
}
template <typename T>
void PngReader<T>::read(unsigned x0, unsigned y0, unsigned w, unsigned h, uint8_t* image)
std::unique_ptr<uint8_t[]> PngReader<T>::read()
{
std::unique_ptr<uint8_t[]> image = std::make_unique<uint8_t[]>(width_ * height_ * 4);
stream_.clear();
stream_.seekg(0, std::ios_base::beg);
@@ -162,54 +164,37 @@ void PngReader<T>::read(unsigned x0, unsigned y0, unsigned w, unsigned h, uint8_
png_set_alpha_mode(png_ptr, PNG_ALPHA_PREMULTIPLIED, PNG_GAMMA_LINEAR);
#endif
if (x0 == 0 && y0 == 0 && w >= width_ && h >= height_)
if (png_get_interlace_type(png_ptr,info_ptr) == PNG_INTERLACE_ADAM7)
{
if (png_get_interlace_type(png_ptr,info_ptr) == PNG_INTERLACE_ADAM7)
{
png_set_interlace_handling(png_ptr); // FIXME: libpng bug?
// according to docs png_read_image
// "..automatically handles interlacing,
// so you don't need to call png_set_interlace_handling()"
}
png_read_update_info(png_ptr, info_ptr);
// we can read whole image at once
// alloc row pointers
const std::unique_ptr<png_bytep[]> rows(new png_bytep[height_]);
for (unsigned row = 0; row < height_; ++row)
rows[row] = (png_bytep)image + row * width_ * 4 ;
png_read_image(png_ptr, rows.get());
png_set_interlace_handling(png_ptr); // FIXME: libpng bug?
// according to docs png_read_image
// "..automatically handles interlacing,
// so you don't need to call png_set_interlace_handling()"
}
png_read_update_info(png_ptr, info_ptr);
// we can read whole image at once
// alloc row pointers
const std::unique_ptr<png_bytep[]> rows(new png_bytep[height_]);
for (unsigned row = 0; row < height_; ++row)
rows[row] = (png_bytep)image.get() + row * width_ * 4 ;
png_read_image(png_ptr, rows.get());
#ifndef PNG_ALPHA_PREMULTIPLIED
// Manually premultiply the image if libpng didn't do it for us.
for (unsigned row = 0; row < height_; ++row) {
for (unsigned x = 0; x < width_; x++) {
png_byte* ptr = &(rows[row][x * 4]);
const float a = ptr[3] / 255.0f;
ptr[0] *= a;
ptr[1] *= a;
ptr[2] *= a;
}
// Manually premultiply the image if libpng didn't do it for us.
for (unsigned row = 0; row < height_; ++row) {
for (unsigned x = 0; x < width_; x++) {
png_byte* ptr = &(rows[row][x * 4]);
const float a = ptr[3] / 255.0f;
ptr[0] *= a;
ptr[1] *= a;
ptr[2] *= a;
}
}
#endif
}
else
{
png_read_update_info(png_ptr, info_ptr);
w=std::min(w, width_ - x0);
h=std::min(h, height_ - y0);
unsigned rowbytes=png_get_rowbytes(png_ptr, info_ptr);
const std::unique_ptr<png_byte[]> row(new png_byte[rowbytes]);
for (unsigned i = 0; i < height_; ++i)
{
png_read_row(png_ptr,row.get(),0);
if (i >= y0 && i < (y0 + h))
{
// TODO: premultiply this
std::copy(&row[x0 * 4], &row[x0 * 4] + w * 4, image + i * width_* 4);
}
}
}
png_read_end(png_ptr,0);
return image;
}
template class PngReader<boost::iostreams::array_source>;