Pl_PNGFilter::Pl_PNGFilter(char const* identifier, Pipeline* next,
			   action_e action, unsigned int columns,
                           unsigned int samples_per_pixel,
                           unsigned int bits_per_sample) :
    Pipeline(identifier, next),
    action(action),
    cur_row(0),
    prev_row(0),
    buf1(0),
    buf2(0),
    pos(0)
{
    if (samples_per_pixel < 1)
    {
        throw std::runtime_error(
            "PNGFilter created with invalid samples_per_pixel");
    }
    if (! ((bits_per_sample == 1) ||
           (bits_per_sample == 2) ||
           (bits_per_sample == 4) ||
           (bits_per_sample == 8) ||
           (bits_per_sample == 16)))
    {
        throw std::runtime_error(
            "PNGFilter created with invalid bits_per_sample not"
            " 1, 2, 4, 8, or 16");
    }
    this->bytes_per_pixel = ((bits_per_sample * samples_per_pixel) + 7) / 8;
    unsigned long long bpr =
        ((columns * bits_per_sample * samples_per_pixel) + 7) / 8;
    if ((bpr == 0) || (bpr > (UINT_MAX - 1)))
    {
        throw std::runtime_error(
            "PNGFilter created with invalid columns value");
    }
    this->bytes_per_row = bpr & UINT_MAX;
    this->buf1 = new unsigned char[this->bytes_per_row + 1];
    this->buf2 = new unsigned char[this->bytes_per_row + 1];
    memset(this->buf1, 0, this->bytes_per_row + 1);
    memset(this->buf2, 0, this->bytes_per_row + 1);
    this->cur_row = this->buf1;
    this->prev_row = this->buf2;

    // number of bytes per incoming row
    this->incoming = (action == a_encode ?
                      this->bytes_per_row :
                      this->bytes_per_row + 1);
}
Pl_PNGFilter::Pl_PNGFilter(char const* identifier, Pipeline* next,
			   action_e action, unsigned int columns,
                           unsigned int samples_per_pixel,
                           unsigned int bits_per_sample) :
    Pipeline(identifier, next),
    action(action),
    cur_row(0),
    prev_row(0),
    buf1(0),
    buf2(0),
    pos(0)
{
    if (samples_per_pixel < 1)
    {
        throw std::runtime_error(
            "PNGFilter created with invalid samples_per_pixel");
    }
    if (! ((bits_per_sample == 1) ||
           (bits_per_sample == 2) ||
           (bits_per_sample == 4) ||
           (bits_per_sample == 8) ||
           (bits_per_sample == 16)))
    {
        throw std::runtime_error(
            "PNGFilter created with invalid bits_per_sample not"
            " 1, 2, 4, 8, or 16");
    }
    this->bytes_per_pixel = ((bits_per_sample * samples_per_pixel) + 7) / 8;
    unsigned long long bpr =
        ((columns * bits_per_sample * samples_per_pixel) + 7) / 8;
    if ((bpr == 0) || (bpr > (UINT_MAX - 1)))
    {
        throw std::runtime_error(
            "PNGFilter created with invalid columns value");
    }
    this->bytes_per_row = bpr & UINT_MAX;
    this->buf1 = new unsigned char[this->bytes_per_row + 1];
    this->buf2 = new unsigned char[this->bytes_per_row + 1];
    memset(this->buf1, 0, this->bytes_per_row + 1);
    memset(this->buf2, 0, this->bytes_per_row + 1);
    this->cur_row = this->buf1;
    this->prev_row = this->buf2;

     
    this->incoming = (action == a_encode ?
                      this->bytes_per_row :
                      this->bytes_per_row + 1);
}