From 36109f60ad16f8f91d3e76fda11e135be6e27d98 Mon Sep 17 00:00:00 2001 From: Toni Melisma Date: Tue, 22 Dec 2020 23:19:07 +0200 Subject: [PATCH] Proper temporary files, remove hardcoded paths --- ...stImageRef_Divide-linux-groovy.golden.jpeg | Bin 0 -> 9957 bytes vips/color.c | 6 +- vips/color.go | 12 +- vips/color.h | 2 +- vips/govips.go | 9 +- vips/{icc_profiles.c => icc_profiles.go} | 231 +++++++++--------- vips/icc_profiles.h | 6 - vips/icc_profiles_test.go | 36 +++ 8 files changed, 170 insertions(+), 132 deletions(-) create mode 100644 resources/jpg-24bit.TestImageRef_Divide-linux-groovy.golden.jpeg rename vips/{icc_profiles.c => icc_profiles.go} (85%) delete mode 100644 vips/icc_profiles.h create mode 100644 vips/icc_profiles_test.go diff --git a/resources/jpg-24bit.TestImageRef_Divide-linux-groovy.golden.jpeg b/resources/jpg-24bit.TestImageRef_Divide-linux-groovy.golden.jpeg new file mode 100644 index 0000000000000000000000000000000000000000..dd8bc054d74ca978ccf72e1af5c372bcfa093850 GIT binary patch literal 9957 zcmeI2e>j`x8poeEKQ&nMMkK_~Fcpd>tuB>I525VB^!OFUP*k#o5osx%{?e}N5xO>< z=FnD~vV#^ClpU&5ThbpxsWFs}Iz3%SPq%1kyR*u9DQmm)m$|O<*Lz<{-aC2j=X>AJ z=lSn@MK`Fs3tD}$#3BIv{2YNE0Du4}5Eej4Huq*g`1xlzlAA5$u}IEZsKFq)5$09B z_%Lq19ub_6u|gbpfCXUjNdVv&5#W$D_F4`blE2I$L;)xle&?{+kn*LXP6q%+`BG7d z2LQW}UqQ;}LN^S&0Re|6;Bf>3o^{tf#WIENvgifW=7#4O`3^QAE z8qM0t+IE$_gQJ6~l{4F!#kONPu;xS{B9TbaBN>rMMl1%6!CG?Yz5=iw_6lB!g=_!@ zhOjWCQv(wK&Bgqz3m?c)iO355e-;3;qPqJ2shUT& zo!Pu=d2)oo$Yyy}PP4p;Fc?t1|Mj`i93_AfKnb7(Py#3clmJQqC4dq@37`Z}0>2|* zg?I>7WPrgS&dGuqGFJh?X=Faen$6^lAzp$WVm&~3j5ac(?EIFK)r}QVyxPcpdo1Fs zZI6FAQIhezMiC?WOii2empFPFz?IW{m3K3;0{DxED zQWa)9r{9an+F80Y=gi41n^}UM?)FUq3OjZ1!OThsfuxd8*h z`f4X{+i^K#CL^b;{_6W>u`3(4X*KrW1ybw@PJu>iKcGJ7sgR{9dp_T)f7(*KioSmQ zFzh%b!?#i^|2MFCF%LSa$7BH;JTjXCgQH1iD(_5-&k~k<}a44=ujM5`Rn6u_dovXx>l#I z9bS$?x4WRx)F-g@kI#T~SM&G=(F4mHLt-_8(JXI_YYk1CPGm1{jK~0zcO0L zw;ff?@XMV7n%qQI&)Z&OPvEtXm0mf!V|4)0ut7{33@PrDxmk zzH(v6W(p^sI;xlvA7`=Q>Az#^Few_c^l#l8nVyuYZjXd|p|gl)%1(of4nHr9?l56gER2-z9v}g^r^_`x^-{(?P*Bq@clxc95w_;u1 zs`aoLVYVpn@9qxi5Aszqgr&p6HqB4Sq^l5D2l1)qd>iE7EpstHDL*(qO`0P=+y3*d zG40(&zsiGr$y*&}l(=2(V_bfeQnh|K84@@y@%)J7M2=u2zE!%|3K7 zy!`g%fUuTUPh~W%CMe!(JnGWbFAMd02EM2>X0I=b^nRL9b|f`sjil3fD^==vm)87b z1{1VvMixg>;GL=Z)jF_m9rwI@fO50JUZLST{;iKk-1vP}PTiG7mkW)~$Sg(=|7BP- zdV`UBES+o4_H9YpdOthRLp_un^vxP#!m-UQo0tJ^vEBz>R9I2Pio!y=Lx{ku#@MLM zuh42n039@V7-zsrJ}}~Ys-U7Yzt4E8Ci_#*nY}-@YrRY5@3J1b#$}p*`gI%S+sd&2 z=)m>;y;m)jE7D)=;CSW5JCp9M)`4KvhT3ZVly-sEx3^fb&g6;Im0OS)#N96ID9T{N zgT*!_9pI#Ke{yFX+g}yq74d?JFQI3S%3NkTHxvu=WNvJ`qK^_v@|C!nEA6?@)U8A0 zfK1wH<9{|Bl^OnZ+o;emXS(>4m<@RmCifn28u~7Hg3#jKYULMEmj3P? int is_colorspace_supported(VipsImage *in) { @@ -11,12 +10,9 @@ int to_colorspace(VipsImage *in, VipsImage **out, VipsInterpretation space) { } // https://libvips.github.io/libvips/API/8.6/libvips-colour.html#vips-icc-transform -int optimize_icc_profile(VipsImage *in, VipsImage **out, int isCmyk) { +int optimize_icc_profile(VipsImage *in, VipsImage **out, int isCmyk, char *srgb_profile_path, char *gray_profile_path) { // todo: check current embedded profile, and skip if already set - char *srgb_profile_path = SRGB_V2_MICRO_ICC_PATH; - char *gray_profile_path = SGRAY_V2_MICRO_ICC_PATH; - int channels = vips_image_get_bands(in); int result; diff --git a/vips/color.go b/vips/color.go index 8405020e..acb1cdd6 100644 --- a/vips/color.go +++ b/vips/color.go @@ -3,6 +3,10 @@ package vips // #cgo pkg-config: vips // #include "color.h" import "C" +import ( + "path/filepath" + "unsafe" +) // Color represents an RGB type Color struct { @@ -58,9 +62,15 @@ func vipsToColorSpace(in *C.VipsImage, interpretation Interpretation) (*C.VipsIm func vipsOptimizeICCProfile(in *C.VipsImage, isCmyk int) (*C.VipsImage, error) { var out *C.VipsImage - if res := int(C.optimize_icc_profile(in, &out, C.int(isCmyk))); res != 0 { + srgbProfilePath := C.CString(filepath.Join(temporaryDirectory, sRGBV2MicroICCProfilePath)) + grayProfilePath := C.CString(filepath.Join(temporaryDirectory, sGrayV2MicroICCProfilePath)) + + if res := int(C.optimize_icc_profile(in, &out, C.int(isCmyk), srgbProfilePath, grayProfilePath)); res != 0 { return nil, handleImageError(out) } + C.free(unsafe.Pointer(srgbProfilePath)) + C.free(unsafe.Pointer(grayProfilePath)) + return out, nil } diff --git a/vips/color.h b/vips/color.h index a5bff4ea..d4154426 100644 --- a/vips/color.h +++ b/vips/color.h @@ -6,4 +6,4 @@ int is_colorspace_supported(VipsImage *in); int to_colorspace(VipsImage *in, VipsImage **out, VipsInterpretation space); -int optimize_icc_profile(VipsImage *in, VipsImage **out, int isCmyk); +int optimize_icc_profile(VipsImage *in, VipsImage **out, int isCmyk, char *srgb_profile_path, char *gray_profile_path); diff --git a/vips/govips.go b/vips/govips.go index 2de1283f..9825991d 100644 --- a/vips/govips.go +++ b/vips/govips.go @@ -4,10 +4,10 @@ package vips // #cgo pkg-config: vips // #include // #include "govips.h" -// #include "icc_profiles.h" import "C" import ( "fmt" + "os" "runtime" "strings" "sync" @@ -97,10 +97,7 @@ func Startup(config *Config) { panic(fmt.Sprintf("Failed to start vips code=%v", err)) } - err = C.icc_profiles_init() - if err != 0 { - panic(fmt.Sprintf("Failed to initialize icc profiles=%v", err)) - } + initializeICCProfiles() running = true @@ -189,6 +186,8 @@ func Shutdown() { return } + os.RemoveAll(temporaryDirectory) + C.vips_shutdown() disableLogging() running = false diff --git a/vips/icc_profiles.c b/vips/icc_profiles.go similarity index 85% rename from vips/icc_profiles.c rename to vips/icc_profiles.go index 198fb12e..aebc591e 100644 --- a/vips/icc_profiles.c +++ b/vips/icc_profiles.go @@ -1,88 +1,94 @@ -#include "header.h" -#include -#include +package vips + +import ( + "fmt" + "io/ioutil" + "path/filepath" +) // ATTRIBUTION: // The following micro icc profile taken from: https://github.com/saucecontrol/Compact-ICC-Profiles. // Read more (very interesting): https://photosauce.net/blog/post/making-a-minimal-srgb-icc-profile-part-1-trim-the-fat-abuse-the-spec -unsigned char srgb_v2_micro_icc[] = { - 0x00, 0x00, 0x01, 0xc8, 0x6c, 0x63, 0x6d, 0x73, 0x02, 0x10, 0x00, 0x00, - 0x6d, 0x6e, 0x74, 0x72, 0x52, 0x47, 0x42, 0x20, 0x58, 0x59, 0x5a, 0x20, - 0x07, 0xe2, 0x00, 0x03, 0x00, 0x14, 0x00, 0x09, 0x00, 0x0e, 0x00, 0x1d, - 0x61, 0x63, 0x73, 0x70, 0x4d, 0x53, 0x46, 0x54, 0x00, 0x00, 0x00, 0x00, - 0x73, 0x61, 0x77, 0x73, 0x63, 0x74, 0x72, 0x6c, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf6, 0xd6, - 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0xd3, 0x2d, 0x68, 0x61, 0x6e, 0x64, - 0x9d, 0x91, 0x00, 0x3d, 0x40, 0x80, 0xb0, 0x3d, 0x40, 0x74, 0x2c, 0x81, - 0x9e, 0xa5, 0x22, 0x8e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09, - 0x64, 0x65, 0x73, 0x63, 0x00, 0x00, 0x00, 0xf0, 0x00, 0x00, 0x00, 0x5f, - 0x63, 0x70, 0x72, 0x74, 0x00, 0x00, 0x01, 0x0c, 0x00, 0x00, 0x00, 0x0c, - 0x77, 0x74, 0x70, 0x74, 0x00, 0x00, 0x01, 0x18, 0x00, 0x00, 0x00, 0x14, - 0x72, 0x58, 0x59, 0x5a, 0x00, 0x00, 0x01, 0x2c, 0x00, 0x00, 0x00, 0x14, - 0x67, 0x58, 0x59, 0x5a, 0x00, 0x00, 0x01, 0x40, 0x00, 0x00, 0x00, 0x14, - 0x62, 0x58, 0x59, 0x5a, 0x00, 0x00, 0x01, 0x54, 0x00, 0x00, 0x00, 0x14, - 0x72, 0x54, 0x52, 0x43, 0x00, 0x00, 0x01, 0x68, 0x00, 0x00, 0x00, 0x60, - 0x67, 0x54, 0x52, 0x43, 0x00, 0x00, 0x01, 0x68, 0x00, 0x00, 0x00, 0x60, - 0x62, 0x54, 0x52, 0x43, 0x00, 0x00, 0x01, 0x68, 0x00, 0x00, 0x00, 0x60, - 0x64, 0x65, 0x73, 0x63, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, - 0x75, 0x52, 0x47, 0x42, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x74, 0x65, 0x78, 0x74, 0x00, 0x00, 0x00, 0x00, - 0x43, 0x43, 0x30, 0x00, 0x58, 0x59, 0x5a, 0x20, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0xf3, 0x54, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x16, 0xc9, - 0x58, 0x59, 0x5a, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x6f, 0xa0, - 0x00, 0x00, 0x38, 0xf2, 0x00, 0x00, 0x03, 0x8f, 0x58, 0x59, 0x5a, 0x20, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x62, 0x96, 0x00, 0x00, 0xb7, 0x89, - 0x00, 0x00, 0x18, 0xda, 0x58, 0x59, 0x5a, 0x20, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x24, 0xa0, 0x00, 0x00, 0x0f, 0x85, 0x00, 0x00, 0xb6, 0xc4, - 0x63, 0x75, 0x72, 0x76, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2a, - 0x00, 0x00, 0x00, 0x7c, 0x00, 0xf8, 0x01, 0x9c, 0x02, 0x75, 0x03, 0x83, - 0x04, 0xc9, 0x06, 0x4e, 0x08, 0x12, 0x0a, 0x18, 0x0c, 0x62, 0x0e, 0xf4, - 0x11, 0xcf, 0x14, 0xf6, 0x18, 0x6a, 0x1c, 0x2e, 0x20, 0x43, 0x24, 0xac, - 0x29, 0x6a, 0x2e, 0x7e, 0x33, 0xeb, 0x39, 0xb3, 0x3f, 0xd6, 0x46, 0x57, - 0x4d, 0x36, 0x54, 0x76, 0x5c, 0x17, 0x64, 0x1d, 0x6c, 0x86, 0x75, 0x56, - 0x7e, 0x8d, 0x88, 0x2c, 0x92, 0x36, 0x9c, 0xab, 0xa7, 0x8c, 0xb2, 0xdb, - 0xbe, 0x99, 0xca, 0xc7, 0xd7, 0x65, 0xe4, 0x77, 0xf1, 0xf9, 0xff, 0xff -}; -unsigned int srgb_v2_micro_icc_len = 456; +var sRGBV2MicroICCProfile = []byte{ + 0x00, 0x00, 0x01, 0xc8, 0x6c, 0x63, 0x6d, 0x73, 0x02, 0x10, 0x00, 0x00, + 0x6d, 0x6e, 0x74, 0x72, 0x52, 0x47, 0x42, 0x20, 0x58, 0x59, 0x5a, 0x20, + 0x07, 0xe2, 0x00, 0x03, 0x00, 0x14, 0x00, 0x09, 0x00, 0x0e, 0x00, 0x1d, + 0x61, 0x63, 0x73, 0x70, 0x4d, 0x53, 0x46, 0x54, 0x00, 0x00, 0x00, 0x00, + 0x73, 0x61, 0x77, 0x73, 0x63, 0x74, 0x72, 0x6c, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf6, 0xd6, + 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0xd3, 0x2d, 0x68, 0x61, 0x6e, 0x64, + 0x9d, 0x91, 0x00, 0x3d, 0x40, 0x80, 0xb0, 0x3d, 0x40, 0x74, 0x2c, 0x81, + 0x9e, 0xa5, 0x22, 0x8e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09, + 0x64, 0x65, 0x73, 0x63, 0x00, 0x00, 0x00, 0xf0, 0x00, 0x00, 0x00, 0x5f, + 0x63, 0x70, 0x72, 0x74, 0x00, 0x00, 0x01, 0x0c, 0x00, 0x00, 0x00, 0x0c, + 0x77, 0x74, 0x70, 0x74, 0x00, 0x00, 0x01, 0x18, 0x00, 0x00, 0x00, 0x14, + 0x72, 0x58, 0x59, 0x5a, 0x00, 0x00, 0x01, 0x2c, 0x00, 0x00, 0x00, 0x14, + 0x67, 0x58, 0x59, 0x5a, 0x00, 0x00, 0x01, 0x40, 0x00, 0x00, 0x00, 0x14, + 0x62, 0x58, 0x59, 0x5a, 0x00, 0x00, 0x01, 0x54, 0x00, 0x00, 0x00, 0x14, + 0x72, 0x54, 0x52, 0x43, 0x00, 0x00, 0x01, 0x68, 0x00, 0x00, 0x00, 0x60, + 0x67, 0x54, 0x52, 0x43, 0x00, 0x00, 0x01, 0x68, 0x00, 0x00, 0x00, 0x60, + 0x62, 0x54, 0x52, 0x43, 0x00, 0x00, 0x01, 0x68, 0x00, 0x00, 0x00, 0x60, + 0x64, 0x65, 0x73, 0x63, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, + 0x75, 0x52, 0x47, 0x42, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x74, 0x65, 0x78, 0x74, 0x00, 0x00, 0x00, 0x00, + 0x43, 0x43, 0x30, 0x00, 0x58, 0x59, 0x5a, 0x20, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0xf3, 0x54, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x16, 0xc9, + 0x58, 0x59, 0x5a, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x6f, 0xa0, + 0x00, 0x00, 0x38, 0xf2, 0x00, 0x00, 0x03, 0x8f, 0x58, 0x59, 0x5a, 0x20, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x62, 0x96, 0x00, 0x00, 0xb7, 0x89, + 0x00, 0x00, 0x18, 0xda, 0x58, 0x59, 0x5a, 0x20, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x24, 0xa0, 0x00, 0x00, 0x0f, 0x85, 0x00, 0x00, 0xb6, 0xc4, + 0x63, 0x75, 0x72, 0x76, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2a, + 0x00, 0x00, 0x00, 0x7c, 0x00, 0xf8, 0x01, 0x9c, 0x02, 0x75, 0x03, 0x83, + 0x04, 0xc9, 0x06, 0x4e, 0x08, 0x12, 0x0a, 0x18, 0x0c, 0x62, 0x0e, 0xf4, + 0x11, 0xcf, 0x14, 0xf6, 0x18, 0x6a, 0x1c, 0x2e, 0x20, 0x43, 0x24, 0xac, + 0x29, 0x6a, 0x2e, 0x7e, 0x33, 0xeb, 0x39, 0xb3, 0x3f, 0xd6, 0x46, 0x57, + 0x4d, 0x36, 0x54, 0x76, 0x5c, 0x17, 0x64, 0x1d, 0x6c, 0x86, 0x75, 0x56, + 0x7e, 0x8d, 0x88, 0x2c, 0x92, 0x36, 0x9c, 0xab, 0xa7, 0x8c, 0xb2, 0xdb, + 0xbe, 0x99, 0xca, 0xc7, 0xd7, 0x65, 0xe4, 0x77, 0xf1, 0xf9, 0xff, 0xff, +} + +const sRGBV2MicroICCProfileLength = 456 // ATTRIBUTION: // The following micro icc profile taken from: https://github.com/saucecontrol/Compact-ICC-Profiles. // Read more (very interesting): https://photosauce.net/blog/post/making-a-minimal-srgb-icc-profile-part-1-trim-the-fat-abuse-the-spec -unsigned char sgray_v2_micro_icc[] = { - 0x00, 0x00, 0x01, 0x50, 0x6c, 0x63, 0x6d, 0x73, 0x02, 0x10, 0x00, 0x00, - 0x6d, 0x6e, 0x74, 0x72, 0x47, 0x52, 0x41, 0x59, 0x58, 0x59, 0x5a, 0x20, - 0x07, 0xe2, 0x00, 0x03, 0x00, 0x14, 0x00, 0x09, 0x00, 0x0e, 0x00, 0x1d, - 0x61, 0x63, 0x73, 0x70, 0x4d, 0x53, 0x46, 0x54, 0x00, 0x00, 0x00, 0x00, - 0x73, 0x61, 0x77, 0x73, 0x63, 0x74, 0x72, 0x6c, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf6, 0xd6, - 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0xd3, 0x2d, 0x68, 0x61, 0x6e, 0x64, - 0x05, 0xd2, 0x02, 0xa7, 0xf9, 0xdd, 0x47, 0x94, 0xc7, 0x4f, 0x4c, 0x5f, - 0x26, 0x82, 0x3a, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, - 0x64, 0x65, 0x73, 0x63, 0x00, 0x00, 0x00, 0xb4, 0x00, 0x00, 0x00, 0x5f, - 0x63, 0x70, 0x72, 0x74, 0x00, 0x00, 0x00, 0xd0, 0x00, 0x00, 0x00, 0x0c, - 0x77, 0x74, 0x70, 0x74, 0x00, 0x00, 0x00, 0xdc, 0x00, 0x00, 0x00, 0x14, - 0x6b, 0x54, 0x52, 0x43, 0x00, 0x00, 0x00, 0xf0, 0x00, 0x00, 0x00, 0x60, - 0x64, 0x65, 0x73, 0x63, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, - 0x75, 0x47, 0x72, 0x79, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x74, 0x65, 0x78, 0x74, 0x00, 0x00, 0x00, 0x00, - 0x43, 0x43, 0x30, 0x00, 0x58, 0x59, 0x5a, 0x20, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0xf3, 0x54, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x16, 0xc9, - 0x63, 0x75, 0x72, 0x76, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2a, - 0x00, 0x00, 0x00, 0x7c, 0x00, 0xf8, 0x01, 0x9c, 0x02, 0x75, 0x03, 0x83, - 0x04, 0xc9, 0x06, 0x4e, 0x08, 0x12, 0x0a, 0x18, 0x0c, 0x62, 0x0e, 0xf4, - 0x11, 0xcf, 0x14, 0xf6, 0x18, 0x6a, 0x1c, 0x2e, 0x20, 0x43, 0x24, 0xac, - 0x29, 0x6a, 0x2e, 0x7e, 0x33, 0xeb, 0x39, 0xb3, 0x3f, 0xd6, 0x46, 0x57, - 0x4d, 0x36, 0x54, 0x76, 0x5c, 0x17, 0x64, 0x1d, 0x6c, 0x86, 0x75, 0x56, - 0x7e, 0x8d, 0x88, 0x2c, 0x92, 0x36, 0x9c, 0xab, 0xa7, 0x8c, 0xb2, 0xdb, - 0xbe, 0x99, 0xca, 0xc7, 0xd7, 0x65, 0xe4, 0x77, 0xf1, 0xf9, 0xff, 0xff -}; -unsigned int sgray_v2_micro_icc_len = 336; +var sGrayV2MicroICCProfile = []byte{ + 0x00, 0x00, 0x01, 0x50, 0x6c, 0x63, 0x6d, 0x73, 0x02, 0x10, 0x00, 0x00, + 0x6d, 0x6e, 0x74, 0x72, 0x47, 0x52, 0x41, 0x59, 0x58, 0x59, 0x5a, 0x20, + 0x07, 0xe2, 0x00, 0x03, 0x00, 0x14, 0x00, 0x09, 0x00, 0x0e, 0x00, 0x1d, + 0x61, 0x63, 0x73, 0x70, 0x4d, 0x53, 0x46, 0x54, 0x00, 0x00, 0x00, 0x00, + 0x73, 0x61, 0x77, 0x73, 0x63, 0x74, 0x72, 0x6c, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf6, 0xd6, + 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0xd3, 0x2d, 0x68, 0x61, 0x6e, 0x64, + 0x05, 0xd2, 0x02, 0xa7, 0xf9, 0xdd, 0x47, 0x94, 0xc7, 0x4f, 0x4c, 0x5f, + 0x26, 0x82, 0x3a, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, + 0x64, 0x65, 0x73, 0x63, 0x00, 0x00, 0x00, 0xb4, 0x00, 0x00, 0x00, 0x5f, + 0x63, 0x70, 0x72, 0x74, 0x00, 0x00, 0x00, 0xd0, 0x00, 0x00, 0x00, 0x0c, + 0x77, 0x74, 0x70, 0x74, 0x00, 0x00, 0x00, 0xdc, 0x00, 0x00, 0x00, 0x14, + 0x6b, 0x54, 0x52, 0x43, 0x00, 0x00, 0x00, 0xf0, 0x00, 0x00, 0x00, 0x60, + 0x64, 0x65, 0x73, 0x63, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, + 0x75, 0x47, 0x72, 0x79, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x74, 0x65, 0x78, 0x74, 0x00, 0x00, 0x00, 0x00, + 0x43, 0x43, 0x30, 0x00, 0x58, 0x59, 0x5a, 0x20, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0xf3, 0x54, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x16, 0xc9, + 0x63, 0x75, 0x72, 0x76, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2a, + 0x00, 0x00, 0x00, 0x7c, 0x00, 0xf8, 0x01, 0x9c, 0x02, 0x75, 0x03, 0x83, + 0x04, 0xc9, 0x06, 0x4e, 0x08, 0x12, 0x0a, 0x18, 0x0c, 0x62, 0x0e, 0xf4, + 0x11, 0xcf, 0x14, 0xf6, 0x18, 0x6a, 0x1c, 0x2e, 0x20, 0x43, 0x24, 0xac, + 0x29, 0x6a, 0x2e, 0x7e, 0x33, 0xeb, 0x39, 0xb3, 0x3f, 0xd6, 0x46, 0x57, + 0x4d, 0x36, 0x54, 0x76, 0x5c, 0x17, 0x64, 0x1d, 0x6c, 0x86, 0x75, 0x56, + 0x7e, 0x8d, 0x88, 0x2c, 0x92, 0x36, 0x9c, 0xab, 0xa7, 0x8c, 0xb2, 0xdb, + 0xbe, 0x99, 0xca, 0xc7, 0xd7, 0x65, 0xe4, 0x77, 0xf1, 0xf9, 0xff, 0xff, +} + +const sGrayV2MicroICCProfileLength = 336 -unsigned char srgb_iec61966_2_1_icc[] = { +var sRGBIEC6196621ICCProfile = []byte{ 0x00, 0x00, 0x0b, 0xe8, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x6d, 0x6e, 0x74, 0x72, 0x52, 0x47, 0x42, 0x20, 0x58, 0x59, 0x5a, 0x20, 0x07, 0xd9, 0x00, 0x03, 0x00, 0x1b, 0x00, 0x15, 0x00, 0x24, 0x00, 0x1f, @@ -336,11 +342,12 @@ unsigned char srgb_iec61966_2_1_icc[] = { 0x30, 0x39, 0x00, 0x00, 0x73, 0x66, 0x33, 0x32, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x0c, 0x44, 0x00, 0x00, 0x05, 0xdf, 0xff, 0xff, 0xf3, 0x26, 0x00, 0x00, 0x07, 0x94, 0x00, 0x00, 0xfd, 0x8f, 0xff, 0xff, 0xfb, 0xa1, - 0xff, 0xff, 0xfd, 0xa2, 0x00, 0x00, 0x03, 0xdb, 0x00, 0x00, 0xc0, 0x75 -}; -unsigned int srgb_iec61966_2_1_icc_len = 3048; + 0xff, 0xff, 0xfd, 0xa2, 0x00, 0x00, 0x03, 0xdb, 0x00, 0x00, 0xc0, 0x75, +} + +const sRGBIEC6196621ICCProfileLength = 3048 -unsigned char generic_gray_gamma_2_2_icc[] = { +var genericGrayGamma22ICCProfile = []byte{ 0x00, 0x00, 0x0e, 0x04, 0x61, 0x70, 0x70, 0x6c, 0x02, 0x00, 0x00, 0x00, 0x6d, 0x6e, 0x74, 0x72, 0x47, 0x52, 0x41, 0x59, 0x58, 0x59, 0x5a, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, @@ -639,46 +646,42 @@ unsigned char generic_gray_gamma_2_2_icc[] = { 0x00, 0x32, 0x00, 0x2e, 0x00, 0x32, 0x00, 0x20, 0x82, 0x72, 0x5f, 0x69, 0x63, 0xcf, 0x8f, 0xf0, 0x90, 0x1a, 0x75, 0x28, 0x70, 0x70, 0x5e, 0xa6, 0x7c, 0xfb, 0x65, 0x70, 0x00, 0x20, 0x00, 0x32, 0x00, 0x2e, 0x00, 0x32, - 0x00, 0x20, 0x63, 0xcf, 0x8f, 0xf0, 0x65, 0x87, 0x4e, 0xf6, 0x00, 0x00 -}; -unsigned int generic_gray_gamma_2_2_icc_len = 3588; - -char *SRGB_V2_MICRO_ICC_PATH = "/tmp/srgb_v2_micro.icc"; -char *SGRAY_V2_MICRO_ICC_PATH = "/tmp/sgray_v2_micro.icc"; -char *SRGB_IEC61966_2_1_ICC_PATH = "/tmp/srgb_iec61966_2_1.icc"; -char *GENERIC_GRAY_GAMMA_2_2_ICC_PATH = "/tmp/generic_gray_gamma_2_2.icc"; - -int store_to_file(unsigned char *buffer, unsigned int buffer_len, char *path); + 0x00, 0x20, 0x63, 0xcf, 0x8f, 0xf0, 0x65, 0x87, 0x4e, 0xf6, 0x00, 0x00, +} -int icc_profiles_init() { - int result = 0; +const genericGrayGamma22ICCProfileLength = 3588 - result += store_to_file(srgb_v2_micro_icc, srgb_v2_micro_icc_len, SRGB_V2_MICRO_ICC_PATH); - result += store_to_file(sgray_v2_micro_icc, sgray_v2_micro_icc_len, SGRAY_V2_MICRO_ICC_PATH); - result += store_to_file(srgb_iec61966_2_1_icc, srgb_iec61966_2_1_icc_len, SRGB_IEC61966_2_1_ICC_PATH); - result += store_to_file(generic_gray_gamma_2_2_icc, generic_gray_gamma_2_2_icc_len, GENERIC_GRAY_GAMMA_2_2_ICC_PATH); +const sRGBV2MicroICCProfilePath = "srgb_v2_micro.icc" +const sGrayV2MicroICCProfilePath = "sgray_v2_micro.icc" +const sRGBIEC6196621ICCProfilePath = "srgb_iec61966_2_1.icc" +const genericGrayGamma22ICCProfilePath = "generic_gray_gamma_2_2.icc" - return result; -} +var temporaryDirectory string -int store_to_file(unsigned char *buffer, unsigned int buffer_len, char *path) { - int result = 1; - int out_fd = open(path, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); +func initializeICCProfiles() { + var err error + temporaryDirectory, err = ioutil.TempDir("", "govips-") + if err != nil { + panic(fmt.Sprintf("Couldn't create temporary directory: %v", err.Error())) + } - if (out_fd == -1) { - // TODO libraries shouldn't printf to stdout, g_warning() should be used instead - printf("failed opening file for writing: %s\n", path); - return -1; - } + err = ioutil.WriteFile(filepath.Join(temporaryDirectory, sRGBV2MicroICCProfilePath), sRGBV2MicroICCProfile, 0600) + if err != nil { + panic(fmt.Sprintf("Couldn't store temporary file for ICC profile 1: %v", err.Error())) + } + err = ioutil.WriteFile(filepath.Join(temporaryDirectory, sGrayV2MicroICCProfilePath), sGrayV2MicroICCProfile, 0600) + if err != nil { + panic(fmt.Sprintf("Couldn't store temporary file for ICC profile 2: %v", err.Error())) + } - if (write(out_fd, buffer, buffer_len) == -1) { - // TODO libraries shouldn't printf to stdout, g_warning() should be used instead - printf("failed writing to file: %s\n", path); - close(out_fd); - return -1; - } + err = ioutil.WriteFile(filepath.Join(temporaryDirectory, sRGBIEC6196621ICCProfilePath), sRGBIEC6196621ICCProfile, 0600) + if err != nil { + panic(fmt.Sprintf("Couldn't store temporary file for ICC profile 3: %v", err.Error())) + } - close(out_fd); - return 0; + err = ioutil.WriteFile(filepath.Join(temporaryDirectory, genericGrayGamma22ICCProfilePath), genericGrayGamma22ICCProfile, 0600) + if err != nil { + panic(fmt.Sprintf("Couldn't store temporary file for ICC profile 4: %v", err.Error())) + } } diff --git a/vips/icc_profiles.h b/vips/icc_profiles.h deleted file mode 100644 index 8ee722a3..00000000 --- a/vips/icc_profiles.h +++ /dev/null @@ -1,6 +0,0 @@ -extern char *SRGB_V2_MICRO_ICC_PATH; -extern char *SGRAY_V2_MICRO_ICC_PATH; -extern char *SRGB_IEC61966_2_1_ICC_PATH; -extern char *GENERIC_GRAY_GAMMA_2_2_ICC_PATH; - -int icc_profiles_init(); diff --git a/vips/icc_profiles_test.go b/vips/icc_profiles_test.go new file mode 100644 index 00000000..187e15ef --- /dev/null +++ b/vips/icc_profiles_test.go @@ -0,0 +1,36 @@ +package vips + +import ( + "io/ioutil" + "path/filepath" + "testing" + + "github.com/stretchr/testify/assert" +) + +func Test_ICCProfileLengths(t *testing.T) { + assert.Equal(t, len(sRGBV2MicroICCProfile), sRGBV2MicroICCProfileLength) + assert.Equal(t, len(sGrayV2MicroICCProfile), sGrayV2MicroICCProfileLength) + assert.Equal(t, len(sRGBIEC6196621ICCProfile), sRGBIEC6196621ICCProfileLength) + assert.Equal(t, len(genericGrayGamma22ICCProfile), genericGrayGamma22ICCProfileLength) +} + +func Test_ICCProfileInitialisation(t *testing.T) { + initializeICCProfiles() + + srgbProfile, err := ioutil.ReadFile(filepath.Join(temporaryDirectory, sRGBV2MicroICCProfilePath)) + assert.NoError(t, err) + assert.Equal(t, sRGBV2MicroICCProfile, srgbProfile) + + grayProfile, err := ioutil.ReadFile(filepath.Join(temporaryDirectory, sGrayV2MicroICCProfilePath)) + assert.NoError(t, err) + assert.Equal(t, sGrayV2MicroICCProfile, grayProfile) + + srgbProfile2, err := ioutil.ReadFile(filepath.Join(temporaryDirectory, sRGBIEC6196621ICCProfilePath)) + assert.NoError(t, err) + assert.Equal(t, sRGBIEC6196621ICCProfile, srgbProfile2) + + grayProfile2, err := ioutil.ReadFile(filepath.Join(temporaryDirectory, genericGrayGamma22ICCProfilePath)) + assert.NoError(t, err) + assert.Equal(t, genericGrayGamma22ICCProfile, grayProfile2) +}