// Generated by CoffeeScript 1.7.1
(function() {
  var PNG, PNGImage, zlib;

  zlib = require('zlib');

  PNG = require('png-js');

  PNGImage = (function() {
    function PNGImage(data, label) {
      this.label = label;
      this.image = new PNG(data);
      this.width = this.image.width;
      this.height = this.image.height;
      this.imgData = this.image.imgData;
      this.obj = null;
    }

    PNGImage.prototype.embed = function(document) {
      var mask, palette, params, rgb, val, x, _i, _len;
      this.document = document;
      if (this.obj) {
        return;
      }
      this.obj = document.ref({
        Type: 'XObject',
        Subtype: 'Image',
        BitsPerComponent: this.image.bits,
        Width: this.width,
        Height: this.height,
        Filter: 'FlateDecode'
      });
      if (!this.image.hasAlphaChannel) {
        params = document.ref({
          Predictor: 15,
          Colors: this.image.colors,
          BitsPerComponent: this.image.bits,
          Columns: this.width
        });
        this.obj.data['DecodeParms'] = params;
        params.end();
      }
      if (this.image.palette.length === 0) {
        this.obj.data['ColorSpace'] = this.image.colorSpace;
      } else {
        palette = document.ref();
        palette.end(new Buffer(this.image.palette));
        this.obj.data['ColorSpace'] = ['Indexed', 'DeviceRGB', (this.image.palette.length / 3) - 1, palette];
      }
      if (this.image.transparency.grayscale) {
        val = this.image.transparency.greyscale;
        return this.obj.data['Mask'] = [val, val];
      } else if (this.image.transparency.rgb) {
        rgb = this.image.transparency.rgb;
        mask = [];
        for (_i = 0, _len = rgb.length; _i < _len; _i++) {
          x = rgb[_i];
          mask.push(x, x);
        }
        return this.obj.data['Mask'] = mask;
      } else if (this.image.transparency.indexed) {
        return this.loadIndexedAlphaChannel();
      } else if (this.image.hasAlphaChannel) {
        return this.splitAlphaChannel();
      } else {
        return this.finalize();
      }
    };

    PNGImage.prototype.finalize = function() {
      var sMask;
      if (this.alphaChannel) {
        sMask = this.document.ref({
          Type: 'XObject',
          Subtype: 'Image',
          Height: this.height,
          Width: this.width,
          BitsPerComponent: 8,
          Filter: 'FlateDecode',
          ColorSpace: 'DeviceGray',
          Decode: [0, 1]
        });
        sMask.end(this.alphaChannel);
        this.obj.data['SMask'] = sMask;
      }
      this.obj.end(this.imgData);
      this.image = null;
      return this.imgData = null;
    };

    PNGImage.prototype.splitAlphaChannel = function() {
      return this.image.decodePixels((function(_this) {
        return function(pixels) {
          var a, alphaChannel, colorByteSize, done, i, imgData, len, p, pixelCount;
          colorByteSize = _this.image.colors * _this.image.bits / 8;
          pixelCount = _this.width * _this.height;
          imgData = new Buffer(pixelCount * colorByteSize);
          alphaChannel = new Buffer(pixelCount);
          i = p = a = 0;
          len = pixels.length;
          while (i < len) {
            imgData[p++] = pixels[i++];
            imgData[p++] = pixels[i++];
            imgData[p++] = pixels[i++];
            alphaChannel[a++] = pixels[i++];
          }
          done = 0;
          zlib.deflate(imgData, function(err, imgData) {
            _this.imgData = imgData;
            if (err) {
              throw err;
            }
            if (++done === 2) {
              return _this.finalize();
            }
          });
          return zlib.deflate(alphaChannel, function(err, alphaChannel) {
            _this.alphaChannel = alphaChannel;
            if (err) {
              throw err;
            }
            if (++done === 2) {
              return _this.finalize();
            }
          });
        };
      })(this));
    };

    PNGImage.prototype.loadIndexedAlphaChannel = function(fn) {
      var transparency;
      transparency = this.image.transparency.indexed;
      return this.image.decodePixels((function(_this) {
        return function(pixels) {
          var alphaChannel, i, j, _i, _ref;
          alphaChannel = new Buffer(_this.width * _this.height);
          i = 0;
          for (j = _i = 0, _ref = pixels.length; _i < _ref; j = _i += 1) {
            alphaChannel[i++] = transparency[pixels[j]];
          }
          return zlib.deflate(alphaChannel, function(err, alphaChannel) {
            _this.alphaChannel = alphaChannel;
            if (err) {
              throw err;
            }
            return _this.finalize();
          });
        };
      })(this));
    };

    return PNGImage;

  })();

  module.exports = PNGImage;

}).call(this);
