<commit_before>const sharp = require('sharp')
const Image = require('./image')
const ImageData = require('./image-data')

class SharpImage {
  static from(bufferOrImageData) {
    if (ImageData.probablyIs(bufferOrImageData)) {
      let imageData = ImageData.normalize(bufferOrImageData)
      ImageData.assert(imageData)
      imageData = ImageData.removeAlphaChannel(imageData)

      return sharp(Buffer.from(imageData.data), {
        raw: {
          channels: 3,
          width: imageData.width,
          height: imageData.height,
        },
      })
    } else if (Buffer.isBuffer(bufferOrImageData)) {
      return sharp(bufferOrImageData)
    } else {
      throw new TypeError('Must be Buffer or image data')
    }
  }
}

class NodeImage extends Image {
  constructor(image, options) {
    super(options)
    this._image = image
  }

  _setFormat(image) {
    if (this._output.format.type === Image.JPEG) {
      return image.jpeg(this._output.format)
    } else if (this._output.format.type === Image.PNG) {
      return image.png()
    } else {
      throw new Error(`Unsupported format: ${this._output.format.type}`)
    }
  }

  toImageData() {
    const metadata = this._image.metadata()
    const pixels = this._image.clone().raw().toBuffer()
    return Promise.all([metadata, pixels]).then(([metadata, pixels]) => {
      return {
        channels: 3,
        format: ImageData.RGB,
        width: metadata.width,
        height: metadata.height,
        data: pixels,
      }
    })
  }<commit_msg>refactor: move toImageData to SharpImage<commit_after>const sharp = require('sharp')
const Image = require('./image')
const ImageData = require('./image-data')

class SharpImage {
  static from(bufferOrImageData) {
    if (ImageData.probablyIs(bufferOrImageData)) {
      let imageData = ImageData.normalize(bufferOrImageData)
      ImageData.assert(imageData)
      imageData = ImageData.removeAlphaChannel(imageData)

      return sharp(Buffer.from(imageData.data), {
        raw: {
          channels: 3,
          width: imageData.width,
          height: imageData.height,
        },
      })
    } else if (Buffer.isBuffer(bufferOrImageData)) {
      return sharp(bufferOrImageData)
    } else {
      throw new TypeError('Must be Buffer or image data')
    }
  }

  static toImageData(image) {
    const metadata = image.metadata()
    const pixels = image.clone().raw().toBuffer()
    return Promise.all([metadata, pixels]).then(([metadata, pixels]) => {
      return {
        channels: 3,
        format: ImageData.RGB,
        width: metadata.width,
        height: metadata.height,
        data: pixels,
      }
    })
  }
}

class NodeImage extends Image {
  constructor(image, options) {
    super(options)
    this._image = image
  }

  _setFormat(image) {
    if (this._output.format.type === Image.JPEG) {
      return image.jpeg(this._output.format)
    } else if (this._output.format.type === Image.PNG) {
      return image.png()
    } else {
      throw new Error(`Unsupported format: ${this._output.format.type}`)
    }
  }

  toImageData() {
    return SharpImage.toImageData(this._image)
  }
{'fraction_non_alphanumeric': 0.12543887647622087, 'fraction_numerical': 0.0012767315671879987, 'mean_word_length': 2.730952380952381, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2537955', 'n_tokens_mistral': 950, 'n_tokens_neox': 929, 'n_words': 194}