<commit_before>noflo = require 'noflo'
sharp = require 'sharp'
Canvas = require('noflo-canvas').canvas

# @runtime noflo-nodejs
# @name ResizeBuffer

exports.getComponent = ->
  c = new noflo.Component

  c.icon = 'expand'
  c.description = 'Resize a given image buffer to a new dimension'

  c.inPorts.add 'buffer',
    datatype: 'object'
    description: 'Image buffer to be resized'
  c.inPorts.add 'width',
    datatype: 'integer'
    description: 'New width'
    required: false
  c.inPorts.add 'height',
    datatype: 'integer'
    description: 'New height'
    required: false
  c.outPorts.add 'buffer',
    datatype: 'object'
  c.outPorts.add 'factor',
    datatype: 'number'
    required: false
  c.outPorts.add 'error',
    datatype: 'object'
    required: false

  noflo.helpers.WirePattern c,
    in: ['buffer']
    params: ['width', 'height']
    out: ['buffer', 'factor']
    async: true
    forwardGroups: true
  , (payload, groups, out, callback) ->
    width = c.params.width
    height = c.params.height
    if not width? and not height?
      width = 256
    try
      inputBuffer = sharp payload
      inputBuffer.metadata (err, metadata) ->
        if err
          return callback err
        # Try to preserve the same format, if there's EXIF
        if metadata.exif?
          inputBuffer
          .resize width, height
          .withMetadata()
          .withoutEnlargement()
          .toBuffer (err, outputBuffer, info) ->
            if err
              return callback err
            originalWidth = metadata.width
            resizedWidth = info.width
            factor = originalWidth / resizedWidth
            out.buffer.send outputBuffer
            out.factor.send factor
            do callback
        else
          inputBuffer
          .resize width, height
          .withMetadata()
          .withoutEnlargement()
          .toFormat('png')
          .toBuffer (err, outputBuffer, info) ->
            if err
              return callback err
            originalWidth = metadata.width
            resizedWidth = info.width
            factor = originalWidth / resizedWidth
            out.buffer.send outputBuffer
            out.factor.send factor
            do callback
    catch err
      return callback err

  c<commit_msg>Deal with narrow images on resize<commit_after>noflo = require 'noflo'
sharp = require 'sharp'
Canvas = require('noflo-canvas').canvas

# @runtime noflo-nodejs
# @name ResizeBuffer

exports.getComponent = ->
  c = new noflo.Component

  c.icon = 'expand'
  c.description = 'Resize a given image buffer to a new dimension'
  c.defaultDimension = 256

  c.inPorts.add 'buffer',
    datatype: 'object'
    description: 'Image buffer to be resized'
  c.inPorts.add 'width',
    datatype: 'integer'
    description: 'New width'
    required: false
  c.inPorts.add 'height',
    datatype: 'integer'
    description: 'New height'
    required: false
  c.outPorts.add 'buffer',
    datatype: 'object'
  c.outPorts.add 'factor',
    datatype: 'number'
    required: false
  c.outPorts.add 'error',
    datatype: 'object'
    required: false

  noflo.helpers.WirePattern c,
    in: ['buffer']
    params: ['width', 'height']
    out: ['buffer', 'factor']
    async: true
    forwardGroups: true
  , (payload, groups, out, callback) ->
    width = c.params.width
    height = c.params.height
    try
      inputBuffer = sharp payload
      inputBuffer.metadata (err, metadata) ->
        if err
          return callback err
        # Default value when nothing is specified
        if not width? and not height?
          # Deal with narrow or wide images
          if metadata.width > metadata.height
            width = c.defaultDimension
          else
            height = c.defaultDimension
        # Try to preserve the same format, if there's EXIF
        if metadata.exif?
          inputBuffer
          .resize width, height
          .withMetadata()
          .withoutEnlargement()
          .toBuffer (err, outputBuffer, info) ->
            if err
              return callback err
            if width
              originalWidth = metadata.width
              resizedWidth = info.width
              factor = originalWidth / resizedWidth
            else
              originalHeight = metadata.height
              resizedHeight = info.height
              factor = originalHeight / resizedHeight
            out.buffer.send outputBuffer
            out.factor.send factor
            do callback
        else
          inputBuffer
          .resize width, height
          .withMetadata()
          .withoutEnlargement()
          .toFormat('png')
          .toBuffer (err, outputBuffer, info) ->
            if err
              return callback err
            if width
              originalWidth = metadata.width
              resizedWidth = info.width
              factor = originalWidth / resizedWidth
            else
              originalHeight = metadata.height
              resizedHeight = info.height
              factor = originalHeight / resizedHeight
            out.buffer.send outputBuffer
            out.factor.send factor
            do callback
    catch err
      return callback err

  c
{'fraction_non_alphanumeric': 0.08350971714450645, 'fraction_numerical': 0.0011545122185876468, 'mean_word_length': 1.8781838316722037, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 14, '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': '3380048', 'n_tokens_mistral': 1412, 'n_tokens_neox': 1391, 'n_words': 422}