#!/usr/bin/perl
# icccurve2c - Extract a curve from an ICC profile into a C array
# Copyright (C) 2002 Laurent HOUDARD <lhoudard@netcourrier.com>

use strict;

my $icc_file = $ARGV[0];
my $tag = $ARGV[1];

die "usage: icccurve2c <ICC profile> <tag>\n" 
    unless (defined $icc_file and defined $tag);

die "$icc_file: file not found\n" unless (-f $icc_file);

$/ = "";

open (DUMP, '-|', "iccdump -v3 $icc_file")
    or die "error while executing iccdump: $!\n" ;

my ($n, $content);

while (<DUMP>) {
    if (($n, $content) =
	$_ =~ m/^tag.*sig\s+'$tag'.*elements\ =\ (\d+)\n(.*)\n\n/xs) {

	print "double $tag\[$n] = {\n";

	my $x = 0;
	my @points = split /\n/, $content;

	foreach my $point (@points) {
	    my @coord = split /:\s+/, $point;
	    print  "  " if ($x % 7 == 0);
	    printf "%01.6f, ", $coord[1];
	    print  "\n" if ($x % 7 == 6);
	    $x++;
	}

	print "\n};\n";
    }
}
