shithub: libicc

Download patch

ref: 0b4b45f393d13203752160695b3326d769cbead3
parent: 65b4913adc23dd0c5c63f43b9a67e07308834a89
author: sirjofri <sirjofri@sirjofri.de>
date: Mon Apr 6 16:30:16 EDT 2026

load tag table

--- a/icc.h
+++ b/icc.h
@@ -64,6 +64,13 @@
 	IDMono = 0x8, /* or color */
 } DevAttribute;
 
+typedef struct ICCTag ICCTag;
+struct ICCTag{
+	u32int signature;
+	u32int offset;
+	u32int size;
+};
+
 typedef struct ICCprofile ICCprofile;
 struct ICCprofile
 {
@@ -95,6 +102,10 @@
 	
 	u32int creator;
 	uchar id[16];
+	
+	uchar *tagdata;
+	ICCTag *tags;
+	ulong ntags;
 };
 
 ICCprofile *iccload(int fd);
--- a/load.c
+++ b/load.c
@@ -139,9 +139,12 @@
 iccload(int fd)
 {
 	uchar header[128];
+	uchar *td;
 	ICCprofile *prof;
+	ICCTag *tag;
 	u8int vmajor;
 	u8int vminor;
+	ulong n;
 	
 	prof = mallocz(sizeof(ICCprofile), 1);
 	if (!prof) {
@@ -198,6 +201,38 @@
 	memcpy(prof->id, &header[84], 16);
 	
 	/* future 100 - 127 */
+	
+	/* load tags */
+	prof->tagdata = mallocz(prof->size - 128, 1);
+	if (!prof->tagdata) {
+		werrstr("malloc tagdata: %r");
+		free(prof);
+		return nil;
+	}
+	if (read(fd, prof->tagdata, prof->size-128) != prof->size-128) {
+		werrstr("read tagdata: %r");
+		free(prof->tagdata);
+		free(prof);
+		return nil;
+	}
+	prof->ntags = iread32(&prof->tagdata[0]);
+	
+	prof->tags = mallocz(prof->ntags * sizeof(ICCTag), 1);
+	if (!prof->tags) {
+		werrstr("malloc tag table: %r");
+		free(prof->tags);
+		free(prof->tagdata);
+		free(prof);
+		return nil;
+	}
+	
+	for (n = 0; n < prof->ntags; n++) {
+		tag = &prof->tags[n];
+		td = &prof->tagdata[n*12] + 4;
+		tag->signature = iread32(td + 0);
+		tag->offset = iread32(td + 4);
+		tag->size = iread32(td + 8); 
+	}
 	
 	return prof;
 }
--- a/test/t.c
+++ b/test/t.c
@@ -1,5 +1,6 @@
 #include <u.h>
 #include <libc.h>
+#include <ctype.h>
 #include "../icc.h"
 
 void
@@ -12,6 +13,28 @@
 }
 
 void
+asascii(char *out, u32int data)
+{
+	char a, b, c, d;
+	
+	a = (data>>24)&0xff;
+	b = (data>>16)&0xff;
+	c = (data>>8 )&0xff;
+	d = (data>>0 )&0xff;
+	
+	if (!(isalnum(a) || isalnum(b) || isalnum(c) || isalnum(d))) {
+		out[0] = 0;
+		return;
+	}
+	
+	out[0] = a;
+	out[1] = b;
+	out[2] = c;
+	out[3] = d;
+	out[4] = 0;
+}
+
+void
 main(void)
 {
 	char buf[512];
@@ -53,6 +76,16 @@
 	
 	n = enc64(buf, sizeof buf, prof->id, sizeof prof->id);
 	fprint(2, "id:     %*s\n", n, buf);
+	
+	fprint(2, "ntags:  %d\n", prof->ntags);
+	
+	for (n = 0; n < prof->ntags; n++) {
+		fprint(2, "  tag: %d\n", n);
+		asascii(buf, prof->tags[n].signature);
+		fprint(2, "    signature: %x (%4s)\n", prof->tags[n].signature, *buf ? buf : "");
+		fprint(2, "    offset:    %d\n", prof->tags[n].offset);
+		fprint(2, "    size:      %d\n", prof->tags[n].size);
+	}
 	
 	exits(nil);
 }
--