Skip to content

Instantly share code, notes, and snippets.

<Car model="Escort" year="1973" reg="ABC123D">
<Passengers>
<Passenger name="Bob"/><Passenger name="Sue"/>
</Passengers>
</Car>
<Car model="Cortina" year="1974" reg="BCD123E">
<Passengers>
<Passenger name="Tim"/><Passenger name="Joe"/><Passenger name="Ann"/>
</Passengers>
</Car>
@worthlesscog
worthlesscog / converter.scala
Created June 17, 2013 18:33
CSV to XML converter
import scala.io.Source
case class Passenger(name: String, ridingIn: String) {
def asNode = <Passenger name={ name }/>
}
case class Car(model: String, year: String, registration: String, passengers: Seq[Passenger]) {
def asNode =
<Car model={ model } year={ year } reg={ registration }>
<Passengers>
@worthlesscog
worthlesscog / passengers.csv
Created June 17, 2013 18:28
Sample data #2
ABC123D Bob
ABC123D Sue
BCD123E Tim
BCD123E Joe
BCD123E Ann
@worthlesscog
worthlesscog / cars.csv
Created June 17, 2013 18:28
Sample data #1
ABC123D Escort 1973
BCD123E Cortina 1974
@worthlesscog
worthlesscog / gist:5798815
Created June 17, 2013 17:58
Quicktime conversion using ffmpeg, mplayer and sox
#!/bin/bash
ffmpeg -probesize 500000000 -i $1.mov -vcodec copy -an video.mov
mplayer $1.mov -dumpaudio -dumpfile $1.raw
sox -e signed-integer -b 24 -L -c 16 -r 48000 $1.raw $1.flac channels 2
ffmpeg -i video.mov -i $1.flac -vcodec libx264 $1.mp4
rm video.mov $1.raw $1.flac
@worthlesscog
worthlesscog / gist:3060273
Created July 6, 2012 13:53
Polymorphic JSON unmarshall with Jackson in Scala
import org.codehaus.jackson.annotate.{ JsonTypeInfo, JsonSubTypes, JsonProperty }
import org.codehaus.jackson.annotate.JsonSubTypes.Type
import org.codehaus.jackson.map.ObjectMapper
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type")
@JsonSubTypes(Array(
new Type(value = classOf[Bingo], name = "x"),
new Type(value = classOf[Bongo], name = "y"),
new Type(value = classOf[Bungo], name = "z")
))
@worthlesscog
worthlesscog / gist:3027566
Created July 1, 2012 08:35
Image compress and save helpers
private void buildImage(BufferedImage sourceImage, int width, int height, String imageType, Object target, float quality) throws IOException {
ImageWriter imageWriter = writerFor(imageType, target);
ImageWriteParam imageWriteParam = compressionParametersFor(imageWriter, quality);
imageWriter.write(null, new IIOImage(sourceImage, null, null), imageWriteParam);
imageWriter.dispose();
}
private ImageWriter writerFor(String imageType, Object target) throws IOException {
ImageWriter imageWriter = ImageIO.getImageWritersByFormatName(imageType).next();
imageWriter.setOutput(target);
@worthlesscog
worthlesscog / gist:3027556
Created July 1, 2012 08:25
Image compress and save
BufferedImage scaledImage = scale(sourceImage, width, height, SMOOTHING_STEPS);
ByteArrayOutputStream buffer = new ByteArrayOutputStream(1024 * 1024);
ImageOutputStream stream = ImageIO.createImageOutputStream(new BufferedOutputStream(buffer));
float percentage = quality;
do {
buffer.reset();
buildImage(scaledImage, width, height, imageType, stream, percentage);
quality = percentage;
percentage -= 0.005f;
@worthlesscog
worthlesscog / gist:3027281
Created July 1, 2012 07:15
Proportionally scale and smooth
/**
* @see http://today.java.net/pub/a/today/2007/04/03/perils-of-image-getscaledinstance.html
*/
private BufferedImage scale(BufferedImage image, int width, int height, int smoothingSteps) {
int w = image.getWidth();
int h = image.getHeight();
int targetH, targetW;
if (w > h) {
targetW = width;
@worthlesscog
worthlesscog / Reducer.scala
Created May 13, 2012 19:51
Collect the numbered chunks for reassembly
import scala.actors.Actor.self
import scala.actors.remote.RemoteActor.{ register, alive }
import scala.actors.Actor
import scala.collection.immutable.TreeMap
import scala.collection.mutable.Map
object Reducer {
def main(args: Array[String]) {
println("Reducer running")