image

Build Status

##Overview

A Dart library to encode and decode various image formats.

The library has no reliance on dart:io, so it can be used for both server and web applications. The image library currently supports the following formats:

  • PNG
  • JPG
  • TGA

##Samples

Load a jpeg, resize it, and save it as a png:

import 'dart:io' as Io;
import 'package:image/image.dart';
void main() {
  // Read a jpeg image from file.
  Image image = readJpg(new Io.File('res/cat-eye04.jpg').readAsBytesSync());

  // Resize the image to a 120x? thumbnail (maintaining the aspect ratio).
  Image thumbnail = copyResize(image, 120);

  // Save the thumbnail as a PNG.
  new Io.File('out/thumbnail-cat-eye04.png')
        ..createSync(recursive: true)
        ..writeAsBytesSync(writePng(thumbnail));
}

Create an image, draw some text, save it as a png:

import 'dart:io' as Io;
import 'package:image/image.dart';
void main() {
  // Create an image
  Image image = new Image(320, 240);
  
  // Fill it with a solid color (blue)
  fill(image, getColor(0, 0, 255));
  
  // Draw some text using 24pt arial font
  drawString(image, arial_24, 0, 0, 'Hello World');
  
  // Draw a line
  drawLine(image, 0, 0, 320, 240, getColor(255, 0, 0), thickness: 3);
  
  // Blur the image
  gaussianBlur(image, 10);
  
  // Generate a PNG
  List<int> png = writePng(image);
  
  // Save it to disk
  new Io.File('out/test.png')
        ..createSync(recursive: true)
        ..writeAsBytesSync(png);
}


##Image Functions - Image brightness(Image src, int brightness);

Adjust the brightness of the image.

  • Image bumpToNormal(Image src, {double strength: 2.0});

Generate a normal map from a heightfield bump image.

  • Image colorOffset(Image src, int red, int green, int blue, int alpha);

Apply an offset to the colors of the image.

  • Image contrast(Image src, num contrast);

Apply the Contrast convolution filter to the image.

  • Image convolution(Image src, List<num> filter, num filterDiv, num offset);

Apply a convolution filter to the image.

  • Image copyCrop(Image src, int x, int y, int w, int h);

Create a cropped copy of the image.

  • Image copyInto(Image dst, Image src, int dst_x, int dst_y, int src_x, int src_y, int dst_w, int dst_h, int src_w, int src_h);

Copy an area of the src image into dst.

  • Image copyResize(Image src, int width, int height = -1, int interpolation = LINEAR);

Create a resized copy of the image.

  • Image copyRotate(Image src, num angle, {int interpolation: LINEAR}) {

Returns a copy of the src image, rotated by angle degrees.

  • Image drawChar(Image image, BitmapFont font, int x, int y, String string, {int color: 0xffffffff});

Draw a single character with the given font.

  • Image drawCircle(Image image, int x0, int y0, int radius, int color);

Draw a circle.

  • Image drawLine(Image image, int x1, int y1, int x2, int y2, int color,

                 {bool antialias: false, num thickness: 1});
    

Draw a line.

  • Image drawNoise(Image image, double sigma, {int type: NOISE_GAUSSIAN, Math.Random random});

Add random noise to pixel values.

  • Image drawPixel(Image image, int x, int y, int color, int opacity = 0xff);

Draw a single pixel into the image, applying alpha and opacity blending.

  • Image drawString(Image image, BitmapFont font, int x, int y, String string, {int color: 0xffffffff});

Draw the string with the given font.

  • Image dropShadow(Image src, int hshadow, int vshadow, int blur,

               {int shadowColor: 0x000000a0});
    

Create a drop-shadow effect.

  • Image emboss(Image src);

Apply the Emboss convolution filter.

  • Image fill(Image image, int color);

Fill the image with the given color.

  • Image fillRect(Image src, int x1, int y1, int x2, int y2, int color);

Fill a rectangle with the given color.

  • Image flip(Image src, int mode);

Flip the image with FLIP_HORIZONTAL, FLIP_VERTICAL, or FLIP_BOTH.

  • Image gaussianBlur(Image src, int radius);

Blur the image.

  • Image grayscale(Image src);

Convert the colors of the image to grayscale.

  • Image invert(Image src);

Inver the colors of the image.

  • Image normalize(Image src, int minValue, int maxValue);

Linearly normalize the pixel values of the image.

  • Image pixelate(Image src, int blockSize, {int mode: PIXELATE_UPPERLEFT});

Pixelate the image.

  • BitmapFont readFontZip(List<int> bytes);

Load a BitmapFont from a zip file.

  • BitmapFont readFont(String fnt, Image page);

Load a BitmapFont from a font file and image.

  • Image readJpg(List<int> bytes);

Load an Image from JPG formatted data.

  • Image readPng(List<int> bytes);

Load an Image from PNG formatted data.

  • Image readTga(List<int> bytes);

Load an Image from TGA formatted data.

  • Image remapColors(Image src, {int red: RED, int green: GREEN, int blue: BLUE, int alpha: ALPHA});

Remap the color channels of an image.

  • Image smooth(Image src, num w);

Apply a smooth convolution filter to the image.

  • List<int> writeJpg(Image image, {int quality: 100});

Generate JPG formatted data for the given image.

  • List<int> writePng(Image image, {int level: 6});

Generate PNG formatted data for the given image.

  • List<int> writeTga(Image image);

Generate TGA formatted data for the given image.

Libraries

image

The image library aims to provide server-side programs the ability to load, manipulate, and save various image file formats.