From 5aeedfc2fe84f618ca05f86fc05b001b7d8ad72b Mon Sep 17 00:00:00 2001 From: "jk7744.park" Date: Tue, 8 Sep 2015 22:45:05 +0900 Subject: [PATCH] tizen 2.3.1 release --- COPYING | 2 +- ChangeLog | 51 + NEWS | 33 +- README | 2 +- configure.ac | 5 +- debian/changelog | 460 ---- debian/compat | 1 - debian/control | 77 - debian/copyright | 36 - debian/eet.1 | 50 - debian/jobs | 0 debian/libeet-bin.install | 1 - debian/libeet-dev.install | 5 - debian/libeet-doc.dirs | 1 - debian/libeet-doc.doc-base | 10 - debian/libeet1.install | 1 - debian/libeet1.symbols | 85 - debian/rules | 24 - doc/{examples.dox => eet_examples.dox} | 21 +- eet-tools.manifest | 8 + eet.manifest | 5 + packaging/eet.spec | 31 +- src/lib/Eet.h | 4325 +++++++++++++++++--------------- src/lib/Makefile.am | 7 +- src/lib/eet_alloc.c | 4 +- src/lib/eet_cipher.c | 5 +- src/lib/eet_connection.c | 10 + src/lib/eet_data.c | 80 +- src/lib/eet_image.c | 1092 +++++++- src/lib/eet_lib.c | 34 +- src/lib/eet_node.c | 2 +- src/lib/lz4/README | 2 +- src/lib/lz4/lz4.c | 914 +++---- src/lib/lz4/lz4.h | 94 +- src/lib/lz4/lz4hc.c | 50 +- src/lib/rg_etc/README | 31 + src/lib/rg_etc/etc2_encoder.c | 1052 ++++++++ src/lib/rg_etc/rg_etc1.c | 2833 +++++++++++++++++++++ src/lib/rg_etc/rg_etc1.h | 78 + src/lib/rg_etc/rg_etc2.c | 345 +++ 40 files changed, 8371 insertions(+), 3496 deletions(-) delete mode 100644 debian/changelog delete mode 100644 debian/compat delete mode 100644 debian/control delete mode 100644 debian/copyright delete mode 100644 debian/eet.1 delete mode 100644 debian/jobs delete mode 100644 debian/libeet-bin.install delete mode 100644 debian/libeet-dev.install delete mode 100644 debian/libeet-doc.dirs delete mode 100644 debian/libeet-doc.doc-base delete mode 100644 debian/libeet1.install delete mode 100644 debian/libeet1.symbols delete mode 100755 debian/rules rename doc/{examples.dox => eet_examples.dox} (95%) create mode 100644 eet-tools.manifest create mode 100644 eet.manifest create mode 100644 src/lib/rg_etc/README create mode 100644 src/lib/rg_etc/etc2_encoder.c create mode 100644 src/lib/rg_etc/rg_etc1.c create mode 100644 src/lib/rg_etc/rg_etc1.h create mode 100644 src/lib/rg_etc/rg_etc2.c diff --git a/COPYING b/COPYING index 296efe7..26179bd 100644 --- a/COPYING +++ b/COPYING @@ -1,6 +1,6 @@ Copyright notice for Eet: -Copyright (C) 2002-2011 Carsten Haitzler and various contributors (see AUTHORS) +Copyright (C) 2002-2014 Carsten Haitzler and various contributors (see AUTHORS) All rights reserved. diff --git a/ChangeLog b/ChangeLog index 0ce223a..3f250fc 100644 --- a/ChangeLog +++ b/ChangeLog @@ -607,3 +607,54 @@ * Add code to detect overrun and underrun in eet_data_descriptor_element_add. * Fix possible wrong size decoding for simple type. + +2012-08-09 Cedric Bail + + * Don't copy string around for nothing. + +2012-08-22 Igor Murzov (GArik_) + + * Fixed memory leak of eet_data_chunk_put(). + +2012-08-24 Cedric Bail + + * Add eet_connection_empty. + +2012-08-26 Cedric Bail + + * Correctly initialize dictionary correctly. + +2012-08-30 Carsten Haitzler (The Rasterman) + + 1.7.0 release + +2012-09-21 Carsten Haitzler (The Rasterman) + + * Fix big endian bug with eet image handling and endianess swapping. + +2012-09-27 Patryk Kaczmarek + + * Add eet file handle null checks to eet_data_write_cipher(), + eet_data_descriptor_decode_cipher(), + eet_data_descriptor_encode_cipher(), eet_data_xattr_cipher_get(), + eet_data_xattr_cipher_set(). + +2012-10-04 Vincent Torri + + * Update lz4 code to rev. 77. This fix compilation on NetBSD 5.0 + +2012-10-20 Cedric Bail + + 1.7.1 release + +2012-11-14 Michal Jagiello + + * Add missing UNLOCK_CACHE in eet_open function. + +2012-12-11 Cedric Bail + + * Fix leak eet_pbkdf2_sha1 with OpenSSL. + +2012-12-12 Daniel Willmann + + * Fix possible buffer overflow in functions relying on EET_T_LAST. diff --git a/NEWS b/NEWS index 456bd18..38379f8 100644 --- a/NEWS +++ b/NEWS @@ -1,10 +1,38 @@ -Eet 1.7.0 +Eet 1.7.4 + +Changes since Eet 1.7.3: +-------------------------- + +Fixes: + * Fix memory leak when using OpenSSL for ciphering long data. + * Fix possible buffer overflow in functions relying on EET_T_LAST + +Eet 1.7.3 + +Changes since Eet 1.7.2: +-------------------------- + +Fixes: + * Updated lz4 code. + +Changes since Eet 1.7.1: +-------------------------- + +Fixes: + * Add missing UNLOCK_CACHE in eet_open function + +Changes since Eet 1.7.0: +-------------------------- + +Fixes: + * Fix PPC (big endian) image codec bug. Changes since Eet 1.6.0: -------------------------- Additions: * Add code to detect overrun and underrun during Eet Data Descriptor setup. + * Add eet_connection_empty. Fixes: * Force destruction of all pending file when shuting down eet. @@ -12,6 +40,9 @@ Fixes: * Check that gnutls and openssl don't return below zero size during decipher. * Fix crash when cyphering huge amount of data. * Possible wrong size decoding of simple type. + * Don't strdup string around for nothing. + * Always initialize dictionary correctly. + * Fixed memory leak of eet_data_chunk_put(). Eet 1.6.0 diff --git a/README b/README index 416214b..989f4bb 100644 --- a/README +++ b/README @@ -1,4 +1,4 @@ -Eet 1.6.0 +Eet 1.7.99 ****************************************************************************** diff --git a/configure.ac b/configure.ac index 5634c49..444b840 100644 --- a/configure.ac +++ b/configure.ac @@ -1,7 +1,7 @@ ##--##--##--##--##--##--##--##--##--##--##--##--##--##--##--##--## ##--##--##--##--##--##--##--##--##--##--##--##--##--##--##--##--## m4_define([v_maj], [1]) -m4_define([v_min], [6]) +m4_define([v_min], [7]) m4_define([v_mic], [99]) m4_define([v_rev], m4_esyscmd([(svnversion "${SVN_REPO_PATH:-.}" | grep -v '\(export\|Unversioned directory\)' || echo 0) | awk -F : '{printf("%s\n", $1);}' | tr -d ' :MSP\n'])) m4_if(v_rev, [0], [m4_define([v_rev], m4_esyscmd([git log 2> /dev/null | (grep -m1 git-svn-id || echo 0) | sed -e 's/.*@\([0-9]*\).*/\1/' | tr -d '\n']))]) @@ -229,8 +229,7 @@ PKG_CHECK_EXISTS([exotic], [ enable_exotic="yes" AC_DEFINE([HAVE_EXOTIC_H], [1], [Define to 1 if you have Exotic.]) - EINA_CONFIGURE_HAVE_EXOTIC="#define EINA_HAVE_EXOTIC" - requirements_pc_eina="exotic ${requirements_pc_eina}" + requirements_pc_eet="exotic ${requirements_pc_eet}" ], [enable_exotic="no"]) diff --git a/debian/changelog b/debian/changelog deleted file mode 100644 index 4421a3d..0000000 --- a/debian/changelog +++ /dev/null @@ -1,460 +0,0 @@ -eet (1.6.0+svn.70375slp2+build02) unstable; urgency=low - - * Package Upload - * Git: slp/pkgs/e/eet - * Tag: eet_1.6.0+svn.70375slp2+build02 - - -- Myungjae Lee Wed, 25 Apr 2012 17:28:05 +0900 - -eet (1.6.0+svn.70375slp2+build01) unstable; urgency=low - - * Package Upload - * Git: slp/pkgs/e/eet - * Tag: eet_1.6.0+svn.70375slp2+build01 - - -- Myungjae Lee Wed, 25 Apr 2012 15:12:39 +0900 - -eet (1.6.0+svn.70308slp2+build01) unstable; urgency=low - - * Package Upload - * Git: slp/pkgs/e/eet - * Tag: eet_1.6.0+svn.70308slp2+build01 - - -- Jaehwan Kim Mon, 23 Apr 2012 15:28:18 +0900 - -eet (1.6.0+svn.70142slp2+build01) unstable; urgency=low - - * Package Upload - * Git: slp/pkgs/e/eet - * Tag: eet_1.6.0+svn.70142slp2+build01 - - -- Jiyoun Park Fri, 13 Apr 2012 18:31:30 +0900 - -eet (1.6.0+svn.69899slp2+build01) unstable; urgency=low - - * Package Upload - * Git: slp/pkgs/e/eet - * Tag: eet_1.6.0+svn.69899slp2+build01 - - -- Jeonghyun Yun Fri, 06 Apr 2012 18:11:00 +0900 - -eet (1.5.0+svn.69626slp2+build01) unstable; urgency=low - - * Package Upload - * Git: slp/pkgs/e/eet - * Tag: eet_1.5.0+svn.69626slp2+build01 - - -- Jeonghyun Yun Wed, 28 Mar 2012 14:27:55 +0900 - -eet (1.5.0+svn.68718slp2+build01) unstable; urgency=low - - * Package Upload - * Git: slp/pkgs/e/eet - * Tag: eet_1.5.0+svn.68718slp2+build01 - - -- Jeonghyun Yun Wed, 07 Mar 2012 16:43:31 +0900 - -eet (1.5.0+svn.68521slp2+build01) unstable; urgency=low - - * Package upload - * Git: slp/pkgs/e/eet - * Tag: eet_1.5.0+svn.68521slp2+build01 - - -- Jeonghyun Yun Fri, 02 Mar 2012 06:30:44 -0500 - -eet (1.5.0+svn.67705slp2+build01) unstable; urgency=low - - * Package Upload - * Git: slp/pkgs/e/eet - * Tag: eet_1.5.0+svn.67705slp2+build01 - - -- Jaehwan Kim Wed, 15 Feb 2012 18:59:11 +0900 - -eet (1.5.0+svn.66958slp2+build01) unstable; urgency=low - - * Package Upload - * Git: slp/pkgs/e/eet - * Tag: eet_1.5.0+svn.66958slp2+build01 - - -- Jaehwan Kim Mon, 16 Jan 2012 18:28:56 +0900 - -eet (1.5.0+svn.65860slp2+build02) unstable; urgency=low - - * libjpeg7 -> libjpeg8 - * Git: slp/pkgs/e/eet - * Tag: eet_1.5.0+svn.65860slp2+build02 - - -- ChunEon Park Wed, 11 Jan 2012 14:08:18 +0900 - -eet (1.5.0+svn.65860slp2+build01) unstable; urgency=low - - * Package Upload for migration - * Git: slp-scm.sec.samsung.net:slp/pkgs/e/eet - * Tag: eet_1.5.0+svn.65860slp2+build01 - - -- Jaehwan Kim Thu, 08 Dec 2011 13:48:37 +0900 - -eet (1.5.0+svn.65577slp2+build01) unstable; urgency=low - - * Package Upload - * Git: slp-scm.sec.samsung.net:slp/pkgs/e/eet - * Tag: eet_1.5.0+svn.65577slp2+build01 - - -- Jaehwan Kim Tue, 29 Nov 2011 13:29:42 +0900 - -eet (1.5.0+svn.65300slp2+build01) unstable; urgency=low - - * Merge with upstream @65300 - - -- Mike McCormack Thu, 17 Nov 2011 08:20:00 +0900 - -eet (1.2.0+svn.64981slp2+build01) unstable; urgency=low - - * Merge with upstream @64981 - - -- Mike McCormack Thu, 10 Nov 2011 11:29:43 +0900 - -eet (1.2.0+svn.64494slp2+build01) unstable; urgency=low - - * Merge with upstream 64494 - - -- Mike McCormack Thu, 03 Nov 2011 09:33:23 +0900 - -eet (1.2.0+svn.63811slp2+build01) unstable; urgency=low - - * Merge with upstream - * Git: 165.213.180.234:slp/pkgs/e/eet - * Tag: eet_1.2.0+svn.63811slp2+build01 - - -- Mike McCormack Wed, 05 Oct 2011 11:33:08 +0900 - -eet (1.2.0+svn.62590slp2+build01) unstable; urgency=low - - * [SVN EFL Migration] eet in SLP is merged with SVN r62590 - * Important Changes - [Migration upstream r62590] Merge remote branch 'origin/upstream' into svn_merge - * Git: 165.213.180.234:slp/pkgs/e/eet - * Tag: eet_1.2.0+svn.62590slp2+build01 - - -- Jaehwan Kim Fri, 02 Sep 2011 18:41:10 +0900 - -eet (1.2.0+svn.60246slp2+build01) unstable; urgency=low - - * [SVN EFL Migration] eet in SLP is merged with SVN r60246 - * Git: 165.213.180.234:slp/pkgs/e/eet - * Tag: eet_1.2.0+svn.60246slp2+build01 - - -- Jaehwan Kim Fri, 24 Jun 2011 18:09:42 +0900 - -eet (1.2.0+svn.58117slp2+build01) unstable; urgency=low - - * [SVN EFL Migration] eet in SLP is merged with SVN r58117 - * Git: 165.213.180.234:slp/pkgs/e/eet - * Tag: eet_1.2.0+svn.58117slp2+build01 - - -- Jaehwan Kim Tue, 05 Apr 2011 15:54:48 +0900 - -eet (1.2.0+svn.57844slp2+build02) unstable; urgency=low - - * Rollback - * Git: slp-scm.sec.samsung.net:slp/pkgs/e/eet - * Tag: eet_1.2.0+svn.57844slp2+build02 - - -- Shinwoo Kim Tue, 29 Mar 2011 23:30:25 +0900 - -eet (1.2.0+svn.57844slp2+build01) unstable; urgency=low - - * [SVN EFL Migration] eet in SLP is merged with SVN r57844 - * Git: 165.213.180.234:slp/pkgs/e/eet - * Tag: eet_1.2.0+svn.57844slp2+build01 - - -- Shinwoo Kim Tue, 29 Mar 2011 18:55:12 +0900 - -eet (1.2.0+svn.57246slp2+build05) unstable; urgency=low - - * [SVN EFL Migration] eet in SLP is merged with SVN r57246 - * Git: 165.213.180.234:slp/pkgs/e/eet - * Tag: eet_1.2.0+svn.57246slp2+build05 - - -- Myungjae Lee Wed, 09 Mar 2011 11:29:22 +0900 - -eet (1.2.0+svn.57246slp2+build04) unstable; urgency=low - - * [SVN EFL Migration] eet in SLP is merged with SVN r57246 - * Git: 165.213.180.234:slp/pkgs/e/eet - * Tag: eet_1.2.0+svn.57246slp2+build04 - - -- Myungjae Lee Wed, 09 Mar 2011 11:14:38 +0900 - -eet (1.2.0+svn.57246slp2+build03) unstable; urgency=low - - * Package Upload : Rollback - * Git: 165.213.180.234:/slp/pkgs/e/eet - * Tag: eet_1.2.0+svn.57246slp2+build03 - - -- WooHyun Jung Tue, 08 Mar 2011 12:10:46 +0900 - -eet (1.2.0+svn.57246slp2+build02) unstable; urgency=low - - * Package upload : Rollback - * Git: 165.213.180.234:/slp/pkgs/e/eet - * Tag: eet_1.2.0+svn.57246slp2+build02 - - -- WooHyun Jung Tue, 08 Mar 2011 11:02:33 +0900 - -eet (1.2.0+svn.57246slp2+build01) unstable; urgency=low - - * [SVN EFL Migration] eet in SLP is merged with SVN r57246 - * Git: 165.213.180.234:slp/pkgs/e/eet - * Tag: eet_1.2.0+svn.57246slp2+build01 - - -- Myungjae Lee Mon, 07 Mar 2011 17:27:55 +0900 - -eet (1.2.0+svn.56194slp2+build02) unstable; urgency=low - - * [SVN EFL Migration] eet in SLP is merged with SVN r56194 - * Git: 165.213.180.234:slp/pkgs/e/eet - * Tag: eet_1.2.0+svn.56194slp2+build02 - - -- WooHyun Jung Thu, 27 Jan 2011 12:20:29 +0900 - -eet (1.2.0+svn.56194slp2+build01) unstable; urgency=low - - * [SVN EFL Migration] eet in SLP is merged with SVN r56194 - * Git: 165.213.180.234:slp/pkgs/e/eet - * Tag: eet_1.2.0+svn.56194slp2+build01 - - -- WooHyun Jung Wed, 26 Jan 2011 13:21:31 +0900 - -eet (1.2.0+svn.55981slp2+build01) unstable; urgency=low - - * [SVN EFL Migration] eet in SLP is merged with SVN r55981 - * Git: 165.213.180.234:slp/pkgs/e/eet - * Tag: eet_1.2.0+svn.55981slp2+build01 - * Git: 165.213.180.234:slp/pkgs/e/eet - * Tag: eet_1.2.0+svn.55981slp2+build01 - - -- WooHyun Jung Wed, 19 Jan 2011 16:13:48 +0900 - -eet (1.2.0+svn.55636slp2+build01) unstable; urgency=low - - * Package Upload - * Git: 165.213.180.234:slp/pkgs/e/eet - * Tag: eet_1.2.0+svn.55636slp2+build01 - - -- Jaehwan Kim Mon, 03 Jan 2011 20:56:30 +0900 - -eet (1.2.0+svn.55490slp2+build01) unstable; urgency=low - - * [SVN EFL Migration] eet in SLP is merged with SVN r55490 - * Git: 165.213.180.234:slp/pkgs/e/eet - * Tag: eet_1.2.0+svn.55490slp2+build01 - - -- Jaehwan Kim Wed, 22 Dec 2010 19:54:50 +0900 - -eet (1.2.0+svn.55432slp2+build01) unstable; urgency=low - - * [SVN's EFL Migration] eet in SLP is merged with SVN r55432. - * Git: 165.213.180.234:slp/pkgs/e/eet - * Tag: eet_1.2.0+svn.55432slp2+build01 - - -- Juyung Seo Tue, 14 Dec 2010 17:30:12 +0900 - -eet (1.2.0+svn.55070slp2+build04) unstable; urgency=low - - * [SVN's EFL Migration] eet in SLP is merged with SVN r55432. - * Git: 165.213.180.234:slp/pkgs/e/eet - * Tag: eet_1.2.0+svn.55070slp2+build04 - - -- Juyung Seo Tue, 14 Dec 2010 14:47:53 +0900 - -eet (1.2.0+svn.55070slp2+build03) unstable; urgency=low - - * [SVN 55070 Merge] - * Update to SVN Revision 55070. - * Git: 165.213.180.234:/git/slp/pkgs/eet - * Tag: eet_1.2.0+svn.55070slp2+build03 - - -- Myoungwoon Kim Thu, 02 Dec 2010 14:12:02 +0900 - -eet (1.2.0+svn.55070slp2+build02) unstable; urgency=low - - * Rollback to eet_1.2.0+svn.51480slp2+build03. - * Git: 165.213.180.234:/git/slp/pkgs/eet - * Tag: eet_1.2.0+svn.55070slp2+build02 - - -- Juyung Seo Thu, 02 Dec 2010 09:41:29 +0900 - -eet (1.2.0+svn.55070slp2+build01) unstable; urgency=low - - * [SVN 55070 Merge] - * Update to SVN Revision 55070. - * Git: 165.213.180.234:/git/slp/pkgs/eet - * Tag: eet_1.2.0+svn.55070slp2+build01 - - -- Myoungwoon Kim Thu, 02 Dec 2010 08:59:59 +0900 - -eet (1.2.0+svn.51480slp2+build03) unstable; urgency=low - - * [SVN 54765 Merge] - * Update to SVN Revision 54765. - * Git: 165.213.180.234:/git/slp/pkgs/eet - * Tag: eet_1.2.0+svn.51480slp2+build03 - - -- Juyung Seo Fri, 26 Nov 2010 15:10:46 +0900 - -eet (1.2.0+svn.51480slp2+build02) unstable; urgency=low - - * add as-needed - * Git: 165.213.180.234:/git/slp/pkgs/eet - * Tag: eet_1.2.0+svn.51480slp2+build02 - - -- Jaehwan.kim Wed, 15 Sep 2010 09:34:32 +0900 - -eet (1.2.0+svn.51480slp2+build01) unstable; urgency=low - - * efl 1.0 alpha upgradey - * Git: 165.213.180.234:/git/slp/pkgs/eet - * Tag: eet_1.2.0+svn.51480slp2+build01 - - -- Jaehwan.kim Tue, 31 Aug 2010 22:39:25 +0900 - -eet (1.2.0+svn.49540slp2+3build04) unstable; urgency=low - - * Packaging. - * Git: 165.213.180.234:/git/slp2.0/slp2.0-pkgs/EFL-pkgs/eet - * Tag: eet_1.2.0+svn.49540slp2+3build04 - - -- Daniel Juyung Seo Thu, 10 Jun 2010 21:09:31 +0900 - -eet (1.2.0+svn.49540slp2+3build03) unstable; urgency=low - - * Packaging. - * Git: 165.213.180.234:/git/slp2.0/slp2.0-pkgs/EFL-pkgs/eet - * Tag: eet_1.2.0+svn.49540slp2+3build03 - - -- Daniel Juyung Seo Thu, 10 Jun 2010 21:04:14 +0900 - -eet (1.2.0+svn.49540slp2+3build02) unstable; urgency=low - - * Packaging. - * Git: 165.213.180.234:/git/slp2.0/slp2.0-pkgs/EFL-pkgs/eet - * Tag: eet_1.2.0+svn.49540slp2+3build02 - - -- Daniel Juyung Seo 목, 10 6월 2010 21:00:51 +0900 - -eet (1.2.0+svn.49540slp2+3) unstable; urgency=low - - * Packaging. - * Git: 165.213.180.234:/git/slp2.0/slp2.0-pkgs/EFL-pkgs/eet - * Tag: eet_1.2.0+svn.49540slp2+3 - - -- Daniel Juyung Seo Thu, 10 Jun 2010 20:46:54 +0900 - -eet (1.2.0+svn.49540slp2+2) unstable; urgency=low - - * Packaging. - * Git: 165.213.180.234:/git/slp2.0/slp2.0-pkgs/EFL-pkgs/eet - * Tag: eet_1.2.0+svn.49540slp2+2 - - -- Daniel Juyung Seo Thu, 10 Jun 2010 20:39:03 +0900 - -eet (1.2.0+svn.49540slp2+1) unstable; urgency=low - - * Packaging. - * Git: 165.213.180.234:/git/slp2.0/slp2.0-pkgs/EFL-pkgs/eet - * Tag: eet_1.2.0+svn.49540slp2+1 - - -- Daniel Juyung Seo Thu, 10 Jun 2010 20:13:45 +0900 - -eet (1.2.0+svn.49540slp2+0) unstable; urgency=low - - * Update opensource EFL from SVN - * SVN revision: 49540 (Total EFL revision: 49550) - * Tag: 1.2.0+svn.49540slp2+0 - - -- Daniel Juyung Seo Thu, 10 Jun 2010 15:50:39 +0900 - -eet (1.2.0+svn20100304slp2) unstable; urgency=low - - * change package version - - -- Jaehwan Kim Thu, 25 Mar 2010 15:56:23 +0900 - -eet (1.2.0+svn20100304-1) unstable; urgency=low - - * EFL_update_revision_46864 - - -- Jaehwan Kim Wed, 10 Mar 2010 16:06:33 +0900 - -eet (1.2.0+svn20100203-2) unstable; urgency=low - - * repack - - -- Jaehwan Kim Thu, 04 Feb 2010 20:21:09 +0900 - -eet (1.2.0+svn20100203-1) unstable; urgency=low - - * EFL_update_revision_45828 - - -- Jaehwan Kim Wed, 03 Feb 2010 16:39:18 +0900 - -eet (1.2.0+svn20100119-2) unstable; urgency=low - - * After cleaning, upload - - -- Jihoon Kim Fri, 22 Jan 2010 02:02:34 +0900 - -eet (1.2.0+svn20100119-1) unstable; urgency=low - - * EFL_update_revision_45322 - - -- Jihoon Kim Tue, 19 Jan 2010 20:44:46 +0900 - -eet (1.2.0+svn20100111-3) unstable; urgency=low - - * reupload EFL i686 - - -- Jaehwan Kim Tue, 12 Jan 2010 17:35:23 +0900 - -eet (1.2.0+svn20100111-2) unstable; urgency=low - - * reupload EFL - - -- Jaehwan Kim Mon, 11 Jan 2010 22:16:25 +0900 - -eet (1.2.0+svn20100111-1) unstable; urgency=low - - * update EFL revision 45026 - - -- Jaehwan Kim Mon, 11 Jan 2010 13:28:02 +0900 - -eet (1.2.0+svn20091229-1) unstable; urgency=low - - * update EFL - - -- Jaehwan Kim Tue, 29 Dec 2009 14:27:01 +0900 - -eet (1.2.0+svn20091112-4) unstable; urgency=low - - * modified control file : changed Architecture all to any (by Juyung Seo) - - -- sangho.g.park Fri, 27 Nov 2009 15:36:30 +0900 - -eet (1.2.0+svn20091112-3) unstable; urgency=low - - * svn stable version - - -- sangho.g.park Thu, 19 Nov 2009 17:32:45 +0900 - -eet (1.2.0+svn20091112-2) unstable; urgency=low - - * jpeg62 -> jpeg7 - - -- sangho.g.park Thu, 12 Nov 2009 16:45:00 +0100 - -eet (1.2.0+svnYYYYMMDD-1) unstable; urgency=low - - * Clean up changelog - - -- quaker Tue, 21 Apr 2009 19:13:08 +0100 diff --git a/debian/compat b/debian/compat deleted file mode 100644 index 7ed6ff8..0000000 --- a/debian/compat +++ /dev/null @@ -1 +0,0 @@ -5 diff --git a/debian/control b/debian/control deleted file mode 100644 index f52a88a..0000000 --- a/debian/control +++ /dev/null @@ -1,77 +0,0 @@ -Source: eet -Section: libs -Priority: optional -Maintainer: Jaehwan Kim , Juyung Seo , Jeonghyun Yun , Myoungwoon Kim , Mike McCormack , ChunEon Park -Build-Depends: dpkg-dev , debhelper (>= 6), cdbs, zlib1g-dev, libjpeg8-dev, pkg-config, libtool, libeina-dev, doxygen -Standards-Version: 3.8.1 -Homepage: http://www.enlightenment.org - -Package: libeet-dev -Section: libdevel -Architecture: any -Depends: libeet1 (= ${binary:Version}), libjpeg8-dev, pkg-config -Recommends: libeet-doc -Description: Enlightenment DR17 file chunk reading/writing library development files - Eet is a tiny library designed to write an arbitary set of chunks of data to a - file and optionally compress each chunk (very much like a zip file) and allow - fast random-access reading of the file later on. It does not do zip as zip - itself has more complexity than we need, and it was much simpler to implement - this once here. - . - This package contains headers and static libraries for development with libeet. - -Package: libeet-doc -Section: doc -Architecture: all -Enhances: libeet-dev -Description: libeet1 API documentation - Eet is a tiny library designed to write an arbitary set of chunks of data to a - file and optionally compress each chunk (very much like a zip file) and allow - fast random-access reading of the file later on. It does not do zip as zip - itself has more complexity than we need, and it was much simpler to implement - this once here. - . - This package contains documentation (html and manpages) for development with - libeet. - -Package: libeet1 -Architecture: any -Depends: ${shlibs:Depends} -Description: Enlightenment DR17 file chunk reading/writing library - Eet is a tiny library designed to write an arbitary set of chunks of data to a - file and optionally compress each chunk (very much like a zip file) and allow - fast random-access reading of the file later on. It does not do zip as zip - itself has more complexity than we need, and it was much simpler to implement - this once here. - . - It's small, fast, and does a job. It's heavily commented and fully documented. - -Package: libeet-bin -Architecture: any -Depends: ${shlibs:Depends} -Description: Enlightenment DR17 file chunk reading/writing utility - Eet is a tiny library designed to write an arbitary set of chunks of data to a - file and optionally compress each chunk (very much like a zip file) and allow - fast random-access reading of the file later on. It does not do zip as zip - itself has more complexity than we need, and it was much simpler to implement - this once here. - . - This package contains eet, an utility that allows you to extract, insert, - encode and decode config blobs created with libeet. - -Package: libeet-dbg -Architecture: any -Section: libdevel -Priority: extra -Depends: libeet1 (= ${binary:Version}) -Description: Enlightenment DR17 file chunk reading/writing library - debug symbols - Eet is a tiny library designed to write an arbitary set of chunks of data to a - file and optionally compress each chunk (very much like a zip file) and allow - fast random-access reading of the file later on. It does not do zip as zip - itself has more complexity than we need, and it was much simpler to implement - this once here - . - This package contains unstripped shared libraries. It is provided primarily - to provide a backtrace with names in a debugger, this makes it somewhat easier - to interpret core dumps. The libraries are installed in /usr/lib/debug and - are automatically used by gdb. diff --git a/debian/copyright b/debian/copyright deleted file mode 100644 index e97e5c3..0000000 --- a/debian/copyright +++ /dev/null @@ -1,36 +0,0 @@ -This package was debianized by Debian Pkg-e Team -Sat, 07 Jul 2007 09:29:10 +0000. - -It was downloaded from http://download.enlightenment.org/snapshots/LATEST/ - -Upstream Authors: - - Enlightenment team - -Copyright: - - Copyright (C) 2000 Carsten Haitzler and various contributors (see AUTHORS) - -License: - - Permission is hereby granted, free of charge, to any person obtaining a - copy of this software and associated documentation files (the "Software"), - to deal in the Software without restriction, including without limitation - the rights to use, copy, modify, merge, publish, distribute, sublicense, - and/or sell copies of the Software, and to permit persons to whom the - Software is furnished to do so, subject to the following conditions: - - The above copyright notice and this permission notice shall be included in - all copies of the Software, its documentation and marketing & publicity - materials, and acknowledgment shall be given in the documentation, - materials and software packages that this Software was used. - - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL - THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER - IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN - CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - -On Debian systems, the complete text of the BSD License can be found -in `/usr/share/common-licenses/BSD'. diff --git a/debian/eet.1 b/debian/eet.1 deleted file mode 100644 index 2abe11c..0000000 --- a/debian/eet.1 +++ /dev/null @@ -1,50 +0,0 @@ -.TH EET 1 "November 4, 2007" eet -.SH NAME -eet \- Small tool to handle eet files. -.SH SYNOPSIS -.B eet -.RI -l " FILE.EET" -.br -.B eet -.RI -r " FILE.EET KEY" -.br -.B eet -.RI "-x|-d" " FILE.EET KEY OUT-FILE" -.br -.B eet -.RI "-i|-e" " FILE.EET KEY IN-FILE COMPRESS" -.br -.SH DESCRIPTION -This manual page documents briefly the -.B eet -command -.PP -\fBeet\fP is a program that can extract, import, delete, list, decode and encode data from eet files. -.SH OPTIONS -.TP -.B \-l -List all keys in FILE.EET. -.TP -.B \-r -Remove KEY in FILE.EET. -.TP -.B \-x -Extract data stored in KEY in FILE.EET and write to OUT-FILE -.TP -.B \-d -Extract and decode data stored in KEY in FILE.EET and write to OUT-FILE -.TP -.B \-i -Insert data to KEY in FILE.EET from IN-FILE and if COMPRESS is 1, compress it -.TP -.B \-e -Insert and encode to KEY in FILE.EET from IN-FILE and if COMPRESS is 1, compress it -.SH BUGS -To report a bug, please visit \fIhttp://bugs.enlightenment.org/\fR -.SH AUTHOR -.TP -eet was written by the Enlightenment Development Team -.I http://web.enlightenment.org -.PP -This manual page was written by Albin Tonnerre , -for the Debian project (but may be used by others). diff --git a/debian/jobs b/debian/jobs deleted file mode 100644 index e69de29..0000000 diff --git a/debian/libeet-bin.install b/debian/libeet-bin.install deleted file mode 100644 index 3399d7b..0000000 --- a/debian/libeet-bin.install +++ /dev/null @@ -1 +0,0 @@ -debian/tmp/usr/bin/eet diff --git a/debian/libeet-dev.install b/debian/libeet-dev.install deleted file mode 100644 index 73de9a8..0000000 --- a/debian/libeet-dev.install +++ /dev/null @@ -1,5 +0,0 @@ -debian/tmp/usr/lib/pkgconfig/* -debian/tmp/usr/include/* -debian/tmp/usr/lib/lib*.a -debian/tmp/usr/lib/lib*.la -debian/tmp/usr/lib/lib*.so diff --git a/debian/libeet-doc.dirs b/debian/libeet-doc.dirs deleted file mode 100644 index a110659..0000000 --- a/debian/libeet-doc.dirs +++ /dev/null @@ -1 +0,0 @@ -usr/share/doc/libeet-doc diff --git a/debian/libeet-doc.doc-base b/debian/libeet-doc.doc-base deleted file mode 100644 index 2d1ef1f..0000000 --- a/debian/libeet-doc.doc-base +++ /dev/null @@ -1,10 +0,0 @@ -Document: eet -Title: Eet Guide -Author: Carsten Haitzler -Abstract: This document describes Eet API - and provides sample C code. -Section: Programming/C - -Format: HTML -Index: /usr/share/doc/libeet-doc/html/index.html -Files: /usr/share/doc/libeet-doc/html/*.html diff --git a/debian/libeet1.install b/debian/libeet1.install deleted file mode 100644 index c45ebcf..0000000 --- a/debian/libeet1.install +++ /dev/null @@ -1 +0,0 @@ -debian/tmp/usr/lib/lib*.so.* diff --git a/debian/libeet1.symbols b/debian/libeet1.symbols deleted file mode 100644 index 9a96c7c..0000000 --- a/debian/libeet1.symbols +++ /dev/null @@ -1,85 +0,0 @@ -libeet.so.1 libeet1 #MINVER# - eet_clearcache@Base 1.0.0 - eet_close@Base 1.0.0 - eet_data_descriptor2_new@Base 1.0.0 - eet_data_descriptor3_new@Base 1.0.0 - eet_data_descriptor_decode@Base 1.0.0 - eet_data_descriptor_decode_cipher@Base 1.2.2 - eet_data_descriptor_element_add@Base 1.0.0 - eet_data_descriptor_encode@Base 1.0.0 - eet_data_descriptor_encode_cipher@Base 1.2.2 - eet_data_descriptor_free@Base 1.0.0 - eet_data_descriptor_new@Base 1.0.0 - eet_data_dump@Base 1.0.0 - eet_data_dump_cipher@Base 1.2.2 - eet_data_image_decode@Base 1.0.0 - eet_data_image_decode_cipher@Base 1.2.2 - eet_data_image_decode_to_surface@Base 1.1.0 - eet_data_image_decode_to_surface_cipher@Base 1.2.2 - eet_data_image_encode@Base 1.0.0 - eet_data_image_encode_cipher@Base 1.2.2 - eet_data_image_header_decode@Base 1.0.0 - eet_data_image_header_decode_cipher@Base 1.2.2 - eet_data_image_header_read@Base 1.0.0 - eet_data_image_header_read_cipher@Base 1.2.2 - eet_data_image_read@Base 1.0.0 - eet_data_image_read_cipher@Base 1.2.2 - eet_data_image_read_to_surface@Base 1.1.0 - eet_data_image_read_to_surface_cipher@Base 1.2.2 - eet_data_image_write@Base 1.0.0 - eet_data_image_write_cipher@Base 1.2.2 - eet_data_node_encode_cipher@Base 1.2.2 - eet_data_node_write_cipher@Base 1.2.2 - eet_data_read@Base 1.0.0 - eet_data_read_cipher@Base 1.2.2 - eet_data_text_dump@Base 1.0.0 - eet_data_text_dump_cipher@Base 1.2.2 - eet_data_text_undump@Base 1.0.0 - eet_data_text_undump_cipher@Base 1.2.2 - eet_data_undump@Base 1.0.0 - eet_data_undump_cipher@Base 1.2.2 - eet_data_write@Base 1.0.0 - eet_data_write_cipher@Base 1.2.2 - eet_delete@Base 1.0.0 - eet_dictionary_get@Base 1.0.0 - eet_dictionary_string_check@Base 1.0.0 - eet_identity_certificate_print@Base 1.1.0 - eet_identity_close@Base 1.1.0 - eet_identity_open@Base 1.1.0 - eet_identity_print@Base 1.1.0 - eet_identity_set@Base 1.1.0 - eet_identity_sha1@Base 1.2.2 - eet_identity_signature@Base 1.2.2 - eet_identity_x509@Base 1.1.0 - eet_init@Base 1.0.0 - eet_list@Base 1.0.0 - eet_memopen_read@Base 1.1.0 - eet_mode_get@Base 1.0.0 - eet_node_array_new@Base 1.2.2 - eet_node_char_new@Base 1.2.2 - eet_node_del@Base 1.2.2 - eet_node_double_new@Base 1.2.2 - eet_node_float_new@Base 1.2.2 - eet_node_hash_new@Base 1.2.2 - eet_node_inlined_string_new@Base 1.2.2 - eet_node_int_new@Base 1.2.2 - eet_node_list_new@Base 1.2.2 - eet_node_long_long_new@Base 1.2.2 - eet_node_null_new@Base 1.2.2 - eet_node_short_new@Base 1.2.2 - eet_node_string_new@Base 1.2.2 - eet_node_struct_child_new@Base 1.2.2 - eet_node_struct_new@Base 1.2.2 - eet_node_unsigned_char_new@Base 1.2.2 - eet_node_unsigned_int_new@Base 1.2.2 - eet_node_unsigned_long_long_new@Base 1.2.2 - eet_node_unsigned_short_new@Base 1.2.2 - eet_node_var_array_new@Base 1.2.2 - eet_num_entries@Base 1.0.0 - eet_open@Base 1.0.0 - eet_read@Base 1.0.0 - eet_read_cipher@Base 1.2.2 - eet_read_direct@Base 1.0.0 - eet_shutdown@Base 1.0.0 - eet_write@Base 1.0.0 - eet_write_cipher@Base 1.2.2 diff --git a/debian/rules b/debian/rules deleted file mode 100755 index 50da389..0000000 --- a/debian/rules +++ /dev/null @@ -1,24 +0,0 @@ -#!/usr/bin/make -f - -include /usr/share/cdbs/1/rules/debhelper.mk -include /usr/share/cdbs/1/class/autotools.mk - -CFLAGS += -fvisibility=hidden -fPIC -LDFLAGS += -fvisibility=hidden -Wl,--hash-style=both -Wl,--as-needed - - -#DEB_INSTALL_MANPAGES_libeet-doc := $(DEB_SRCDIR)/doc/man/man3/E*.3 -#DEB_INSTALL_MANPAGES_libeet-bin := $(DEB_SRCDIR)/debian/eet.1 -DEB_DH_STRIP_ARGS := --dbg-package=libeet-dbg -DEB_CONFIGURE_EXTRA_FLAGS := --disable-openssl --disable-cypher --disable-signature --disable-gnutls --disable-doc -DEB_CONFIGURE_SCRIPT := ./autogen.sh -DEB_MAKE_CLEAN_TARGET := distclean - -#build/libeet-doc:: -# make doc - -#install/libeet-doc:: -# cp -R $(DEB_SRCDIR)/doc/html debian/libeet-doc/usr/share/doc/libeet-doc/ - -clean:: - [ ! -f Makefile ] || make distclean diff --git a/doc/examples.dox b/doc/eet_examples.dox similarity index 95% rename from doc/examples.dox rename to doc/eet_examples.dox index b842584..2e89bb9 100644 --- a/doc/examples.dox +++ b/doc/eet_examples.dox @@ -1,5 +1,6 @@ /** - * @page Examples Examples + * @internal + * @page eet_examples EET Examples * * Here is a page with examples. * @@ -12,18 +13,10 @@ * @ref Example_Eet_Data_File_Descriptor_02 * * @ref Example_Eet_Data_Cipher_Decipher - * - * List of examples - */ - -/** - * @page Example_Eet_Basic Very basic Eet example - * - * @includelineno eet-basic.c - * @example eet-basic.c */ /** + * @internal * @page Example_Eet_File Example of the various ways to interface with an Eet File * * @includelineno eet-file.c @@ -31,6 +24,7 @@ */ /** + * @internal * @page Example_Eet_Data_Simple Simple data example * * @includelineno eet-data-simple.c @@ -38,6 +32,7 @@ */ /** + * @internal * @page Example_Eet_Data_Nested Nested data example * * @includelineno eet-data-nested.c @@ -45,13 +40,14 @@ */ /** + * @internal * @page Example_Eet_Data_File_Descriptor_01 File descriptor data example * * @includelineno eet-data-file_descriptor_01.c * @example eet-data-file_descriptor_01.c */ - /** + * @internal * @page Example_Eet_Data_File_Descriptor_02 File descriptor data example, with Eet unions and variants * * This is an example much like the one shown in @ref @@ -142,10 +138,10 @@ * check for him/herself. The complete code of the example follows. * * @includelineno eet-data-file_descriptor_02.c - * @example eet-data-file_descriptor_02.c */ /** + * @internal * @page Example_Eet_Data_Cipher_Decipher Eet data cipher/decipher example * * In this example, we exemplify the usage of eet_write_cipher() and @@ -180,5 +176,4 @@ * code of the example follows. * * @includelineno eet-data-cipher_decipher.c - * @example eet-data-cipher_decipher.c */ diff --git a/eet-tools.manifest b/eet-tools.manifest new file mode 100644 index 0000000..3b6661c --- /dev/null +++ b/eet-tools.manifest @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/eet.manifest b/eet.manifest new file mode 100644 index 0000000..97e8c31 --- /dev/null +++ b/eet.manifest @@ -0,0 +1,5 @@ + + + + + diff --git a/packaging/eet.spec b/packaging/eet.spec index d6bffb6..d5c15a5 100644 --- a/packaging/eet.spec +++ b/packaging/eet.spec @@ -1,15 +1,14 @@ Name: eet Summary: Library for speedy data storage, retrieval, and compression -Version: 1.6.0+svn.74530slp2+build01 +Version: 1.7.1+svn.77495+build14 Release: 1 Group: System/Libraries -License: BSD +License: BSD 2-Clause URL: http://www.enlightenment.org/ Source0: %{name}-%{version}.tar.gz Requires(post): /sbin/ldconfig Requires(postun): /sbin/ldconfig BuildRequires: eina-devel -BuildRequires: gnutls-devel BuildRequires: zlib-devel BuildRequires: libjpeg-devel @@ -51,19 +50,22 @@ Enlightenment DR17 file chunk reading/writing library (tools) %build -export CFLAGS+=" -fvisibility=hidden -fPIC" +export CFLAGS+=" -fvisibility=hidden -fPIC -Wall" export LDFLAGS+=" -fvisibility=hidden -Wl,--hash-style=both -Wl,--as-needed" -%autogen --disable-static -%configure --disable-static \ - --disable-openssl --disable-cypher --disable-signature --disable-gnutls +%autogen --disable-static \ + --disable-openssl \ + --disable-signature \ + --disable-gnutls make %{?jobs:-j%jobs} %install -rm -rf %{buildroot} %make_install +mkdir -p %{buildroot}/%{_datadir}/license +cp %{_builddir}/%{buildsubdir}/COPYING %{buildroot}/%{_datadir}/license/%{name} +cp %{_builddir}/%{buildsubdir}/COPYING %{buildroot}/%{_datadir}/license/%{name}-tools %post -p /sbin/ldconfig @@ -75,6 +77,8 @@ rm -rf %{buildroot} %files %defattr(-,root,root,-) %{_libdir}/libeet.so.* +%{_datadir}/license/%{name} +%manifest %{name}.manifest %files devel @@ -82,11 +86,6 @@ rm -rf %{buildroot} %{_includedir}/* %{_libdir}/*.so %{_libdir}/pkgconfig/eet.pc - - -%files tools -%defattr(-,root,root,-) -%{_bindir}/* %{_datadir}/eet/examples/eet-basic.c %{_datadir}/eet/examples/eet-data-cipher_decipher.c %{_datadir}/eet/examples/eet-data-file_descriptor_01.c @@ -95,3 +94,9 @@ rm -rf %{buildroot} %{_datadir}/eet/examples/eet-data-simple.c %{_datadir}/eet/examples/eet-file.c + +%files tools +%defattr(-,root,root,-) +%{_bindir}/* +%{_datadir}/license/%{name}-tools +%manifest %{name}-tools.manifest diff --git a/src/lib/Eet.h b/src/lib/Eet.h index 17af8dc..ade1023 100644 --- a/src/lib/Eet.h +++ b/src/lib/Eet.h @@ -1,30 +1,28 @@ /** - @brief Eet Data Handling Library Public API Calls + * @internal + @defgroup Eet_Group Eet + @ingroup EFL_Group - These routines are used for Eet Library interaction + @brief Eet Data Handling Library Public API Calls - @mainpage Eet Library Documentation + These routines are used for Eet Library interaction. - @version 1.6.0 - @date 2000-2012 + @page eet_main Eet - Please see the @ref authors page for contact details. + @date 2000 (created) @section toc Table of Contents - @li @ref intro - @li @ref example - @li @ref compiling - @li @ref install - @li @ref next_steps - @li @ref intro_example + @li @ref eet_main_intro + @li @ref eet_main_next_steps + @li @ref eet_main_intro_example - @section intro What is Eet? + @section eet_main_intro Introduction It is a tiny library designed to write an arbitrary set of chunks of data to a file and optionally compress each chunk (very much like a zip file) and allow fast random-access reading of the file later on. It does not - do zip as a zip itself has more complexity than is needed, and it was much + do zip, as a zip itself has more complexity than is needed, and it is much simpler to implement this once here. Eet is extremely fast, small and simple. Eet files can be very small and @@ -40,7 +38,21 @@ encoded in a platform independent way and can be written and read by any architecture. - @section example A simple example on using Eet + @section eet_main_next_steps Next Steps + + After you understood what Eet is and installed it in your system you + should understand the programming interface. We would recommend + you to take a while to learn @ref Eina_Group as it is very convenient and + optimized, and Eet provides integration with it. + + Recommended reading: + + @li @ref Eet_File_Group to know the basics to open and save files. + @li @ref Eet_Data_Group to know the convenient way to serialize and + parse your data structures automatically. Just create your + descriptors and let Eet do the work for you. + + @section eet_main_intro_example Introductory Examples Here is a simple example on how to use Eet to save a series of strings to a file and load them again. The advantage of using Eet over just @@ -56,92 +68,12 @@ @include eet-basic.c - @section compiling How to compile using Eet ? - - Eet is a library your application links to. The procedure for this is very - simple. You simply have to compile your application with the appropriate - compiler flags that the @p pkg-config script outputs. For example: - - Compiling C or C++ files into object files: - - @verbatim - gcc -c -o main.o main.c `pkg-config --cflags eet` - @endverbatim - - Linking object files into a binary executable: - - @verbatim - gcc -o my_application main.o `pkg-config --libs eet` - @endverbatim - - You simply have to make sure that pkg-config is in your shell's PATH (see - the manual page for your appropriate shell) and eet.pc in /usr/lib/pkgconfig - or its path is in the PKG_CONFIG_PATH environment variable. It's that simple - to link and use Eet once you have written your code to use it. - - Since the program is linked to Eet, it is now able to use any advertised - API calls to serialize your data. - - You should make sure you add any extra compile and link flags to your - compile commands that your application may need as well. The above example - is only guaranteed to make Eet add it's own requirements. - - - @section install How is it installed? - - Simple: - - @verbatim - ./configure - make - su - - ... - make install - @endverbatim - - @section next_steps Next Steps - - After you understood what Eet is and installed it in your system you - should proceed understanding the programming interface. We'd recommend - you to take a while to learn Eina - (http://docs.enlightenment.org/auto/eina/) as it is very convenient - and optimized, and Eet provides integration with it. - - Recommended reading: - - @li @ref Eet_File_Group to know the basics to open and save files. - @li @ref Eet_Data_Group to know the convenient way to serialize and - parse your data structures automatically. Just create your - descriptors and let Eet do the work for you. - - @section intro_example Introductory Examples - - @ref Examples + More examples can be found at @ref eet_examples. @todo Document data format for images and data structures. */ -/** - @page authors Authors - @author Carsten Haitzler - @author David Goodlad - @author Cedric Bail - @author Arnaud de Turckheim - @author Luis Felipe Strano Moraes - @author Chidambar Zinnoury - @author Vincent Torri - @author Gustavo Sverzut Barbieri - @author Raphael Kubo da Costa - @author Mathieu Taillefumier - @author Albin "Lutin" Tonnerre - @author Adam Simpkins - @author Mike Blumenkrantz - - Please contact to get in - contact with the developers and maintainers. - */ - #ifndef _EET_H #define _EET_H @@ -180,15 +112,26 @@ extern "C" { #endif /* ifdef __cplusplus */ /** + * @internal * @file Eet.h * @brief The file that provides the eet functions. * - * This header provides the Eet management functions. + * @details This header provides the Eet management functions. * */ +/** + * @internal + * @defgroup Eet_General_Group Top level functions + * @ingroup Eet_Group + * + * @brief This group provides functions that affect Eet as a whole. + * + * @{ + */ + #define EET_VERSION_MAJOR 1 -#define EET_VERSION_MINOR 6 +#define EET_VERSION_MINOR 8 /** * @typedef Eet_Version * @@ -214,37 +157,31 @@ extern "C" { */ typedef struct _Eet_Version { - int major; /** < major (binary or source incompatible changes) */ - int minor; /** < minor (new features, bugfixes, major improvements version) */ - int micro; /** < micro (bugfix, internal improvements, no new features version) */ - int revision; /** < svn revision (0 if a proper release or the svn revision number Eet is built from) */ + int major; /**< Major (binary or source incompatible changes) */ + int minor; /**< Minor (new features, bugfixes, major improvements version) */ + int micro; /**< Micro (bugfix, internal improvements, no new features version) */ + int revision; /**< svn revision (@c 0 if a proper release or the svn revision number Eet is built from) */ } Eet_Version; EAPI extern Eet_Version *eet_version; /** - * @defgroup Eet_Group Top level functions - * Functions that affect Eet as a whole. - * - * @{ - */ - -/** + * @internal * @enum _Eet_Error - * All the error identifiers known by Eet. + * @brief Enumeration of all the error identifiers known by Eet. */ typedef enum _Eet_Error { - EET_ERROR_NONE, /**< No error, it's all fine! */ - EET_ERROR_BAD_OBJECT, /**< Given object or handle is NULL or invalid */ - EET_ERROR_EMPTY, /**< There was nothing to do */ + EET_ERROR_NONE, /**< No error, it is all fine! */ + EET_ERROR_BAD_OBJECT, /**< Given object or handle is @c NULL or invalid */ + EET_ERROR_EMPTY, /**< There is nothing to do */ EET_ERROR_NOT_WRITABLE, /**< Could not write to file or file is #EET_FILE_MODE_READ */ EET_ERROR_OUT_OF_MEMORY, /**< Could not allocate memory */ EET_ERROR_WRITE_ERROR, /**< Failed to write data to destination */ EET_ERROR_WRITE_ERROR_FILE_TOO_BIG, /**< Failed to write file since it is too big */ EET_ERROR_WRITE_ERROR_IO_ERROR, /**< Failed to write due a generic Input/Output error */ EET_ERROR_WRITE_ERROR_OUT_OF_SPACE, /**< Failed to write due out of space */ - EET_ERROR_WRITE_ERROR_FILE_CLOSED, /**< Failed to write because file was closed */ + EET_ERROR_WRITE_ERROR_FILE_CLOSED, /**< Failed to write because file is closed */ EET_ERROR_MMAP_FAILED, /**< Could not mmap file */ EET_ERROR_X509_ENCODING_FAILED, /**< Could not encode using X509 */ EET_ERROR_SIGNATURE_FAILED, /**< Could not validate signature */ @@ -259,9 +196,12 @@ typedef enum _Eet_Error /** * @} */ - + /** - * @defgroup Eet_Compression Eet Compression Levels + * @internal + * @defgroup Eet_Compression_Group Eet Compression Levels + * @ingroup Eet_General_Group + * * Compression modes/levels supported by Eet. * * @{ @@ -269,7 +209,7 @@ typedef enum _Eet_Error /** * @enum _Eet_Compression - * All the compression modes known by Eet. + * @brief Enumeration for the compression modes known by Eet. */ typedef enum _Eet_Compression @@ -281,221 +221,222 @@ typedef enum _Eet_Compression EET_COMPRESSION_HI = 9, /**< Slow but high compression level (Zlib) @since 1.7 */ EET_COMPRESSION_VERYFAST = 10, /**< Very fast, but lower compression ratio (LZ4HC) @since 1.7 */ EET_COMPRESSION_SUPERFAST = 11, /**< Very fast, but lower compression ratio (faster to compress than EET_COMPRESSION_VERYFAST) (LZ4) @since 1.7 */ - - EET_COMPRESSION_LOW2 = 3, /**< Space filler for compatibility. Don't use it @since 1.7 */ - EET_COMPRESSION_MED1 = 4, /**< Space filler for compatibility. Don't use it @since 1.7 */ - EET_COMPRESSION_MED2 = 5, /**< Space filler for compatibility. Don't use it @since 1.7 */ - EET_COMPRESSION_HI1 = 7, /**< Space filler for compatibility. Don't use it @since 1.7 */ - EET_COMPRESSION_HI2 = 8 /**< Space filler for compatibility. Don't use it @since 1.7 */ + + EET_COMPRESSION_LOW2 = 3, /**< Space filler for compatibility. Do not use it @since 1.7 */ + EET_COMPRESSION_MED1 = 4, /**< Space filler for compatibility. Do not use it @since 1.7 */ + EET_COMPRESSION_MED2 = 5, /**< Space filler for compatibility. Do not use it @since 1.7 */ + EET_COMPRESSION_HI1 = 7, /**< Space filler for compatibility. Do not use it @since 1.7 */ + EET_COMPRESSION_HI2 = 8 /**< Space filler for compatibility. Do not use it @since 1.7 */ } Eet_Compression; /**< Eet compression modes @since 1.7 */ - + /** * @} */ /** - * Initialize the EET library. + * @brief Initializes the EET library. + * @since 1.0.0 * - * The first time this function is called, it will perform all the internal - * initialization required for the library to function properly and increment - * the initialization counter. Any subsequent call only increment this counter - * and return its new value, so it's safe to call this function more than once. + * @remarks The first time this function is called, it performs all the internal + * initialization required for the library to function properly and increment + * the initialization counter. Any subsequent call only increment this counter + * and return its new value. So it is safe to call this function more than once. * - * @return The new init count. Will be 0 if initialization failed. + * @return The new init count, \n + * otherwise @c 0 if initialization failed * - * @since 1.0.0 - * @ingroup Eet_Group + * @ingroup Eet_General_Group */ EAPI int eet_init(void); /** - * Shut down the EET library. - * - * If eet_init() was called more than once for the running application, - * eet_shutdown() will decrement the initialization counter and return its - * new value, without doing anything else. When the counter reaches 0, all - * of the internal elements will be shutdown and any memory used freed. + * @brief Shuts down the EET library. + * @since 1.0.0 * - * @return The new init count. + * @remarks If eet_init() is called more than once for the running application, + * eet_shutdown() decrements the initialization counter and return its + * new value, without doing anything else. When the counter reaches @c 0, all + * of the internal elements are shutdown and any memory used is freed. * - * @since 1.0.0 + * @return The new init count * @ingroup Eet_Group */ EAPI int eet_shutdown(void); /** - * Clear eet cache + * @brief Clears eet cache. + * @since 1.0.0 * - * For a faster access to previously accessed data, Eet keeps an internal - * cache of files. These files will be freed automatically only when - * they are unused and the cache gets full, in order based on the last time - * they were used. - * On systems with little memory this may present an unnecessary constraint, - * so eet_clearcache() is available for users to reclaim the memory used by - * files that are no longer needed. Those that were open using - * ::EET_FILE_MODE_WRITE or ::EET_FILE_MODE_READ_WRITE and have modifications, - * will be written down to disk before flushing them from memory. + * @remarks For a faster access to previously accessed data, Eet keeps an internal + * cache of files. These files are freed automatically only when + * they are unused and the cache gets full, in order based on the last time + * they were used. On systems with little memory this may present an unnecessary + * constraint. So eet_clearcache() is available for users to reclaim the memory + * used by files that are no longer needed. Those that were open using + * ::EET_FILE_MODE_WRITE or ::EET_FILE_MODE_READ_WRITE and have modifications, + * are written down to disk before flushing them from memory. * - * @since 1.0.0 - * @ingroup Eet_Group + * @ingroup Eet_General_Group */ EAPI void eet_clearcache(void); /** + * @internal * @defgroup Eet_File_Group Eet File Main Functions + * @ingroup Eet_Group * - * Functions to create, destroy and do basic manipulation of - * #Eet_File handles. + * @brief This group provides functions to create, destroy, and do basic manipulation of + * #Eet_File handles. * * This sections explains how to use the most basic Eet functions, which - * are used to work with eet files, read data from them, store it back in or + * are used to work with eet files, read data from them, store it back in, or * take a look at what entries it contains, without making use of the * serialization capabilities explained in @ref Eet_Data_Group. * - * The following example will serve as an introduction to most, if not all, + * The following example serves as an introduction to most, if not all, * of these functions. * * If you are only using Eet, this is the only header you need to include. * @dontinclude eet-file.c * @skipline Eet.h * - * Now let's create ourselves an eet file to play with. The following function + * Now create an eet file to play with. The following function * shows step by step how to open a file and write some data in it. - * First, we define our file handler and some other things we'll put in it. + * First, define your file handler and some other things that you put in it. * @line static int * @skip Eet_File * @until "; * @skip eet_open * - * We open a new file in write mode, and if it fails, we just return, since - * there's not much more we can do about it.. + * Open a new file in write mode, and if it fails, just return, since + * there is not much more you can do about it. * @until return * - * Now, we need to write some data in our file. For now, strings will suffice, - * so let's just dump a bunch of them in there. + * Now, write some data in your file. For now, strings suffice. + * So just dump a bunch of them in there. * @until } * - * As you can see, we copied a string into our static buffer, which is a bit + * As you can see, you copied a string into your static buffer, which is a bit * bigger than the full length of the string, and then told Eet to write it * into the file, compressed, returning the size of the data written into the * file. - * This is all to show that Eet treats data as just data. It doesn't matter - * what that data represents (for now), it's all just bytes for it. As running - * the following code will show, we took a string of around 30 bytes and put it - * in a buffer of 1024 bytes, but the returned size won't be any of those. + * This is all to show that Eet treats data as just data. It does not matter + * what that data represents (for now). It is all just bytes for it. As running + * the following code shows, you took a string of around 30 bytes and put it + * in a buffer of 1024 bytes, but the returned size is not any of those. * @until printf * - * Next, we copy into our buffer our set of strings, including their null + * Next, copy into your buffer your set of strings, including their null * terminators and write them into the file. No error checking for the sake * of brevity. And a call to eet_sync() to make sure all out data is - * properly written down to disk, even though we haven't yet closed the file. + * properly written down to disk, even though you have not yet closed the file. * @until eet_sync * - * One more write, this time our large array of binary data and... well, I - * couldn't come up with a valid use of the last set of strings we stored, - * so let's take it out from the file with eet_delete(). + * One more write, this time your large array of binary data and... well, + * a valid use of the last set of strings you stored is not identified here, + * so take it out from the file with eet_delete(). * @until eet_delete * - * Finally, we close the file, saving any changes back to disk and return. - * Notice how, if there's any error closing the file or saving its contents, - * the return value from the function will be a false one, which later on - * will make the program exit with an error code. + * Finally, close the file, saving any changes back to disk and return. + * Notice how, if there is any error closing the file or saving its contents, + * the return value from the function is a false one, which later on + * makes the program exit with an error code. * @until return * - * Moving onto our main function, we will open the same file and read it back. - * Trivial, but it'll show how we can do so in more than one way. We'll skip - * the variable declarations, as they aren't very different from what we've + * Moving onto your main function, you open the same file and read it back. + * Trivial, but it shows how you can do so in more than one way. You skip + * the variable declarations, as they are not very different from what you have * seen already. * - * We start from the beginning by initializing Eet so things in general work. - * Forgetting to do so will result in weird results or crashes when calling + * You start from the beginning by initializing Eet so things in general work. + * Forgetting to do so results in weird results or crashes when calling * any eet function, so if you experience something like that, the first thing * to look at is whether eet_init() is missing. - * Then we call our @p create_eet_file function, described above, to make - * sure we have something to work with. If the function fails it will return - * 0 and we just exit, since nothing from here onwards will work anyway. + * Then call your @a create_eet_file function, described above, to make + * sure you have something to work with. If the function fails it returns + * @c 0 and just exits, since nothing from here onwards works anyway. * @skip eet_init * @until return * - * Let's take a look now at what entries our file has. For this, we use - * eet_list(), which will return a list of strings, each being the name of - * one entry. Since we skipped before, it may be worth noting that @p list - * is declared as a @p char **. - * The @p num parameter will, of course, have the number of entries contained + * Take a look now at what entries our file has. For this, use + * eet_list(), which returns a list of strings, each being the name of + * one entry. Since this is skipped before, it may be worth noting that @a list + * is declared as a @a char **. + * The @a num parameter, of course, has the number of entries contained * in our file. - * If everything's fine, we'll get our list and print it to the screen, and - * once done with it, we free the list. That's just the list, not its contents, - * as they are internal strings used by Eet and trying to free them will surely - * break things. + * If everything is fine, you get your list and print it to the screen, and + * once done with it, free the list. That is just the list, not its contents, + * as they are internal strings used by Eet and trying to free them surely + * breaks things. * @until } * * Reading back plain data is simple. Just a call to eet_read() with the file - * to read from, and the name of the entry we are interested in. We get back - * our data and the passed @p size parameter will contain the size of it. If - * the data was stored compressed, it will decompressed first. + * to read from, and the name of the entry you are interested in. You get back + * your data and the passed @a size parameter contains the size of it. If + * the data is stored compressed, it decompresses first. * @until } * * Another simple read for the set of strings from before, except those were - * deleted, so we should get a NULL return and continue normally. + * deleted, so you should get a @c NULL return and continue normally. * @until } * - * Finally, we'll get our binary data in the same way we got the strings. Once - * again, it makes no difference for Eet what the data is, it's up to us to + * Finally, you get your binary data in the same way you got the strings. Once + * again, it makes no difference for Eet what the data is, it is up to you to * know how to handle it. * @until { * - * Now some cheating, we know that this data is an Eet file because, well... - * we just know it. So we are going to open it and take a look at its insides. - * For this, eet_open() won't work, as it needs to have a file on disk to read - * from and all we have is some data in RAM. + * Now some cheating, you know that this data is an Eet file because, well... + * you just know it. So you are going to open it and take a look at its insides. + * For this, eet_open() does not work, as it needs to have a file on disk to read + * from and all you have is some data in RAM. * - * So how do we do? One way would be to create a normal file and write down - * our data, then open it with eet_open(). Another, faster and more efficient - * if all we want to do is read the file, is to use eet_memopen_read(). + * So how to do this? One way would be to create a normal file and write down + * your data, then open it with eet_open(). Another, faster and more efficient way, + * if all you want to do is read the file, is to use eet_memopen_read(). * @until memopen * - * As you can see, the size we got from our previous read was put to good use - * this time. Unlike the first one where all we had were strings, the size - * of the data read only serves to demonstrate that we are reading back the - * entire size of our original @p buf variable. + * As you can see, the size you got from your previous read is put to good use + * this time. Unlike the first one where all you had were strings, the size + * of the data read only serves to demonstrate that you are reading back the + * entire size of your original @a buf variable. * * A little peeking to see how many entries the file has and to make an - * example of eet_num_entries() to get that number when we don't care about + * example of eet_num_entries() to get that number when you do not care about * their names. * @until printf * - * More cheating follows. Just like we knew this was an Eet file, we also know - * what key to read from, and ontop of that we know that the data in it is not + * More cheating follows. Just like you knew this is an Eet file, you also know + * what key to read from, and on top of that you know that the data in it is not * compressed. - * Knowing all this allows us to take some shortcuts. + * Knowing all this allows you to take some shortcuts. * @until read_direct * - * That's a direct print of our data, whatever that data is. We don't want - * to worry about having to free it later, so we just used eet_direct_read() + * That is a direct print of your data, whatever that data is. We do not want + * to worry about having to free it later, so just used eet_direct_read() * to tell Eet to gives a pointer to the internal data in the file, without - * duplicating it. Since we said that data was not compressed, we shouldn't - * worry about printing garbage to the screen (and yes, we also know the data + * duplicating it. Since you said that data is not compressed, you should not + * worry about printing garbage to the screen (and yes, you also know the data * is yet another string). - * We also don't care about the size of the data as it was stored in the file, - * so we passed NULL as the size parameter. - * One very important note about this, however, is that we don't care about + * You also do not care about the size of the data as it is stored in the file, + * so you passed @c NULL as the size parameter. + * One very important note about this, however, is that you do not care about * the size parameter because the data in the file contains the null * terminator for the string. So when using Eet to store strings this way, - * it's very important to consider whether you will keep that final null + * it is very important to consider whether you keep that final null * byte, or to always get the size read and do the necessary checks and copies. - * It's up to the user and the particular use cases to decide how this will - * be done. + * It is up to the user and the particular use cases to decide how this is + * done. * * With everything done, close this second file and free the data used to open - * it. And this is important, we can't free that data until we are done with + * it. And this is important, you cannot free that data until you are done with * the file, as Eet is using it. When opening with eet_memopen_read(), the data * passed to it must be available for as long as the the file is open. * @until } * - * Finally, we close the first file, shutdown all internal resources used by + * Finally, close the first file, shutdown all internal resources used by * Eet and leave our main function, thus terminating our program. * @until return * @@ -505,24 +446,49 @@ eet_clearcache(void); /** * @enum _Eet_File_Mode - * Modes that a file can be opened. + * @brief Enumeration for modes in which a file can be opened. */ typedef enum _Eet_File_Mode { EET_FILE_MODE_INVALID = -1, - EET_FILE_MODE_READ, /**< File is read-only. */ - EET_FILE_MODE_WRITE, /**< File is write-only. */ + EET_FILE_MODE_READ, /**< File is read-only */ + EET_FILE_MODE_WRITE, /**< File is write-only */ EET_FILE_MODE_READ_WRITE /**< File is for both read and write */ -} Eet_File_Mode; /**< Modes that a file can be opened. */ +} Eet_File_Mode; /**< Modes that a file can be opened */ + +/** + * @enum _Eet_Image_Encoding + * @brief Enumeration for lossy encoding for image. + * @remarks Backported from EFL 1.10 and EFL 1.11 + */ +typedef enum _Eet_Image_Encoding +{ + EET_IMAGE_LOSSLESS = 0, + EET_IMAGE_JPEG = 1, + EET_IMAGE_ETC1 = 2, + EET_IMAGE_ETC2_RGB = 3, + EET_IMAGE_ETC2_RGBA = 4, + EET_IMAGE_ETC1_ALPHA = 5, +} Eet_Image_Encoding; + +typedef enum _Eet_Colorspace +{ + EET_COLORSPACE_ARGB8888 = 0, + /* The number between are reserved to preserve compatibility with evas */ + EET_COLORSPACE_ETC1 = 9, + EET_COLORSPACE_RGB8_ETC2 = 10, + EET_COLORSPACE_RGBA8_ETC2_EAC = 11, + EET_COLORSPACE_ETC1_ALPHA = 12 +} Eet_Colorspace; /** * @typedef Eet_File - * Opaque handle that defines an Eet file (or memory). + * @brief The structure type containing an opaque handle that defines an Eet file (or memory). * - * This handle will be returned by the functions eet_open() and + * This handle is returned by the functions eet_open() and * eet_memopen_read() and is used by every other function that affects the * file in any way. When you are done with it, call eet_close() to close it - * and, if the file was open for writing, write down to disk any changes made + * and, if the file is open for writing, write down to disk any changes made * to it. * * @see eet_open() @@ -533,101 +499,97 @@ typedef struct _Eet_File Eet_File; /** * @typedef Eet_Dictionary - * Opaque handle that defines a file-backed (mmaped) dictionary of strings. + * @brief The structure type containing an opaque handle that defines a file-backed (mmaped) dictionary of strings. */ typedef struct _Eet_Dictionary Eet_Dictionary; /** - * @} - */ - -/** - * Open an eet file on disk, and returns a handle to it. - * @param file The file path to the eet file. eg: @c "/tmp/file.eet". - * @param mode The mode for opening. Either #EET_FILE_MODE_READ, - * #EET_FILE_MODE_WRITE or #EET_FILE_MODE_READ_WRITE. - * @return An opened eet file handle. - * @ingroup Eet_File_Group - * - * This function will open an exiting eet file for reading, and build - * the directory table in memory and return a handle to the file, if it - * exists and can be read, and no memory errors occur on the way, otherwise - * NULL will be returned. - * - * It will also open an eet file for writing. This will, if successful, - * delete the original file and replace it with a new empty file, till - * the eet file handle is closed or flushed. If it cannot be opened for - * writing or a memory error occurs, NULL is returned. - * - * You can also open the file for read/write. If you then write a key that - * does not exist it will be created, if the key exists it will be replaced - * by the new data. - * - * If the same file is opened multiple times, then the same file handle will - * be returned as eet maintains an internal list of all currently open - * files. Note that it considers files opened for read only and those opened - * for read/write and write only as 2 separate sets. Those that do not write - * to the file and those that do. Eet will allow 2 handles to the same file - * if they are in the 2 separate lists/groups. That means opening a file for - * read only looks in the read only set, and returns a handle to that file - * handle and increments its reference count. If you open a file for read/write - * or write only it looks in the write set and returns a handle after - * incrementing the reference count. You need to close an eet file handle - * as many times as it has been opened to maintain correct reference counts. - * Files whose modified timestamp or size do not match those of the existing - * referenced file handles will not be returned and a new handle will be - * returned instead. - * - * @since 1.0.0 + * @brief Opens an eet file on disk, and returns a handle to it. + * @since 1.0.0 + * + * @remarks This function opens an exiting eet file for reading, and build + * the directory table in memory and return a handle to the file, if it + * exists and can be read, and no memory errors occur on the way, otherwise + * @c NULL is returned. + * + * @remarks It also opens an eet file for writing. This, if successful, + * deletes the original file and replace it with a new empty file, till + * the eet file handle is closed or flushed. If it cannot be opened for + * writing or a memory error occurs, @c NULL is returned. + * + * @remarks You can also open the file for read/write. If you then write a key that + * does not exist it is created, if the key exists it is replaced + * by the new data. + * + * @remarks If the same file is opened multiple times, then the same file handle + * is returned as eet maintains an internal list of all currently open + * files. Note that it considers files opened for read only and those opened + * for read/write and write only as two separate sets. Those that do not write + * to the file and those that do. Eet allows two handles to the same file + * if they are in the two separate lists/groups. That means opening a file for + * read only looks in the read only set, and returns a handle to that file + * handle and increments its reference count. If you open a file for read/write + * or write only, it looks in the write set and returns a handle after + * incrementing the reference count. You need to close an eet file handle + * as many times as it has been opened to maintain correct reference counts. + * Files whose modified timestamp or size do not match those of the existing + * referenced file handles are not returned and a new handle is returned instead. + * + * @param[in] file The file path to the eet file. eg: @c "/tmp/file.eet" + * @param[in] mode The mode for opening. Either #EET_FILE_MODE_READ, + * #EET_FILE_MODE_WRITE or #EET_FILE_MODE_READ_WRITE + * @return An opened eet file handle */ EAPI Eet_File * eet_open(const char *file, Eet_File_Mode mode); /** - * Open an eet file directly from a memory location. The data is not copied, - * so you must keep it around as long as the eet file is open. There is - * currently no cache for this kind of Eet_File, so it's reopened every time - * you use eet_memopen_read. - * @param data Address of file in memory. - * @param size Size of memory to be read. - * @return A handle to the file. + * @brief Opens an eet file directly from a memory location. * - * Files opened this way will always be in read-only mode. + * @since 1.1.0 * - * @since 1.1.0 - * @ingroup Eet_File_Group + * @remarks The data is not copied, so you must keep it around + * as long as the eet file is open. There is + * currently no cache for this kind of Eet_File, + * so it is reopened every time you use eet_memopen_read. + * + * @remarks Files opened this way are always in read-only mode. + * + * @param[in] data The address of file in memory + * @param[in] size The size of memory to be read + * @return A handle to the file */ EAPI Eet_File * eet_memopen_read(const void *data, size_t size); /** - * Get the mode an Eet_File was opened with. - * @param ef A valid eet file handle. - * @return The mode ef was opened with. + * @brief Gets the mode an Eet_File is opened with. * - * @since 1.0.0 - * @ingroup Eet_File_Group + * @since 1.0.0 + * + * @param[in] ef A valid eet file handle + * @return The mode ef is opened with */ EAPI Eet_File_Mode eet_mode_get(Eet_File *ef); /** - * Close an eet file handle and flush pending writes. - * @param ef A valid eet file handle. - * @return An eet error identifier. + * @brief Closes an eet file handle and flush pending writes. * - * This function will flush any pending writes to disk if the eet file - * was opened for write, and free all data associated with the file handle - * and file, and close the file. If it was opened for read (or read/write), - * the file handle may still be held open internally for caching purposes. - * To flush speculatively held eet file handles use eet_clearcache(). + * @details This function flushes any pending writes to disk if the eet file + * is opened for write, and free all data associated with the file handle + * and file, and close the file. If it is opened for read (or read/write), + * the file handle may still be held open internally for caching purposes. + * To flush speculatively held eet file handles use eet_clearcache(). * - * If the eet file handle is not valid nothing will be done. + * @since 1.0.0 * - * @since 1.0.0 - * @ingroup Eet_File_Group + * @remarks If the eet file handle is not valid nothing is done. + * + * @param[in] ef A valid eet file handle + * @return An eet error identifier * * @see eet_clearcache() */ @@ -635,90 +597,89 @@ EAPI Eet_Error eet_close(Eet_File *ef); /** - * Sync content of an eet file handle, flushing pending writes. - * @param ef A valid eet file handle. - * @return An eet error identifier. + * @brief Syncs content of an eet file handle, flushing pending writes. + * @details This function flushes any pending writes to disk. The eet file must + * be opened for write. * - * This function will flush any pending writes to disk. The eet file must - * be opened for write. + * @since 1.2.4 * - * If the eet file handle is not valid nothing will be done. + * @remarks If the eet file handle is not valid nothing is done. * - * @since 1.2.4 - * @ingroup Eet_File_Group + * @param[in] ef A valid eet file handle + * @return An eet error identifier */ EAPI Eet_Error eet_sync(Eet_File *ef); /** - * Return a handle to the shared string dictionary of the Eet file - * @param ef A valid eet file handle. - * @return A handle to the dictionary of the file + * @brief Gets a handle to the shared string dictionary of the Eet file. * - * This function returns a handle to the dictionary of an Eet file whose - * handle is @p ef, if a dictionary exists. NULL is returned otherwise or - * if the file handle is known to be invalid. + * @details This function returns a handle to the dictionary of an Eet file whose + * handle is @a ef, if a dictionary exists. @c NULL is returned otherwise or + * if the file handle is known to be invalid. * + * @since 1.0.0 + * + * @param[in] ef A valid eet file handle + * @return A handle to the dictionary of the file * @see eet_dictionary_string_check() to know if given string came - * from the dictionary or it was dynamically allocated using + * from the dictionary or it is dynamically allocated using * the #Eet_Data_Descriptor_Class instructions. - * - * @since 1.0.0 - * @ingroup Eet_File_Group */ EAPI Eet_Dictionary * eet_dictionary_get(Eet_File *ef); /** - * Check if a given string comes from a given dictionary - * @param ed A valid dictionary handle - * @param string A valid 0 byte terminated C string - * @return 1 if it is in the dictionary, 0 otherwise + * @brief Checks whether a given string comes from a given dictionary. * - * This checks the given dictionary to see if the given string is actually - * inside that dictionary (i.e. comes from it) and returns 1 if it does. - * If the dictionary handle is invalid, the string is NULL or the string is - * not in the dictionary, 0 is returned. + * @details This checks the given dictionary to see if the given string is actually + * inside that dictionary (i.e. comes from it) and returns @c 1 if it does. + * If the dictionary handle is invalid, the string is @c NULL or the string is + * not in the dictionary, @c 0 is returned. * - * @since 1.0.0 - * @ingroup Eet_File_Group + * @since 1.0.0 + * + * @param[in] ed A valid dictionary handle + * @param[in] string A valid @c 0 byte terminated C string + * @return @c 1 if it is in the dictionary, \n + * otherwise @c 0 if it is not in the dictionary */ EAPI int eet_dictionary_string_check(Eet_Dictionary *ed, const char *string); /** - * Return the number of strings inside a dictionary - * @param ed A valid dictionary handle - * @return the number of strings inside a dictionary + * @brief Gets the number of strings inside a dictionary. + * @since 1.6.0 * - * @since 1.6.0 - * @ingroup Eet_File_Group + * @param[in] ed A valid dictionary handle + * @return The number of strings inside a dictionary */ EAPI int eet_dictionary_count(const Eet_Dictionary *ed); /** - * Read a specified entry from an eet file and return data - * @param ef A valid eet file handle opened for reading. - * @param name Name of the entry. eg: "/base/file_i_want". - * @param size_ret Number of bytes read from entry and returned. - * @return The data stored in that entry in the eet file. + * @brief Reads a specified entry from an eet file and returns data. * - * This function finds an entry in the eet file that is stored under the - * name specified, and returns that data, decompressed, if successful. - * NULL is returned if the lookup fails or if memory errors are - * encountered. It is the job of the calling program to call free() on - * the returned data. The number of bytes in the returned data chunk are - * placed in size_ret. + * @details This function finds an entry in the eet file that is stored under the + * name specified, and returns that data, decompressed, if successful. + * @c NULL is returned if the lookup fails or if memory errors are + * encountered. It is the job of the calling program to call free() on + * the returned data. The number of bytes in the returned data chunk are + * placed in size_ret. * - * If the eet file handle is not valid NULL is returned and size_ret is - * filled with 0. + * @since 1.0.0 * - * @see eet_read_cipher() + * @remarks If the eet file handle is not valid @c NULL is returned and @a size_ret is + * filled with @c 0. * - * @since 1.0.0 - * @ingroup Eet_File_Group + * @param[in] ef A valid eet file handle opened for reading + * @param[in] name The name of the entry \n + * For example: "/base/file_i_want". + * @param[out] size_ret The number of bytes read from entry and returned + * @return The data stored in that entry in the eet file + * + * @see eet_read_cipher() */ EAPI void * eet_read(Eet_File *ef, @@ -726,24 +687,23 @@ eet_read(Eet_File *ef, int *size_ret); /** - * Read a specified entry from an eet file and return data - * @param ef A valid eet file handle opened for reading. - * @param name Name of the entry. eg: "/base/file_i_want". - * @param size_ret Number of bytes read from entry and returned. - * @return The data stored in that entry in the eet file. - * - * This function finds an entry in the eet file that is stored under the - * name specified, and returns that data if not compressed and successful. - * NULL is returned if the lookup fails or if memory errors are - * encountered or if the data is compressed. The calling program must never - * call free() on the returned data. The number of bytes in the returned - * data chunk are placed in size_ret. + * @brief Reads a specified entry from an eet file and returns the data. * - * If the eet file handle is not valid NULL is returned and size_ret is - * filled with 0. + * @details This function finds an entry in the eet file that is stored under the + * name specified, and returns that data if not compressed and successful. + * @c NULL is returned if the lookup fails or if memory errors are + * encountered or if the data is compressed. The calling program must never + * call free() on the returned data. The number of bytes in the returned + * data chunk are placed in size_ret. + * @since 1.0.0 * - * @since 1.0.0 - * @ingroup Eet_File_Group + * @remarks If the eet file handle is not valid, @c NULL is returned and @a size_ret is + * filled with @a 0. + * @param[in] ef A valid eet file handle opened for reading. + * @param[in] name The name of the entry \n + * For example: "/base/file_i_want". + * @param[out] size_ret The number of bytes read from entry and returned + * @return The data stored in that entry in the eet file */ EAPI const void * eet_read_direct(Eet_File *ef, @@ -751,32 +711,34 @@ eet_read_direct(Eet_File *ef, int *size_ret); /** - * Write a specified entry to an eet file handle - * @param ef A valid eet file handle opened for writing. - * @param name Name of the entry. eg: "/base/file_i_want". - * @param data Pointer to the data to be stored. - * @param size Length in bytes in the data to be stored. - * @param compress Compression flags (1 == compress, 0 = don't compress). - * @return bytes written on successful write, 0 on failure. + * @brief Writes a specified entry to an eet file handle. * - * This function will write the specified chunk of data to the eet file - * and return greater than 0 on success. 0 will be returned on failure. + * @details This function writes the specified chunk of data to the eet file + * and return greater than @c 0 on success. @c 0 is returned on failure. * - * The eet file handle must be a valid file handle for an eet file opened - * for writing. If it is not, 0 will be returned and no action will be - * performed. + * @since 1.0.0 * - * Name, and data must not be NULL, and size must be > 0. If these - * conditions are not met, 0 will be returned. + * @remarks The eet file handle must be a valid file handle for an eet file opened + * for writing. If it is not, @c 0 is returned and no action is + * performed. * - * The data will be copied (and optionally compressed) in ram, pending - * a flush to disk (it will stay in ram till the eet file handle is - * closed though). + * @remarks Name and data must not be @c NULL, and size must be > @c 0. If these + * conditions are not met, @c 0 is returned. * - * @see eet_write_cipher() + * @remarks The data is copied (and optionally compressed) in RAM, pending + * a flush to disk (it stays in RAM till the eet file handle is + * closed though). * - * @since 1.0.0 - * @ingroup Eet_File_Group + * @param[in] ef A valid eet file handle opened for writing + * @param[in] name The name of the entry \n + * For example: "/base/file_i_want". + * @param[in] data The pointer to the data to be stored + * @param[in] size The length in bytes in the data to be stored + * @param[in] compress The compression flags (1 = compress, 0 = do not compress) + * @return The bytes written on successful write, \n + * otherwise @c 0 on failure + * + * @see eet_write_cipher() */ EAPI int eet_write(Eet_File *ef, @@ -786,41 +748,45 @@ eet_write(Eet_File *ef, int compress); /** - * Delete a specified entry from an Eet file being written or re-written - * @param ef A valid eet file handle opened for writing. - * @param name Name of the entry. eg: "/base/file_i_want". - * @return Success or failure of the delete. + * @brief Deletes a specified entry from an Eet file being written or re-written. * - * This function will delete the specified chunk of data from the eet file - * and return greater than 0 on success. 0 will be returned on failure. + * @details This function deletes the specified chunk of data from the eet file + * and return greater than @c 0 on success. @c 0 is returned on failure. * - * The eet file handle must be a valid file handle for an eet file opened - * for writing. If it is not, 0 will be returned and no action will be - * performed. + * @since 1.0.0 * - * Name, must not be NULL, otherwise 0 will be returned. + * @remarks The eet file handle must be a valid file handle for an eet file opened + * for writing. If it is not, @c 0 is returned and no action is performed. * - * @since 1.0.0 - * @ingroup Eet_File_Group + * @remarks Name must not be @c NULL, otherwise @c 0 is returned. + * + * @param[in] ef A valid eet file handle opened for writing + * @param[in] name The name of the entry \n + * For example: "/base/file_i_want". + * @return A number > @c 0 if the entry is deleted successfully, \n + * otherwise @c 0 on failure */ EAPI int eet_delete(Eet_File *ef, const char *name); /** - * Alias a specific section to another one. Destination may exist or not, - * no checks are done. - * @param ef A valid eet file handle opened for writing. - * @param name Name of the new entry. eg: "/base/file_i_want". - * @param destination Actual source of the aliased entry eg: "/base/the_real_stuff_i_want". - * @param compress Compression flags (1 == compress, 0 = don't compress). - * @return EINA_TRUE on success, EINA_FALSE on failure. + * @brief Aliases a specific section to another one. + * @since 1.3.3 * - * Name and Destination must not be NULL, otherwise EINA_FALSE will be returned. - * The equivalent of this would be calling 'ln -s destination name' + * @remarks Destination may exist or not, no checks are done. * - * @since 1.3.3 - * @ingroup Eet_File_Group + * @remarks Name and Destination must not be @c NULL, otherwise @c EINA_FALSE is returned. + * The equivalent of this would be calling 'ln -s destination name'. + * + * @param[in] ef A valid eet file handle opened for writing + * @param[in] name The name of the new entry \n + * For example: "/base/file_i_want". + * @param[in] destination The actual source of the aliased entry \n + * For example: "/base/the_real_stuff_i_want". + * @param[in] compress The compression flags (1 == compress, 0 = do not compress) + * @return @c EINA_TRUE if it is aliased successfully, \n + * otherwise @c EINA_FALSE on failure */ EAPI Eina_Bool eet_alias(Eet_File *ef, @@ -829,62 +795,64 @@ eet_alias(Eet_File *ef, int compress); /** - * Retrieve the filename of an Eet_File - * @param ef A valid eet file handle opened for writing. - * @return The stringshared file string opened with eet_open(), or NULL on error + * @brief Gets the filename of an Eet_File. + * @since 1.6 * - * @note This function will return NULL for files opened with eet_memopen_read() + * @remarks This function returns @c NULL for files opened with eet_memopen_read() * - * @since 1.6 - * @ingroup Eet_File_Group + * @param[in] ef A valid eet file handle opened for writing + * @return The stringshared file string opened with eet_open(), \n + * otherwise @c NULL on error */ EAPI const char * eet_file_get(Eet_File *ef); /** - * Retrieve the destination name of an alias - * @param ef A valid eet file handle opened for writing - * @param name Name of the entry. eg: "/base/file_i_want" - * @return Destination of the alias. eg: "/base/the_real_stuff_i_want", NULL on failure + * @brief Gets the destination name of an alias. * - * Name must not be NULL, otherwise NULL will be returned. + * @since 1.5 * - * @since 1.5 - * @ingroup Eet_File_Group + * @remarks Name must not be @c NULL, otherwise @c NULL is returned. + * + * @param[in] ef A valid eet file handle opened for writing + * @param[in] name The name of the entry \n + * For example: "/base/file_i_want" + * @return The destination of the alias, example: "/base/the_real_stuff_i_want", \n + * otherwise @c NULL on failure */ EAPI const char * eet_alias_get(Eet_File *ef, const char *name); /** - * List all entries in eet file matching shell glob. - * @param ef A valid eet file handle. - * @param glob A shell glob to match against. - * @param count_ret Number of entries found to match. - * @return Pointer to an array of strings. + * @brief Lists all entries in eet file matching shell glob. * - * This function will list all entries in the eet file matching the - * supplied shell glob and return an allocated list of their names, if - * there are any, and if no memory errors occur. + * @details This function lists all entries in the eet file matching the + * supplied shell glob and return an allocated list of their names, if + * there are any, and if no memory errors occur. * - * The eet file handle must be valid and glob must not be NULL, or NULL - * will be returned and count_ret will be filled with 0. + * @since 1.0.0 * - * The calling program must call free() on the array returned, but NOT - * on the string pointers in the array. They are taken as read-only - * internals from the eet file handle. They are only valid as long as - * the file handle is not closed. When it is closed those pointers in the - * array are now not valid and should not be used. + * @remarks The eet file handle must be valid and glob must not be @c NULL, or @c NULL + * is returned and @a count_ret is filled with @c 0. * - * On success the array returned will have a list of string pointers - * that are the names of the entries that matched, and count_ret will have - * the number of entries in this array placed in it. + * @remarks The calling program must call free() on the array returned, but NOT + * on the string pointers in the array. They are taken as read-only + * internals from the eet file handle. They are only valid as long as + * the file handle is not closed. When it is closed those pointers in the + * array are now not valid and should not be used. * - * Hint: an easy way to list all entries in an eet file is to use a glob - * value of "*". + * @remarks On success the array returned has a list of string pointers + * that are the names of the entries that matched, and @a count_ret has + * the number of entries in this array placed in it. * - * @since 1.0.0 - * @ingroup Eet_File_Group + * @remarks Hint: an easy way to list all entries in an eet file is to use a glob + * value of "*". + * + * @param[in] ef A valid eet file handle + * @param[in] glob A shell glob to match against + * @param[out] count_ret The number of entries found to match + * @return The pointer to an array of strings */ EAPI char ** eet_list(Eet_File *ef, @@ -892,50 +860,56 @@ eet_list(Eet_File *ef, int *count_ret); /** - * Return the number of entries in the specified eet file. - * @param ef A valid eet file handle. - * @return Number of entries in ef or -1 if the number of entries - * cannot be read due to open mode restrictions. + * @brief Gets the number of entries in the specified eet file. + * @since 1.0.0 * - * @since 1.0.0 - * @ingroup Eet_File_Group + * @param[in] ef A valid eet file handle + * @return The number of entries in ef, \n + * otherwise @c -1 if the number of entries cannot be read due to open mode restrictions */ EAPI int eet_num_entries(Eet_File *ef); /** + * @} + */ + +/** + * @internal * @defgroup Eet_File_Cipher_Group Eet File Ciphered Main Functions + * @ingroup Eet_File_Group * * Most of the @ref Eet_File_Group have alternative versions that * accounts for ciphers to protect their content. * * @see @ref Eet_Cipher_Group * - * @ingroup Eet_File_Group + * @{ */ /** - * Read a specified entry from an eet file and return data using a cipher. - * @param ef A valid eet file handle opened for reading. - * @param name Name of the entry. eg: "/base/file_i_want". - * @param size_ret Number of bytes read from entry and returned. - * @param cipher_key The key to use as cipher. - * @return The data stored in that entry in the eet file. + * @brief Reads a specified entry from an eet file and returns the data using a cipher. * - * This function finds an entry in the eet file that is stored under the - * name specified, and returns that data, decompressed, if successful. - * NULL is returned if the lookup fails or if memory errors are - * encountered. It is the job of the calling program to call free() on - * the returned data. The number of bytes in the returned data chunk are - * placed in size_ret. + * @details This function finds an entry in the eet file that is stored under the + * name specified, and returns that data, decompressed, if successful. + * @c NULL is returned if the lookup fails or if memory errors are + * encountered. It is the job of the calling program to call free() on + * the returned data. The number of bytes in the returned data chunk are + * placed in size_ret. + * @since 1.0.0 * - * If the eet file handle is not valid NULL is returned and size_ret is - * filled with 0. + * @remarks If the eet file handle is not valid, @c NULL is returned and @a size_ret is + * filled with @c 0. * - * @see eet_read() + * @param[in] ef A valid eet file handle opened for reading + * @param[in] name The name of the entry \n + * For example: "/base/file_i_want". + * @param[out] size_ret The number of bytes read from entry and returned + * @param[in] cipher_key The key to use as cipher + * @return The data stored in that entry in the eet file, \n + * otherwise @c NULL on error * - * @since 1.0.0 - * @ingroup Eet_File_Cipher_Group + * @see eet_read() */ EAPI void * eet_read_cipher(Eet_File *ef, @@ -944,33 +918,33 @@ eet_read_cipher(Eet_File *ef, const char *cipher_key); /** - * Write a specified entry to an eet file handle using a cipher. - * @param ef A valid eet file handle opened for writing. - * @param name Name of the entry. eg: "/base/file_i_want". - * @param data Pointer to the data to be stored. - * @param size Length in bytes in the data to be stored. - * @param compress Compression flags (1 == compress, 0 = don't compress). - * @param cipher_key The key to use as cipher. - * @return bytes written on successful write, 0 on failure. + * @brief Writes a specified entry to an eet file handle using a cipher. * - * This function will write the specified chunk of data to the eet file - * and return greater than 0 on success. 0 will be returned on failure. + * @details This function writes the specified chunk of data to the eet file + * and return greater than @c 0 on success. @c 0 is returned on failure. + * @since 1.0.0 * - * The eet file handle must be a valid file handle for an eet file opened - * for writing. If it is not, 0 will be returned and no action will be - * performed. + * @remarks The eet file handle must be a valid file handle for an eet file opened + * for writing. If it is not, @c 0 is returned and no action is performed. * - * Name, and data must not be NULL, and size must be > 0. If these - * conditions are not met, 0 will be returned. + * @remarks @a name and @a data must not be @c NULL, and size must be > @c 0. If these + * conditions are not met, @c 0 is returned. * - * The data will be copied (and optionally compressed) in ram, pending - * a flush to disk (it will stay in ram till the eet file handle is - * closed though). + * @remarks The data is copied (and optionally compressed) in RAM, pending + * a flush to disk (it stays in RAM till the eet file handle is + * closed though). * - * @see eet_write() + * @param[in] ef A valid eet file handle opened for writing + * @param[in] name The name of the entry \n + * For example: "/base/file_i_want". + * @param[in] data The pointer to the data to be stored + * @param[in] size The length in bytes in the data to be stored + * @param[in] compress The compression flags (1 == compress, 0 = do not compress) + * @param[in] cipher_key The key to use as cipher + * @return The bytes written on successful write, \n + * otherwise @c 0 on failure * - * @since 1.0.0 - * @ingroup Eet_File_Cipher_Group + * @see eet_write() */ EAPI int eet_write_cipher(Eet_File *ef, @@ -981,59 +955,71 @@ eet_write_cipher(Eet_File *ef, const char *cipher_key); /** + * @} + */ + +/** + * @internal * @defgroup Eet_File_Image_Group Image Store and Load + * @ingroup Eet_File_Group + * + * @brief This group provides functions for image storing and loading. * * Eet efficiently stores and loads images, including alpha * channels and lossy compressions. * * Eet can handle both lossy compression with different levels of quality and - * non-lossy compression with different compression levels. It's also possible, + * non-lossy compression with different compression levels. It is also possible, * given an image data, to only read its header to get the image information * without decoding the entire content for it. * - * The encode family of functions will take an image raw buffer and its + * The encode family of functions takes an image raw buffer and its * parameters and compress it in memory, returning the new buffer. - * Likewise, the decode functions will read from the given location in memory + * Likewise, the decode functions reads from the given location in memory * and return the uncompressed image. * - * The read and write functions will, respectively, encode and decode to or + * The read and write functions, respectively, encode and decode to or * from an Eet file, under the specified key. * * These functions are fairly low level and the same functionality can be * achieved using Evas and Edje, making it much easier to work with images * as well as not needing to worry about things like scaling them. + * + * @{ */ /** - * Read just the header data for an image and dont decode the pixels. - * @param ef A valid eet file handle opened for reading. - * @param name Name of the entry. eg: "/base/file_i_want". - * @param w A pointer to the unsigned int to hold the width in pixels. - * @param h A pointer to the unsigned int to hold the height in pixels. - * @param alpha A pointer to the int to hold the alpha flag. - * @param compress A pointer to the int to hold the compression amount. - * @param quality A pointer to the int to hold the quality amount. - * @param lossy A pointer to the int to hold the lossiness flag. - * @return 1 on successful decode, 0 otherwise + * @brief Reads the header data for an image but does not decode the pixels. + * + * @details This function reads and decodes the image header data stored under + * the given key and Eet file. * - * Reads and decodes the image header data stored under the given key and - * Eet file. + * @since 1.0.0 * - * The information decoded is placed in each of the parameters, which must be - * provided. The width and height, measured in pixels, will be stored under - * the variables pointed by @p w and @p h, respectively. If the read or - * decode of the header fails, this values will be 0. The @p alpha parameter - * will be 1 or 0, denoting if the alpha channel of the image is used or not. - * If the image was losslessly compressed, the @p compress parameter will hold - * the compression amount used, ranging from 0 to 9 and @p lossy will be 0. - * In the case of lossy compression, @p lossy will be 1, and the compression - * quality will be placed under @p quality, with a value ranging from 0 to 100. + * @remarks The information decoded is placed in each of the parameters, which must be + * provided. The width and height, measured in pixels, is stored under + * the variables pointed by @a w and @a h, respectively. If the read or + * decode of the header fails, this value is @c 0. The @a alpha parameter + * is @c 1 or @c 0, denoting if the alpha channel of the image is used or not. + * If the image is losslessly compressed, the @a compress parameter holds + * the compression amount used, ranging from @c 0 to @c 9 and @a lossy is @c 0. + * In the case of lossy compression, @a lossy is @c 1, and the compression + * quality is placed under @a quality, with a value ranging from @c 0 to @c 100. + * + * @param[in] ef A valid eet file handle opened for reading + * @param[in] name The name of the entry \n + * For example: "/base/file_i_want". + * @param[out] w A pointer to the unsigned int to hold the width in pixels + * @param[out] h A pointer to the unsigned int to hold the height in pixels + * @param[out] alpha A pointer to the int to hold the alpha flag + * @param[out] compress A pointer to the int to hold the compression amount + * @param[out] quality A pointer to the int to hold the quality amount + * @param[out] lossy A pointer to the int to hold the lossiness flag + * @return @c 1 if decoded successfully, \n + * otherwise @c 0 otherwise * * @see eet_data_image_header_decode() * @see eet_data_image_header_read_cipher() - * - * @since 1.0.0 - * @ingroup Eet_File_Image_Group */ EAPI int eet_data_image_header_read(Eet_File *ef, @@ -1043,42 +1029,42 @@ eet_data_image_header_read(Eet_File *ef, int *alpha, int *compress, int *quality, - int *lossy); + Eet_Image_Encoding *lossy); /** - * Read image data from the named key in the eet file. - * @param ef A valid eet file handle opened for reading. - * @param name Name of the entry. eg: "/base/file_i_want". - * @param w A pointer to the unsigned int to hold the width in pixels. - * @param h A pointer to the unsigned int to hold the height in pixels. - * @param alpha A pointer to the int to hold the alpha flag. - * @param compress A pointer to the int to hold the compression amount. - * @param quality A pointer to the int to hold the quality amount. - * @param lossy A pointer to the int to hold the lossiness flag. - * @return The image pixel data decoded + * @brief Reads the image data from the named key in the eet file. * - * Reads and decodes the image stored in the given Eet file under the named - * key. + * @details This function reads and decodes the image stored in the given Eet file under the named key. * - * The returned pixel data is a linear array of pixels starting from the - * top-left of the image, scanning row by row from left to right. Each pile - * is a 32bit value, with the high byte being the alpha channel, the next being - * red, then green, and the low byte being blue. + * @since 1.0.0 * - * The rest of the parameters are the same as in eet_data_image_header_read(). + * @remarks The returned pixel data is a linear array of pixels starting from the + * top-left of the image, scanning row by row from left to right. Each pile + * is a 32bit value, with the high byte being the alpha channel, the next being + * red, then green, and the low byte being blue. * - * On success the function returns a pointer to the image data decoded. The - * calling application is responsible for calling free() on the image data - * when it is done with it. On failure NULL is returned and the parameter - * values may not contain any sensible data. + * @remarks The rest of the parameters are the same as in eet_data_image_header_read(). + * + * @remarks On success, the function returns a pointer to the image data decoded. The + * calling application is responsible for calling free() on the image data + * when it is done with it. On failure @c NULL is returned and the parameter + * values may not contain any sensible data. + * + * @param[in] ef A valid eet file handle opened for reading + * @param[in] name The name of the entry \n + * For example: "/base/file_i_want". + * @param[out] w A pointer to the unsigned int to hold the width in pixels + * @param[out] h A pointer to the unsigned int to hold the height in pixels + * @param[out] alpha A pointer to the int to hold the alpha flag + * @param[out] compress A pointer to the int to hold the compression amount + * @param[out] quality A pointer to the int to hold the quality amount + * @param[out] lossy A pointer to the int to hold the lossiness flag + * @return The image pixel data decoded * * @see eet_data_image_header_read() * @see eet_data_image_decode() * @see eet_data_image_read_cipher() * @see eet_data_image_read_to_surface() - * - * @since 1.0.0 - * @ingroup Eet_File_Image_Group */ EAPI void * eet_data_image_read(Eet_File *ef, @@ -1088,54 +1074,57 @@ eet_data_image_read(Eet_File *ef, int *alpha, int *compress, int *quality, - int *lossy); - -/** - * Read image data from the named key in the eet file and store it in the given buffer. - * @param ef A valid eet file handle opened for reading. - * @param name Name of the entry. eg: "/base/file_i_want". - * @param src_x The starting x coordinate from where to dump the stream. - * @param src_y The starting y coordinate from where to dump the stream. - * @param d A pointer to the pixel surface. - * @param w The expected width in pixels of the pixel surface to decode. - * @param h The expected height in pixels of the pixel surface to decode. - * @param row_stride The length of a pixels line in the destination surface. - * @param alpha A pointer to the int to hold the alpha flag. - * @param compress A pointer to the int to hold the compression amount. - * @param quality A pointer to the int to hold the quality amount. - * @param lossy A pointer to the int to hold the lossiness flag. - * @return 1 on success, 0 otherwise. - * - * Reads and decodes the image stored in the given Eet file, placing the - * resulting pixel data in the buffer pointed by the user. - * - * Like eet_data_image_read(), it takes the image data stored under the - * @p name key in the @p ef file, but instead of returning a new buffer with - * the pixel data, it places the result in the buffer pointed by @p d, which - * must be provided by the user and of sufficient size to hold the requested - * portion of the image. - * - * The @p src_x and @p src_y parameters indicate the top-left corner of the - * section of the image to decode. These have to be higher or equal than 0 and - * less than the respective total width and height of the image. The width - * and height of the section of the image to decode are given in @p w and @p h - * and also can't be higher than the total width and height of the image. - * - * The @p row_stride parameter indicates the length in bytes of each line in - * the destination buffer and it has to be at least @p w * 4. - * - * All the other parameters are the same as in eet_data_image_read(). - * - * On success the function returns 1, and 0 on failure. On failure the - * parameter values may not contain any sensible data. + Eet_Image_Encoding *lossy); + +/** + * @brief Reads image data from the named key in the eet file and stores it in the given buffer. + * + * @details This function reads and decodes the image stored in the given Eet file, placing the + * resulting pixel data in the buffer pointed by the user. + * + * @since 1.0.2 + * + * @remarks Like eet_data_image_read(), it takes the image data stored under the + * @a name key in the @a ef file, but instead of returning a new buffer with + * the pixel data, it places the result in the buffer pointed by @a d, which + * must be provided by the user and of sufficient size to hold the requested + * portion of the image. + * + * @remarks The @a src_x and @a src_y parameters indicate the top-left corner of the + * section of the image to decode. These have to be higher or equal than @c 0 and + * less than the respective total width and height of the image. The width + * and height of the section of the image to decode are given in @a w and @a h + * and also cannot be higher than the total width and height of the image. + * + * @remarks The @a row_stride parameter indicates the length in bytes of each line in + * the destination buffer and it has to be at least @a w * 4. + * + * @remarks All the other parameters are the same as in eet_data_image_read(). + * + * @remarks On success, the function returns @c 1, and @c 0 on failure. On failure, the + * parameter values may not contain any sensible data. + * + * @param[in] ef A valid eet file handle opened for reading + * @param[in] name The name of the entry \n + * For example: "/base/file_i_want". + * @param[in] src_x The starting x coordinate from where to dump the stream + * @param[in] src_y The starting y coordinate from where to dump the stream + * @param[out] d A pointer to the pixel surface + * @param[in] w The expected width in pixels of the pixel surface to decode + * @param[in] h The expected height in pixels of the pixel surface to decode + * @param[in] row_stride The length of a pixels line in the destination surface + * @param[out] alpha A pointer to the int to hold the alpha flag + * @param[out] compress A pointer to the int to hold the compression amount + * @param[out] quality A pointer to the int to hold the quality amount + * @param[out] lossy A pointer to the int to hold the lossiness flag + * @return @c 1 if the image data is read and stored successfully, \n + * otherwise @c 0 on failure * * @see eet_data_image_read() * @see eet_data_image_decode() * @see eet_data_image_decode_to_surface() * @see eet_data_image_read_to_surface_cipher() - * - * @since 1.0.2 - * @ingroup Eet_File_Image_Group + * @see eet_data_image_decode_to_cspace_surface_cipher() */ EAPI int eet_data_image_read_to_surface(Eet_File *ef, @@ -1149,44 +1138,46 @@ eet_data_image_read_to_surface(Eet_File *ef, int *alpha, int *compress, int *quality, - int *lossy); - -/** - * Write image data to the named key in an eet file. - * @param ef A valid eet file handle opened for writing. - * @param name Name of the entry. eg: "/base/file_i_want". - * @param data A pointer to the image pixel data. - * @param w The width of the image in pixels. - * @param h The height of the image in pixels. - * @param alpha The alpha channel flag. - * @param compress The compression amount. - * @param quality The quality encoding amount. - * @param lossy The lossiness flag. - * @return Success if the data was encoded and written or not. - * - * This function takes image pixel data and encodes it in an eet file - * stored under the supplied name key, and returns how many bytes were - * actually written to encode the image data. - * - * The data expected is the same format as returned by eet_data_image_read. - * If this is not the case weird things may happen. Width and height must - * be between 1 and 8000 pixels. The alpha flags can be 0 or 1 (0 meaning - * the alpha values are not useful and 1 meaning they are). Compress can - * be from 0 to 9 (0 meaning no compression, 9 meaning full compression). - * This is only used if the image is not lossily encoded. Quality is used on - * lossy compression and should be a value from 0 to 100. The lossy flag - * can be 0 or 1. 0 means encode losslessly and 1 means to encode with - * image quality loss (but then have a much smaller encoding). - * - * On success this function returns the number of bytes that were required - * to encode the image data, or on failure it returns 0. + Eet_Image_Encoding *lossy); + +/** + * @brief Writes image data to the named key in an eet file. + * + * @details This function takes image pixel data and encodes it in an eet file + * stored under the supplied name key, and returns how many bytes were + * actually written to encode the image data. + * + * @since 1.0.0 + * + * @remarks The data expected is the same format as returned by eet_data_image_read. + * If this is not the case, weird things may happen. Width and height must + * be between @c 1 and @c 8000 pixels. The alpha flags can be @c 0 or @c 1 (@c 0 meaning + * the alpha values are not useful and @c 1 meaning they are). Compress can + * be from @c 0 to @c 9 (@c 0 meaning no compression, @c 9 meaning full compression). + * This is only used if the image is not lossily encoded. Quality is used on + * lossy compression and should be a value from @c 0 to @c 100. The lossy flag + * can be @c 0 or @c 1. @c 0 means encode losslessly and @c 1 means to encode with + * image quality loss (but then have a much smaller encoding). + * + * @remarks On success, this function returns the number of bytes that were required + * to encode the image data, or on failure it returns @c 0. + * + * @param[in] ef A valid eet file handle opened for writing + * @param[in] name The name of the entry \n + * For example: "/base/file_i_want". + * @param[in] data A pointer to the image pixel data + * @param[in] w The width of the image in pixels + * @param[in] h The height of the image in pixels + * @param[in] alpha The alpha channel flag + * @param[in] compress The compression amount + * @param[in] quality The quality encoding amount + * @param[in] lossy The lossiness flag + * @return The number of bytes to encode the image data, \n + * otherwise @c 0 on failure * * @see eet_data_image_read() * @see eet_data_image_encode() * @see eet_data_image_write_cipher() - * - * @since 1.0.0 - * @ingroup Eet_File_Image_Group */ EAPI int eet_data_image_write(Eet_File *ef, @@ -1197,32 +1188,33 @@ eet_data_image_write(Eet_File *ef, int alpha, int compress, int quality, - int lossy); + Eet_Image_Encoding lossy); /** - * Decode Image data header only to get information. - * @param data The encoded pixel data. - * @param size The size, in bytes, of the encoded pixel data. - * @param w A pointer to the unsigned int to hold the width in pixels. - * @param h A pointer to the unsigned int to hold the height in pixels. - * @param alpha A pointer to the int to hold the alpha flag. - * @param compress A pointer to the int to hold the compression amount. - * @param quality A pointer to the int to hold the quality amount. - * @param lossy A pointer to the int to hold the lossiness flag. - * @return 1 on success, 0 on failure. + * @brief Decodes image data header to get information. + * + * @details This function works exactly like eet_data_image_header_read(), but instead + * of reading from an Eet file, it takes the buffer of size @a size pointed + * by @a data, which must be a valid Eet encoded image. + * + * @since 1.0.0 * - * This function works exactly like eet_data_image_header_read(), but instead - * of reading from an Eet file, it takes the buffer of size @p size pointed - * by @p data, which must be a valid Eet encoded image. + * @remarks On success, the function returns @c 1 indicating the header is read and + * decoded properly, or @c 0 on failure. * - * On success the function returns 1 indicating the header was read and - * decoded properly, or 0 on failure. + * @param[in] data The encoded pixel data + * @param[in] size The size, in bytes, of the encoded pixel data + * @param[out] w A pointer to the unsigned int to hold the width in pixels + * @param[out] h A pointer to the unsigned int to hold the height in pixels + * @param[out] alpha A pointer to the int to hold the alpha flag + * @param[out] compress A pointer to the int to hold the compression amount + * @param[out] quality A pointer to the int to hold the quality amount + * @param[out] lossy A pointer to the int to hold the lossiness flag + * @return @c 1 if the image data header is decoded successfully, \n + * otherwise @c 0 on failure * * @see eet_data_image_header_read() * @see eet_data_image_header_decode_cipher() - * - * @since 1.0.0 - * @ingroup Eet_File_Image_Group */ EAPI int eet_data_image_header_decode(const void *data, @@ -1232,37 +1224,37 @@ eet_data_image_header_decode(const void *data, int *alpha, int *compress, int *quality, - int *lossy); + Eet_Image_Encoding *lossy); /** - * Decode Image data into pixel data. - * @param data The encoded pixel data. - * @param size The size, in bytes, of the encoded pixel data. - * @param w A pointer to the unsigned int to hold the width in pixels. - * @param h A pointer to the unsigned int to hold the height in pixels. - * @param alpha A pointer to the int to hold the alpha flag. - * @param compress A pointer to the int to hold the compression amount. - * @param quality A pointer to the int to hold the quality amount. - * @param lossy A pointer to the int to hold the lossiness flag. - * @return The image pixel data decoded + * @brief Decodes image data into pixel data. + * + * @details This function takes encoded pixel data and decodes it into raw RGBA + * pixels on success. + * @since 1.0.0 * - * This function takes encoded pixel data and decodes it into raw RGBA - * pixels on success. + * @remarks It works exactly like eet_data_image_read(), but it takes the encoded + * data in the @a data buffer of size @a size, instead of reading from a file. + * All the others parameters are also the same. * - * It works exactly like eet_data_image_read(), but it takes the encoded - * data in the @p data buffer of size @p size, instead of reading from a file. - * All the others parameters are also the same. + * @remarks On success, the function returns a pointer to the image data decoded. The + * calling application is responsible for calling free() on the image data + * when it is done with it. On failure, @c NULL is returned and the parameter + * values may not contain any sensible data. * - * On success the function returns a pointer to the image data decoded. The - * calling application is responsible for calling free() on the image data - * when it is done with it. On failure NULL is returned and the parameter - * values may not contain any sensible data. + * @param[in] data The encoded pixel data + * @param[in] size The size, in bytes, of the encoded pixel data + * @param[out] w A pointer to the unsigned int to hold the width in pixels + * @param[out] h A pointer to the unsigned int to hold the height in pixels + * @param[out] alpha A pointer to the int to hold the alpha flag + * @param[out] compress A pointer to the int to hold the compression amount + * @param[out] quality A pointer to the int to hold the quality amount + * @param[out] lossy A pointer to the int to hold the lossiness flag + * @return The image pixel data decoded, \n + * @c otherwise @c NULL on failure * * @see eet_data_image_read() * @see eet_data_image_decode_cipher() - * - * @since 1.0.0 - * @ingroup Eet_File_Image_Group */ EAPI void * eet_data_image_decode(const void *data, @@ -1272,35 +1264,35 @@ eet_data_image_decode(const void *data, int *alpha, int *compress, int *quality, - int *lossy); - -/** - * Decode Image data into pixel data and stores in the given buffer. - * @param data The encoded pixel data. - * @param size The size, in bytes, of the encoded pixel data. - * @param src_x The starting x coordinate from where to dump the stream. - * @param src_y The starting y coordinate from where to dump the stream. - * @param d A pointer to the pixel surface. - * @param w The expected width in pixels of the pixel surface to decode. - * @param h The expected height in pixels of the pixel surface to decode. - * @param row_stride The length of a pixels line in the destination surface. - * @param alpha A pointer to the int to hold the alpha flag. - * @param compress A pointer to the int to hold the compression amount. - * @param quality A pointer to the int to hold the quality amount. - * @param lossy A pointer to the int to hold the lossiness flag. - * @return 1 on success, 0 otherwise. - * - * Like eet_data_image_read_to_surface(), but reading the given @p data buffer - * instead of a file. - * - * On success the function returns 1, and 0 on failure. On failure the - * parameter values may not contain any sensible data. + Eet_Image_Encoding *lossy); + +/** + * @brief Decodes image data into pixel data and stores in the given buffer. + * @since 1.0.2 + * + * @remarks Like eet_data_image_read_to_surface(), but reading the given @a data buffer + * instead of a file. + * + * @remarks On success, the function returns @c 1, and @c 0 on failure. On failure, the + * parameter values may not contain any sensible data. + * + * @param[in] data The encoded pixel data + * @param[in] size The size, in bytes, of the encoded pixel data + * @param[in] src_x The starting x coordinate from where to dump the stream + * @param[in] src_y The starting y coordinate from where to dump the stream + * @param[out] d A pointer to the pixel surface + * @param[in] w The expected width in pixels of the pixel surface to decode + * @param[in] h The expected height in pixels of the pixel surface to decode + * @param[in] row_stride The length of a pixels line in the destination surface + * @param[out] alpha A pointer to the int to hold the alpha flag + * @param[out] compress A pointer to the int to hold the compression amount + * @param[out] quality A pointer to the int to hold the quality amount + * @param[out] lossy A pointer to the int to hold the lossiness flag + * @return @c 1 if the image is decoded successfully, \n + * otherwise @c 0 on failure * * @see eet_data_image_read_to_surface() * @see eet_data_image_decode_to_surface_cipher() - * - * @since 1.0.2 - * @ingroup Eet_File_Image_Group */ EAPI int eet_data_image_decode_to_surface(const void *data, @@ -1314,37 +1306,36 @@ eet_data_image_decode_to_surface(const void *data, int *alpha, int *compress, int *quality, - int *lossy); + Eet_Image_Encoding *lossy); /** - * Encode image data for storage or transmission. - * @param data A pointer to the image pixel data. - * @param size_ret A pointer to an int to hold the size of the returned data. - * @param w The width of the image in pixels. - * @param h The height of the image in pixels. - * @param alpha The alpha channel flag. - * @param compress The compression amount. - * @param quality The quality encoding amount. - * @param lossy The lossiness flag. - * @return The encoded image data. + * @brief Encodes image data for storage or transmission. * - * This function stakes image pixel data and encodes it with compression and - * possible loss of quality (as a trade off for size) for storage or - * transmission to another system. + * @details This function encodes image pixel data with compression and + * possible loss of quality (as a trade off for size) for storage or + * transmission to another system. + * @since 1.0.0 * - * It works like eet_data_image_write(), but instead of writing the encoded - * image into an Eet file, it allocates a new buffer of the size required and - * returns the encoded data in it. + * @remarks It works like eet_data_image_write(), but instead of writing the encoded + * image into an Eet file, it allocates a new buffer of the size required and + * returns the encoded data in it. * - * On success this function returns a pointer to the encoded data that you - * can free with free() when no longer needed. + * @remarks On success, this function returns a pointer to the encoded data that you + * can free with free() when no longer needed. + * + * @param[in] data A pointer to the image pixel data + * @param[out] size_ret A pointer to an int to hold the size of the returned data + * @param[in] w The width of the image in pixels + * @param[in] h The height of the image in pixels + * @param[in] alpha The alpha channel flag + * @param[in] compress The compression amount + * @param[in] quality The quality encoding amount + * @param[in] lossy The lossiness flag + * @return The encoded image data * * @see eet_data_image_write() * @see eet_data_image_read() * @see eet_data_image_encode_cipher() - * - * @since 1.0.0 - * @ingroup Eet_File_Image_Group */ EAPI void * eet_data_image_encode(const void *data, @@ -1354,55 +1345,84 @@ eet_data_image_encode(const void *data, int alpha, int compress, int quality, - int lossy); + Eet_Image_Encoding lossy); /** + * @brief Gets the colorspace that Eet can decode into of a given eet image resource. + * + * @since 1.10.0 + * + * @param[in] ef A valid eet file handle opened for reading + * @param[in] name The name of the entry \n + * For example: "/base/file_i_want". + * @param[in] cipher_key The key to use as cipher + * @param[in] cspaces The returned pointer by Eet to a list of possible decoding colorspace + * finished by @c EET_COLORSPACE_ARGB8888 \n + * If @c NULL, only EET_COLORSPACE_ARGB8888 is supported. + * @return @c 1 if the colorspace is obtained successfully, \n + * otherwise @c 0 on failure + */ +EAPI int +eet_data_image_colorspace_get(Eet_File *ef, + const char *name, + const char *cipher_key, + const Eet_Colorspace **cspaces); + +/** + * @} + */ + +/** + * @internal * @defgroup Eet_File_Image_Cipher_Group Image Store and Load using a Cipher + * @ingroup Eet_File_Image_Group * * Most of the @ref Eet_File_Image_Group have alternative versions * that accounts for ciphers to protect their content. * * @see @ref Eet_Cipher_Group * - * @ingroup Eet_File_Image_Group + * @{ */ /** - * Read just the header data for an image and dont decode the pixels using a cipher. - * @param ef A valid eet file handle opened for reading. - * @param name Name of the entry. eg: "/base/file_i_want". - * @param cipher_key The key to use as cipher. - * @param w A pointer to the unsigned int to hold the width in pixels. - * @param h A pointer to the unsigned int to hold the height in pixels. - * @param alpha A pointer to the int to hold the alpha flag. - * @param compress A pointer to the int to hold the compression amount. - * @param quality A pointer to the int to hold the quality amount. - * @param lossy A pointer to the int to hold the lossiness flag. - * @return 1 on successful decode, 0 otherwise - * - * This function reads an image from an eet file stored under the named - * key in the eet file and return a pointer to the decompressed pixel data. - * - * The other parameters of the image (width, height etc.) are placed into - * the values pointed to (they must be supplied). The pixel data is a linear - * array of pixels starting from the top-left of the image scanning row by - * row from left to right. Each pixel is a 32bit value, with the high byte - * being the alpha channel, the next being red, then green, and the low byte - * being blue. The width and height are measured in pixels and will be - * greater than 0 when returned. The alpha flag is either 0 or 1. 0 denotes - * that the alpha channel is not used. 1 denotes that it is significant. - * Compress is filled with the compression value/amount the image was - * stored with. The quality value is filled with the quality encoding of - * the image file (0 - 100). The lossy flags is either 0 or 1 as to if - * the image was encoded lossily or not. - * - * On success the function returns 1 indicating the header was read and - * decoded properly, or 0 on failure. + * @brief Reads only the header data for an image and does not decode the pixels using a cipher. * - * @see eet_data_image_header_read() + * @details This function reads an image from an eet file stored under the named + * key in the eet file and returns a pointer to the decompressed pixel data. + * + * @since 1.0.0 + * + * @remarks The other parameters of the image (width, height etc.) are placed into + * the values pointed to (they must be supplied). The pixel data is a linear + * array of pixels starting from the top-left of the image scanning row by + * row from left to right. Each pixel is a 32bit value, with the high byte + * being the alpha channel, the next being red, then green, and the low byte + * being blue. The width and height are measured in pixels and is + * greater than @c 0 when returned. The alpha flag is either @c 0 or @c 1. @c 0 denotes + * that the alpha channel is not used. @c 1 denotes that it is significant. + * Compress is filled with the compression value or amount the image is + * stored with. The quality value is filled with the quality encoding of + * the image file (0 - 100). The lossy flags is either @c 0 or @c 1 as to if + * the image is encoded lossily or not. * - * @since 1.0.0 - * @ingroup Eet_File_Image_Cipher_Group + * @remarks On success the function returns @c 1 indicating the header is read and + * decoded properly, or @c 0 on failure. + * + * @param[in] ef A valid eet file handle opened for reading + * @param[in] name The name of the entry \n + * For example: "/base/file_i_want". + * @param[in] cipher_key The key to use as cipher + * @param[out] w A pointer to the unsigned int to hold the width in pixels + * @param[out] h A pointer to the unsigned int to hold the height in pixels + * @param[out] alpha A pointer to the int to hold the alpha flag + * @param[out] compress A pointer to the int to hold the compression amount + * @param[out] quality A pointer to the int to hold the quality amount + * @param[out] lossy A pointer to the int to hold the lossiness flag + * @return @c 1 is the image is read successfully, \n + * otherwise @c 0 on failure + * + * @see eet_data_image_header_read() */ EAPI int eet_data_image_header_read_cipher(Eet_File *ef, @@ -1413,46 +1433,47 @@ eet_data_image_header_read_cipher(Eet_File *ef, int *alpha, int *compress, int *quality, - int *lossy); - -/** - * Read image data from the named key in the eet file using a cipher. - * @param ef A valid eet file handle opened for reading. - * @param name Name of the entry. eg: "/base/file_i_want". - * @param cipher_key The key to use as cipher. - * @param w A pointer to the unsigned int to hold the width in pixels. - * @param h A pointer to the unsigned int to hold the height in pixels. - * @param alpha A pointer to the int to hold the alpha flag. - * @param compress A pointer to the int to hold the compression amount. - * @param quality A pointer to the int to hold the quality amount. - * @param lossy A pointer to the int to hold the lossiness flag. - * @return The image pixel data decoded - * - * This function reads an image from an eet file stored under the named - * key in the eet file and return a pointer to the decompressed pixel data. - * - * The other parameters of the image (width, height etc.) are placed into - * the values pointed to (they must be supplied). The pixel data is a linear - * array of pixels starting from the top-left of the image scanning row by - * row from left to right. Each pixel is a 32bit value, with the high byte - * being the alpha channel, the next being red, then green, and the low byte - * being blue. The width and height are measured in pixels and will be - * greater than 0 when returned. The alpha flag is either 0 or 1. 0 denotes - * that the alpha channel is not used. 1 denotes that it is significant. - * Compress is filled with the compression value/amount the image was - * stored with. The quality value is filled with the quality encoding of - * the image file (0 - 100). The lossy flags is either 0 or 1 as to if - * the image was encoded lossily or not. - * - * On success the function returns a pointer to the image data decoded. The - * calling application is responsible for calling free() on the image data - * when it is done with it. On failure NULL is returned and the parameter - * values may not contain any sensible data. + Eet_Image_Encoding *lossy); + +/** + * @brief Reads image data from the named key in the eet file using a cipher. + * + * @details This function reads an image from an eet file stored under the named + * key in the eet file and return a pointer to the decompressed pixel data. + * + * @since 1.0.0 + * + * @remarks The other parameters of the image (width, height etc.) are placed into + * the values pointed to (they must be supplied). The pixel data is a linear + * array of pixels starting from the top-left of the image scanning row by + * row from left to right. Each pixel is a 32bit value, with the high byte + * being the alpha channel, the next being red, then green, and the low byte + * being blue. The width and height are measured in pixels and is + * greater than @c 0 when returned. The alpha flag is either @c 0 or @c 1. @c 0 denotes + * that the alpha channel is not used. @c 1 denotes that it is significant. + * Compress is filled with the compression value or amount the image is + * stored with. The quality value is filled with the quality encoding of + * the image file (0 - 100). The lossy flags is either @c 0 or @c 1 as to if + * the image is encoded lossily or not. + * + * @remarks On success, the function returns a pointer to the image data decoded. The + * calling application is responsible for calling free() on the image data + * when it is done with it. On failure, @c NULL is returned and the parameter + * values may not contain any sensible data. + * + * @param[in] ef A valid eet file handle opened for reading + * @param[in] name The name of the entry \n + * For example: "/base/file_i_want". + * @param[in] cipher_key The key to use as cipher + * @param[out] w A pointer to the unsigned int to hold the width in pixels + * @param[out] h A pointer to the unsigned int to hold the height in pixels + * @param[out] alpha A pointer to the int to hold the alpha flag + * @param[out] compress A pointer to the int to hold the compression amount + * @param[out] quality A pointer to the int to hold the quality amount + * @param[out] lossy A pointer to the int to hold the lossiness flag + * @return The image pixel data decoded * * @see eet_data_image_read() - * - * @since 1.0.0 - * @ingroup Eet_File_Image_Cipher_Group */ EAPI void * eet_data_image_read_cipher(Eet_File *ef, @@ -1463,48 +1484,52 @@ eet_data_image_read_cipher(Eet_File *ef, int *alpha, int *compress, int *quality, - int *lossy); - -/** - * Read image data from the named key in the eet file using a cipher. - * @param ef A valid eet file handle opened for reading. - * @param name Name of the entry. eg: "/base/file_i_want". - * @param cipher_key The key to use as cipher. - * @param src_x The starting x coordinate from where to dump the stream. - * @param src_y The starting y coordinate from where to dump the stream. - * @param d A pointer to the pixel surface. - * @param w The expected width in pixels of the pixel surface to decode. - * @param h The expected height in pixels of the pixel surface to decode. - * @param row_stride The length of a pixels line in the destination surface. - * @param alpha A pointer to the int to hold the alpha flag. - * @param compress A pointer to the int to hold the compression amount. - * @param quality A pointer to the int to hold the quality amount. - * @param lossy A pointer to the int to hold the lossiness flag. - * @return 1 on success, 0 otherwise. - * - * This function reads an image from an eet file stored under the named - * key in the eet file and return a pointer to the decompressed pixel data. - * - * The other parameters of the image (width, height etc.) are placed into - * the values pointed to (they must be supplied). The pixel data is a linear - * array of pixels starting from the top-left of the image scanning row by - * row from left to right. Each pixel is a 32bit value, with the high byte - * being the alpha channel, the next being red, then green, and the low byte - * being blue. The width and height are measured in pixels and will be - * greater than 0 when returned. The alpha flag is either 0 or 1. 0 denotes - * that the alpha channel is not used. 1 denotes that it is significant. - * Compress is filled with the compression value/amount the image was - * stored with. The quality value is filled with the quality encoding of - * the image file (0 - 100). The lossy flags is either 0 or 1 as to if - * the image was encoded lossily or not. - * - * On success the function returns 1, and 0 on failure. On failure the - * parameter values may not contain any sensible data. + Eet_Image_Encoding *lossy); + +/** + * @brief Reads image data from the named key in the eet file using a cipher. + * + * @details This function reads an image from an eet file stored under the named + * key in the eet file and store the decompressed pixel data in the provided + * surface with an @c EET_COLORSPACE_ARGB8888 colorspace. + * + * @since 1.0.2 + * + * @remarks The other parameters of the image (width, height etc.) are placed into + * the values pointed to (they must be supplied). The pixel data is a linear + * array of pixels starting from the top-left of the image scanning row by + * row from left to right. Each pixel is a 32bit value, with the high byte + * being the alpha channel, the next being red, then green, and the low byte + * being blue. The width and height are measured in pixels and is + * greater than @c 0 when returned. The alpha flag is either @c 0 or @c 1. @c 0 denotes + * that the alpha channel is not used. @c 1 denotes that it is significant. + * Compress is filled with the compression value/amount the image is + * stored with. The quality value is filled with the quality encoding of + * the image file (0 - 100). The lossy flags is either @c 0 or @c 1 as to if + * the image is encoded lossily or not. + * + * @remarks On success, the function returns @c 1, and @c 0 on failure. On failure, the + * parameter values may not contain any sensible data. + * + * @param[in] ef A valid eet file handle opened for reading + * @param[in] name The name of the entry \n + * For example: "/base/file_i_want". + * @param[in] cipher_key The key to use as cipher + * @param[in] src_x The starting x coordinate from where to dump the stream + * @param[in] src_y The starting y coordinate from where to dump the stream + * @param[out] d A pointer to the pixel surface + * @param[in] w The expected width in pixels of the pixel surface to decode + * @param[in] h The expected height in pixels of the pixel surface to decode + * @param[in] row_stride The length of a pixels line in the destination surface + * @param[out] alpha A pointer to the int to hold the alpha flag + * @param[out] compress A pointer to the int to hold the compression amount + * @param[out] quality A pointer to the int to hold the quality amount + * @param[out] lossy A pointer to the int to hold the lossiness flag + * @return @c 1 if image data is read successfully, + * otherwise @c 0 on failure * * @see eet_data_image_read_to_surface() - * - * @since 1.0.2 - * @ingroup Eet_File_Image_Cipher_Group + * @see eet_data_image_decode_to_cspace_surface_cipher() */ EAPI int eet_data_image_read_to_surface_cipher(Eet_File *ef, @@ -1519,43 +1544,171 @@ eet_data_image_read_to_surface_cipher(Eet_File *ef, int *alpha, int *compress, int *quality, - int *lossy); - -/** - * Write image data to the named key in an eet file using a cipher. - * @param ef A valid eet file handle opened for writing. - * @param name Name of the entry. eg: "/base/file_i_want". - * @param cipher_key The key to use as cipher. - * @param data A pointer to the image pixel data. - * @param w The width of the image in pixels. - * @param h The height of the image in pixels. - * @param alpha The alpha channel flag. - * @param compress The compression amount. - * @param quality The quality encoding amount. - * @param lossy The lossiness flag. - * @return Success if the data was encoded and written or not. - * - * This function takes image pixel data and encodes it in an eet file - * stored under the supplied name key, and returns how many bytes were - * actually written to encode the image data. - * - * The data expected is the same format as returned by eet_data_image_read. - * If this is not the case weird things may happen. Width and height must - * be between 1 and 8000 pixels. The alpha flags can be 0 or 1 (0 meaning - * the alpha values are not useful and 1 meaning they are). Compress can - * be from 0 to 9 (0 meaning no compression, 9 meaning full compression). - * This is only used if the image is not lossily encoded. Quality is used on - * lossy compression and should be a value from 0 to 100. The lossy flag - * can be 0 or 1. 0 means encode losslessly and 1 means to encode with - * image quality loss (but then have a much smaller encoding). - * - * On success this function returns the number of bytes that were required - * to encode the image data, or on failure it returns 0. + Eet_Image_Encoding *lossy); + + +/** + * @brief Reads image data from the named key in the eet file using a cipher. + * + * @details This function reads an image from an eet file stored under the named + * key in the eet file and store the decompressed pixel data in the provided + * surface colorspace. + * + * @since 1.10.0 + * + * @remarks The other parameters of the image (width, height etc.) are placed into + * the values pointed to (they must be supplied). The pixel data is a linear + * array of pixels starting from the top-left of the image scanning row by + * row from left to right. Each pixel is a 32bit value, with the high byte + * being the alpha channel, the next being red, then green, and the low byte + * being blue. The width and height are measured in pixels and is + * greater than @c 0 when returned. The alpha flag is either @c 0 or @c 1. @c 0 denotes + * that the alpha channel is not used. @c 1 denotes that it is significant. + * Compress is filled with the compression value/amount the image is + * stored with. The quality value is filled with the quality encoding of + * the image file (0 - 100). The lossy flags is either @c 0 or @c 1 as to if + * the image is encoded lossily or not. + * + * @remarks On success the function returns @c 1, and @c 0 on failure. On failure the + * parameter values may not contain any sensible data. + * + * @param[in] ef A valid eet file handle opened for reading + * @param[in] name The name of the entry \n + * For example: "/base/file_i_want". + * @param[in] cipher_key The key to use as cipher + * @param[in] src_x The starting x coordinate from where to dump the stream + * @param[in] src_y The starting y coordinate from where to dump the stream + * @param[out] d A pointer to the pixel surface + * @param[in] w The expected width in pixels of the pixel surface to decode + * @param[in] h The expected height in pixels of the pixel surface to decode + * @param[in] row_stride The length of a pixels line in the destination surface + * @param[in] cspace The color space of the pixels bsurface + * @param[out] alpha A pointer to the int to hold the alpha flag + * @param[out] comp A pointer to the int to hold the compression amount + * @param[out] quality A pointer to the int to hold the quality amount + * @param[out] lossy A pointer to the int to hold the lossiness flag + * @return @c 1 if image data is read successfully, \n + * otherwise @c 0 on failure * - * @see eet_data_image_write() + * @see eet_data_image_read_to_surface() + * @see eet_data_image_decode_to_cspace_surface_cipher() + * @see eet_data_image_read_to_surface_cipher() + */ +EAPI int +eet_data_image_read_to_cspace_surface_cipher(Eet_File *ef, + const char *name, + const char *cipher_key, + unsigned int src_x, + unsigned int src_y, + unsigned int *d, + unsigned int w, + unsigned int h, + unsigned int row_stride, + Eet_Colorspace cspace, + int *alpha, + int *comp, + int *quality, + Eet_Image_Encoding *lossy); + + +/** + * @brief Reads image data from the named key in the eet file using a cipher. + * + * @details This function reads an image from an eet file stored under the named + * key in the eet file and store the decompressed pixels in the specified + * color space inside the given surface. + * + * @since 1.10.0 + * + * @remarks The other parameters of the image (width, height etc.) are placed into + * the values pointed to (they must be supplied). The pixel data is a linear + * array of pixels starting from the top-left of the image scanning row by + * row from left to right. Each pixel is a 32bit value, with the high byte + * being the alpha channel, the next being red, then green, and the low byte + * being blue. The width and height are measured in pixels and is + * greater than @c 0 when returned. The alpha flag is either @c 0 or @c 1. @c 0 denotes + * that the alpha channel is not used. @c 1 denotes that it is significant. + * Compress is filled with the compression value or amount the image is + * stored with. The quality value is filled with the quality encoding of + * the image file (0 - 100). The lossy flags is either @c 0 or @c 1 as to if + * the image is encoded lossily or not. + * + * @remarks On success the function returns @c 1, and @c 0 on failure. On failure the + * parameter values may not contain any sensible data. + * + * @param[in] data A pointer to the image pixel data + * @param[in] cipher_key The key to use as cipher + * @param[in] size The size, in bytes, of the encoded pixel data + * @param[in] src_x The starting x coordinate from where to dump the stream + * @param[in] src_y The starting y coordinate from where to dump the stream + * @param[out] d A pointer to the pixel surface + * @param[in] w The expected width in pixels of the pixel surface to decode + * @param[in] h The expected height in pixels of the pixel surface to decode + * @param[in] row_stride The length of a pixels line in the destination surface + * @param[in] cspace The color space of the pixel surface + * @param[out] alpha A pointer to the int to hold the alpha flag + * @param[out] comp A pointer to the int to hold the compression amount + * @param[out] quality A pointer to the int to hold the quality amount + * @param[out] lossy A pointer to the int to hold the lossiness flag + * @return @c 1 if image data is read successfully, \n + * otherwise @c 0 on failure + * + * @see eet_data_image_read_to_surface() + * @see eet_data_image_read_to_surface_cipher() + */ + +EAPI int +eet_data_image_decode_to_cspace_surface_cipher(const void *data, + const char *cipher_key, + int size, + unsigned int src_x, + unsigned int src_y, + unsigned int *d, + unsigned int w, + unsigned int h, + unsigned int row_stride, + Eet_Colorspace cspace, + int *alpha, + int *comp, + int *quality, + Eet_Image_Encoding *lossy); + +/** + * @brief Writes image data to the named key in an eet file using a cipher. + * + * @details This function takes image pixel data and encodes it in an eet file + * stored under the supplied name key, and returns how many bytes were + * actually written to encode the image data. + * @since 1.0.0 + * + * @remarks The data expected is the same format as returned by eet_data_image_read. + * If this is not the case weird things may happen. Width and height must + * be between @c 1 and @c 8000 pixels. The alpha flags can be @c 0 or @c 1 (@c 0 meaning + * the alpha values are not useful and @c 1 meaning they are). Compress can + * be from @c 0 to @c 9 (@c 0 meaning no compression, @c 9 meaning full compression). + * This is only used if the image is not lossily encoded. Quality is used on + * lossy compression and should be a value from @c 0 to @c 100. The lossy flag + * can be @c 0 or @c 1. @c 0 means encode losslessly and @c 1 means to encode with + * image quality loss (but then have a much smaller encoding). + * + * @remarks On success, this function returns the number of bytes that were required + * to encode the image data, or on failure it returns @c 0. + * + * @param[in] ef A valid eet file handle opened for writing + * @param[in] name The name of the entry \n + * For example: "/base/file_i_want". + * @param[in] cipher_key The key to use as cipher + * @param[in] data A pointer to the image pixel data + * @param[in] w The width of the image in pixels + * @param[in] h The height of the image in pixels + * @param[in] alpha The alpha channel flag + * @param[in] compress The compression amount + * @param[in] quality The quality encoding amount + * @param[in] lossy The lossiness flag + * @return The number of bytes to encode the image data, \n + * otherwise @c 0 on failure * - * @since 1.0.0 - * @ingroup Eet_File_Image_Cipher_Group + * @see eet_data_image_write() */ EAPI int eet_data_image_write_cipher(Eet_File *ef, @@ -1567,44 +1720,44 @@ eet_data_image_write_cipher(Eet_File *ef, int alpha, int compress, int quality, - int lossy); - -/** - * Decode Image data header only to get information using a cipher. - * @param data The encoded pixel data. - * @param cipher_key The key to use as cipher. - * @param size The size, in bytes, of the encoded pixel data. - * @param w A pointer to the unsigned int to hold the width in pixels. - * @param h A pointer to the unsigned int to hold the height in pixels. - * @param alpha A pointer to the int to hold the alpha flag. - * @param compress A pointer to the int to hold the compression amount. - * @param quality A pointer to the int to hold the quality amount. - * @param lossy A pointer to the int to hold the lossiness flag. - * @return 1 on success, 0 on failure. - * - * This function takes encoded pixel data and decodes it into raw RGBA - * pixels on success. - * - * The other parameters of the image (width, height etc.) are placed into - * the values pointed to (they must be supplied). The pixel data is a linear - * array of pixels starting from the top-left of the image scanning row by - * row from left to right. Each pixel is a 32bit value, with the high byte - * being the alpha channel, the next being red, then green, and the low byte - * being blue. The width and height are measured in pixels and will be - * greater than 0 when returned. The alpha flag is either 0 or 1. 0 denotes - * that the alpha channel is not used. 1 denotes that it is significant. - * Compress is filled with the compression value/amount the image was - * stored with. The quality value is filled with the quality encoding of - * the image file (0 - 100). The lossy flags is either 0 or 1 as to if - * the image was encoded lossily or not. - * - * On success the function returns 1 indicating the header was read and - * decoded properly, or 0 on failure. + Eet_Image_Encoding lossy); + +/** + * @brief Decodes image data header only to get information using a cipher. + * + * @details This function takes encoded pixel data and decodes it into raw RGBA + * pixels on success. + * @since 1.0.0 + * + * @remarks The other parameters of the image (width, height etc.) are placed into + * the values pointed to (they must be supplied). The pixel data is a linear + * array of pixels starting from the top-left of the image scanning row by + * row from left to right. Each pixel is a 32bit value, with the high byte + * being the alpha channel, the next being red, then green, and the low byte + * being blue. The width and height are measured in pixels and is + * greater than @c 0 when returned. The alpha flag is either @c 0 or @c 1. @c 0 denotes + * that the alpha channel is not used. @c 1 denotes that it is significant. + * Compress is filled with the compression value or amount the image is + * stored with. The quality value is filled with the quality encoding of + * the image file (0 - 100). The lossy flags is either @c 0 or @c 1 as to if + * the image is encoded lossily or not. + * + * @remarks On success, the function returns @c 1 indicating the header is read and + * decoded properly, or @c 0 on failure. + * + * @param[in] data The encoded pixel data + * @param[in] cipher_key The key to use as cipher + * @param[in] size The size, in bytes, of the encoded pixel data + * @param[out] w A pointer to the unsigned int to hold the width in pixels + * @param[out] h A pointer to the unsigned int to hold the height in pixels + * @param[out] alpha A pointer to the int to hold the alpha flag + * @param[out] compress A pointer to the int to hold the compression amount + * @param[out] quality A pointer to the int to hold the quality amount + * @param[out] lossy A pointer to the int to hold the lossiness flag + * @return @c 1 if the header is decoded successfully, \n + * otherwise @c 0 on failure * * @see eet_data_image_header_decode() - * - * @since 1.0.0 - * @ingroup Eet_File_Image_Cipher_Group */ EAPI int eet_data_image_header_decode_cipher(const void *data, @@ -1615,46 +1768,46 @@ eet_data_image_header_decode_cipher(const void *data, int *alpha, int *compress, int *quality, - int *lossy); - -/** - * Decode Image data into pixel data using a cipher. - * @param data The encoded pixel data. - * @param cipher_key The key to use as cipher. - * @param size The size, in bytes, of the encoded pixel data. - * @param w A pointer to the unsigned int to hold the width in pixels. - * @param h A pointer to the unsigned int to hold the height in pixels. - * @param alpha A pointer to the int to hold the alpha flag. - * @param compress A pointer to the int to hold the compression amount. - * @param quality A pointer to the int to hold the quality amount. - * @param lossy A pointer to the int to hold the lossiness flag. - * @return The image pixel data decoded - * - * This function takes encoded pixel data and decodes it into raw RGBA - * pixels on success. - * - * The other parameters of the image (width, height etc.) are placed into - * the values pointed to (they must be supplied). The pixel data is a linear - * array of pixels starting from the top-left of the image scanning row by - * row from left to right. Each pixel is a 32bit value, with the high byte - * being the alpha channel, the next being red, then green, and the low byte - * being blue. The width and height are measured in pixels and will be - * greater than 0 when returned. The alpha flag is either 0 or 1. 0 denotes - * that the alpha channel is not used. 1 denotes that it is significant. - * Compress is filled with the compression value/amount the image was - * stored with. The quality value is filled with the quality encoding of - * the image file (0 - 100). The lossy flags is either 0 or 1 as to if - * the image was encoded lossily or not. - * - * On success the function returns a pointer to the image data decoded. The - * calling application is responsible for calling free() on the image data - * when it is done with it. On failure NULL is returned and the parameter - * values may not contain any sensible data. + Eet_Image_Encoding *lossy); + +/** + * @brief Decodes image data into pixel data using a cipher. + * + * @details This function takes encoded pixel data and decodes it into raw RGBA + * pixels on success. + * @since 1.0.0 + * + * @remarks The other parameters of the image (width, height etc.) are placed into + * the values pointed to (they must be supplied). The pixel data is a linear + * array of pixels starting from the top-left of the image scanning row by + * row from left to right. Each pixel is a 32bit value, with the high byte + * being the alpha channel, the next being red, then green, and the low byte + * being blue. The width and height are measured in pixels and is + * greater than @c 0 when returned. The alpha flag is either @c 0 or @c 1. @c 0 denotes + * that the alpha channel is not used. @c 1 denotes that it is significant. + * Compress is filled with the compression value or amount the image is + * stored with. The quality value is filled with the quality encoding of + * the image file (0 - 100). The lossy flags is either @c 0 or @c 1 as to if + * the image is encoded lossily or not. + * + * @remarks On success, the function returns a pointer to the image data decoded. The + * calling application is responsible for calling free() on the image data + * when it is done with it. On failure, @c NULL is returned and the parameter + * values may not contain any sensible data. + * + * @param[in] data The encoded pixel data + * @param[in] cipher_key The key to use as cipher + * @param[in] size The size, in bytes, of the encoded pixel data + * @param[out] w A pointer to the unsigned int to hold the width in pixels + * @param[out] h A pointer to the unsigned int to hold the height in pixels + * @param[out] alpha A pointer to the int to hold the alpha flag + * @param[out] compress A pointer to the int to hold the compression amount + * @param[out] quality A pointer to the int to hold the quality amount + * @param[out] lossy A pointer to the int to hold the lossiness flag + * @return The image pixel data decoded, \n + * otherwise @c NULL on failure * * @see eet_data_image_decode() - * - * @since 1.0.0 - * @ingroup Eet_File_Image_Cipher_Group */ EAPI void * eet_data_image_decode_cipher(const void *data, @@ -1665,48 +1818,48 @@ eet_data_image_decode_cipher(const void *data, int *alpha, int *compress, int *quality, - int *lossy); - -/** - * Decode Image data into pixel data using a cipher. - * @param data The encoded pixel data. - * @param cipher_key The key to use as cipher. - * @param size The size, in bytes, of the encoded pixel data. - * @param src_x The starting x coordinate from where to dump the stream. - * @param src_y The starting y coordinate from where to dump the stream. - * @param d A pointer to the pixel surface. - * @param w The expected width in pixels of the pixel surface to decode. - * @param h The expected height in pixels of the pixel surface to decode. - * @param row_stride The length of a pixels line in the destination surface. - * @param alpha A pointer to the int to hold the alpha flag. - * @param compress A pointer to the int to hold the compression amount. - * @param quality A pointer to the int to hold the quality amount. - * @param lossy A pointer to the int to hold the lossiness flag. - * @return 1 on success, 0 otherwise. - * - * This function takes encoded pixel data and decodes it into raw RGBA - * pixels on success. - * - * The other parameters of the image (alpha, compress etc.) are placed into - * the values pointed to (they must be supplied). The pixel data is a linear - * array of pixels starting from the top-left of the image scanning row by - * row from left to right. Each pixel is a 32bit value, with the high byte - * being the alpha channel, the next being red, then green, and the low byte - * being blue. The width and height are measured in pixels and will be - * greater than 0 when returned. The alpha flag is either 0 or 1. 0 denotes - * that the alpha channel is not used. 1 denotes that it is significant. - * Compress is filled with the compression value/amount the image was - * stored with. The quality value is filled with the quality encoding of - * the image file (0 - 100). The lossy flags is either 0 or 1 as to if - * the image was encoded lossily or not. - * - * On success the function returns 1, and 0 on failure. On failure the - * parameter values may not contain any sensible data. + Eet_Image_Encoding *lossy); + +/** + * @brief Decodes image data into pixel data using a cipher. + * + * @details This function takes encoded pixel data and decodes it into raw RGBA + * pixels on success. + * @since 1.0.2 + * + * @remarks The other parameters of the image (alpha, compress etc.) are placed into + * the values pointed to (they must be supplied). The pixel data is a linear + * array of pixels starting from the top-left of the image scanning row by + * row from left to right. Each pixel is a 32bit value, with the high byte + * being the alpha channel, the next being red, then green, and the low byte + * being blue. The width and height are measured in pixels and is + * greater than @c 0 when returned. The alpha flag is either @c 0 or @c 1. @c 0 denotes + * that the alpha channel is not used. @c 1 denotes that it is significant. + * Compress is filled with the compression value or amount the image is + * stored with. The quality value is filled with the quality encoding of + * the image file (0 - 100). The lossy flags is either @c 0 or @c 1 as to if + * the image is encoded lossily or not. + * + * @remarks On success, the function returns @c 1, and @c 0 on failure. On failure, the + * parameter values may not contain any sensible data. + * + * @param[in] data The encoded pixel data + * @param[in] cipher_key The key to use as cipher + * @param[in] size The size, in bytes, of the encoded pixel data + * @param[in] src_x The starting x coordinate from where to dump the stream + * @param[in] src_y The starting y coordinate from where to dump the stream + * @param[out] d A pointer to the pixel surface + * @param[in] w The expected width in pixels of the pixel surface to decode + * @param[in] h The expected height in pixels of the pixel surface to decode + * @param[in] row_stride The length of a pixels line in the destination surface + * @param[out] alpha A pointer to the int to hold the alpha flag + * @param[out] compress A pointer to the int to hold the compression amount + * @param[out] quality A pointer to the int to hold the quality amount + * @param[out] lossy A pointer to the int to hold the lossiness flag + * @return @c 1 if the image data is decoded successfully, \n + * otherwise @c 0 on failure * * @see eet_data_image_decode_to_surface() - * - * @since 1.0.2 - * @ingroup Eet_File_Image_Cipher_Group */ EAPI int eet_data_image_decode_to_surface_cipher(const void *data, @@ -1721,42 +1874,41 @@ eet_data_image_decode_to_surface_cipher(const void *data, int *alpha, int *compress, int *quality, - int *lossy); - -/** - * Encode image data for storage or transmission using a cipher. - * @param data A pointer to the image pixel data. - * @param cipher_key The key to use as cipher. - * @param size_ret A pointer to an int to hold the size of the returned data. - * @param w The width of the image in pixels. - * @param h The height of the image in pixels. - * @param alpha The alpha channel flag. - * @param compress The compression amount. - * @param quality The quality encoding amount. - * @param lossy The lossiness flag. - * @return The encoded image data. - * - * This function stakes image pixel data and encodes it with compression and - * possible loss of quality (as a trade off for size) for storage or - * transmission to another system. - * - * The data expected is the same format as returned by eet_data_image_read. - * If this is not the case weird things may happen. Width and height must - * be between 1 and 8000 pixels. The alpha flags can be 0 or 1 (0 meaning - * the alpha values are not useful and 1 meaning they are). Compress can - * be from 0 to 9 (0 meaning no compression, 9 meaning full compression). - * This is only used if the image is not lossily encoded. Quality is used on - * lossy compression and should be a value from 0 to 100. The lossy flag - * can be 0 or 1. 0 means encode losslessly and 1 means to encode with - * image quality loss (but then have a much smaller encoding). - * - * On success this function returns a pointer to the encoded data that you - * can free with free() when no longer needed. + Eet_Image_Encoding *lossy); + +/** + * @brief Encodes image data for storage or transmission using a cipher. + * + * @details This function encodes image pixel data with compression and + * possible loss of quality (as a trade off for size) for storage or + * transmission to another system. + * @since 1.0.0 + * + * @remarks The data expected is the same format as returned by eet_data_image_read. + * If this is not the case weird things may happen. Width and height must + * be between @c 1 and @c 8000 pixels. The alpha flags can be @c 0 or @c 1 (@c 0 meaning + * the alpha values are not useful and @c 1 meaning they are). Compress can + * be from @c 0 to @c 9 (@c 0 meaning no compression, @c 9 meaning full compression). + * This is only used if the image is not lossily encoded. Quality is used on + * lossy compression and should be a value from @c 0 to @c 100. The lossy flag + * can be @c 0 or @c 1. @c 0 means encode losslessly and @c 1 means to encode with + * image quality loss (but then have a much smaller encoding). + * + * @remarks On success, this function returns a pointer to the encoded data that you + * can free with free() when no longer needed. + * + * @param[in] data A pointer to the image pixel data + * @param[in] cipher_key The key to use as cipher + * @param[in] size_ret A pointer to an int to hold the size of the returned data + * @param[in] w The width of the image in pixels + * @param[in] h The height of the image in pixels + * @param[in] alpha The alpha channel flag + * @param[in] compress The compression amount + * @param[in] quality The quality encoding amount + * @param[in] lossy The lossiness flag + * @return The encoded image data * * @see eet_data_image_encode() - * - * @since 1.0.0 - * @ingroup Eet_File_Image_Cipher_Group */ EAPI void * eet_data_image_encode_cipher(const void *data, @@ -1766,14 +1918,20 @@ eet_data_image_encode_cipher(const void *data, int alpha, int compress, int quality, - int lossy, + Eet_Image_Encoding lossy, int *size_ret); /** + * @} + */ + +/** + * @internal * @defgroup Eet_Cipher_Group Cipher, Identity and Protection Mechanisms + * @ingroup Eet_Group * * Eet allows one to protect entries of an #Eet_File - * individually. This may be used to ensure data was not tampered or + * individually. This may be used to ensure data is not tampered or * that third party does not read your data. * * @see @ref Eet_File_Cipher_Group @@ -1790,39 +1948,38 @@ eet_data_image_encode_cipher(const void *data, typedef struct _Eet_Key Eet_Key; /** - * @} - */ - -/** - * Callback used to request if needed the password of a private key. - * - * @param buffer the buffer where to store the password. - * @param size the maximum password size (size of buffer, including '@\0'). - * @param rwflag if the buffer is also readable or just writable. - * @param data currently unused, may contain some context in future. - * @return 1 on success and password was set to @p buffer, 0 on failure. + * @brief Callback to request whether a password is needed for a private key. + * @since 1.2.0 * - * @since 1.2.0 + * @param buffer The buffer to store the password + * @param size The maximum password size \n + * The size of buffer, including '@\0'. + * @param rwflag Indicator for the buffer mode \n + * Whether it is also readable or just writeable. + * @param data Currently unused, may contain some context in future + * @return @c 1 on success and password is set to @a buffer, \n + * otherwise @c 0 on failure * @ingroup Eet_Cipher_Group */ typedef int (*Eet_Key_Password_Callback)(char *buffer, int size, int rwflag, void *data); /** - * Create an Eet_Key needed for signing an eet file. + * @brief Creates an Eet_Key needed for signing an eet file. + * @since 1.2.0 * - * The certificate should provide the public that match the private key. - * No verification is done to ensure that. + * @remarks The certificate should provide the public key that matches the private key. + * No verification is done to ensure that. * - * @param certificate_file The file where to find the certificate. - * @param private_key_file The file that contains the private key. - * @param cb Function to callback if password is required to unlock - * private key. - * @return A key handle to use, or @c NULL on failure. + * @remarks You need to compile signature support in EET. * - * @see eet_identity_close() + * @param[in] certificate_file The file to find the certificate + * @param[in] private_key_file The file that contains the private key + * @param[in] cb The callback to check whether password is required to unlock + * private key + * @return A key handle to use, \n + * otherwise @c NULL on failure * - * @since 1.2.0 - * @ingroup Eet_Cipher_Group + * @see eet_identity_close() */ EAPI Eet_Key * eet_identity_open(const char *certificate_file, @@ -1830,111 +1987,114 @@ eet_identity_open(const char *certificate_file, Eet_Key_Password_Callback cb); /** - * Close and release all resource used by an Eet_Key. An - * reference counter prevent it from being freed until all file - * using it are also closed. + * @brief Closes and releases all resources used by an Eet_Key. + * @since 1.2.0 * - * @param key the key handle to close and free resources. + * @remarks A reference counter prevents it from being freed until all the files + * using it are also closed. * - * @since 1.2.0 - * @ingroup Eet_Cipher_Group + * @param[in] key The key handle to close and free resources */ EAPI void eet_identity_close(Eet_Key *key); /** - * Set a key to sign a file - * - * @param ef the file to set the identity. - * @param key the key handle to set as identity. - * @return #EET_ERROR_BAD_OBJECT if @p ef is invalid or - * #EET_ERROR_NONE on success. + * @brief Sets a key to sign a file. + * @since 1.2.0 * - * @since 1.2.0 - * @ingroup Eet_Cipher_Group + * @param[in] ef The file to set the identity + * @param[in] key The key handle to set as identity + * @return #EET_ERROR_NONE is the key is set successfully, \n + * otherwise #EET_ERROR_BAD_OBJECT if @a ef is invalid */ EAPI Eet_Error eet_identity_set(Eet_File *ef, Eet_Key *key); /** - * Display both private and public key of an Eet_Key. + * @brief Displays both private and public key of an Eet_Key. + * @since 1.2.0 * - * @param key the handle to print. - * @param out where to print. + * @remarks You need to compile signature support in EET. * - * @since 1.2.0 - * @ingroup Eet_Cipher_Group + * @param[in] key The handle to print + * @param[in] out The output destination to print */ EAPI void eet_identity_print(Eet_Key *key, FILE *out); /** - * Get the x509 der certificate associated with an Eet_File. Will return NULL - * if the file is not signed. - * - * @param ef The file handle to query. - * @param der_length The length of returned data, may be @c NULL. - * @return the x509 certificate or @c NULL on error. + * @brief Gets the x509 der certificate associated with an Eet_File. + * @details This function returns @c NULL if the file is not signed. + * @since 1.2.0 * - * @since 1.2.0 - * @ingroup Eet_Cipher_Group + * @param[in] ef The file handle to query + * @param[out] der_length The length of returned data \n + * This may be @c NULL. + * @return The x509 certificate, \n + * otherwise @c NULL on error */ EAPI const void * eet_identity_x509(Eet_File *ef, int *der_length); /** - * Get the raw signature associated with an Eet_File. Will return NULL - * if the file is not signed. + * @brief Gets the raw signature associated with an Eet_File. + * @details This function returns @c NULL if the file is not signed. * - * @param ef The file handle to query. - * @param signature_length The length of returned data, may be @c NULL. - * @return the raw signature or @c NULL on error. - * - * @ingroup Eet_Cipher_Group + * @param[in] ef The file handle to query + * @param[out] signature_length The length of returned data \n + * This may be @c NULL + * @return The raw signature, \n + * otherwise @c NULL on error */ EAPI const void * eet_identity_signature(Eet_File *ef, int *signature_length); /** - * Get the SHA1 associated with a file. Could be the one used to - * sign the data or if the data where not signed, it will be the - * SHA1 of the file. + * @brief Gets the SHA1 associated with a file. + * @since 1.2.0 * - * @param ef The file handle to query. - * @param sha1_length The length of returned data, may be @c NULL. - * @return the associated SHA1 or @c NULL on error. + * @remarks This could be used to sign the data or if the data where not signed, + * it is the SHA1 of the file. * - * @since 1.2.0 - * @ingroup Eet_Cipher_Group + * @param[in] ef The file handle to query + * @param[out] sha1_length The length of returned data \n + * This may be @c NULL. + * @return The associated SHA1, \n + * otherwise @c NULL on error */ EAPI const void * eet_identity_sha1(Eet_File *ef, int *sha1_length); /** - * Display the x509 der certificate to out. + * @brief Displays the x509 der certificate on @a out. + * @since 1.2.0 * - * @param certificate the x509 certificate to print - * @param der_length The length the certificate. - * @param out where to print. + * @remarks You need to compile signature support in EET. * - * @since 1.2.0 - * @ingroup Eet_Cipher_Group + * @param[in] certificate The x509 certificate to print + * @param[in] der_length The length of the certificate + * @param[in] out The output destination */ EAPI void eet_identity_certificate_print(const unsigned char *certificate, int der_length, FILE *out); +/** + * @} + */ /** + * @internal * @defgroup Eet_Data_Group Eet Data Serialization + * @ingroup Eet_Group * - * Convenience functions to serialize and parse complex data - * structures to binary blobs. + * @brief This group provides convenience functions to serialize and parse complex data + * structures to binary blobs. * * While Eet core just handles binary blobs, it is often required * to save some structured data of different types, such as @@ -1954,93 +2114,93 @@ eet_identity_certificate_print(const unsigned char *certificate, * defined. * * Given that C provides no introspection, this process can be - * quite cumbersome, so we provide lots of macros and convenience - * functions to aid creating the types. + * quite cumbersome, so lots of macros and convenience + * functions are provided to aid creating the types. * - * We make now a quick overview of some of the most commonly used elements + * The following is a quick overview of some of the most commonly used elements * of this part of the library. A simple example of a configuration system - * will work as a somewhat real life example that is still simple enough to + * works as a somewhat real life example that is still simple enough to * follow. - * Only the relevant sections will be shown here, but you can get the full + * Only the relevant sections are shown here, but you can get the full * code @ref eet-data-simple.c "here". * - * Ignoring the included headers, we'll begin by defining our configuration + * Ignoring the included headers, you begin by defining your configuration * struct. * @dontinclude eet-data-simple.c * @skip typedef * @until } * - * When using Eet, you don't think in matters of what data the program needs - * to run and which you would like to store. It's all the same and if it makes - * more sense to keep them together, it's perfectly fine to do so. At the time + * When using Eet, you do not think in matters of what data the program needs + * to run and which you would like to store. It is all the same and if it makes + * more sense to keep them together, it is perfectly fine to do so. At the time * of telling Eet how your data is comprised you can leave out the things * that are runtime only and let Eet take care of the rest for you. * * The key used to store the config follows, as well as the variable used to - * store our data descriptor. - * This last one is very important. It's the one thing that Eet will use to + * store the data descriptor. + * This last one is very important. It is the one thing that Eet uses to * identify your data, both at the time of writing it to the file and when * loading from it. * @skipline MY_CONF * @skipline Eet_Data_Descriptor * - * Now we'll see how to create this descriptor, so Eet knows how to handle - * our data later on. - * Begin our function by declaring an Eet_Data_Descriptor_Class, which is + * Now you see how to create this descriptor, so Eet knows how to handle + * your data later on. + * Begin the function by declaring an Eet_Data_Descriptor_Class, which is * used to create the actual descriptor. This class contains the name of - * our data type, its size and several functions that dictate how Eet should - * handle memory to allocate the necessary bits to bring our data to life. - * You, as a user, will very hardly set this class' contents directly. The + * your data type, its size and several functions that dictate how Eet should + * handle memory to allocate the necessary bits to bring your data to life. + * You, as a user, very hardly set this class' contents directly. The * most common scenario is to use one of the provided macros that set it using - * the Eina data types, so that's what we'll be doing across all our examples. + * the Eina data types, so that is what you are doing across all your examples. * @skip static void * @until eet_data_descriptor_stream_new * - * Now that we have our descriptor, we need to make it describe something. - * We do so by telling it which members of our struct we want it to know about + * Now that you have your descriptor, you need to make it describe something. + * Do so by telling it which members of your struct you want it to know about * and their types. - * The eet_data_descriptor_element_add() function takes care of this, but it's + * The eet_data_descriptor_element_add() function takes care of this, but it is * too cumbersome for normal use, so several macros are provided that make * it easier to handle. Even with them, however, code can get very repetitive - * and it's not uncommon to define custom macros using them to save on typing. + * and it is not uncommon to define custom macros using them to save on typing. * @skip #define * @until } * - * Now our descriptor knows about the parts of our structure that we are + * Now your descriptor knows about the parts of your structure that you are * interesting in saving. You can see that not all of them are there, yet Eet - * will find those that need saving and do the right thing. When loading our - * data, any non-described fields in the structure will be zeroed, so there's + * finds those that need saving and do the right thing. When loading your + * data, any non-described fields in the structure are zeroed, so there is * no need to worry about garbage memory in them. - * Refer to the documentation of #EET_DATA_DESCRIPTOR_ADD_BASIC to understand - * what our macro does. + * See the documentation of #EET_DATA_DESCRIPTOR_ADD_BASIC to understand + * what your macro does. * - * We are done with our descriptor init function and it's proper to have the + * You are done with your descriptor init function and it is proper to have the * relevant shutdown. Proper coding guidelines indiciate that all memory - * allocated should be freed when the program ends, and since you will most - * likely keep your descriptor around for the life or your application, it's + * allocated should be freed when the program ends, and since you must most + * likely keep your descriptor around for the life or your application, it is * only right to free it at the end. * @skip static void * @until } * * Not listed here, but included in the full example are functions to create - * a blank configuration and free it. The first one will only be used when + * a blank configuration and free it. The first one is only used when * no file exists to load from, or nothing is found in it, but the latter is - * used regardless of where our data comes from. Unless you are reading direct - * data from the Eet file, you will be in charge of freeing anything loaded + * used regardless of where your data comes from. Unless you are reading direct + * data from the Eet file, you are in charge of freeing anything loaded * from it. * - * Now it's time to look at how we can load our config from some file. + * Now it is time to look at how you can load your config from some file. * Begin by opening the Eet file normally. * @skip static My_Conf_Type * @until } * - * And now we need to read the data from the file and decode it using our - * descriptor. Fortunately, that's all done in one single step. + * And now you need to read the data from the file and decode it using your + * descriptor. Fortunately, that is all done in one single step. * @until goto * - * And that's it for all Eet cares about. But since we are dealing with a + * And that is it for all Eet cares about. But since you are dealing with a * common case, as is save and load of user configurations, the next fragment - * of code shows why we have a version field in our struct, and how you can + * of code shows why you have a version field in our struct, and how you can * use it to load older configuration files and update them as needed. * @until } * @@ -2059,10 +2219,10 @@ eet_identity_certificate_print(const unsigned char *certificate, * @skip return * @until } * - * To close, our main function, which doesn't do much. Just take some arguments + * To close, your main function, which does not do much. Just take some arguments * from the command line with the name of the file to load and another one - * where to save again. If input file doesn't exist, a new config structure - * will be created and saved to our output file. + * where to save again. If input file does not exist, a new config structure + * is created and saved to our output file. * @skip int main * @until return ret * @until } @@ -2072,20 +2232,23 @@ eet_identity_certificate_print(const unsigned char *certificate, * @li @ref eet_data_file_descriptor * @li @ref Example_Eet_Data_File_Descriptor_02 * @li @ref Example_Eet_Data_Cipher_Decipher + * + * @{ */ /** + * @internal * @page eet_data_nested_example Nested structures and Eet Data Descriptors * - * We've seen already a simple example of how to use Eet Data Descriptors - * to handle our structures, but it didn't show how this works when you + * You have seen already a simple example of how to use Eet Data Descriptors + * to handle your structures, but it did not show how this works when you * have structures inside other structures. * - * Now, there's a very simple case of this, for when you have inline structs - * to keep your big structure more organized, you don't need anything else + * Now, there is a very simple case of this, for when you have inline structs + * to keep your big structure more organized, you do not need anything else * besides what @ref eet-data-simple.c "this simple example does". - * Just use something like @p some_struct.sub_struct.member when adding the - * member to the descriptor and it will work. + * Just use something like @a some_struct.sub_struct.member when adding the + * member to the descriptor and it works. * * For example: * @code @@ -2115,20 +2278,20 @@ eet_identity_certificate_print(const unsigned char *certificate, * } * @endcode * - * But this is not what we are here for today. When we talk about nested - * structures, what we really want are things like lists and hashes to be + * But this is not what you are going to do. When you talk about nested + * structures, what you really want are things like lists and hashes to be * taken into consideration automatically, and all their contents saved and * loaded just like ordinary integers and strings are. * * And of course, Eet can do that, and considering the work it saves you as a - * programmer, we could say it's even easier to do than handling just integers. + * programmer, you could say it is even easier to do than handling just integers. * - * Let's begin with our example then, which is not all too different from the + * Let us begin with your example then, which is not all too different from the * simple one introduced earlier. * - * We won't ignore the headers this time to show how easy it is to use Eina - * data types with Eet, but we'll still skip most of the code that is not - * pertinent to what we want to show now, but as usual, you can get it full + * You do not ignore the headers this time to show how easy it is to use Eina + * data types with Eet, but you still skip most of the code that is not + * pertinent to what you want to show now, but as usual, you can get it full * by following @ref eet-data-nested.c "this link". * * @dontinclude eet-data-nested.c @@ -2137,123 +2300,124 @@ eet_identity_certificate_print(const unsigned char *certificate, * @skip typedef struct * @until } My_Conf_Subtype * - * Extremely similar to our previous example. Just a new struct in there, and - * a pointer to a list in the one we already had. Handling a list of subtypes - * is easy on our program, but now we'll see what Eet needs to work with them + * Extremely similar to your previous example. Just a new struct in there, and + * a pointer to a list in the one you already had. Handling a list of subtypes + * is easy on your program, but now you see what Eet needs to work with them * (Hint: it's easy too). * @skip _my_conf_descriptor * @until _my_conf_sub_descriptor * - * Since we have two structures now, it's only natural that we'll need two - * descriptors. One for each, which will be defined exactly as before. + * Since you have two structures now, it is only natural that you need two + * descriptors. One for each, which are defined exactly as before. * @skip static void * @until eddc * @skip EET_EINA_STREAM_DATA_DESCRIPTOR_CLASS_SET * @until _my_conf_sub_descriptor * - * We create our descriptors, each for one type, and as before, we are going to + * Create your descriptors, each for one type, and as before, you are going to * use a simple macro to set their contents, to save on typing. * @skip #define * @until EET_T_UCHAR * - * So far, nothing new. We have our descriptors and we know already how to - * save them separately. But what we want is to link them together, and even - * more so, we want our main type to hold a list of more than one of the new - * sub type. So how do we do that? + * So far, nothing new. You have your descriptors and you know already how to + * save them separately. But what you want is to link them together, and even + * more so, you want your main type to hold a list of more than one of the new + * sub type. So how do you do that? * - * Simple enough, we tell Eet that our main descriptor will hold a list, of - * which each node will point to some type described by our new descriptor. + * Simple enough, you tell Eet that your main descriptor holds a list, of + * which each node points to some type described by your new descriptor. * @skip EET_DATA_DESCRIPTOR_ADD_LIST * @until _my_conf_sub_descriptor * - * And that's all. We are closing the function now so as to not leave dangling - * curly braces, but there's nothing more to show in this example. Only other - * additions are the necessary code to free our new data, but you can see it + * And that is all. You are closing the function now so as to not leave dangling + * curly braces, but there is nothing more to show in this example. Only other + * additions are the necessary code to free your new data, but you can see it * in the full code listing. * @until } */ /** + * @internal * @page eet_data_file_descriptor Advanced use of Eet Data Descriptors * * A real life example is usually the best way to see how things are used, * but they also involve a lot more code than what needs to be shown, so - * instead of going that way, we'll be borrowing some pieces from one in - * the following example. It's been slightly modified from the original - * source to show more of the varied ways in which Eet can handle our data. + * instead of going that way, you are borrowing some pieces from one in + * the following example. It is slightly modified from the original + * source to show more of the varied ways in which Eet can handle your data. * * @ref eet-data-file_descriptor_01.c "This example" shows a cache of user - * accounts and messages received, and it's a bit more interactive than + * accounts and messages received, and it is a bit more interactive than * previous examples. * - * Let's begin by looking at the structures we'll be using. First we have - * one to define the messages the user receives and one for the one he posts. + * Begin by looking at the structures that you are using. First you have + * one to define the messages the user receives and one for the one the user posts. * Straight forward and nothing new here. * @dontinclude eet-data-file_descriptor_01.c * @skip typedef * @until My_Post * - * One more to declare the account itself. This one will contain a list of - * all messages received, and the posts we make ourselves will be kept in an + * One more to declare the account itself. This one contains a list of + * all messages received, and the posts you make ourselves are kept in an * array. No special reason other than to show how to use arrays with Eet. * @until My_Account * - * Finally, the main structure to hold our cache of accounts. We'll be looking - * for these accounts by their names, so let's keep them in a hash, using + * Finally, the main structure to hold your cache of accounts. You are looking + * for these accounts by their names, so keep them in a hash, using * that name as the key. * @until My_Cache * - * As explained before, we need one descriptor for each struct we want Eet - * to handle, but this time we also want to keep around our Eet file and its - * string dictionary. You will see why in a moment. + * As explained before, you need one descriptor for each struct that you want Eet + * to handle, but this time you also want to keep around your Eet file and its + * string dictionary. You see why in a moment. * @skip Eet_Data_Descriptor * @until _my_post_descriptor * @skip Eet_File * @until Eet_Dictionary * - * The differences begin now. They aren't much, but we'll be creating our - * descriptors differently. Things can be added to our cache, but we won't - * be modifying the current contents, so we can consider the data read from + * The differences begin now. They are not much, but you are creating your + * descriptors differently. Things can be added to your cache, but you are not + * modifying the current contents, so you can consider the data read from * it to be read-only, and thus allow Eet to save time and memory by not - * duplicating thins unnecessary. + * duplicating things unnecessarily. * @skip static void * @until _my_post_descriptor * - * As the comment in the code explains, we are asking Eet to give us strings + * As the comment in the code explains, you are asking Eet to give strings * directly from the mapped file, which avoids having to load it in memory * and data duplication. * Of course, there are things to take into account when doing things this - * way, and they will be mentioned as we encounter those special cases. + * way, and they are mentioned as you encounter those special cases. * - * Next comes the actual description of our data, just like we did in the + * Next comes the actual description of your data, just like you did in the * previous examples. * @skip #define * @until #undef * @until #define * @until #undef * - * And the account struct's description doesn't add much new, but it's worth + * And the account struct's description does not add much new, but it is worth * commenting on it. * @skip #define * @until _my_post_descriptor * - * How to add a list we've seen before, but now we are also adding an array. - * There's nothing really special about it, but it's important to note that + * You have seen before how to add a list, but now you are also adding an array. + * There is nothing really special about it, but it is important to note that * the EET_DATA_DESCRIPTOR_ADD_VAR_ARRAY is used to add arrays of variable - * length to a descriptor. That is, arrays just like the one we defined. - * Since there's no way in C to know how long they are, we need to keep - * track of the count ourselves and Eet needs to know how to do so as well. - * That's what the @p posts_count member of our struct is for. When adding - * our array member, this macro will look for another variable in the struct - * named just like the array, but with @p _count attached to the end. - * When saving our data, Eet will know how many elements the array contains + * length to a descriptor. That is, arrays just like the one you defined. + * Since there is no way in C to know how long they are, you need to keep + * track of the count yourselves and Eet needs to know how to do so as well. + * That is what the @a posts_count member of your struct is for. When adding + * your array member, this macro looks for another variable in the struct + * named just like the array, but with @a _count attached to the end. + * When saving your data, Eet knows how many elements the array contains * by looking into this count variable. When loading back from a file, this - * variable will be set to the right number of elements. + * variable is set to the right number of elements. * * Another option for arrays is to use EET_DATA_DESCRIPTOR_ADD_ARRAY, which * takes care of fixed sized arrays. - * For example, let's suppose that we want to keep track of only the last - * ten posts the user sent, and we declare our account struct as follows + * For example, suppose that you want to keep track of only the last + * ten posts the user sent, and declare your account struct as follows: * @code * typedef struct * { @@ -2263,92 +2427,88 @@ eet_identity_certificate_print(const unsigned char *certificate, * My_Post posts[10]; * } My_Account; * @endcode - * Then we would add the array to our descriptor with + * Then add the array to your descriptor with * @code * EET_DATA_DESCRIPTOR_ADD_ARRAY(_my_account_descriptor, My_Account, "posts", * posts, _my_post_descriptor); * @endcode * - * Notice how this time we don't have a @p posts_count variable in our struct. - * We could have it for the program to keep track of how many posts the - * array actually contains, but Eet no longer needs it. Being defined that - * way the array is already taking up all the memory needed for the ten - * elements, and it is possible in C to determine how much it is in code. - * When saving our data, Eet will just dump the entire memory blob into the - * file, regardless of how much of it is really used. So it's important to + * Notice how this time you do not have a @a posts_count variable in your struct. + * You could have it for the program to keep track of how many posts the + * array actually contains, but Eet no longer needs it. Having defined + * the way the array is already taking up all the memory needed for the ten + * elements, it is possible in C to determine how much it is in code. + * When saving your data, Eet just dumps the entire memory blob into the + * file, regardless of how much of it is really used. So it is important to * take into consideration this kind of things when defining your data types. - * Each has its uses, its advantages and disadvantages and it's up to you + * Each has its uses, its advantages and disadvantages and it is up to you * to decide which to use. * - * Now, going back to our example, we have to finish adding our data to the + * Now, going back to your example, you have to finish adding your data to the * descriptors. We are only missing the main one for the cache, which - * contains our hash of accounts. + * contains your hash of accounts. * Unless you are using your own hash functions when setting the descriptor * class, always use hashes with string type keys. * @skip #define * @until } * - * If you remember, we told Eet not to duplicate memory when possible at the - * time of loading back our data. But this doesn't mean everything will be - * loaded straight from disk and we don't have to worry about freeing it. + * If you remember, you told Eet not to duplicate memory when possible at the + * time of loading back your data. But this does not mean everything is + * loaded straight from disk and you do not have to worry about freeing it. * Data in the Eet file is compressed and encoded, so it still needs to be - * decoded and memory will be allocated to convert it back into something we - * can use. We also need to take care of anything we add in the current + * decoded and memory is allocated to convert it back into something that you + * can use. You also need to take care of anything you add in the current * instance of the program. - * To summarize, any string we get from Eet is likely to be a pointer to the - * internal dictionary, and trying to free it will, in the best case, crash - * our application right away. + * To summarize, any string you get from Eet is likely to be a pointer to the + * internal dictionary, and trying to free it, in the best case, crashes + * your application right away. * - * So how do we know if we have to free a string? We check if it's part of - * the dictionary, and if it's not there we can be sure it's safe to get + * So how do you know if you have to free a string? You check if it is part of + * the dictionary, and if it is not there, you can be sure it is safe to get * rid of it. * @skip static void * @skip } * @skip static void * @until } * - * See how this is used when adding a new message to our cache. + * See how this is used when adding a new message to your cache. * @skip static My_Message * @until return msg * @until free(msg) * @until } * - * Skipping all the utility functions used by our program (remember you can - * look at the full example @ref eet-data-file_descriptor_01.c "here") we get to - * our cache loading code. Nothing out of the ordinary at first, just the - * same old open file, read data using our main descriptor to decode it - * into something we can use and check version of loaded data and if it doesn't + * Skipping all the utility functions used by your program (remember you can + * look at the full example @ref eet-data-file_descriptor_01.c "here") you get to + * your cache loading code. Nothing out of the ordinary at first, just the + * same old open file, read data using your main descriptor to decode it + * into something you can use and check version of loaded data and if it does not * match, do something accordingly. * @skip static My_Cache * @until } * @until } * @until } * - * Then comes the interesting part. Remember how we kept two more global - * variables with our descriptors? One of them we already used to check if - * it was right to free a string or not, but we didn't know where it came from. - * Loading our data straight from the mmapped file means that we can't close - * it until we are done using it, so we need to keep its handler around until - * then. It also means that any changes done to the file can, and will, - * invalidate all our pointers to the file backed data, so if we add something - * and save the file, we need to reload our cache. - * - * Thus our load function checks if we had an open file, if there is it gets - * closed and our variable is updated to the new handler. Then we get the - * string dictionary we use to check if a string is part of it or not. - * Updating any references to the cache data is up you as a programmer to - * handle properly, there's nothing Eet can do in this situation. + * Then comes the interesting part. Remember how you kept two more global + * variables with your descriptors? One of them you already used to check if + * it is right to free a string or not, but you did not know where it came from. + * Loading your data straight from the mmapped file means that you cannot close + * it until you are done using it, so you need to keep its handler around until + * then. It also means that any changes done to the file can, and do, + * invalidate all your pointers to the file backed data, so if you add something + * and save the file, you need to reload your cache. + * + * Thus your load function checks if you had an open file, if there is, it gets + * closed and your variable is updated to the new handler. Then you get the + * string dictionary you use to check whether a string is part of it or not. + * Updating any references to the cache data is up to you as a programmer to + * handle properly; there is nothing Eet can do in this situation. * @until } * - * The save function doesn't have anything new, and all that's left after it - * is the main program, which doesn't really have anything of interest within - * the scope of what we are learning. + * The save function does not have anything new, and all that is left after it + * is the main program, which does not really have anything of interest within + * the scope of what you are learning. */ -/** - * @addtogroup Eet_Data_Group - * @{ - */ #define EET_T_UNKNOW 0 /**< Unknown data encoding type */ #define EET_T_CHAR 1 /**< Data type: char */ #define EET_T_SHORT 2 /**< Data type: short */ @@ -2366,7 +2526,7 @@ eet_identity_certificate_print(const unsigned char *certificate, #define EET_T_F32P32 14 /**< Data type: fixed point 32.32 */ #define EET_T_F16P16 15 /**< Data type: fixed point 16.16 */ #define EET_T_F8P24 16 /**< Data type: fixed point 8.24 */ -#define EET_T_LAST 18 /**< Last data type */ +#define EET_T_LAST 17 /**< Last data type */ #define EET_G_UNKNOWN 100 /**< Unknown group data encoding type */ #define EET_G_ARRAY 101 /**< Fixed size array group type */ @@ -2382,12 +2542,12 @@ eet_identity_certificate_print(const unsigned char *certificate, /** * @typedef Eet_Data_Descriptor * - * Opaque handle that have information on a type members. + * @brief The structure type containing an opaque handle that have information on a type members. * * Descriptors are created using an #Eet_Data_Descriptor_Class, and they - * describe the contents of the structure that will be serialized by Eet. - * Not all members need be described by it, just those that should be handled - * by Eet. This way it's possible to have one structure with both data to be + * describe the contents of the structure that is serialized by Eet. + * Not all members need to be described by it, just those that should be handled + * by Eet. This way it is possible to have one structure with both data to be * saved to a file, like application configuration, and runtime information * that would be meaningless to store, but is appropriate to keep together * during the program execution. @@ -2404,18 +2564,18 @@ typedef struct _Eet_Data_Descriptor Eet_Data_Descriptor; /** * @def EET_DATA_DESCRIPTOR_CLASS_VERSION - * The version of #Eet_Data_Descriptor_Class at the time of the - * distribution of the sources. One should define this to its - * version member so it is compatible with abi changes, or at least - * will not crash with them. + * @brief Definition of the version of #Eet_Data_Descriptor_Class at the time of the + * distribution of the sources. One should define this to its + * version member so it is compatible with API changes, or at least + * do not crash with them. */ #define EET_DATA_DESCRIPTOR_CLASS_VERSION 4 /** * @typedef Eet_Data_Descriptor_Class * - * Instructs Eet about memory management for different needs under - * serialization and parse process. + * @brief The structure type that instructs Eet about memory management for different needs under + * serialization and parse process. */ typedef struct _Eet_Data_Descriptor_Class Eet_Data_Descriptor_Class; @@ -2438,84 +2598,81 @@ typedef const char * (*Eet_Descriptor_Type_Get_Callback)(cons typedef Eina_Bool (*Eet_Descriptor_Type_Set_Callback)(const char *type, void *data, Eina_Bool unknow); typedef void * (*Eet_Descriptor_Array_Alloc_Callback)(size_t size); typedef void (*Eet_Descriptor_Array_Free_Callback)(void *mem); + /** + * @internal * @struct _Eet_Data_Descriptor_Class * - * Instructs Eet about memory management for different needs under - * serialization and parse process. + * @brief The structure type that instructs Eet about memory management for different needs under + * serialization and parse process. * * The list and hash methods match the Eina API, so for a more detailed * reference on them, look at the Eina_List and Eina_Hash documentation, * respectively. - * For the most part these will be used with the standard Eina functions, + * For the most part these are used with the standard Eina functions, * so using EET_EINA_STREAM_DATA_DESCRIPTOR_CLASS_SET() and - * EET_EINA_FILE_DATA_DESCRIPTOR_CLASS_SET() will set up everything + * EET_EINA_FILE_DATA_DESCRIPTOR_CLASS_SET() set up everything * accordingly. */ struct _Eet_Data_Descriptor_Class { - int version; /**< ABI version. Should always be set to #EET_DATA_DESCRIPTOR_CLASS_VERSION */ + int version; /**< API version. Should always be set to #EET_DATA_DESCRIPTOR_CLASS_VERSION */ const char *name; /**< Name of the user data type to be serialized */ int size; /**< Size in bytes of the user data type to be serialized */ struct { - Eet_Descriptor_Mem_Alloc_Callback mem_alloc; /**< how to allocate memory (usually malloc()) */ - Eet_Descriptor_Mem_Free_Callback mem_free; /**< how to free memory (usually free()) */ - Eet_Descriptor_Str_Alloc_Callback str_alloc; /**< how to allocate a string */ - Eet_Descriptor_Str_Free_Callback str_free; /**< how to free a string */ - Eet_Descriptor_List_Next_Callback list_next; /**< how to iterate to the next element of a list. Receives and should return the list node. */ - Eet_Descriptor_List_Append_Callback list_append; /**< how to append data @p d to list which head node is @p l */ - Eet_Descriptor_List_Data_Callback list_data; /**< retrieves the data from node @p l */ - Eet_Descriptor_List_Free_Callback list_free; /**< free all the nodes from the list which head node is @p l */ - Eet_Descriptor_Hash_Foreach_Callback hash_foreach; /**< iterates over all elements in the hash @p h in no specific order */ - Eet_Descriptor_Hash_Add_Callback hash_add; /**< add a new data @p d with key @p k in hash @p h */ - Eet_Descriptor_Hash_Free_Callback hash_free; /**< free all entries from the hash @p h */ - Eet_Descriptor_Str_Direct_Alloc_Callback str_direct_alloc; /**< how to allocate a string directly from file backed/mmaped region pointed by @p str */ - Eet_Descriptor_Str_Direct_Free_Callback str_direct_free; /**< how to free a string returned by str_direct_alloc */ - Eet_Descriptor_Type_Get_Callback type_get; /**< get the type, as used in the union or variant mapping, that should be used to store the given data into the eet file. */ - Eet_Descriptor_Type_Set_Callback type_set; /**< called when loading a mapped type with the given @p type used to describe the type in the descriptor */ - Eet_Descriptor_Array_Alloc_Callback array_alloc; /**< how to allocate memory for array (usually malloc()) */ - Eet_Descriptor_Array_Free_Callback array_free; /**< how to free memory for array (usually free()) */ + Eet_Descriptor_Mem_Alloc_Callback mem_alloc; /**< Called when allocating memory (usually malloc()) */ + Eet_Descriptor_Mem_Free_Callback mem_free; /**< Called when freeing memory (usually free()) */ + Eet_Descriptor_Str_Alloc_Callback str_alloc; /**< Called when allocating a string */ + Eet_Descriptor_Str_Free_Callback str_free; /**< Called when freeing a string */ + Eet_Descriptor_List_Next_Callback list_next; /**< Called when iterating to the next element of a list. Receives and should return the list node. */ + Eet_Descriptor_List_Append_Callback list_append; /**< Called when appending data @a d to list with head node @a l */ + Eet_Descriptor_List_Data_Callback list_data; /**< Called when retrieving the data from node @a l */ + Eet_Descriptor_List_Free_Callback list_free; /**< Called when freeing all the nodes from the list with head node @a l */ + Eet_Descriptor_Hash_Foreach_Callback hash_foreach; /**< Called when iterating over all elements in the hash @a h in no specific order */ + Eet_Descriptor_Hash_Add_Callback hash_add; /**< Called when adding a new data @a d with key @a k in hash @a h */ + Eet_Descriptor_Hash_Free_Callback hash_free; /**< Called when freeing all entries from the hash @a h */ + Eet_Descriptor_Str_Direct_Alloc_Callback str_direct_alloc; /**< Called when allocating a string directly from file backed or mmaped region pointed by @a str */ + Eet_Descriptor_Str_Direct_Free_Callback str_direct_free; /**< Called when freeing a string returned by str_direct_alloc */ + Eet_Descriptor_Type_Get_Callback type_get; /**< Called when getting the type, as used in the union or variant mapping, that should be used to store the given data into the eet file */ + Eet_Descriptor_Type_Set_Callback type_set; /**< Called when loading a mapped type with the given @a type used to describe the type in the descriptor */ + Eet_Descriptor_Array_Alloc_Callback array_alloc; /**< Called when allocating memory for array (usually malloc()) */ + Eet_Descriptor_Array_Free_Callback array_free; /**< Called when freeing memory for array (usually free()) */ } func; }; /** - * @} - */ - -/** - * Create a new empty data structure descriptor. - * @param name The string name of this data structure (most be a - * global constant and never change). - * @param size The size of the struct (in bytes). - * @param func_list_next The function to get the next list node. - * @param func_list_append The function to append a member to a list. - * @param func_list_data The function to get the data from a list node. - * @param func_list_free The function to free an entire linked list. - * @param func_hash_foreach The function to iterate through all - * hash table entries. - * @param func_hash_add The function to add a member to a hash table. - * @param func_hash_free The function to free an entire hash table. - * @return A new empty data descriptor. - * - * This function creates a new data descriptor and returns a handle to the - * new data descriptor. On creation it will be empty, containing no contents - * describing anything other than the shell of the data structure. - * - * You add structure members to the data descriptor using the macros - * EET_DATA_DESCRIPTOR_ADD_BASIC(), EET_DATA_DESCRIPTOR_ADD_SUB() and - * EET_DATA_DESCRIPTOR_ADD_LIST(), depending on what type of member you are - * adding to the description. + * @brief Creates a new empty data structure descriptor. + * + * @details This function creates a new data descriptor and returns a handle to the + * new data descriptor. On creation it is empty, containing no contents + * describing anything other than the shell of the data structure. + * @since 1.0.0 + * @remarks You add structure members to the data descriptor using the macros + * EET_DATA_DESCRIPTOR_ADD_BASIC(), EET_DATA_DESCRIPTOR_ADD_SUB() and + * EET_DATA_DESCRIPTOR_ADD_LIST(), depending on what type of member you are + * adding to the description. + * + * @remarks Once you have described all the members of a struct you want loaded, or + * saved eet can load and save those members for you, encode them into + * endian-independent serialised data chunks for transmission across a + * a network or more. + * + * @remarks The function pointers to the list and hash table functions are only + * needed if you use those data types, else you can pass @c NULL instead. + * + * @param name The string name of this data structure \n + * A global constant and never changes. + * @param size The size of the struct (in bytes) + * @param func_list_next The function to get the next list node + * @param func_list_append The function to append a member to a list + * @param func_list_data The function to get the data from a list node + * @param func_list_free The function to free an entire linked list + * @param func_hash_foreach The function to iterate through all hash table entries + * @param func_hash_add The function to add a member to a hash table + * @param func_hash_free The function to free an entire hash table + * @return A new empty data descriptor * - * Once you have described all the members of a struct you want loaded, or - * saved eet can load and save those members for you, encode them into - * endian-independent serialised data chunks for transmission across a - * a network or more. - * - * The function pointers to the list and hash table functions are only - * needed if you use those data types, else you can pass NULL instead. - * - * @since 1.0.0 * @ingroup Eet_Data_Group * * @deprecated use eet_data_descriptor_stream_new() or @@ -2534,7 +2691,7 @@ eet_data_descriptor_new(const char *name, /* * FIXME: * - * moving to this api from the old above. this will break things when the + * Moving to this API from the old above. this breaks things when the * move happens - but be warned */ EINA_DEPRECATED EAPI Eet_Data_Descriptor * @@ -2543,61 +2700,64 @@ EINA_DEPRECATED EAPI Eet_Data_Descriptor * eet_data_descriptor3_new(const Eet_Data_Descriptor_Class *eddc); /** - * This function creates a new data descriptor and returns a handle to the - * new data descriptor. On creation it will be empty, containing no contents - * describing anything other than the shell of the data structure. - * @param eddc The class from where to create the data descriptor. - * @return A handle to the new data descriptor. + * @brief Creates a new data descriptor and returns a handle to the + * new data descriptor. * - * You add structure members to the data descriptor using the macros - * EET_DATA_DESCRIPTOR_ADD_BASIC(), EET_DATA_DESCRIPTOR_ADD_SUB() and - * EET_DATA_DESCRIPTOR_ADD_LIST(), depending on what type of member you are - * adding to the description. + * @since 1.2.3 * - * Once you have described all the members of a struct you want loaded or - * saved, eet can load and save those members for you, encode them into - * endian-independent serialised data chunks for transmission across a - * network or more. + * @remarks On creation it is empty, containing no contents + * describing anything other than the shell of the data structure. * - * This function specially ignores str_direct_alloc and str_direct_free. It - * is useful when the eet_data you are reading doesn't have a dictionary, - * like network stream or IPC. It also mean that all string will be allocated - * and duplicated in memory. + * @remarks You add structure members to the data descriptor using the macros + * EET_DATA_DESCRIPTOR_ADD_BASIC(), EET_DATA_DESCRIPTOR_ADD_SUB() and + * EET_DATA_DESCRIPTOR_ADD_LIST(), depending on what type of member you are + * adding to the description. * - * @since 1.2.3 - * @ingroup Eet_Data_Group + * @remarks Once you have described all the members of a struct you want loaded or + * saved, eet can load and save those members for you, encode them into + * endian-independent serialised data chunks for transmission across a + * network or more. + * + * @remarks This function specially ignores str_direct_alloc and str_direct_free. It + * is useful when the eet_data you are reading does not have a dictionary, + * like network stream or IPC. It also means that all strings are allocated + * and duplicated in memory. + * + * @param[in] eddc The class from where to create the data descriptor + * @return A handle to the new data descriptor */ EAPI Eet_Data_Descriptor * eet_data_descriptor_stream_new(const Eet_Data_Descriptor_Class *eddc); /** - * This function creates a new data descriptor and returns a handle to the - * new data descriptor. On creation it will be empty, containing no contents - * describing anything other than the shell of the data structure. - * @param eddc The class from where to create the data descriptor. - * @return A handle to the new data descriptor. - * - * You add structure members to the data descriptor using the macros - * EET_DATA_DESCRIPTOR_ADD_BASIC(), EET_DATA_DESCRIPTOR_ADD_SUB() and - * EET_DATA_DESCRIPTOR_ADD_LIST(), depending on what type of member you are - * adding to the description. - * - * Once you have described all the members of a struct you want loaded or - * saved, eet can load and save those members for you, encode them into - * endian-independent serialised data chunks for transmission across a - * a network or more. - * - * This function uses str_direct_alloc and str_direct_free. It is - * useful when the eet_data you are reading come from a file and - * have a dictionary. This will reduce memory use and improve the - * possibility for the OS to page this string out. - * However, the load speed and memory saving comes with some drawbacks to keep - * in mind. If you never modify the contents of the structures loaded from - * the file, all you need to remember is that closing the eet file will make - * the strings go away. On the other hand, should you need to free a string, - * before doing so you have to verify that it's not part of the eet dictionary. - * You can do this in the following way, assuming @p ef is a valid Eet_File - * and @p str is a string loaded from said file. + * @brief Creates a new data descriptor and returns a handle to the + * new data descriptor. + * @since 1.2.3 + * + * @remarks On creation it is empty, containing no contents + * describing anything other than the shell of the data structure. + * + * @remarks You add structure members to the data descriptor using the macros + * EET_DATA_DESCRIPTOR_ADD_BASIC(), EET_DATA_DESCRIPTOR_ADD_SUB() and + * EET_DATA_DESCRIPTOR_ADD_LIST(), depending on what type of member you are + * adding to the description. + * + * @remarks Once you have described all the members of a struct you want loaded or + * saved, eet can load and save those members for you, encode them into + * endian-independent serialised data chunks for transmission across a + * a network or more. + * + * @remarks This function uses str_direct_alloc and str_direct_free. It is + * useful when the eet_data you are reading come from a file and + * have a dictionary. This reduces memory use and improve the + * possibility for the OS to page this string out. + * However, the load speed and memory saving comes with some drawbacks to keep + * in mind. If you never modify the contents of the structures loaded from + * the file, all you need to remember is that closing the eet file makes + * the strings go away. On the other hand, should you need to free a string, + * before doing so you have to verify that it is not part of the eet dictionary. + * You can do this in the following way, assuming @a ef is a valid Eet_File + * and @a str is a string loaded from said file. * * @code * void eet_string_free(Eet_File *ef, const char *str) @@ -2609,34 +2769,34 @@ eet_data_descriptor_stream_new(const Eet_Data_Descriptor_Class *eddc); * // of it, so we can't free it, just return. * return; * } - * // We assume eina_stringshare was used on the descriptor + * // We assume eina_stringshare is used on the descriptor * eina_stringshare_del(str); * } * @endcode * - * @since 1.2.3 - * @ingroup Eet_Data_Group + * @param[in] eddc The class from where to create the data descriptor + * @return A handle to the new data descriptor */ EAPI Eet_Data_Descriptor * eet_data_descriptor_file_new(const Eet_Data_Descriptor_Class *eddc); /** - * This function is an helper that set all the parameters of an - * Eet_Data_Descriptor_Class correctly when you use Eina data type - * with a stream. - * @param eddc The Eet_Data_Descriptor_Class you want to set. - * @param eddc_size The size of the Eet_Data_Descriptor_Class at the compilation time. - * @param name The name of the structure described by this class. - * @param size The size of the structure described by this class. - * @return EINA_TRUE if the structure was correctly set (The only - * reason that could make it fail is if you did give wrong - * parameter). + * @brief Sets all the parameters of an Eet_Data_Descriptor_Class correctly + * when you use Eina data type with a stream. * - * @note Unless there's a very specific reason to use this function directly, - * the EET_EINA_STREAM_DATA_DESCRIPTOR_CLASS_SET macro is recommended. + * @details This function is a helper. + * @since 1.2.3 * - * @since 1.2.3 - * @ingroup Eet_Data_Group + * @remarks Unless there is a very specific reason to use this function directly, + * use the EET_EINA_STREAM_DATA_DESCRIPTOR_CLASS_SET() macro. + * + * @param[in] eddc The Eet_Data_Descriptor_Class you want to set + * @param[in] eddc_size The size of the Eet_Data_Descriptor_Class at the compilation time + * @param[in] name The name of the structure described by this class + * @param[in] size The size of the structure described by this class + * @return @c EINA_TRUE if the structure is correctly set, \n + * otherwise @c EINA_FALSE on failure \n + * The only reason that could make it fail is if you did give wrong parameter. */ EAPI Eina_Bool eet_eina_stream_data_descriptor_class_set(Eet_Data_Descriptor_Class *eddc, @@ -2645,39 +2805,39 @@ eet_eina_stream_data_descriptor_class_set(Eet_Data_Descriptor_Class *eddc, int size); /** - * This macro is an helper that set all the parameter of an - * Eet_Data_Descriptor_Class correctly when you use Eina data type - * with stream. - * @param clas The Eet_Data_Descriptor_Class you want to set. - * @param type The type of the structure described by this class. - * @return EINA_TRUE if the structure was correctly set (The only - * reason that could make it fail is if you did give wrong - * parameter). + * @brief Sets all the parameter of an Eet_Data_Descriptor_Class correctly + * when you use Eina data type with stream. + * + * @details This is a macro. + * @since 1.2.3 + * + * @param clas The Eet_Data_Descriptor_Class you want to set + * @param type The type of the structure described by this class + * @return @c EINA_TRUE if the structure is correctly set, \n + * otherwise @c EINA_FALSE on failure \n + * The only reason that could make it fail is if you did give wrong parameter. * * @see eet_data_descriptor_stream_new - * @since 1.2.3 - * @ingroup Eet_Data_Group */ #define EET_EINA_STREAM_DATA_DESCRIPTOR_CLASS_SET(clas, type) \ (eet_eina_stream_data_descriptor_class_set(clas, sizeof (*(clas)), # type, sizeof(type))) /** - * This function is an helper that set all the parameter of an - * Eet_Data_Descriptor_Class correctly when you use Eina data type - * with a file. - * @param eddc The Eet_Data_Descriptor_Class you want to set. - * @param eddc_size The size of the Eet_Data_Descriptor_Class at the compilation time. - * @param name The name of the structure described by this class. - * @param size The size of the structure described by this class. - * @return EINA_TRUE if the structure was correctly set (The only - * reason that could make it fail is if you did give wrong - * parameter). + * @brief Sets all the parameter of an Eet_Data_Descriptor_Class correctly + * when you use Eina data type with a file. + * @details This is a helper function. + * @since 1.2.3 * - * @note Unless there's a very specific reason to use this function directly, - * the EET_EINA_FILE_DATA_DESCRIPTOR_CLASS_SET macro is recommended. + * @remarks Unless there is a very specific reason to use this function directly, + * use the EET_EINA_FILE_DATA_DESCRIPTOR_CLASS_SET macro. * - * @since 1.2.3 - * @ingroup Eet_Data_Group + * @param[in] eddc The Eet_Data_Descriptor_Class you want to set + * @param[in] eddc_size The size of the Eet_Data_Descriptor_Class at the compilation time + * @param[in] name The name of the structure described by this class + * @param[in] size The size of the structure described by this class + * @return @c EINA_TRUE if the structure is correctly set, \n + * otherwise @c EINA_FALSE on failure \n + * The only reason that could make it fail is if you did give wrong parameter. */ EAPI Eina_Bool eet_eina_file_data_descriptor_class_set(Eet_Data_Descriptor_Class *eddc, @@ -2686,61 +2846,60 @@ eet_eina_file_data_descriptor_class_set(Eet_Data_Descriptor_Class *eddc, int size); /** - * This macro is an helper that set all the parameter of an - * Eet_Data_Descriptor_Class correctly when you use Eina data type - * with file. - * @param clas The Eet_Data_Descriptor_Class you want to set. - * @param type The type of the structure described by this class. - * @return EINA_TRUE if the structure was correctly set (The only - * reason that could make it fail is if you did give wrong - * parameter). + * @brief Sets all the parameter of an Eet_Data_Descriptor_Class correctly + * when you use Eina data type with file. + * + * @details This macro is a helper. + * @since 1.2.3 + * + * @param clas The Eet_Data_Descriptor_Class you want to set + * @param type The type of the structure described by this class + * @return @c EINA_TRUE if the structure is correctly set, \n + * otherwise @c EINA_FALSE on failure \n + * The only reason that could make it fail is if you did give wrong parameter. * * @see eet_data_descriptor_file_new - * @since 1.2.3 - * @ingroup Eet_Data_Group */ #define EET_EINA_FILE_DATA_DESCRIPTOR_CLASS_SET(clas, type) \ (eet_eina_file_data_descriptor_class_set(clas, sizeof (*(clas)), # type, sizeof(type))) /** - * This function frees a data descriptor when it is not needed anymore. - * @param edd The data descriptor to free. + * @brief Frees a data descriptor when it is not needed anymore. * - * This function takes a data descriptor handle as a parameter and frees all - * data allocated for the data descriptor and the handle itself. After this - * call the descriptor is no longer valid. + * @details This function takes a data descriptor handle as a parameter and frees all + * data allocated for the data descriptor and the handle itself. After this + * call the descriptor is no longer valid. + * @since 1.0.0 * - * @since 1.0.0 - * @ingroup Eet_Data_Group + * @param[in] edd The data descriptor to free */ EAPI void eet_data_descriptor_free(Eet_Data_Descriptor *edd); /** - * This function is an internal used by macros. - * - * This function is used by macros EET_DATA_DESCRIPTOR_ADD_BASIC(), - * EET_DATA_DESCRIPTOR_ADD_SUB() and EET_DATA_DESCRIPTOR_ADD_LIST(). It is - * complex to use by hand and should be left to be used by the macros, and - * thus is not documented. - * - * @param edd The data descriptor handle to add element (member). - * @param name The name of element to be serialized. - * @param type The type of element to be serialized, like - * #EET_T_INT. If #EET_T_UNKNOW, then it is considered to be a - * group, list or hash. - * @param group_type If element type is #EET_T_UNKNOW, then the @p - * group_type will specify if it is a list (#EET_G_LIST), - * array (#EET_G_ARRAY) and so on. If #EET_G_UNKNOWN, then - * the member is a subtype (pointer to another type defined by - * another #Eet_Data_Descriptor). - * @param offset byte offset inside the source memory to be serialized. - * @param count number of elements (if #EET_G_ARRAY or #EET_G_VAR_ARRAY). - * @param counter_name variable that defines the name of number of elements. - * @param subtype If contains a subtype, then its data descriptor. - * - * @since 1.0.0 - * @ingroup Eet_Data_Group + * @brief This function is an internal used by macros. + * + * @details This function is used by macros EET_DATA_DESCRIPTOR_ADD_BASIC(), + * EET_DATA_DESCRIPTOR_ADD_SUB() and EET_DATA_DESCRIPTOR_ADD_LIST(). + * @since 1.0.0 + * + * @remarks It is complex to use by hand and should be left to be used by the macros, and + * thus is not documented. + * + * @param[in] edd The data descriptor handle to add element (member) + * @param[in] name The name of element to be serialized + * @param[in] type The type of element to be serialized, like #EET_T_INT \n + * If #EET_T_UNKNOW, then it is considered to be a + * group, list or hash. + * @param[in] group_type If element type is #EET_T_UNKNOW, then the @a + * group_type specifies if it is a list (#EET_G_LIST), + * array (#EET_G_ARRAY) and so on \n + * If #EET_G_UNKNOWN, then the member is a subtype (pointer + * to another type defined by another #Eet_Data_Descriptor). + * @param[in] offset The byte offset inside the source memory to be serialized + * @param[in] count The number of elements (if #EET_G_ARRAY or #EET_G_VAR_ARRAY) + * @param[in] counter_name The number of elements + * @param[in] subtype The data descriptor, if this contains a subtype */ EAPI void eet_data_descriptor_element_add(Eet_Data_Descriptor *edd, @@ -2754,31 +2913,30 @@ eet_data_descriptor_element_add(Eet_Data_Descriptor *edd, Eet_Data_Descriptor *subtype); /** - * Read a data structure from an eet file and decodes it. - * @param ef The eet file handle to read from. - * @param edd The data descriptor handle to use when decoding. - * @param name The key the data is stored under in the eet file. - * @return A pointer to the decoded data structure. + * @brief Reads a data structure from an eet file and decodes it. + * @since 1.0.0 * - * This function decodes a data structure stored in an eet file, returning - * a pointer to it if it decoded successfully, or NULL on failure. This - * can save a programmer dozens of hours of work in writing configuration - * file parsing and writing code, as eet does all that work for the program - * and presents a program-friendly data structure, just as the programmer - * likes. Eet can handle members being added or deleted from the data in - * storage and safely zero-fills unfilled members if they were not found - * in the data. It checks sizes and headers whenever it reads data, allowing - * the programmer to not worry about corrupt data. + * @remarks This function decodes a data structure stored in an eet file, returning + * a pointer to it if it decoded successfully, or @c NULL on failure. This + * can save a programmer dozens of hours of work in writing configuration + * file parsing and writing code, as eet does all that work for the program + * and presents a program-friendly data structure, just as the programmer + * likes. Eet can handle members being added or deleted from the data in + * storage and safely zero-fills unfilled members if they were not found + * in the data. It checks sizes and headers whenever it reads data, allowing + * the programmer to not worry about corrupt data. * - * Once a data structure has been described by the programmer with the - * fields they wish to save or load, storing or retrieving a data structure - * from an eet file, or from a chunk of memory is as simple as a single - * function call. + * @remarks Once a data structure has been described by the programmer with the + * fields they wish to save or load, storing or retrieving a data structure + * from an eet file, or from a chunk of memory is as simple as a single + * function call. * - * @see eet_data_read_cipher() + * @param[in] ef The eet file handle to read from + * @param[in] edd The data descriptor handle to use when decoding + * @param[in] name The key the data is stored under in the eet file + * @return A pointer to the decoded data structure * - * @since 1.0.0 - * @ingroup Eet_Data_Group + * @see eet_data_read_cipher() */ EAPI void * eet_data_read(Eet_File *ef, @@ -2786,23 +2944,24 @@ eet_data_read(Eet_File *ef, const char *name); /** - * Write a data structure from memory and store in an eet file. - * @param ef The eet file handle to write to. - * @param edd The data descriptor to use when encoding. - * @param name The key to store the data under in the eet file. - * @param data A pointer to the data structure to save and encode. - * @param compress Compression flags for storage. - * @return bytes written on successful write, 0 on failure. + * @brief Writes a data structure from memory and store in an eet file. * - * This function is the reverse of eet_data_read(), saving a data structure - * to an eet file. The file must have been opening in write mode and the data - * will be kept in memory until the file is either closed or eet_sync() is - * called to flush any unwritten changes. + * @since 1.0.0 * - * @see eet_data_write_cipher() + * @remarks This function is the reverse of eet_data_read(), saving a data structure + * to an eet file. The file must have been opening in write mode and the data + * is kept in memory until the file is either closed or eet_sync() is + * called to flush any unwritten changes. * - * @since 1.0.0 - * @ingroup Eet_Data_Group + * @param[in] ef The eet file handle to write to + * @param[in] edd The data descriptor to use when encoding + * @param[in] name The key to store the data under in the eet file + * @param[in] data A pointer to the data structure to save and encode + * @param[in] compress The compression flags for storage + * @return The bytes written on successful write, \n + * otherwise @c 0 on failure + * + * @see eet_data_write_cipher() */ EAPI int eet_data_write(Eet_File *ef, @@ -2814,23 +2973,17 @@ eet_data_write(Eet_File *ef, typedef void (*Eet_Dump_Callback)(void *data, const char *str); /** - * Dump an eet encoded data structure into ascii text - * @param data_in The pointer to the data to decode into a struct. - * @param size_in The size of the data pointed to in bytes. - * @param dumpfunc The function to call passed a string when new - * data is converted to text - * @param dumpdata The data to pass to the @p dumpfunc callback. - * @return 1 on success, 0 on failure - * - * This function will take a chunk of data encoded by - * eet_data_descriptor_encode() and convert it into human readable - * ascii text. It does this by calling the @p dumpfunc callback - * for all new text that is generated. This callback should append - * to any existing text buffer and will be passed the pointer @p - * dumpdata as a parameter as well as a string with new text to be - * appended. + * @brief Dumps an eet encoded data structure into ascii text. + * @details This function takes a chunk of data encoded by + * eet_data_descriptor_encode() and convert it into human readable + * ascii text. It does this by calling the @a dumpfunc callback + * for all new text that is generated. This callback should append + * to any existing text buffer and is passed the pointer @a + * dumpdata as a parameter as well as a string with new text to be + * appended. + * @since 1.0.0 * - * Example: + * @remarks The following is an example: * * @code * void output(void *data, const char *string) @@ -2855,10 +3008,15 @@ typedef void (*Eet_Dump_Callback)(void *data, const char *str); * } * @endcode * - * @see eet_data_text_dump_cipher() + * @param[in] data_in The pointer to the data to decode into a struct + * @param[in] size_in The size of the data pointed to in bytes + * @param[in] dumpfunc The function to call passed a string when new + * data is converted to text + * @param[in] dumpdata The data to pass to the @a dumpfunc callback + * @return @c 1 if the data structure is dumped into ascii successfully, \n + * otherwise @c 0 on failure * - * @since 1.0.0 - * @ingroup Eet_Data_Group + * @see eet_data_text_dump_cipher() */ EAPI int eet_data_text_dump(const void *data_in, @@ -2867,23 +3025,23 @@ eet_data_text_dump(const void *data_in, void *dumpdata); /** - * Take an ascii encoding from eet_data_text_dump() and re-encode in binary. - * @param text The pointer to the string data to parse and encode. - * @param textlen The size of the string in bytes (not including 0 - * byte terminator). - * @param size_ret This gets filled in with the encoded data blob - * size in bytes. - * @return The encoded data on success, NULL on failure. + * @brief Takes an ascii encoding from eet_data_text_dump() and re-encodes in binary. * - * This function will parse the string pointed to by @p text and return - * an encoded data lump the same way eet_data_descriptor_encode() takes an - * in-memory data struct and encodes into a binary blob. @p text is a normal - * C string. + * @details This function parses the string pointed to by @a text and return + * an encoded data lump the same way eet_data_descriptor_encode() takes an + * in-memory data struct and encodes into a binary blob. @a text is a normal + * C string. + * @since 1.0.0 * - * @see eet_data_text_undump_cipher() + * @param[in] text The pointer to the string data to parse and encode + * @param[in] textlen The size of the string in bytes (not including @c 0 + * byte terminator) + * @param[out] size_ret This gets filled in with the encoded data blob + * size in bytes + * @return The encoded data on success, \n + * otherwise @c NULL on failure * - * @since 1.0.0 - * @ingroup Eet_Data_Group + * @see eet_data_text_undump_cipher() */ EAPI void * eet_data_text_undump(const char *text, @@ -2891,27 +3049,28 @@ eet_data_text_undump(const char *text, int *size_ret); /** - * Dump an eet encoded data structure from an eet file into ascii text - * @param ef A valid eet file handle. - * @param name Name of the entry. eg: "/base/file_i_want". - * @param dumpfunc The function to call passed a string when new - * data is converted to text - * @param dumpdata The data to pass to the @p dumpfunc callback. - * @return 1 on success, 0 on failure + * @brief Dumps an eet encoded data structure from an eet file into ascii text. * - * This function will take an open and valid eet file from - * eet_open() request the data encoded by - * eet_data_descriptor_encode() corresponding to the key @p name - * and convert it into human readable ascii text. It does this by - * calling the @p dumpfunc callback for all new text that is - * generated. This callback should append to any existing text - * buffer and will be passed the pointer @p dumpdata as a parameter - * as well as a string with new text to be appended. + * @details This function takes an open and valid eet file from + * eet_open() request the data encoded by + * eet_data_descriptor_encode() corresponding to the key @a name + * and convert it into human readable ascii text. It does this by + * calling the @a dumpfunc callback for all new text that is + * generated. This callback should append to any existing text + * buffer and is passed the pointer @a dumpdata as a parameter + * as well as a string with new text to be appended. + * @since 1.0.0 * - * @see eet_data_dump_cipher() + * @param[in] ef A valid eet file handle + * @param[in] name The name of the entry \n + * For example: "/base/file_i_want". + * @param[in] dumpfunc The function to call passed a string when new + * data is converted to text + * @param[in] dumpdata The data to pass to the @a dumpfunc callback + * @return @c 1 if the data structure is dumped into ascii text successfully, \n + * otherwise @c 0 on failure * - * @since 1.0.0 - * @ingroup Eet_Data_Group + * @see eet_data_dump_cipher() */ EAPI int eet_data_dump(Eet_File *ef, @@ -2920,26 +3079,27 @@ eet_data_dump(Eet_File *ef, void *dumpdata); /** - * Take an ascii encoding from eet_data_dump() and re-encode in binary. - * @param ef A valid eet file handle. - * @param name Name of the entry. eg: "/base/file_i_want". - * @param text The pointer to the string data to parse and encode. - * @param textlen The size of the string in bytes (not including 0 - * byte terminator). - * @param compress Compression flags (1 == compress, 0 = don't compress). - * @return 1 on success, 0 on failure + * @brief Takes an ascii encoding from eet_data_dump() and re-encodes in binary. * - * This function will parse the string pointed to by @p text, - * encode it the same way eet_data_descriptor_encode() takes an - * in-memory data struct and encodes into a binary blob. + * @details This function parses the string pointed to by @a text, + * encode it the same way eet_data_descriptor_encode() takes an + * in-memory data struct and encodes into a binary blob. + * @since 1.0.0 * - * The data (optionally compressed) will be in ram, pending a flush to - * disk (it will stay in ram till the eet file handle is closed though). + * @remarks The data (optionally compressed) is in RAM, pending a flush to + * disk (it stays in RAM till the eet file handle is closed though). * - * @see eet_data_undump_cipher() + * @param[in] ef A valid eet file handle + * @param[in] name The name of the entry \n + * For example: "/base/file_i_want". + * @param[in] text The pointer to the string data to parse and encode + * @param[in] textlen The size of the string in bytes (not including @c 0 + * byte terminator) + * @param[in] compress The compression flags (1 = compress, 0 = do not compress) + * @return @c 1 if the ascii encoding is re-encoded successfully, \n + * otherwise @c 0 on failure * - * @since 1.0.0 - * @ingroup Eet_Data_Group + * @see eet_data_undump_cipher() */ EAPI int eet_data_undump(Eet_File *ef, @@ -2949,31 +3109,31 @@ eet_data_undump(Eet_File *ef, int compress); /** - * Decode a data structure from an arbitrary location in memory. - * @param edd The data descriptor to use when decoding. - * @param data_in The pointer to the data to decode into a struct. - * @param size_in The size of the data pointed to in bytes. - * @return NULL on failure, or a valid decoded struct pointer on success. + * @brief Decodes a data structure from an arbitrary location in memory. * - * This function will decode a data structure that has been encoded using - * eet_data_descriptor_encode(), and return a data structure with all its - * elements filled out, if successful, or NULL on failure. + * @details This function decodes a data structure that has been encoded using + * eet_data_descriptor_encode(), and return a data structure with all its + * elements filled out, if successful, or @c NULL on failure. + * @since 1.0.0 * - * The data to be decoded is stored at the memory pointed to by @p data_in, - * and is described by the descriptor pointed to by @p edd. The data size is - * passed in as the value to @p size_in, ande must be greater than 0 to - * succeed. + * @remarks The data to be decoded is stored at the memory pointed to by @a data_in, + * and is described by the descriptor pointed to by @a edd. The data size is + * passed in as the value to @a size_in, and must be greater than @c 0 to + * succeed. * - * This function is useful for decoding data structures delivered to the - * application by means other than an eet file, such as an IPC or socket - * connection, raw files, shared memory etc. + * @remarks This function is useful for decoding data structures delivered to the + * application by means other than an eet file, such as an IPC or socket + * connection, raw files, and shared memory. * - * Please see eet_data_read() for more information. + * @remarks See eet_data_read() for more information. * - * @see eet_data_descriptor_decode_cipher() + * @param[in] edd The data descriptor to use when decoding + * @param[in] data_in The pointer to the data to decode into a struct + * @param[in] size_in The size of the data pointed to in bytes + * @return A valid decoded struct pointer on success, \n + * otherwise @c NULL on failure * - * @since 1.0.0 - * @ingroup Eet_Data_Group + * @see eet_data_descriptor_decode_cipher() */ EAPI void * eet_data_descriptor_decode(Eet_Data_Descriptor *edd, @@ -2981,33 +3141,33 @@ eet_data_descriptor_decode(Eet_Data_Descriptor *edd, int size_in); /** - * Encode a dsata struct to memory and return that encoded data. - * @param edd The data descriptor to use when encoding. - * @param data_in The pointer to the struct to encode into data. - * @param size_ret pointer to the an int to be filled with the decoded size. - * @return NULL on failure, or a valid encoded data chunk on success. + * @brief Encodes a data struct to memory and returns that encoded data. * - * This function takes a data structutre in memory and encodes it into a - * serialised chunk of data that can be decoded again by - * eet_data_descriptor_decode(). This is useful for being able to transmit - * data structures across sockets, pipes, IPC or shared file mechanisms, - * without having to worry about memory space, machine type, endianness etc. + * @details This function takes a data structutre in memory and encodes it into a + * serialised chunk of data that can be decoded again by + * eet_data_descriptor_decode(). This is useful for being able to transmit + * data structures across sockets, pipes, IPC or shared file mechanisms, + * without having to worry about memory space, machine type, endianness, and so on. + * @since 1.0.0 * - * The parameter @p edd must point to a valid data descriptor, and - * @p data_in must point to the right data structure to encode. If not, the - * encoding may fail. + * @remarks The parameter @a edd must point to a valid data descriptor, and + * @a data_in must point to the right data structure to encode. If not, the + * encoding may fail. * - * On success a non NULL valid pointer is returned and what @p size_ret - * points to is set to the size of this decoded data, in bytes. When the - * encoded data is no longer needed, call free() on it. On failure NULL is - * returned and what @p size_ret points to is set to 0. + * @remarks On success, a non-NULL valid pointer is returned and what @a size_ret + * points to is set to the size of this decoded data, in bytes. When the + * encoded data is no longer needed, call free() on it. On failure @c NULL is + * returned and what @a size_ret points to is set to @c 0. * - * Please see eet_data_write() for more information. + * @remarks See eet_data_write() for more information. * - * @see eet_data_descriptor_encode_cipher() + * @param[in] edd The data descriptor to use when encoding + * @param[in] data_in The pointer to the struct to encode into data + * @param[out] size_ret The pointer to the an int to be filled with the decoded size + * @return A valid encoded data chunk on success, \n + * otherwise @c NULL on failure * - * @since 1.0.0 - * @ingroup Eet_Data_Group + * @see eet_data_descriptor_encode_cipher() */ EAPI void * eet_data_descriptor_encode(Eet_Data_Descriptor *edd, @@ -3015,27 +3175,26 @@ eet_data_descriptor_encode(Eet_Data_Descriptor *edd, int *size_ret); /** - * Add a basic data element to a data descriptor. - * @param edd The data descriptor to add the type to. - * @param struct_type The type of the struct. - * @param name The string name to use to encode/decode this member - * (must be a constant global and never change). - * @param member The struct member itself to be encoded. - * @param type The type of the member to encode. - * - * This macro is a convenience macro provided to add a member to - * the data descriptor @p edd. The type of the structure is - * provided as the @p struct_type parameter (for example: struct - * my_struct). The @p name parameter defines a string that will be - * used to uniquely name that member of the struct (it is suggested - * to use the struct member itself). The @p member parameter is - * the actual struct member itself (for example: values), and @p type is the - * basic data type of the member which must be one of: EET_T_CHAR, EET_T_SHORT, - * EET_T_INT, EET_T_LONG_LONG, EET_T_FLOAT, EET_T_DOUBLE, EET_T_UCHAR, - * EET_T_USHORT, EET_T_UINT, EET_T_ULONG_LONG or EET_T_STRING. - * - * @since 1.0.0 - * @ingroup Eet_Data_Group + * @brief Adds a basic data element to a data descriptor. + * + * @details This macro is a convenience macro provided to add a member to + * the data descriptor @a edd. The type of the structure is + * provided as the @a struct_type parameter (for example: struct + * my_struct). The @a name parameter defines a string that is + * used to uniquely name that member of the struct (it is suggested + * to use the struct member itself). The @a member parameter is + * the actual struct member itself (for example: values), and @a type is the + * basic data type of the member. The valid values for @a type are: EET_T_CHAR, EET_T_SHORT, + * EET_T_INT, EET_T_LONG_LONG, EET_T_FLOAT, EET_T_DOUBLE, EET_T_UCHAR, + * EET_T_USHORT, EET_T_UINT, EET_T_ULONG_LONG or EET_T_STRING. + * @since 1.0.0 + * + * @param edd The data descriptor to add the type to + * @param struct_type The type of the struct + * @param name The string name to use to encode or decode this member \n + * This must be a constant global and never change. + * @param member The struct member itself to be encoded + * @param type The type of the member to encode */ #define EET_DATA_DESCRIPTOR_ADD_BASIC(edd, struct_type, name, member, type) \ do { \ @@ -3047,22 +3206,21 @@ eet_data_descriptor_encode(Eet_Data_Descriptor *edd, } while(0) /** - * Add a sub-element type to a data descriptor - * @param edd The data descriptor to add the type to. - * @param struct_type The type of the struct. - * @param name The string name to use to encode/decode this member - * (must be a constant global and never change). - * @param member The struct member itself to be encoded. - * @param subtype The type of sub-type struct to add. + * @brief Adds a sub-element type to a data descriptor. * - * This macro lets you easily add a sub-type (a struct that's pointed to - * by this one). All the parameters are the same as for - * EET_DATA_DESCRIPTOR_ADD_BASIC(), with the @p subtype being the exception. - * This must be the data descriptor of the struct that is pointed to by - * this element. + * @details This macro lets you easily add a sub-type (a struct that is pointed to + * by this one). All the parameters are the same as for + * EET_DATA_DESCRIPTOR_ADD_BASIC(), with the @a subtype being the exception. + * This must be the data descriptor of the struct that is pointed to by + * this element. + * @since 1.0.0 * - * @since 1.0.0 - * @ingroup Eet_Data_Group + * @param edd The data descriptor to add the type to + * @param struct_type The type of the struct + * @param name The string name to use to encode or decode this member \n + * This must be a constant global and never changes. + * @param member The struct member itself to be encoded + * @param subtype The type of sub-type struct to add */ #define EET_DATA_DESCRIPTOR_ADD_SUB(edd, struct_type, name, member, subtype) \ do { \ @@ -3074,21 +3232,20 @@ eet_data_descriptor_encode(Eet_Data_Descriptor *edd, } while (0) /** - * Add a linked list type to a data descriptor - * @param edd The data descriptor to add the type to. - * @param struct_type The type of the struct. - * @param name The string name to use to encode/decode this member - * (must be a constant global and never change). - * @param member The struct member itself to be encoded. - * @param subtype The type of linked list member to add. + * @brief Adds a linked list type to a data descriptor. * - * This macro lets you easily add a linked list of other data types. All the - * parameters are the same as for EET_DATA_DESCRIPTOR_ADD_BASIC(), with the - * @p subtype being the exception. This must be the data descriptor of the - * element that is in each member of the linked list to be stored. + * @details This macro lets you easily add a linked list of other data types. All the + * parameters are the same as for EET_DATA_DESCRIPTOR_ADD_BASIC(), with the + * @a subtype being the exception. This must be the data descriptor of the + * element that is in each member of the linked list to be stored. + * @since 1.0.0 * - * @since 1.0.0 - * @ingroup Eet_Data_Group + * @param edd The data descriptor to add the type to + * @param struct_type The type of the struct + * @param name The string name to use to encode or decode this member \n + * This must be a constant global and never changes. + * @param member The struct member itself to be encoded + * @param subtype The type of linked list member to add */ #define EET_DATA_DESCRIPTOR_ADD_LIST(edd, struct_type, name, member, subtype) \ do { \ @@ -3100,18 +3257,17 @@ eet_data_descriptor_encode(Eet_Data_Descriptor *edd, } while (0) /** - * Add a linked list of string to a data descriptor - * @param edd The data descriptor to add the type to. - * @param struct_type The type of the struct. - * @param name The string name to use to encode/decode this member - * (must be a constant global and never change). - * @param member The struct member itself to be encoded. + * @brief Adds a linked list of string to a data descriptor. * - * This macro lets you easily add a linked list of char *. All the - * parameters are the same as for EET_DATA_DESCRIPTOR_ADD_BASIC(). + * @details This macro lets you easily add a linked list of char *. All the + * parameters are the same as for EET_DATA_DESCRIPTOR_ADD_BASIC(). + * @since 1.5.0 * - * @since 1.5.0 - * @ingroup Eet_Data_Group + * @param edd The data descriptor to add the type to + * @param struct_type The type of the struct + * @param name The string name to use to encode or decode this member \n + * This must be a constant global and never changes. + * @param member The struct member itself to be encoded */ #define EET_DATA_DESCRIPTOR_ADD_LIST_STRING(edd, struct_type, name, member) \ do { \ @@ -3123,22 +3279,21 @@ eet_data_descriptor_encode(Eet_Data_Descriptor *edd, } while (0) /** - * Add a hash type to a data descriptor - * @param edd The data descriptor to add the type to. - * @param struct_type The type of the struct. - * @param name The string name to use to encode/decode this member - * (must be a constant global and never change). - * @param member The struct member itself to be encoded. - * @param subtype The type of hash member to add. + * @brief Adds a hash type to a data descriptor. * - * This macro lets you easily add a hash of other data types. All the - * parameters are the same as for EET_DATA_DESCRIPTOR_ADD_BASIC(), with the - * @p subtype being the exception. This must be the data descriptor of the - * element that is in each member of the hash to be stored. - * The hash keys must be strings. + * @details This macro lets you easily add a hash of other data types. All the + * parameters are the same as for EET_DATA_DESCRIPTOR_ADD_BASIC(), with the + * @a subtype being the exception. This must be the data descriptor of the + * element that is in each member of the hash to be stored. + * The hash keys must be strings. + * @since 1.0.0 * - * @since 1.0.0 - * @ingroup Eet_Data_Group + * @param edd The data descriptor to add the type to + * @param struct_type The type of the struct + * @param name The string name to use to encode or decode this member \n + * This must be a constant global and never changes. + * @param member The struct member itself to be encoded + * @param subtype The type of hash member to add */ #define EET_DATA_DESCRIPTOR_ADD_HASH(edd, struct_type, name, member, subtype) \ do { \ @@ -3150,18 +3305,17 @@ eet_data_descriptor_encode(Eet_Data_Descriptor *edd, } while (0) /** - * Add a hash of string to a data descriptor - * @param edd The data descriptor to add the type to. - * @param struct_type The type of the struct. - * @param name The string name to use to encode/decode this member - * (must be a constant global and never change). - * @param member The struct member itself to be encoded. + * @brief Adds a hash of string to a data descriptor. * - * This macro lets you easily add a hash of string elements. All the - * parameters are the same as for EET_DATA_DESCRIPTOR_ADD_HASH(). + * @details This macro lets you easily add a hash of string elements. All the + * parameters are the same as for EET_DATA_DESCRIPTOR_ADD_HASH(). + * @since 1.3.4 * - * @since 1.3.4 - * @ingroup Eet_Data_Group + * @param edd The data descriptor to add the type to + * @param struct_type The type of the struct + * @param name The string name to use to encode or decode this member \n + * This must be a constant global and never changes. + * @param member The struct member itself to be encoded */ #define EET_DATA_DESCRIPTOR_ADD_HASH_STRING(edd, struct_type, name, member) \ do { \ @@ -3173,22 +3327,21 @@ eet_data_descriptor_encode(Eet_Data_Descriptor *edd, } while (0) /** - * Add an array of basic data elements to a data descriptor. - * @param edd The data descriptor to add the type to. - * @param struct_type The type of the struct. - * @param name The string name to use to encode/decode this member - * (must be a constant global and never change). - * @param member The struct member itself to be encoded. - * @param type The type of the member to encode. + * @brief Adds an array of basic data elements to a data descriptor. * - * This macro lets you easily add a fixed size array of basic data - * types. All the parameters are the same as for - * EET_DATA_DESCRIPTOR_ADD_BASIC(). - * The array must be defined with a fixed size in the declaration of the - * struct containing it. + * @details This macro lets you easily add a fixed size array of basic data + * types. All the parameters are the same as for + * EET_DATA_DESCRIPTOR_ADD_BASIC(). + * The array must be defined with a fixed size in the declaration of the + * struct containing it. + * @since 1.5.0 * - * @since 1.5.0 - * @ingroup Eet_Data_Group + * @param edd The data descriptor to add the type to + * @param struct_type The type of the struct + * @param name The string name to use to encode or decode this member \n + * This must be a constant global and never changes. + * @param member The struct member itself to be encoded + * @param type The type of the member to encode */ #define EET_DATA_DESCRIPTOR_ADD_BASIC_ARRAY(edd, struct_type, name, member, type) \ do { \ @@ -3202,24 +3355,23 @@ eet_data_descriptor_encode(Eet_Data_Descriptor *edd, } while(0) /** - * Add a variable array of basic data elements to a data descriptor. - * @param edd The data descriptor to add the type to. - * @param struct_type The type of the struct. - * @param name The string name to use to encode/decode this member - * (must be a constant global and never change). - * @param member The struct member itself to be encoded. - * @param type The type of the member to encode. + * @brief Adds a variable array of basic data elements to a data descriptor. * - * This macro lets you easily add a variable size array of basic data - * types. All the parameters are the same as for - * EET_DATA_DESCRIPTOR_ADD_BASIC(). This assumes you have - * a struct member (of type EET_T_INT) called member_count (note the - * _count appended to the member) that holds the number of items in - * the array. This array will be allocated separately to the struct it - * is in. + * @details This macro lets you easily add a variable size array of basic data + * types. All the parameters are the same as for + * EET_DATA_DESCRIPTOR_ADD_BASIC(). This assumes you have + * a struct member (of type EET_T_INT) called member_count (note the + * _count appended to the member) that holds the number of items in + * the array. This array is allocated separately to the struct it is in. * - * @since 1.6.0 - * @ingroup Eet_Data_Group + * @since 1.6.0 + * + * @param edd The data descriptor to add the type to + * @param struct_type The type of the struct + * @param name The string name to use to encode or decode this member \n + * This must be a constant global and never changes. + * @param member The struct member itself to be encoded + * @param type The type of the member to encode */ #define EET_DATA_DESCRIPTOR_ADD_BASIC_VAR_ARRAY(edd, struct_type, name, member, type) \ do { \ @@ -3234,24 +3386,23 @@ eet_data_descriptor_encode(Eet_Data_Descriptor *edd, } while(0) /** - * Add a fixed size array type to a data descriptor - * @param edd The data descriptor to add the type to. - * @param struct_type The type of the struct. - * @param name The string name to use to encode/decode this member - * (must be a constant global and never change). - * @param member The struct member itself to be encoded. - * @param subtype The type of hash member to add. + * @brief Adds a fixed size array type to a data descriptor. * - * This macro lets you easily add a fixed size array of other data - * types. All the parameters are the same as for - * EET_DATA_DESCRIPTOR_ADD_BASIC(), with the @p subtype being the - * exception. This must be the data descriptor of the element that - * is in each member of the array to be stored. - * The array must be defined with a fixed size in the declaration of the - * struct containing it. + * @details This macro lets you easily add a fixed size array of other data + * types. All the parameters are the same as for + * EET_DATA_DESCRIPTOR_ADD_BASIC(), with the @a subtype being the + * exception. This must be the data descriptor of the element that + * is in each member of the array to be stored. + * The array must be defined with a fixed size in the declaration of the + * struct containing it. + * @since 1.0.2 * - * @since 1.0.2 - * @ingroup Eet_Data_Group + * @param edd The data descriptor to add the type to + * @param struct_type The type of the struct + * @param name The string name to use to encode or decode this member \n + * This must be a constant global and never changes. + * @param member The struct member itself to be encoded + * @param subtype The type of hash member to add */ #define EET_DATA_DESCRIPTOR_ADD_ARRAY(edd, struct_type, name, member, subtype) \ do { \ @@ -3264,26 +3415,24 @@ eet_data_descriptor_encode(Eet_Data_Descriptor *edd, } while (0) /** - * Add a variable size array type to a data descriptor - * @param edd The data descriptor to add the type to. - * @param struct_type The type of the struct. - * @param name The string name to use to encode/decode this member - * (must be a constant global and never change). - * @param member The struct member itself to be encoded. - * @param subtype The type of hash member to add. - * - * This macro lets you easily add a variable size array of other data - * types. All the parameters are the same as for - * EET_DATA_DESCRIPTOR_ADD_BASIC(), with the @p subtype being the - * exception. This must be the data descriptor of the element that - * is in each member of the array to be stored. This assumes you have - * a struct member (of type EET_T_INT) called member_count (note the - * _count appended to the member) that holds the number of items in - * the array. This array will be allocated separately to the struct it - * is in. - * - * @since 1.0.2 - * @ingroup Eet_Data_Group + * @brief Adds a variable size array type to a data descriptor. + * + * @details This macro lets you easily add a variable size array of other data + * types. All the parameters are the same as for + * EET_DATA_DESCRIPTOR_ADD_BASIC(), with the @a subtype being the + * exception. This must be the data descriptor of the element that + * is in each member of the array to be stored. This assumes you have + * a struct member (of type EET_T_INT) called member_count (note the + * _count appended to the member) that holds the number of items in + * the array. This array is allocated separately to the struct it is in. + * @since 1.0.2 + * + * @param edd The data descriptor to add the type to + * @param struct_type The type of the struct + * @param name The string name to use to encode or decode this member \n + * This must be a constant global and never changes. + * @param member The struct member itself to be encoded + * @param subtype The type of hash member to add */ #define EET_DATA_DESCRIPTOR_ADD_VAR_ARRAY(edd, struct_type, name, member, subtype) \ do { \ @@ -3301,18 +3450,17 @@ eet_data_descriptor_encode(Eet_Data_Descriptor *edd, } while (0) /** - * Add a variable size array type to a data descriptor - * @param edd The data descriptor to add the type to. - * @param struct_type The type of the struct. - * @param name The string name to use to encode/decode this member - * (must be a constant global and never change). - * @param member The struct member itself to be encoded. + * @brief Adds a variable size array type to a data descriptor. * - * This macro lets you easily add a variable size array of strings. All - * the parameters are the same as for EET_DATA_DESCRIPTOR_ADD_BASIC(). + * @details This macro lets you easily add a variable size array of strings. All + * the parameters are the same as for EET_DATA_DESCRIPTOR_ADD_BASIC(). + * @since 1.4.0 * - * @since 1.4.0 - * @ingroup Eet_Data_Group + * @param edd The data descriptor to add the type to + * @param struct_type The type of the struct + * @param name The string name to use to encode or decode this member \n + * This must be a constant global and never changes. + * @param member The struct member itself to be encoded */ #define EET_DATA_DESCRIPTOR_ADD_VAR_ARRAY_STRING(edd, struct_type, name, member) \ do { \ @@ -3330,22 +3478,22 @@ eet_data_descriptor_encode(Eet_Data_Descriptor *edd, } while (0) /** - * Add an union type to a data descriptor - * @param edd The data descriptor to add the type to. - * @param struct_type The type of the struct. - * @param name The string name to use to encode/decode this member - * (must be a constant global and never change). - * @param member The struct member itself to be encoded. - * @param type_member The member that give hints on what is in the union. - * @param unified_type Describe all possible type the union could handle. + * @brief Adds a union type to a data descriptor. * - * This macro lets you easily add an union with a member that specify what is inside. - * The @p unified_type is an Eet_Data_Descriptor, but only the entry that match the name - * returned by type_get will be used for each serialized data. The type_get and type_set - * callback of unified_type should be defined. + * @details This macro lets you easily add a union with a member that specifies what is inside. + * The @a unified_type is an Eet_Data_Descriptor, but only the entry that matches the name + * returned by type_get is used for each serialized data. The type_get and type_set + * callback of @a unified_type should be defined. + * @since 1.2.4 + * + * @param edd The data descriptor to add the type to + * @param struct_type The type of the struct + * @param name The string name to use to encode or decode this member \n + * This must be a constant global and never changes. + * @param member The struct member itself to be encoded + * @param type_member The member that give hints on what is in the union + * @param unified_type The union type * - * @since 1.2.4 - * @ingroup Eet_Data_Group * @see Eet_Data_Descriptor_Class */ #define EET_DATA_DESCRIPTOR_ADD_UNION(edd, struct_type, name, member, type_member, unified_type) \ @@ -3360,24 +3508,24 @@ eet_data_descriptor_encode(Eet_Data_Descriptor *edd, } while (0) /** - * Add a automatically selectable type to a data descriptor - * @param edd The data descriptor to add the type to. - * @param struct_type The type of the struct. - * @param name The string name to use to encode/decode this member - * (must be a constant global and never change). - * @param member The struct member itself to be encoded. - * @param type_member The member that give hints on what is in the union. - * @param unified_type Describe all possible type the union could handle. + * @brief Adds an automatically selectable type to a data descriptor. * - * This macro lets you easily define what the content of @p member points to depending of - * the content of @p type_member. The type_get and type_set callback of unified_type should - * be defined. If the the type is not know at the time of restoring it, eet will still call - * type_set of @p unified_type but the pointer will be set to a serialized binary representation - * of what eet know. This make it possible, to save this pointer again by just returning the string - * given previously and telling it by setting unknow to EINA_TRUE. + * @details This macro lets you easily define what the content of @a member points to depending of + * the content of @a type_member. The type_get and type_set callback of @a unified_type should + * be defined. If the the type is not known at the time of restoring it, eet still calls + * type_set of @a unified_type but the pointer is set to a serialized binary representation + * of what eet know. This makes it possible, to save this pointer again by just returning the string + * given previously and telling it by setting unknown to @c EINA_TRUE. + * @since 1.2.4 + * + * @param edd The data descriptor to add the type to + * @param struct_type The type of the struct + * @param name The string name to use to encode or decode this member \n + * This must be a constant global and never changes. + * @param member The struct member itself to be encoded. + * @param type_member The member that give hints on what is in the union. + * @param unified_type Describe all possible type the union could handle. * - * @since 1.2.4 - * @ingroup Eet_Data_Group * @see Eet_Data_Descriptor_Class */ #define EET_DATA_DESCRIPTOR_ADD_VARIANT(edd, struct_type, name, member, type_member, unified_type) \ @@ -3392,13 +3540,11 @@ eet_data_descriptor_encode(Eet_Data_Descriptor *edd, } while (0) /** - * Add a mapping to a data descriptor that will be used by union, variant or inherited type - * @param unified_type The data descriptor to add the mapping to. - * @param name The string name to get/set type. - * @param subtype The matching data descriptor. - * - * @since 1.2.4 - * @ingroup Eet_Data_Group + * brief Adds a mapping to a data descriptor that is used by union, variant or inherited type. + * @since 1.2.4 + * @param unified_type The data descriptor to add the mapping to + * @param name The string name to get/set type + * @param subtype The matching data descriptor * @see Eet_Data_Descriptor_Class */ #define EET_DATA_DESCRIPTOR_ADD_MAPPING(unified_type, name, subtype) \ @@ -3412,43 +3558,48 @@ eet_data_descriptor_encode(Eet_Data_Descriptor *edd, subtype) /** + * @} + */ + +/** + * @internal * @defgroup Eet_Data_Cipher_Group Eet Data Serialization using A Ciphers + * @ingroup Eet_Data_Group * * Most of the @ref Eet_Data_Group have alternative versions that * accounts for ciphers to protect their content. * * @see @ref Eet_Cipher_Group * - * @ingroup Eet_Data_Group + * @{ */ /** - * Read a data structure from an eet file and decodes it using a cipher. - * @param ef The eet file handle to read from. - * @param edd The data descriptor handle to use when decoding. - * @param name The key the data is stored under in the eet file. - * @param cipher_key The key to use as cipher. - * @return A pointer to the decoded data structure. + * @brief Reads a data structure from an eet file and decodes it using a cipher. * - * This function decodes a data structure stored in an eet file, returning - * a pointer to it if it decoded successfully, or NULL on failure. This - * can save a programmer dozens of hours of work in writing configuration - * file parsing and writing code, as eet does all that work for the program - * and presents a program-friendly data structure, just as the programmer - * likes. Eet can handle members being added or deleted from the data in - * storage and safely zero-fills unfilled members if they were not found - * in the data. It checks sizes and headers whenever it reads data, allowing - * the programmer to not worry about corrupt data. + * @details This function decodes a data structure stored in an eet file, returning + * a pointer to it if it decoded successfully, or @c NULL on failure. This + * can save a programmer dozens of hours of work in writing configuration + * file parsing and writing code, as eet does all that work for the program + * and presents a program-friendly data structure, just as the programmer + * likes. Eet can handle members being added or deleted from the data in + * storage and safely zero-fills unfilled members if they were not found + * in the data. It checks sizes and headers whenever it reads data, allowing + * the programmer to not worry about corrupt data. + * @since 1.0.0 * - * Once a data structure has been described by the programmer with the - * fields they wish to save or load, storing or retrieving a data structure - * from an eet file, or from a chunk of memory is as simple as a single - * function call. + * @remarks Once a data structure has been described by the programmer with the + * fields they wish to save or load, storing or retrieving a data structure + * from an eet file, or from a chunk of memory is as simple as a single + * function call. * - * @see eet_data_read() + * @param[in] ef The eet file handle to read from + * @param[in] edd The data descriptor handle to use when decoding + * @param[in] name The key the data is stored under in the eet file + * @param[in] cipher_key The key to use as cipher + * @return A pointer to the decoded data structure * - * @since 1.0.0 - * @ingroup Eet_Data_Cipher_Group + * @see eet_data_read() */ EAPI void * eet_data_read_cipher(Eet_File *ef, @@ -3457,27 +3608,65 @@ eet_data_read_cipher(Eet_File *ef, const char *cipher_key); /** - * Read a data structure from an eet extended attribute and decodes it using a cipher. - * @param filename The file to extract the extended attribute from. - * @param attribute The attribute to get the data from. - * @param edd The data descriptor handle to use when decoding. - * @param cipher_key The key to use as cipher. - * @return A pointer to the decoded data structure. + * @brief Reads a data structure from an eet file and decodes it into a buffer using a cipher. + * + * @details This function decodes a data structure stored in an eet file, returning + * a pointer to it if it decoded successfully, or NULL on failure. This + * can save a programmer dozens of hours of work in writing configuration + * file parsing and writing code, as eet does all that work for the program + * and presents a program-friendly data structure, just as the programmer + * likes. Eet can handle members being added or deleted from the data in + * storage and safely zero-fills unfilled members if they were not found + * in the data. It checks sizes and headers whenever it reads data, allowing + * the programmer to not worry about corrupt data. + * @since 1.10.0 + * + * @remarks Once a data structure has been described by the programmer with the + * fields they wish to save or load, storing or retrieving a data structure + * from an eet file, or from a chunk of memory is as simple as a single + * function call. + * + * @param[in] ef The eet file handle to read from + * @param[in] edd The data descriptor handle to use when decoding + * @param[in] name The key the data is stored under in the eet file + * @param[in] cipher_key The key to use as cipher + * @param[in] buffer The buffer + * @param[in] buffer_size The buffer size + * @return A pointer to buffer if successful, \n + * otherwise @c NULL on error * - * This function decodes a data structure stored in an eet extended attribute, - * returning a pointer to it if it decoded successfully, or NULL on failure. - * Eet can handle members being added or deleted from the data in - * storage and safely zero-fills unfilled members if they were not found - * in the data. It checks sizes and headers whenever it reads data, allowing - * the programmer to not worry about corrupt data. - * - * Once a data structure has been described by the programmer with the - * fields they wish to save or load, storing or retrieving a data structure - * from an eet file, from a chunk of memory or from an extended attribute - * is as simple as a single function call. - * - * @since 1.5.0 - * @ingroup Eet_Data_Cipher_Group + * @see eet_data_read_cipher() + */ +EAPI void * +eet_data_read_cipher_buffer(Eet_File *ef, + Eet_Data_Descriptor *edd, + const char *name, + const char *cipher_key, + char *buffer, + int buffer_size); + +/** + * @brief Reads a data structure from an eet extended attribute and decodes it using a cipher. + * + * @details This function decodes a data structure stored in an eet extended attribute, + * returning a pointer to it if it decoded successfully, or @c NULL on failure. + * Eet can handle members being added or deleted from the data in + * storage and safely zero-fills unfilled members if they were not found + * in the data. It checks sizes and headers whenever it reads data, allowing + * you to not worry about corrupt data. + * @since 1.5.0 + * + * @remarks Once you have described a data structure with the + * fields you wish to save or load, storing or retrieving a data structure + * from an eet file, from a chunk of memory or from an extended attribute + * is as simple as a single function call. + * + * @param[in] filename The file to extract the extended attribute from + * @param[in] attribute The attribute to get the data from + * @param[in] edd The data descriptor handle to use when decoding + * @param[in] cipher_key The key to use as cipher + * @return A pointer to the decoded data structure, \n + * otherwise @c NULL on failure */ EAPI void * eet_data_xattr_cipher_get(const char *filename, @@ -3486,21 +3675,22 @@ eet_data_xattr_cipher_get(const char *filename, const char *cipher_key); /** - * Write a data structure from memory and store in an eet file - * using a cipher. - * @param ef The eet file handle to write to. - * @param edd The data descriptor to use when encoding. - * @param name The key to store the data under in the eet file. - * @param cipher_key The key to use as cipher. - * @param data A pointer to the data structure to save and encode. - * @param compress Compression flags for storage. - * @return bytes written on successful write, 0 on failure. + * @brief Writes a data structure from memory and store in an eet file + * using a cipher. * - * This function is the reverse of eet_data_read_cipher(), saving a data structure - * to an eet file. + * @details This function is the reverse of eet_data_read_cipher(), saving a data structure + * to an eet file. * - * @since 1.0.0 - * @ingroup Eet_Data_Cipher_Group + * @since 1.0.0 + * + * @param[in] ef The eet file handle to write to + * @param[in] edd The data descriptor to use when encoding + * @param[in] name The key to store the data under in the eet file + * @param[in] cipher_key The key to use as cipher + * @param[in] data A pointer to the data structure to save and encode + * @param[in] compress The compression flags for storage + * @return The bytes written on successful write, \n + * otherwise @c 0 on failure */ EAPI int eet_data_write_cipher(Eet_File *ef, @@ -3511,21 +3701,21 @@ eet_data_write_cipher(Eet_File *ef, int compress); /** - * Write a data structure from memory and store in an eet extended attribute - * using a cipher. - * @param filename The file to write the extended attribute to. - * @param attribute The attribute to store the data to. - * @param edd The data descriptor to use when encoding. - * @param cipher_key The key to use as cipher. - * @param data A pointer to the data structure to save and encode. - * @param flags The policy to use when setting the data. - * @return EINA_TRUE on success, EINA_FALSE on failure. + * @brief Writes a data structure from memory and store in an eet extended attribute + * using a cipher. * - * This function is the reverse of eet_data_xattr_cipher_get(), saving a data structure - * to an eet extended attribute. + * @details This function is the reverse of eet_data_xattr_cipher_get(), saving a data structure + * to an eet extended attribute. + * @since 1.5.0 * - * @since 1.5.0 - * @ingroup Eet_Data_Cipher_Group + * @param[in] filename The file to write the extended attribute to + * @param[in] attribute The attribute to store the data to + * @param[in] edd The data descriptor to use when encoding + * @param[in] cipher_key The key to use as cipher + * @param[in] data A pointer to the data structure to save and encode + * @param[in] flags The policy to use when setting the data + * @return @c EINA_TRUE if structure is written successfully, \n + * otherwise @c EINA_FALSE on failure */ EAPI Eina_Bool eet_data_xattr_cipher_set(const char *filename, @@ -3536,24 +3726,18 @@ eet_data_xattr_cipher_set(const char *filename, Eina_Xattr_Flags flags); /** - * Dump an eet encoded data structure into ascii text using a cipher. - * @param data_in The pointer to the data to decode into a struct. - * @param cipher_key The key to use as cipher. - * @param size_in The size of the data pointed to in bytes. - * @param dumpfunc The function to call passed a string when new - * data is converted to text - * @param dumpdata The data to pass to the @p dumpfunc callback. - * @return 1 on success, 0 on failure + * @brief Dumps an eet encoded data structure into ascii text using a cipher. * - * This function will take a chunk of data encoded by - * eet_data_descriptor_encode() and convert it into human readable - * ascii text. It does this by calling the @p dumpfunc callback - * for all new text that is generated. This callback should append - * to any existing text buffer and will be passed the pointer @p - * dumpdata as a parameter as well as a string with new text to be - * appended. + * @details This function takes a chunk of data encoded by + * eet_data_descriptor_encode() and convert it into human readable + * ascii text. It does this by calling the @a dumpfunc callback + * for all new text that is generated. This callback should append + * to any existing text buffer and is passed the pointer @a + * dumpdata as a parameter as well as a string with new text to be + * appended. + * @since 1.0.0 * - * Example: + * @remarks The following is an example: * * @code * void output(void *data, const char *string) @@ -3578,10 +3762,16 @@ eet_data_xattr_cipher_set(const char *filename, * } * @endcode * - * @see eet_data_text_dump() + * @param[in] data_in The pointer to the data to decode into a struct + * @param[in] cipher_key The key to use as cipher + * @param[in] size_in The size of the data pointed to in bytes + * @param[in] dumpfunc The function to call passed a string when new + * data is converted to text + * @param[in] dumpdata The data to pass to the @a dumpfunc callback + * @return @c 1 if the structure is dumped into ascii successfully, \n + * otherwise @c 0 on failure * - * @since 1.0.0 - * @ingroup Eet_Data_Cipher_Group + * @see eet_data_text_dump() */ EAPI int eet_data_text_dump_cipher(const void *data_in, @@ -3591,25 +3781,23 @@ eet_data_text_dump_cipher(const void *data_in, void *dumpdata); /** - * Take an ascii encoding from eet_data_text_dump() and re-encode - * in binary using a cipher. - * @param text The pointer to the string data to parse and encode. - * @param cipher_key The key to use as cipher. - * @param textlen The size of the string in bytes (not including 0 - * byte terminator). - * @param size_ret This gets filled in with the encoded data blob - * size in bytes. - * @return The encoded data on success, NULL on failure. + * @brief Takes an ascii encoding from eet_data_text_dump() and re-encodes + * in binary using a cipher. * - * This function will parse the string pointed to by @p text and return - * an encoded data lump the same way eet_data_descriptor_encode() takes an - * in-memory data struct and encodes into a binary blob. @p text is a normal - * C string. + * @details This function parses the string pointed to by @a text and return + * an encoded data lump the same way eet_data_descriptor_encode() takes an + * in-memory data struct and encodes into a binary blob. @a text is a normal + * C string. + * @since 1.0.0 * - * @see eet_data_text_undump() + * @param[in] text The pointer to the string data to parse and encode + * @param[in] cipher_key The key to use as cipher + * @param[in] textlen The size of the string in bytes (not including @c 0 byte terminator) + * @param[out] size_ret This gets filled in with the encoded data blob size in bytes + * @return The encoded data, \n + * otherwise @c NULL on failure * - * @since 1.0.0 - * @ingroup Eet_Data_Cipher_Group + * @see eet_data_text_undump() */ EAPI void * eet_data_text_undump_cipher(const char *text, @@ -3618,29 +3806,30 @@ eet_data_text_undump_cipher(const char *text, int *size_ret); /** - * Dump an eet encoded data structure from an eet file into ascii - * text using a cipher. - * @param ef A valid eet file handle. - * @param name Name of the entry. eg: "/base/file_i_want". - * @param cipher_key The key to use as cipher. - * @param dumpfunc The function to call passed a string when new - * data is converted to text - * @param dumpdata The data to pass to the @p dumpfunc callback. - * @return 1 on success, 0 on failure - * - * This function will take an open and valid eet file from - * eet_open() request the data encoded by - * eet_data_descriptor_encode() corresponding to the key @p name - * and convert it into human readable ascii text. It does this by - * calling the @p dumpfunc callback for all new text that is - * generated. This callback should append to any existing text - * buffer and will be passed the pointer @p dumpdata as a parameter - * as well as a string with new text to be appended. + * @brief Dumps an eet encoded data structure from an eet file into ascii + * text using a cipher. + * + * @details This function takes an open and valid eet file from + * eet_open() request the data encoded by + * eet_data_descriptor_encode() corresponding to the key @a name + * and convert it into human readable ascii text. It does this by + * calling the @a dumpfunc callback for all new text that is + * generated. This callback should append to any existing text + * buffer and is passed the pointer @a dumpdata as a parameter + * as well as a string with new text to be appended. + * @since 1.0.0 + * + * @param[in] ef A valid eet file handle + * @param[in] name The name of the entry \n + * For example: "/base/file_i_want". + * @param[in] cipher_key The key to use as cipher + * @param[in] dumpfunc The function to call passed a string when new + * data is converted to text + * @param[in] dumpdata The data to pass to the @a dumpfunc callback + * @return @c 1 if the encoded data structure is dumped successfully, \n + * otherwise @c 0 on failure * * @see eet_data_dump() - * - * @since 1.0.0 - * @ingroup Eet_Data_Cipher_Group */ EAPI int eet_data_dump_cipher(Eet_File *ef, @@ -3650,28 +3839,28 @@ eet_data_dump_cipher(Eet_File *ef, void *dumpdata); /** - * Take an ascii encoding from eet_data_dump() and re-encode in - * binary using a cipher. - * @param ef A valid eet file handle. - * @param name Name of the entry. eg: "/base/file_i_want". - * @param cipher_key The key to use as cipher. - * @param text The pointer to the string data to parse and encode. - * @param textlen The size of the string in bytes (not including 0 - * byte terminator). - * @param compress Compression flags (1 == compress, 0 = don't compress). - * @return 1 on success, 0 on failure + * @brief Takes an ascii encoding from eet_data_dump() and re-encodes in + * binary using a cipher. * - * This function will parse the string pointed to by @p text, - * encode it the same way eet_data_descriptor_encode() takes an - * in-memory data struct and encodes into a binary blob. + * @details This function parses the string pointed to by @a text, + * encode it the same way eet_data_descriptor_encode() takes an + * in-memory data struct and encodes into a binary blob. + * @since 1.0.0 * - * The data (optionally compressed) will be in ram, pending a flush to - * disk (it will stay in ram till the eet file handle is closed though). + * @remarks The data (optionally compressed) is in RAM, pending a flush to + * disk (it stays in RAM till the eet file handle is closed though). * - * @see eet_data_undump() + * @param[in] ef A valid eet file handle + * @param[in] name The name of the entry \n + * For example: "/base/file_i_want". + * @param[in] cipher_key The key to use as cipher + * @param[in] text The pointer to the string data to parse and encode + * @param[in] textlen The size of the string in bytes (not including @c 0 byte terminator) + * @param[in] compress The compression flags (1 = compress, 0 = do not compress) + * @return @c 1 if the ascii encoding is re-encoded successfully, \n + * otherwise @c 0 on failure * - * @since 1.0.0 - * @ingroup Eet_Data_Cipher_Group + * @see eet_data_undump() */ EAPI int eet_data_undump_cipher(Eet_File *ef, @@ -3682,33 +3871,32 @@ eet_data_undump_cipher(Eet_File *ef, int compress); /** - * Decode a data structure from an arbitrary location in memory - * using a cipher. - * @param edd The data descriptor to use when decoding. - * @param data_in The pointer to the data to decode into a struct. - * @param cipher_key The key to use as cipher. - * @param size_in The size of the data pointed to in bytes. - * @return NULL on failure, or a valid decoded struct pointer on success. + * @brief Decodes a data structure from an arbitrary location in memory + * using a cipher. * - * This function will decode a data structure that has been encoded using - * eet_data_descriptor_encode(), and return a data structure with all its - * elements filled out, if successful, or NULL on failure. + * @details This function decodes a data structure that has been encoded using + * eet_data_descriptor_encode(), and return a data structure with all its + * elements filled out, if successful, or @c NULL on failure. + * @since 1.0.0 * - * The data to be decoded is stored at the memory pointed to by @p data_in, - * and is described by the descriptor pointed to by @p edd. The data size is - * passed in as the value to @p size_in, ande must be greater than 0 to - * succeed. + * @remarks The data to be decoded is stored at the memory pointed to by @a data_in, + * and is described by the descriptor pointed to by @a edd. The data size is + * passed in as the value to @a size_in, and must be greater than @c 0 to succeed. * - * This function is useful for decoding data structures delivered to the - * application by means other than an eet file, such as an IPC or socket - * connection, raw files, shared memory etc. + * @remarks This function is useful for decoding data structures delivered to the + * application by means other than an eet file, such as an IPC or socket + * connection, raw files, and shared memory. * - * Please see eet_data_read() for more information. + * @remarks See eet_data_read() for more information. * - * @see eet_data_descriptor_decode() + * @param[in] edd The data descriptor to use when decoding + * @param[in] data_in The pointer to the data to decode into a struct + * @param[in] cipher_key The key to use as cipher + * @param[in] size_in The size of the data pointed to in bytes + * @return A valid decoded struct pointer on success, \n + * otherwise @c NULL on failure * - * @since 1.0.0 - * @ingroup Eet_Data_Cipher_Group + * @see eet_data_descriptor_decode() */ EAPI void * eet_data_descriptor_decode_cipher(Eet_Data_Descriptor *edd, @@ -3717,35 +3905,35 @@ eet_data_descriptor_decode_cipher(Eet_Data_Descriptor *edd, int size_in); /** - * Encode a data struct to memory and return that encoded data - * using a cipher. - * @param edd The data descriptor to use when encoding. - * @param data_in The pointer to the struct to encode into data. - * @param cipher_key The key to use as cipher. - * @param size_ret pointer to the an int to be filled with the decoded size. - * @return NULL on failure, or a valid encoded data chunk on success. + * @brief Encodes a data struct to memory and return that encoded data + * using a cipher. * - * This function takes a data structutre in memory and encodes it into a - * serialised chunk of data that can be decoded again by - * eet_data_descriptor_decode(). This is useful for being able to transmit - * data structures across sockets, pipes, IPC or shared file mechanisms, - * without having to worry about memory space, machine type, endianess etc. + * @details This function takes a data structure in memory and encodes it into a + * serialised chunk of data that can be decoded again by + * eet_data_descriptor_decode(). This is useful for being able to transmit + * data structures across sockets, pipes, IPC or shared file mechanisms, + * without having to worry about memory space, machine type, endianess, and so on. + * @since 1.0.0 * - * The parameter @p edd must point to a valid data descriptor, and - * @p data_in must point to the right data structure to encode. If not, the - * encoding may fail. + * @remarks The parameter @a edd must point to a valid data descriptor, and + * @a data_in must point to the right data structure to encode. If not, the + * encoding may fail. * - * On success a non NULL valid pointer is returned and what @p size_ret - * points to is set to the size of this decoded data, in bytes. When the - * encoded data is no longer needed, call free() on it. On failure NULL is - * returned and what @p size_ret points to is set to 0. + * @remarks On success a non NULL valid pointer is returned and what @a size_ret + * points to is set to the size of this decoded data, in bytes. When the + * encoded data is no longer needed, call free() on it. On failure @c NULL is + * returned and what @a size_ret points to is set to @c 0. * - * Please see eet_data_write() for more information. + * @remarks See eet_data_write() for more information. * - * @see eet_data_descriptor_encode() + * @param[in] edd The data descriptor to use when encoding + * @param[in] data_in The pointer to the struct to encode into data + * @param[in] cipher_key The key to use as cipher + * @param[out] size_ret The pointer to the an int to be filled with the decoded size + * @return A valid encoded data chunk, \n + * otherwise @c NULL on failure * - * @since 1.0.0 - * @ingroup Eet_Data_Cipher_Group + * @see eet_data_descriptor_encode() */ EAPI void * eet_data_descriptor_encode_cipher(Eet_Data_Descriptor *edd, @@ -3754,29 +3942,36 @@ eet_data_descriptor_encode_cipher(Eet_Data_Descriptor *edd, int *size_ret); /** + * @} + */ + +/** + * @internal * @defgroup Eet_Node_Group Low-level Serialization Structures. + * @ingroup Eet_Group * - * Functions that create, destroy and manipulate serialization nodes - * used by @ref Eet_Data_Group. + * @brief This group provides functions that create, destroy and manipulate serialization nodes + * used by @ref Eet_Data_Group. * * @{ */ /** * @typedef Eet_Node - * Opaque handle to manage serialization node. + * @brief The structure type containing an opaque handle to manage serialization node. */ typedef struct _Eet_Node Eet_Node; /** * @typedef Eet_Node_Data - * Contains an union that can fit any kind of node. + * @brief The structure type containing a union that can fit any kind of node. */ typedef struct _Eet_Node_Data Eet_Node_Data; /** + * @internal * @struct _Eet_Node_Data - * Contains an union that can fit any kind of node. + * @brief The structure type containing a union that can fit any kind of node. */ struct _Eet_Node_Data { @@ -3796,12 +3991,7 @@ struct _Eet_Node_Data }; /** - * @} - */ - -/** * TODO FIX ME - * @ingroup Eet_Node_Group */ EAPI Eet_Node * eet_node_char_new(const char *name, @@ -3809,7 +3999,6 @@ eet_node_char_new(const char *name, /** * TODO FIX ME - * @ingroup Eet_Node_Group */ EAPI Eet_Node * eet_node_short_new(const char *name, @@ -3817,7 +4006,6 @@ eet_node_short_new(const char *name, /** * TODO FIX ME - * @ingroup Eet_Node_Group */ EAPI Eet_Node * eet_node_int_new(const char *name, @@ -3825,7 +4013,6 @@ eet_node_int_new(const char *name, /** * TODO FIX ME - * @ingroup Eet_Node_Group */ EAPI Eet_Node * eet_node_long_long_new(const char *name, @@ -3833,7 +4020,6 @@ eet_node_long_long_new(const char *name, /** * TODO FIX ME - * @ingroup Eet_Node_Group */ EAPI Eet_Node * eet_node_float_new(const char *name, @@ -3841,7 +4027,6 @@ eet_node_float_new(const char *name, /** * TODO FIX ME - * @ingroup Eet_Node_Group */ EAPI Eet_Node * eet_node_double_new(const char *name, @@ -3849,7 +4034,6 @@ eet_node_double_new(const char *name, /** * TODO FIX ME - * @ingroup Eet_Node_Group */ EAPI Eet_Node * eet_node_unsigned_char_new(const char *name, @@ -3857,7 +4041,6 @@ eet_node_unsigned_char_new(const char *name, /** * TODO FIX ME - * @ingroup Eet_Node_Group */ EAPI Eet_Node * eet_node_unsigned_short_new(const char *name, @@ -3865,7 +4048,6 @@ eet_node_unsigned_short_new(const char *name, /** * TODO FIX ME - * @ingroup Eet_Node_Group */ EAPI Eet_Node * eet_node_unsigned_int_new(const char *name, @@ -3873,7 +4055,6 @@ eet_node_unsigned_int_new(const char *name, /** * TODO FIX ME - * @ingroup Eet_Node_Group */ EAPI Eet_Node * eet_node_unsigned_long_long_new(const char *name, @@ -3881,7 +4062,6 @@ eet_node_unsigned_long_long_new(const char *name, /** * TODO FIX ME - * @ingroup Eet_Node_Group */ EAPI Eet_Node * eet_node_string_new(const char *name, @@ -3889,7 +4069,6 @@ eet_node_string_new(const char *name, /** * TODO FIX ME - * @ingroup Eet_Node_Group */ EAPI Eet_Node * eet_node_inlined_string_new(const char *name, @@ -3897,14 +4076,12 @@ eet_node_inlined_string_new(const char *name, /** * TODO FIX ME - * @ingroup Eet_Node_Group */ EAPI Eet_Node * eet_node_null_new(const char *name); /** * TODO FIX ME - * @ingroup Eet_Node_Group */ EAPI Eet_Node * eet_node_list_new(const char *name, @@ -3912,7 +4089,6 @@ eet_node_list_new(const char *name, /** * TODO FIX ME - * @ingroup Eet_Node_Group */ EAPI Eet_Node * eet_node_array_new(const char *name, @@ -3921,7 +4097,6 @@ eet_node_array_new(const char *name, /** * TODO FIX ME - * @ingroup Eet_Node_Group */ EAPI Eet_Node * eet_node_var_array_new(const char *name, @@ -3929,7 +4104,6 @@ eet_node_var_array_new(const char *name, /** * TODO FIX ME - * @ingroup Eet_Node_Group */ EAPI Eet_Node * eet_node_hash_new(const char *name, @@ -3938,7 +4112,6 @@ eet_node_hash_new(const char *name, /** * TODO FIX ME - * @ingroup Eet_Node_Group */ EAPI Eet_Node * eet_node_struct_new(const char *name, @@ -3946,44 +4119,45 @@ eet_node_struct_new(const char *name, /** * TODO FIX ME - * @ingroup Eet_Node_Group */ EAPI Eet_Node * eet_node_struct_child_new(const char *parent, Eet_Node *child); /** - * @brief Get a node's child nodes - * @param node The node - * @return The first child node which contains a pointer to the - * next child node and the parent. - * @since 1.5 + * @brief Gets a node's child nodes. + * @since 1.5 + * + * @param[in] node The node + * @return The first child node which contains a pointer to the + * next child node and the parent */ EAPI Eet_Node * eet_node_children_get(Eet_Node *node); /** - * @brief Get the next node in a list of nodes - * @param node The node - * @return A node which contains a pointer to the - * next child node and the parent. - * @since 1.5 + * @brief Gets the next node in a list of nodes. + * @since 1.5 + * + * @param[in] node The node + * @return A node which contains a pointer to the + * next child node and the parent */ EAPI Eet_Node * eet_node_next_get(Eet_Node *node); /** - * @brief Get the parent node of a node - * @param node The node - * @return The parent node of @p node - * @since 1.5 + * @brief Gets the parent node of a node. + * @since 1.5 + * + * @param[in] node The node + * @return The parent node of @a node */ EAPI Eet_Node * eet_node_parent_get(Eet_Node *node); /** * TODO FIX ME - * @ingroup Eet_Node_Group */ EAPI void eet_node_list_append(Eet_Node *parent, @@ -3992,7 +4166,6 @@ eet_node_list_append(Eet_Node *parent, /** * TODO FIX ME - * @ingroup Eet_Node_Group */ EAPI void eet_node_struct_append(Eet_Node *parent, @@ -4001,7 +4174,6 @@ eet_node_struct_append(Eet_Node *parent, /** * TODO FIX ME - * @ingroup Eet_Node_Group */ EAPI void eet_node_hash_add(Eet_Node *parent, @@ -4011,7 +4183,6 @@ eet_node_hash_add(Eet_Node *parent, /** * TODO FIX ME - * @ingroup Eet_Node_Group */ EAPI void eet_node_dump(Eet_Node *n, @@ -4020,33 +4191,33 @@ eet_node_dump(Eet_Node *n, void *dumpdata); /** - * @brief Return the type of a node - * @param node The node - * @return The node's type (EET_T_$TYPE) - * @since 1.5 + * @brief Gets the type of a node. + * @since 1.5 + * + * @param[in] node The node + * @return The node's type (EET_T_$TYPE) */ EAPI int eet_node_type_get(Eet_Node *node); /** - * @brief Return the node's data - * @param node The node - * @return The data contained in the node - * @since 1.5 + * @brief Gets the node's data. + * @since 1.5 + * + * @param[in] node The node + * @return The data contained in the node */ EAPI Eet_Node_Data * eet_node_value_get(Eet_Node *node); /** * TODO FIX ME - * @ingroup Eet_Node_Group */ EAPI void eet_node_del(Eet_Node *n); /** * TODO FIX ME - * @ingroup Eet_Node_Group */ EAPI void * eet_data_node_encode_cipher(Eet_Node *node, @@ -4055,7 +4226,6 @@ eet_data_node_encode_cipher(Eet_Node *node, /** * TODO FIX ME - * @ingroup Eet_Node_Group */ EAPI Eet_Node * eet_data_node_decode_cipher(const void *data_in, @@ -4064,7 +4234,6 @@ eet_data_node_decode_cipher(const void *data_in, /** * TODO FIX ME - * @ingroup Eet_Node_Group */ EAPI Eet_Node * eet_data_node_read_cipher(Eet_File *ef, @@ -4073,7 +4242,6 @@ eet_data_node_read_cipher(Eet_File *ef, /** * TODO FIX ME - * @ingroup Eet_Node_Group */ EAPI int eet_data_node_write_cipher(Eet_File *ef, @@ -4086,7 +4254,7 @@ eet_data_node_write_cipher(Eet_File *ef, /** * @typedef Eet_Node_Walk - * Describes how to walk trees of #Eet_Node. + * @brief The structure type that describes how to walk trees of #Eet_Node. */ typedef struct _Eet_Node_Walk Eet_Node_Walk; @@ -4100,8 +4268,9 @@ typedef void * (*Eet_Node_Walk_Hash_Callback)(void *parent, const c typedef void * (*Eet_Node_Walk_Simple_Callback)(int type, Eet_Node_Data *data, void *user_data); /** + * @internal * @struct _Eet_Node_Walk - * Describes how to walk trees of #Eet_Node. + * @brief The structure type that describes how to walk trees of #Eet_Node. */ struct _Eet_Node_Walk { @@ -4122,50 +4291,49 @@ eet_node_walk(void *parent, Eet_Node_Walk *cb, void *user_data); -/*******/ +/** + * @} + */ /** + * @internal * @defgroup Eet_Connection_Group Helper function to use eet over a network link + * @ingroup Eet_Group * - * Function that reconstruct and prepare packet of @ref Eet_Data_Group to be send. + * @remarks This group provides function that reconstructs and prepares packet of @ref Eet_Data_Group to be send. * + * @{ */ /** * @typedef Eet_Connection - * Opaque handle to track paquet for a specific connection. - * - * @ingroup Eet_Connection_Group + * @brief The structure type containing an opaque handle to track packet for a specific connection. */ typedef struct _Eet_Connection Eet_Connection; /** * @typedef Eet_Read_Cb - * Called back when an @ref Eet_Data_Group has been received completely and could be used. - * - * @ingroup Eet_Connection_Group + * @brief Called when an @ref Eet_Data_Group has been received completely and could be used. */ typedef Eina_Bool Eet_Read_Cb (const void *eet_data, size_t size, void *user_data); /** * @typedef Eet_Write_Cb - * Called back when a packet containing @ref Eet_Data_Group data is ready to be send. - * - * @ingroup Eet_Connection_Group + * @brief Called when a packet containing @ref Eet_Data_Group data is ready to be sent. */ typedef Eina_Bool Eet_Write_Cb (const void *data, size_t size, void *user_data); /** - * Instanciate a new connection to track. - * @param eet_read_cb Function to call when one Eet_Data packet has been fully assemble. - * @param eet_write_cb Function to call when one Eet_Data packet is ready to be send over the wire. - * @param user_data Pointer provided to both functions to be used as a context handler. - * @return NULL on failure, or a valid Eet_Connection handler. + * @brief Instantiates a new connection to track. + * @since 1.2.4 * - * For every connection to track you will need a separate Eet_Connection provider. + * @remarks For every connection to track, you need a separate Eet_Connection provider. * - * @since 1.2.4 - * @ingroup Eet_Connection_Group + * @param[in] eet_read_cb The function to call when one Eet_Data packet has been fully assembled + * @param[in] eet_write_cb The function to call when one Eet_Data packet is ready to be sent over the wire + * @param[in] user_data The pointer provided to both functions to be used as a context handler + * @return A valid Eet_Connection handler, \n + * otherwise @c NULL on failure */ EAPI Eet_Connection * eet_connection_new(Eet_Read_Cb *eet_read_cb, @@ -4173,18 +4341,19 @@ eet_connection_new(Eet_Read_Cb *eet_read_cb, const void *user_data); /** - * Process a raw packet received over the link - * @param conn Connection handler to track. - * @param data Raw data packet. - * @param size The size of that packet. - * @return 0 on complete success, any other value indicate where in the stream it got wrong (It could be before that packet). + * @brief Processes a raw packet received over the link. + * @since 1.2.4 * - * Every time you receive a packet related to your connection, you should pass - * it to that function so that it could process and assemble packet has you - * receive it. It will automatically call Eet_Read_Cb when one is fully received. + * @remarks Every time you receive a packet related to your connection, you should pass + * it to that function so that it could process and assemble packet that you + * receive. It automatically calls Eet_Read_Cb when one is fully received. * - * @since 1.2.4 - * @ingroup Eet_Connection_Group + * @param[in] conn The connection handler to track + * @param[in] data The raw data packet + * @param[in] size The size of that packet + * @return @c 0 on complete success, \n + * otherwise any other value which indicates the stream it got wrong \n + * It could be before that packet. */ EAPI int eet_connection_received(Eet_Connection *conn, @@ -4192,21 +4361,34 @@ eet_connection_received(Eet_Connection *conn, size_t size); /** - * Convert a complex structure and prepare it to be send. - * @param conn Connection handler to track. - * @param edd The data descriptor to use when encoding. - * @param data_in The pointer to the struct to encode into data. - * @param cipher_key The key to use as cipher. - * @return EINA_TRUE if the data where correctly send, EINA_FALSE if they don't. + * @brief Checks whether the Eet_Connection has received some partial data. + * @since 1.7 * - * This function serialize data_in with edd, assemble the packet and call - * Eet_Write_Cb when ready. The data passed Eet_Write_Cb are temporary allocated - * and will vanish just after the return of the callback. + * @remarks Eet_Connection buffer data until the received data can be unserialized correctly. This + * function lets you know if there is some data inside that buffer waiting for more data to + * be received before being processed. + * @param[in] conn The connection handler to request + * @return @c EINA_TRUE if there is some data pending inside, \n + * otherwise @c EINA_FALSE + */ +EAPI Eina_Bool eet_connection_empty(Eet_Connection *conn); + +/** + * @brief Converts a complex structure and prepares it to be send. * - * @see eet_data_descriptor_encode_cipher + * @details This function serializes data_in with edd, assembles the packet and calls + * Eet_Write_Cb when ready. The data passed Eet_Write_Cb is temporary allocated + * and vanishes just after the return of the callback. + * @since 1.2.4 + * + * @param[in] conn The connection handler to track + * @param[in] edd The data descriptor to use when encoding + * @param[in] data_in The pointer to the struct to encode into data + * @param[in] cipher_key The key to use as cipher + * @return @c EINA_TRUE if the complex structure is converted successfully, \n + * otherwise @c EINA_FALSE on failure * - * @since 1.2.4 - * @ingroup Eet_Connection_Group + * @see eet_data_descriptor_encode_cipher */ EAPI Eina_Bool eet_connection_send(Eet_Connection *conn, @@ -4215,20 +4397,20 @@ eet_connection_send(Eet_Connection *conn, const char *cipher_key); /** - * Convert a Eet_Node tree and prepare it to be send. - * @param conn Connection handler to track. - * @param node The data tree to use when encoding. - * @param cipher_key The key to use as cipher. - * @return EINA_TRUE if the data where correctly send, EINA_FALSE if they don't. + * @brief Converts a Eet_Node tree and prepares it to be send. * - * This function serialize node, assemble the packet and call - * Eet_Write_Cb when ready. The data passed Eet_Write_Cb are temporary allocated - * and will vanish just after the return of the callback. + * @details This function serializes node, assembles the packet and calls + * Eet_Write_Cb when ready. The data passed Eet_Write_Cb is temporary allocated + * and vanishes just after the return of the callback. + * @since 1.2.4 * - * @see eet_data_node_encode_cipher + * @param[in] conn The connection handler to track + * @param[in] node The data tree to use when encoding + * @param[in] cipher_key The key to use as cipher + * @return @c EINA_TRUE if the tree is converted successfully, \n + * otherwise @c EINA_FALSE on failure * - * @since 1.2.4 - * @ingroup Eet_Connection_Group + * @see eet_data_node_encode_cipher */ EAPI Eina_Bool eet_connection_node_send(Eet_Connection *conn, @@ -4236,19 +4418,20 @@ eet_connection_node_send(Eet_Connection *conn, const char *cipher_key); /** - * Close a connection and lost its track. - * @param conn Connection handler to close. - * @param on_going Signal if a partial packet wasn't completed. - * @return the user_data passed to both callback. + * @brief Closes a connection and loses its track. + * @since 1.2.4 * - * @since 1.2.4 - * @ingroup Eet_Connection_Group + * @param[in] conn The connection handler to close + * @param[in] on_going The signal if a partial packet is not completed + * @return The user_data passed to both callback */ EAPI void * eet_connection_close(Eet_Connection *conn, Eina_Bool *on_going); -/***************************************************************************/ +/** + * @} + */ #ifdef __cplusplus } diff --git a/src/lib/Makefile.am b/src/lib/Makefile.am index 438aecc..56c2e5c 100644 --- a/src/lib/Makefile.am +++ b/src/lib/Makefile.am @@ -5,6 +5,7 @@ AM_CPPFLAGS = \ -I. \ -I$(top_srcdir)/src/lib \ -I$(top_srcdir)/src/lib/lz4 \ +-I$(top_srcdir)/src/lib/rg_etc \ -I$(top_builddir)/src/lib \ -DPACKAGE_BIN_DIR=\"$(bindir)\" \ -DPACKAGE_LIB_DIR=\"$(libdir)\" \ @@ -31,7 +32,11 @@ eet_utils.c \ lz4/lz4.c \ lz4/lz4.h \ lz4/lz4hc.c \ -lz4/lz4hc.h +lz4/lz4hc.h \ +rg_etc/rg_etc1.c \ +rg_etc/rg_etc2.c \ +rg_etc/etc2_encoder.c \ +rg_etc/rg_etc1.h libeet_la_LIBADD = @EET_LIBS@ -lm libeet_la_LDFLAGS = -no-undefined @lt_enable_auto_import@ -version-info @version_info@ @release_info@ diff --git a/src/lib/eet_alloc.c b/src/lib/eet_alloc.c index 1671ccd..d7fde6e 100644 --- a/src/lib/eet_alloc.c +++ b/src/lib/eet_alloc.c @@ -21,11 +21,13 @@ struct _Eet_Mempool TYPE * \ Type##_malloc(unsigned int num) \ { \ + if (!Type##_mp.mp) return NULL; \ return eina_mempool_malloc(Type##_mp.mp, num * sizeof (TYPE)); \ } \ TYPE * \ Type##_calloc(unsigned int num) \ { \ + if (!Type##_mp.mp) return NULL; \ return eina_mempool_calloc(Type##_mp.mp, num * sizeof (TYPE)); \ } \ void \ @@ -64,7 +66,7 @@ eet_mempool_init(void) mempool_array[i]->mp = eina_mempool_add(choice, mempool_array[i]->name, NULL, mempool_array[i]->size, 16); if (!mempool_array[i]->mp) { - if (!strcmp(choice, "pass_through")) + if (!(!strcmp(choice, "pass_through"))) { ERR("Falling back to pass through ! Previously tried '%s' mempool.", choice); choice = "pass_through"; diff --git a/src/lib/eet_cipher.c b/src/lib/eet_cipher.c index 5bd9f25..3317538 100644 --- a/src/lib/eet_cipher.c +++ b/src/lib/eet_cipher.c @@ -1379,12 +1379,13 @@ eet_pbkdf2_sha1(const char *key, for (k = 0; k < tmp_len; k++) p[k] ^= digest[k]; } - } # ifdef HAVE_GNUTLS # else - HMAC_cleanup(&hctx); + HMAC_cleanup(&hctx); # endif /* ifdef HAVE_GNUTLS */ + } + return 0; } diff --git a/src/lib/eet_connection.c b/src/lib/eet_connection.c index 7b6b934..a192d4e 100644 --- a/src/lib/eet_connection.c +++ b/src/lib/eet_connection.c @@ -170,6 +170,12 @@ _eet_connection_raw_send(Eet_Connection *conn, } EAPI Eina_Bool +eet_connection_empty(Eet_Connection *conn) +{ + return conn->size ? EINA_FALSE : EINA_TRUE; +} + +EAPI Eina_Bool eet_connection_send(Eet_Connection *conn, Eet_Data_Descriptor *edd, const void *data_in, @@ -179,6 +185,8 @@ eet_connection_send(Eet_Connection *conn, int data_size; Eina_Bool ret = EINA_FALSE; + EINA_SAFETY_ON_NULL_RETURN_VAL(conn, EINA_FALSE); + flat_data = eet_data_descriptor_encode_cipher(edd, data_in, cipher_key, @@ -198,6 +206,8 @@ eet_connection_node_send(Eet_Connection *conn, int data_size; Eina_Bool ret = EINA_FALSE; + EINA_SAFETY_ON_NULL_RETURN_VAL(conn, EINA_FALSE); + data = eet_data_node_encode_cipher(node, cipher_key, &data_size); if (!data) return EINA_FALSE; if (_eet_connection_raw_send(conn, data, data_size)) diff --git a/src/lib/eet_data.c b/src/lib/eet_data.c index bd6bbd9..00e729a 100644 --- a/src/lib/eet_data.c +++ b/src/lib/eet_data.c @@ -110,7 +110,7 @@ struct _Eet_Data_Group_Type_Codec struct _Eet_Data_Chunk { - char *name; + const char *name; int len; int size; int hash; @@ -1422,7 +1422,7 @@ eet_data_chunk_new(void *data, || type == EET_T_F8P24) type = EET_T_DOUBLE; - chnk->name = strdup(name); + chnk->name = name; chnk->len = strlen(name) + 1; chnk->size = size; chnk->data = data; @@ -1434,9 +1434,6 @@ eet_data_chunk_new(void *data, static inline void eet_data_chunk_free(Eet_Data_Chunk *chnk) { - if (chnk->name) - free(chnk->name); - free(chnk); } @@ -1563,9 +1560,9 @@ case EET_T_ ## Type: type += EET_I_ ## Type; break; if (chnk->data) eet_data_stream_write(ds, chnk->data, chnk->size); - free(string); -on_error: free(size); +on_error: + free(string); } /*---*/ @@ -1624,7 +1621,7 @@ _eet_descriptor_hash_free(Eet_Data_Descriptor *edd) static Eet_Data_Element * _eet_descriptor_hash_find(Eet_Data_Descriptor *edd, - char *name, + const char *name, int hash) { Eet_Data_Descriptor_Hash *bucket; @@ -1946,6 +1943,8 @@ eet_data_descriptor_element_add(Eet_Data_Descriptor *edd, Eet_Data_Element *ede; Eet_Data_Element *tmp; + EINA_SAFETY_ON_NULL_RETURN(edd); + /* Sanity check to avoid crash later at runtime */ if (type < EET_T_UNKNOW || type >= EET_T_LAST) @@ -1973,7 +1972,7 @@ eet_data_descriptor_element_add(Eet_Data_Descriptor *edd, } else if ((offset + sizeof (void*)) > (unsigned int) edd->size) { - CRIT("Preventing later buffer overrun : offset = %i, estimated size = %i in a structure of %i bytes", offset, sizeof (void*), edd->size); + CRIT("Preventing later buffer overrun : offset = %i, estimated size = %zu in a structure of %i bytes", offset, sizeof (void*), edd->size); return ; } @@ -2152,6 +2151,8 @@ eet_data_write_cipher(Eet_File *ef, int size; int val; + EINA_SAFETY_ON_NULL_RETURN_VAL(edd, 0); + ed = eet_dictionary_get(ef); data_enc = _eet_data_descriptor_encode(ed, edd, data, &size); @@ -2585,6 +2586,7 @@ _eet_data_dump_token_get(const char *src, char *tok = NULL; int in_token = 0; int in_quote = 0; + int in_escape = 0; int tlen = 0, tsize = 0; #define TOK_ADD(x) \ @@ -2602,25 +2604,32 @@ _eet_data_dump_token_get(const char *src, { if (in_token) { - if (in_quote) + if (in_escape) { - if ((p[0] == '\"') && (p > src) && (p[-1] != '\\')) + switch (p[0]) { + case 'n': + TOK_ADD('\n'); + break; + case '"': + case '\'': + case '\\': + TOK_ADD(p[0]); + break; + default: + ERR("Unknow escape character %#x (%c). Append as is", + p[0], p[0]); + TOK_ADD(p[0]); + } + in_escape = 0; + } + else if (p[0] == '\\') + { + in_escape = 1; + } + else if (in_quote) + { + if (p[0] == '\"') in_quote = 0; - else if ((p[0] == '\\') && (*len > 1) && - (p[1] == '\"')) - { -/* skip */ - } - else if ((p[0] == '\\') && (p > src) && (p[-1] == '\\')) - { -/* skip */ - } - else if ((p[0] == '\\') && (*len > 1) && (p[1] == 'n')) - { -/* skip */ - } - else if ((p[0] == 'n') && (p > src) && (p[-1] == '\\')) - TOK_ADD('\n'); else TOK_ADD(p[0]); } @@ -2882,8 +2891,15 @@ _eet_data_dump_encode(int parent_type, node->type); } else - /* A Hash without key will not decode correctly. */ - return NULL; + { + /* A Hash without key will not decode correctly. */ + + ds->data = NULL; + ds->size = 0; + eet_data_stream_free(ds); + + return NULL; + } for (n = node->values; n; n = n->next) { @@ -4740,6 +4756,8 @@ eet_data_descriptor_decode_cipher(Eet_Data_Descriptor *edd, Eet_Free_Context context; unsigned int deciphered_len = size_in; + EINA_SAFETY_ON_NULL_RETURN_VAL(edd, NULL); + if (cipher_key && data_in) if (eet_decipher(data_in, size_in, cipher_key, strlen(cipher_key), &deciphered, &deciphered_len)) @@ -4941,6 +4959,8 @@ eet_data_descriptor_encode_cipher(Eet_Data_Descriptor *edd, unsigned int ciphered_len = 0; int size; + EINA_SAFETY_ON_NULL_RETURN_VAL(edd, NULL); + ret = _eet_data_descriptor_encode(NULL, edd, data_in, &size); if (cipher_key && ret) { @@ -4986,6 +5006,8 @@ eet_data_xattr_cipher_get(const char *filename, void *ret; ssize_t size; + EINA_SAFETY_ON_NULL_RETURN_VAL(edd, NULL); + blob = eina_xattr_get(filename, attribute, &size); if (!blob) return NULL; @@ -5007,6 +5029,8 @@ eet_data_xattr_cipher_set(const char *filename, int size; Eina_Bool ret; + EINA_SAFETY_ON_NULL_RETURN_VAL(edd, EINA_FALSE); + blob = eet_data_descriptor_encode_cipher(edd, data, cipher_key, &size); if (!blob) return EINA_FALSE; diff --git a/src/lib/eet_image.c b/src/lib/eet_image.c index 00acda8..efe7108 100644 --- a/src/lib/eet_image.c +++ b/src/lib/eet_image.c @@ -2,33 +2,18 @@ # include #endif /* ifdef HAVE_CONFIG_H */ -#ifdef HAVE_ALLOCA_H -# include -#elif defined __GNUC__ -# define alloca __builtin_alloca -#elif defined _AIX -# define alloca __alloca -#elif defined _MSC_VER -# include -# define alloca _alloca -#else /* ifdef HAVE_ALLOCA_H */ -# include -# ifdef __cplusplus -extern "C" -# endif /* ifdef __cplusplus */ -void *alloca(size_t); -#endif /* ifdef HAVE_ALLOCA_H */ +#ifdef __OpenBSD__ +# include +#endif /* ifdef __OpenBSD__ */ #ifdef HAVE_NETINET_IN_H -# ifdef __OpenBSD__ -# include -# endif /* ifdef __OpenBSD__ */ # include -#endif /* ifdef HAVE_NETINET_IN_H */ +#endif #ifdef _WIN32 # include # define HAVE_BOOLEAN +# define XMD_H /* This prevents libjpeg to redefine INT32 */ #endif /* ifdef _WIN32 */ #include @@ -43,6 +28,42 @@ void *alloca(size_t); #include "lz4.h" #include "lz4hc.h" +#include "rg_etc1.h" + +#ifdef BUILD_NEON +#include +#endif + +#ifndef WORDS_BIGENDIAN +/* x86 */ +#define A_VAL(p) (((uint8_t *)(p))[3]) +#define R_VAL(p) (((uint8_t *)(p))[2]) +#define G_VAL(p) (((uint8_t *)(p))[1]) +#define B_VAL(p) (((uint8_t *)(p))[0]) +#else +/* ppc */ +#define A_VAL(p) (((uint8_t *)(p))[0]) +#define R_VAL(p) (((uint8_t *)(p))[1]) +#define G_VAL(p) (((uint8_t *)(p))[2]) +#define B_VAL(p) (((uint8_t *)(p))[3]) +#endif + +#define ARGB_JOIN(a,r,g,b) \ + (((a) << 24) + ((r) << 16) + ((g) << 8) + (b)) + +#define OFFSET_BLOCK_SIZE 4 +#define OFFSET_ALGORITHM 5 +#define OFFSET_OPTIONS 6 +#define OFFSET_WIDTH 8 +#define OFFSET_HEIGHT 12 +#define OFFSET_BLOCKS 16 + +#undef MIN +#define MIN(x, y) (((x) < (y)) ? (x) : (y)) + +#undef MAX +#define MAX(x, y) (((x) > (y)) ? (x) : (y)) + /*---*/ typedef struct _JPEG_error_mgr *emptr; @@ -342,7 +363,7 @@ _JPEGFatalErrorHandler(j_common_ptr cinfo) } static void -_JPEGErrorHandler(j_common_ptr cinfo __UNUSED__) +_JPEGErrorHandler(j_common_ptr cinfo EINA_UNUSED) { /* emptr errmgr; */ @@ -353,8 +374,8 @@ _JPEGErrorHandler(j_common_ptr cinfo __UNUSED__) } static void -_JPEGErrorHandler2(j_common_ptr cinfo __UNUSED__, - int msg_level __UNUSED__) +_JPEGErrorHandler2(j_common_ptr cinfo EINA_UNUSED, + int msg_level EINA_UNUSED) { /* emptr errmgr; */ @@ -677,6 +698,350 @@ eet_data_image_jpeg_alpha_decode(const void *data, return 1; } +// FIXME: Importing two functions from evas here: premul & unpremul +static void +_eet_argb_premul(unsigned int *data, unsigned int len) +{ + unsigned int *de = data + len; + + while (data < de) + { + unsigned int a = 1 + (*data >> 24); + + *data = (*data & 0xff000000) + + (((((*data) >> 8) & 0xff) * a) & 0xff00) + + (((((*data) & 0x00ff00ff) * a) >> 8) & 0x00ff00ff); + data++; + } +} + +static void +_eet_argb_unpremul(unsigned int *data, unsigned int len) +{ + unsigned int *de = data + len; + unsigned int p_val = 0x00000000, p_res = 0x00000000; + + while (data < de) + { + unsigned int a = (*data >> 24) + 1; + + if (p_val == *data) *data = p_res; + else + { + p_val = *data; + if ((a > 1) && (a < 256)) + *data = ARGB_JOIN(a, + (R_VAL(data) * 255) / a, + (G_VAL(data) * 255) / a, + (B_VAL(data) * 255) / a); + else if (a == 1) + *data = 0x00000000; + p_res = *data; + } + data++; + } +} + +static inline unsigned int +_tgv_length_get(const char *m, unsigned int length, unsigned int *offset) +{ + unsigned int r = 0; + unsigned int shift = 0; + + while (*offset < length && ((*m) & 0x80)) + { + r = r | (((*m) & 0x7F) << shift); + shift += 7; + m++; + (*offset)++; + } + if (*offset < length) + { + r = r | (((*m) & 0x7F) << shift); + (*offset)++; + } + + return r; +} + +static int +roundup(int val, int rup) +{ + if (val >= 0 && rup > 0) + return (val + rup - 1) - ((val + rup - 1) % rup); + return 0; +} + +static int +eet_data_image_etc2_decode(const void *data, + unsigned int length, + unsigned int *p, + unsigned int dst_x, + unsigned int dst_y, + unsigned int dst_w, + unsigned int dst_h, + Eina_Bool alpha, + Eet_Colorspace cspace, + Eet_Image_Encoding lossy) +{ + const char *m = NULL; + unsigned int bwidth, bheight; + unsigned char *p_etc; + char *buffer = NULL; + Eina_Rectangle master; + unsigned int block_length; + unsigned int offset; + unsigned int x, y, w, h; + unsigned int block_count; + unsigned int etc_width = 0; + unsigned int etc_block_size; + unsigned int num_planes = 1, plane, alpha_offset = 0; + Eet_Colorspace file_cspace; + Eina_Bool compress, blockless, unpremul; + + m = data; + + if (strncmp(m, "TGV1", 4) != 0) + return 0; + + compress = m[OFFSET_OPTIONS] & 0x1; + blockless = (m[OFFSET_OPTIONS] & 0x2) != 0; + unpremul = (m[OFFSET_OPTIONS] & 0x4) != 0; + w = ntohl(*((unsigned int*) &(m[OFFSET_WIDTH]))); + h = ntohl(*((unsigned int*) &(m[OFFSET_HEIGHT]))); + + switch (m[OFFSET_ALGORITHM] & 0xFF) + { + case 0: + if (lossy != EET_IMAGE_ETC1) return 0; + file_cspace = EET_COLORSPACE_ETC1; + if (alpha != EINA_FALSE) return 0; + etc_block_size = 8; + break; + case 1: + if (lossy != EET_IMAGE_ETC2_RGB) return 0; + file_cspace = EET_COLORSPACE_RGB8_ETC2; + if (alpha != EINA_FALSE) return 0; + etc_block_size = 8; + break; + case 2: + if (lossy != EET_IMAGE_ETC2_RGBA) return 0; + file_cspace = EET_COLORSPACE_RGBA8_ETC2_EAC; + if (alpha != EINA_TRUE) return 0; + etc_block_size = 16; + break; + case 3: + if (lossy != EET_IMAGE_ETC1_ALPHA) return 0; + file_cspace = EET_COLORSPACE_ETC1_ALPHA; + if (alpha != EINA_TRUE) return 0; + etc_block_size = 8; + num_planes = 2; + alpha_offset = ((w + 2 + 3) / 4) * ((h + 2 + 3) / 4) * 8 / sizeof(*p_etc); + break; + default: + return 0; + } + etc_width = ((w + 2 + 3) / 4) * etc_block_size; + + if (cspace != EET_COLORSPACE_ARGB8888 && cspace != file_cspace) + { + if (!((cspace == EET_COLORSPACE_RGB8_ETC2) && (file_cspace == EET_COLORSPACE_ETC1))) + return 0; + // else: ETC2 is compatible with ETC1 and is preferred + } + + if (blockless) + { + bwidth = roundup(w + 2, 4); + bheight = roundup(h + 2, 4); + } + else + { + bwidth = 4 << (m[OFFSET_BLOCK_SIZE] & 0x0f); + bheight = 4 << ((m[OFFSET_BLOCK_SIZE] & 0xf0) >> 4); + } + + EINA_RECTANGLE_SET(&master, dst_x, dst_y, dst_w, dst_h); + + switch (cspace) + { + case EET_COLORSPACE_ETC1: + case EET_COLORSPACE_RGB8_ETC2: + case EET_COLORSPACE_RGBA8_ETC2_EAC: + case EET_COLORSPACE_ETC1_ALPHA: + if (master.x % 4 || master.y % 4) + abort(); + break; + case EET_COLORSPACE_ARGB8888: + // Offset to take duplicated pixels into account + master.x += 1; + master.y += 1; + break; + default: abort(); + } + + p_etc = (unsigned char*) p; + offset = OFFSET_BLOCKS; + + // Allocate space for each ETC block (8 or 16 bytes per 4 * 4 pixels group) + block_count = bwidth * bheight / (4 * 4); + if (compress) + buffer = alloca(etc_block_size * block_count); + + for (plane = 0; plane < num_planes; plane++) + for (y = 0; y < h + 2; y += bheight) + for (x = 0; x < w + 2; x += bwidth) + { + Eina_Rectangle current; + const char *data_start; + const char *it; + unsigned int expand_length; + unsigned int i, j; + + block_length = _tgv_length_get(m + offset, length, &offset); + + if (block_length == 0) goto on_error; + + data_start = m + offset; + offset += block_length; + + EINA_RECTANGLE_SET(¤t, x, y, + bwidth, bheight); + + if (!eina_rectangle_intersection(¤t, &master)) + continue; + + if (compress) + { + expand_length = LZ4_uncompress(data_start, buffer, + block_count * etc_block_size); + // That's an overhead for now, need to be fixed + if (expand_length != block_length) + goto on_error; + } + else + { + buffer = (void*) data_start; + if (block_count * etc_block_size != block_length) + goto on_error; + } + it = buffer; + + for (i = 0; i < bheight; i += 4) + for (j = 0; j < bwidth; j += 4, it += etc_block_size) + { + Eina_Rectangle current_etc; + unsigned int temporary[4 * 4]; + unsigned int offset_x, offset_y; + int k, l; + + EINA_RECTANGLE_SET(¤t_etc, x + j, y + i, 4, 4); + + if (!eina_rectangle_intersection(¤t_etc, ¤t)) + continue; + + switch (cspace) + { + case EET_COLORSPACE_ARGB8888: + switch (file_cspace) + { + case EET_COLORSPACE_ETC1: + case EET_COLORSPACE_ETC1_ALPHA: + if (!rg_etc1_unpack_block(it, temporary, 0)) + { + // TODO: Should we decode as RGB8_ETC2? + fprintf(stderr, "ETC1: Block starting at {%i, %i} is corrupted!\n", x + j, y + i); + continue; + } + break; + case EET_COLORSPACE_RGB8_ETC2: + rg_etc2_rgb8_decode_block((uint8_t *) it, temporary); + break; + case EET_COLORSPACE_RGBA8_ETC2_EAC: + rg_etc2_rgba8_decode_block((uint8_t *) it, temporary); + break; + default: abort(); + } + + offset_x = current_etc.x - x - j; + offset_y = current_etc.y - y - i; + + if (!plane) + { +#ifdef BUILD_NEON + if (eina_cpu_features_get() & EINA_CPU_NEON) + { + uint32_t *dst = &p[current_etc.x - 1 + (current_etc.y - 1) * master.w]; + uint32_t *src = &temporary[offset_x + offset_y * 4]; + for (k = 0; k < current_etc.h; k++) + { + if (current_etc.w == 4) + vst1q_u32(dst, vld1q_u32(src)); + else if (current_etc.w == 3) + { + vst1_u32(dst, vld1_u32(src)); + *(dst + 2) = *(src + 2); + } + else if (current_etc.w == 2) + vst1_u32(dst, vld1_u32(src)); + else + *dst = *src; + dst += master.w; + src += 4; + } + } + else +#endif + for (k = 0; k < current_etc.h; k++) + { + memcpy(&p[current_etc.x - 1 + (current_etc.y - 1 + k) * master.w], + &temporary[offset_x + (offset_y + k) * 4], + current_etc.w * sizeof (unsigned int)); + } + } + else + { + for (k = 0; k < current_etc.h; k++) + for (l = 0; l < current_etc.w; l++) + { + unsigned int *rgbdata = + &p[current_etc.x - 1 + (current_etc.y - 1 + k) * master.w + l]; + unsigned int *adata = + &temporary[offset_x + (offset_y + k) * 4 + l]; + A_VAL(rgbdata) = G_VAL(adata); + } + } + break; + case EET_COLORSPACE_ETC1: + case EET_COLORSPACE_RGB8_ETC2: + case EET_COLORSPACE_RGBA8_ETC2_EAC: + memcpy(&p_etc[(current_etc.x / 4) * etc_block_size + + (current_etc.y / 4) * etc_width], + it, etc_block_size); + break; + case EET_COLORSPACE_ETC1_ALPHA: + memcpy(&p_etc[(current_etc.x / 4) * etc_block_size + + (current_etc.y / 4) * etc_width + + plane * alpha_offset], + it, etc_block_size); + break; + default: + abort(); + } + } // bx,by inside blocks + } // x,y macroblocks + + // TODO: Add support for more unpremultiplied modes (ETC2) + if ((cspace == EET_COLORSPACE_ARGB8888) && unpremul) + _eet_argb_premul(p, w * h); + + return 1; + +on_error: + ERR("ETC image data is corrupted in this EET file"); + return 0; +} + static void * eet_data_image_lossless_convert(int *size, const void *data, @@ -746,15 +1111,32 @@ eet_data_image_lossless_compressed_convert(int *size, { unsigned char *d, *comp; - int *header, ret, ok = 1; + int *header, *bigend_data = NULL, ret, ok = 1; uLongf buflen = 0; buflen = (((w * h * 101) / 100) + 3) * 4; ret = LZ4_compressBound((w * h * 4)); if ((ret > 0) && ((uLongf)ret > buflen)) buflen = ret; - + + if (_eet_image_words_bigendian) + { + unsigned int i; + + bigend_data = (int *) malloc(w * h * 4); + if (!bigend_data) return NULL; + + memcpy(bigend_data, data, w * h * 4); + for (i = 0; i < w * h; i++) SWAP32(bigend_data[i]); + + data = (const char *) bigend_data; + } + comp = malloc(buflen); - if (!comp) return NULL; + if (!comp) + { + free(bigend_data); + return NULL; + } switch (compression) { @@ -779,6 +1161,7 @@ eet_data_image_lossless_compressed_convert(int *size, if ((!ok) || (buflen > (w * h * 4))) { free(comp); + free(bigend_data); *size = -1; return NULL; } @@ -787,6 +1170,7 @@ eet_data_image_lossless_compressed_convert(int *size, if (!d) { free(comp); + free(bigend_data); return NULL; } @@ -802,7 +1186,8 @@ eet_data_image_lossless_compressed_convert(int *size, { unsigned int i; - for (i = 0; i < ((w * h) + 8); i++) SWAP32(header[i]); + for (i = 0; i < 8; i++) SWAP32(header[i]); + free(bigend_data); } memcpy(d + (8 * sizeof(int)), comp, buflen); @@ -812,6 +1197,329 @@ eet_data_image_lossless_compressed_convert(int *size, } } +static int +_block_size_get(int size) +{ + static const int MAX_BLOCK = 6; // 256 pixels + + int k = 0; + while ((4 << k) < size) k++; + k = MAX(0, k - 1); + if ((size * 3 / 2) >= (4 << k)) return MAX(0, MIN(k - 1, MAX_BLOCK)); + return MIN(k, MAX_BLOCK); +} + +static inline void +_alpha_to_greyscale_convert(uint32_t *data, int len) +{ + for (int k = 0; k < len; k++) + { + int alpha = A_VAL(data); + *data++ = ARGB_JOIN(alpha, alpha, alpha, alpha); + } +} + +static void * +eet_data_image_etc1_compressed_convert(int *size, + const unsigned char *data8, + unsigned int w, + unsigned int h, + int quality, + int compress, + Eet_Image_Encoding lossy) +{ + rg_etc1_pack_params param; + uint8_t *comp = NULL; + uint8_t *buffer; + uint32_t *data; + uint32_t nl_width, nl_height; + uint8_t header[8] = "TGV1"; + int block_width, block_height, macro_block_width, macro_block_height; + int block_count, image_stride, image_height, etc_block_size; + int num_planes = 1; + Eet_Colorspace cspace; + Eina_Bool unpremul = EINA_FALSE, alpha_texture = EINA_FALSE; + Eina_Binbuf *r = NULL; + void *result; + const char *codec; + + r = eina_binbuf_new(); + if (!r) return NULL; + + image_stride = w; + image_height = h; + nl_width = htonl(image_stride); + nl_height = htonl(image_height); + compress = !!compress; + + // Disable dithering, as it will deteriorate the quality of flat surfaces + param.m_dithering = 0; + + if (quality > 95) + param.m_quality = rg_etc1_high_quality; + else if (quality > 30) + param.m_quality = rg_etc1_medium_quality; + else + param.m_quality = rg_etc1_low_quality; + + // header[4]: 4 bit block width, 4 bit block height + block_width = _block_size_get(image_stride + 2); + block_height = _block_size_get(image_height + 2); + header[4] = (block_height << 4) | block_width; + + // header[5]: 0 for ETC1, 1 for RGB8_ETC2, 2 for RGBA8_ETC2_EAC, 3 for ETC1_ALPHA + switch (lossy) + { + case EET_IMAGE_ETC1: + cspace = EET_COLORSPACE_ETC1; + etc_block_size = 8; + header[5] = 0; + codec = "ETC1"; + break; + case EET_IMAGE_ETC2_RGB: + cspace = EET_COLORSPACE_RGB8_ETC2; + etc_block_size = 8; + header[5] = 1; + codec = "ETC2 (RGB)"; + break; + case EET_IMAGE_ETC2_RGBA: + cspace = EET_COLORSPACE_RGBA8_ETC2_EAC; + etc_block_size = 16; + header[5] = 2; + codec = "ETC2 (RGBA)"; + break; + case EET_IMAGE_ETC1_ALPHA: + cspace = EET_COLORSPACE_ETC1_ALPHA; + etc_block_size = 8; + num_planes = 2; // RGB and Alpha + header[5] = 3; + codec = "ETC1+Alpha"; + break; + default: abort(); + } + + // header[6]: 0 for raw, 1, for LZ4 compressed, 4 for unpremultiplied RGBA + // blockless mode (0x2) is never used here + header[6] = (compress ? 0x1 : 0x0) | (unpremul ? 0x4 : 0x0); + + // header[7]: unused options + // Note: consider extending the header instead of filling all the bits here + header[7] = 0; + + // Encoding being super slow, let's inform the user first. + // FIXME: Ctrl+C must be handled + INF("Encoding %dx%d image to %s, this may take a while...", w, h, codec); + + // Write header + eina_binbuf_append_length(r, header, sizeof (header)); + eina_binbuf_append_length(r, (unsigned char*) &nl_width, sizeof (nl_width)); + eina_binbuf_append_length(r, (unsigned char*) &nl_height, sizeof (nl_height)); + + // Real block size in pixels, obviously a multiple of 4 + macro_block_width = 4 << block_width; + macro_block_height = 4 << block_height; + + // Number of ETC1 blocks in a compressed block + block_count = (macro_block_width * macro_block_height) / (4 * 4); + buffer = alloca(block_count * etc_block_size); + + if (compress) + comp = alloca(LZ4_compressBound(block_count * etc_block_size)); + + // Write a whole plane (RGB or Alpha) + for (int plane = 0; plane < num_planes; plane++) + { + if (!alpha_texture) + { + // Normal mode + data = (uint32_t *) data8; + } + else if (!plane) + { + int len = image_stride * image_height; + // RGB plane for ETC1+Alpha + data = malloc(len * 4); + if (!data) goto finish; + memcpy(data, data8, len * 4); + if (unpremul) _eet_argb_unpremul(data, len); + } + else + { + // Alpha plane for ETC1+Alpha + _alpha_to_greyscale_convert(data, image_stride * image_height); + } + + // Write macro block + for (int y = 0; y < image_height + 2; y += macro_block_height) + { + uint32_t *input, *last_col, *last_row, *last_pix; + int real_y; + int wlen; + + if (y == 0) real_y = 0; + else if (y < image_height + 1) real_y = y - 1; + else real_y = image_height - 1; + + for (int x = 0; x < image_stride + 2; x += macro_block_width) + { + uint8_t *offset = buffer; + int real_x = x; + + if (x == 0) real_x = 0; + else if (x < image_stride + 1) real_x = x - 1; + else real_x = image_stride - 1; + + input = data + real_y * image_stride + real_x; + last_row = data + image_stride * (image_height - 1) + real_x; + last_col = data + (real_y + 1) * image_stride - 1; + last_pix = data + image_height * image_stride - 1; + + for (int by = 0; by < macro_block_height; by += 4) + { + int dup_top = ((y + by) == 0) ? 1 : 0; + int max_row = MAX(0, MIN(4, image_height - real_y - by)); + int oy = (y == 0) ? 1 : 0; + + for (int bx = 0; bx < macro_block_width; bx += 4) + { + int dup_left = ((x + bx) == 0) ? 1 : 0; + int max_col = MAX(0, MIN(4, image_stride - real_x - bx)); + uint32_t todo[16] = { 0 }; + int row, col; + int ox = (x == 0) ? 1 : 0; + + if (dup_left) + { + // Duplicate left column + for (row = 0; row < max_row; row++) + todo[row * 4] = input[row * image_stride]; + for (row = max_row; row < 4; row++) + todo[row * 4] = last_row[0]; + } + + if (dup_top) + { + // Duplicate top row + for (col = 0; col < max_col; col++) + todo[col] = input[MAX(col + bx - ox, 0)]; + for (col = max_col; col < 4; col++) + todo[col] = last_col[0]; + } + + for (row = dup_top; row < 4; row++) + { + for (col = dup_left; col < max_col; col++) + { + if (row < max_row) + { + // Normal copy + todo[row * 4 + col] = input[(row + by - oy) * image_stride + bx + col - ox]; + } + else + { + // Copy last line + todo[row * 4 + col] = last_row[col + bx - ox]; + } + } + for (col = max_col; col < 4; col++) + { + // Right edge + if (row < max_row) + { + // Duplicate last column + todo[row * 4 + col] = last_col[MAX(row + by - oy, 0) * image_stride]; + } + else + { + // Duplicate very last pixel again and again + todo[row * 4 + col] = *last_pix; + } + } + } + + switch (cspace) + { + case EET_COLORSPACE_ETC1: + case EET_COLORSPACE_ETC1_ALPHA: + rg_etc1_pack_block(offset, (uint32_t *) todo, ¶m); + break; + case EET_COLORSPACE_RGB8_ETC2: + etc2_rgb8_block_pack(offset, (uint32_t *) todo, ¶m); + break; + case EET_COLORSPACE_RGBA8_ETC2_EAC: + etc2_rgba8_block_pack(offset, (uint32_t *) todo, ¶m); + break; + default: return 0; + } + +#ifdef DEBUG_STATS + if (plane == 0) + { + // Decode to compute PSNR, this is slow. + uint32_t done[16]; + + if (alpha) + rg_etc2_rgba8_decode_block(offset, done); + else + rg_etc2_rgb8_decode_block(offset, done); + + for (int k = 0; k < 16; k++) + { + const int r = (R_VAL(&(todo[k])) - R_VAL(&(done[k]))); + const int g = (G_VAL(&(todo[k])) - G_VAL(&(done[k]))); + const int b = (B_VAL(&(todo[k])) - B_VAL(&(done[k]))); + const int a = (A_VAL(&(todo[k])) - A_VAL(&(done[k]))); + mse += r*r + g*g + b*b; + if (alpha) mse_alpha += a*a; + mse_div++; + } + } +#endif + + offset += etc_block_size; + } + } + + if (compress) + { + wlen = LZ4_compressHC((char *) buffer, (char *) comp, + block_count * etc_block_size); + } + else + { + comp = buffer; + wlen = block_count * etc_block_size; + } + + if (wlen > 0) + { + unsigned int blen = wlen; + + while (blen) + { + unsigned char plen; + + plen = blen & 0x7F; + blen = blen >> 7; + + if (blen) plen = 0x80 | plen; + eina_binbuf_append_length(r, &plen, 1); + } + eina_binbuf_append_length(r, (unsigned char *) comp, wlen); + } + } // 4 rows + } // macroblocks + } // planes + +finish: + if (alpha_texture) free(data); + *size = eina_binbuf_length_get(r); + result = eina_binbuf_string_steal(r); + eina_binbuf_free(r); + + return result; +} + static void * eet_data_image_jpeg_convert(int *size, const void *data, @@ -1096,7 +1804,7 @@ eet_data_image_write_cipher(Eet_File *ef, int alpha, int comp, int quality, - int lossy) + Eet_Image_Encoding lossy) { void *d = NULL; int size = 0; @@ -1123,7 +1831,7 @@ eet_data_image_write(Eet_File *ef, int alpha, int comp, int quality, - int lossy) + Eet_Image_Encoding lossy) { return eet_data_image_write_cipher(ef, name, @@ -1146,7 +1854,7 @@ eet_data_image_read_cipher(Eet_File *ef, int *alpha, int *comp, int *quality, - int *lossy) + Eet_Image_Encoding *lossy) { unsigned int *d = NULL; void *data = NULL; @@ -1180,26 +1888,27 @@ eet_data_image_read(Eet_File *ef, int *alpha, int *comp, int *quality, - int *lossy) + Eet_Image_Encoding *lossy) { return eet_data_image_read_cipher(ef, name, NULL, w, h, alpha, comp, quality, lossy); } EAPI int -eet_data_image_read_to_surface_cipher(Eet_File *ef, - const char *name, - const char *cipher_key, - unsigned int src_x, - unsigned int src_y, - unsigned int *d, - unsigned int w, - unsigned int h, - unsigned int row_stride, - int *alpha, - int *comp, - int *quality, - int *lossy) +eet_data_image_read_to_cspace_surface_cipher(Eet_File *ef, + const char *name, + const char *cipher_key, + unsigned int src_x, + unsigned int src_y, + unsigned int *d, + unsigned int w, + unsigned int h, + unsigned int row_stride, + Eet_Colorspace cspace, + int *alpha, + int *comp, + int *quality, + Eet_Image_Encoding *lossy) { void *data = NULL; int free_data = 0; @@ -1217,9 +1926,9 @@ eet_data_image_read_to_surface_cipher(Eet_File *ef, return 0; } - res = eet_data_image_decode_to_surface(data, size, src_x, src_y, d, - w, h, row_stride, alpha, - comp, quality, lossy); + res = eet_data_image_decode_to_cspace_surface_cipher(data, NULL, size, src_x, src_y, d, + w, h, row_stride, cspace, alpha, + comp, quality, lossy); if (free_data) free(data); @@ -1228,6 +1937,27 @@ eet_data_image_read_to_surface_cipher(Eet_File *ef, } EAPI int +eet_data_image_read_to_surface_cipher(Eet_File *ef, + const char *name, + const char *cipher_key, + unsigned int src_x, + unsigned int src_y, + unsigned int *d, + unsigned int w, + unsigned int h, + unsigned int row_stride, + int *alpha, + int *comp, + int *quality, + Eet_Image_Encoding *lossy) +{ + return eet_data_image_read_to_cspace_surface_cipher(ef, name, cipher_key, + src_x, src_y, d, w, h, row_stride, + EET_COLORSPACE_ARGB8888, + alpha, comp, quality, lossy); +} + +EAPI int eet_data_image_read_to_surface(Eet_File *ef, const char *name, unsigned int src_x, @@ -1239,7 +1969,7 @@ eet_data_image_read_to_surface(Eet_File *ef, int *alpha, int *comp, int *quality, - int *lossy) + Eet_Image_Encoding *lossy) { return eet_data_image_read_to_surface_cipher(ef, name, NULL, src_x, src_y, d, @@ -1257,7 +1987,7 @@ eet_data_image_header_read_cipher(Eet_File *ef, int *alpha, int *comp, int *quality, - int *lossy) + Eet_Image_Encoding *lossy) { void *data = NULL; int size = 0; @@ -1291,7 +2021,7 @@ eet_data_image_header_read(Eet_File *ef, int *alpha, int *comp, int *quality, - int *lossy) + Eet_Image_Encoding *lossy) { return eet_data_image_header_read_cipher(ef, name, NULL, w, h, alpha, @@ -1306,7 +2036,7 @@ eet_data_image_encode_cipher(const void *data, int alpha, int comp, int quality, - int lossy, + Eet_Image_Encoding lossy, int *size_ret) { void *d = NULL; @@ -1314,24 +2044,36 @@ eet_data_image_encode_cipher(const void *data, unsigned int ciphered_sz = 0; int size = 0; - if (lossy == 0) - { - if (comp > 0) - d = eet_data_image_lossless_compressed_convert(&size, data, - w, h, alpha, comp); - - /* eet_data_image_lossless_compressed_convert will refuse to compress something - if the result is bigger than the entry. */ - if (comp <= 0 || !d) - d = eet_data_image_lossless_convert(&size, data, w, h, alpha); - } - else + switch (lossy) { - if (!alpha) - d = eet_data_image_jpeg_convert(&size, data, w, h, alpha, quality); - else - d = eet_data_image_jpeg_alpha_convert(&size, data, - w, h, alpha, quality); + case EET_IMAGE_LOSSLESS: + if (comp > 0) + d = eet_data_image_lossless_compressed_convert(&size, data, + w, h, alpha, comp); + + /* eet_data_image_lossless_compressed_convert will refuse to compress something + if the result is bigger than the entry. */ + if (comp <= 0 || !d) + d = eet_data_image_lossless_convert(&size, data, w, h, alpha); + break; + case EET_IMAGE_JPEG: + if (!alpha) + d = eet_data_image_jpeg_convert(&size, data, w, h, alpha, quality); + else + d = eet_data_image_jpeg_alpha_convert(&size, data, + w, h, alpha, quality); + break; + case EET_IMAGE_ETC1: + case EET_IMAGE_ETC2_RGB: + if (alpha) abort(); + // fallthrough + case EET_IMAGE_ETC2_RGBA: + case EET_IMAGE_ETC1_ALPHA: + d = eet_data_image_etc1_compressed_convert(&size, data, w, h, + quality, comp, lossy); + break; + default: + abort(); } if (cipher_key) @@ -1346,8 +2088,8 @@ eet_data_image_encode_cipher(const void *data, size = ciphered_sz; } else - if (ciphered_d) - free(ciphered_d); + if (ciphered_d) + free(ciphered_d); } if (size_ret) @@ -1364,7 +2106,7 @@ eet_data_image_encode(const void *data, int alpha, int comp, int quality, - int lossy) + Eet_Image_Encoding lossy) { return eet_data_image_encode_cipher(data, NULL, w, h, alpha, comp, quality, lossy, size_ret); @@ -1379,7 +2121,7 @@ eet_data_image_header_decode_cipher(const void *data, int *alpha, int *comp, int *quality, - int *lossy) + Eet_Image_Encoding *lossy) { int header[8]; void *deciphered_d = NULL; @@ -1394,8 +2136,10 @@ eet_data_image_header_decode_cipher(const void *data, size = deciphered_sz; } else - if (deciphered_d) - free(deciphered_d); + { + free(deciphered_d); + deciphered_d = NULL; + } } if (_eet_image_words_bigendian == -1) @@ -1410,7 +2154,10 @@ eet_data_image_header_decode_cipher(const void *data, } if (size < 32) - return 0; + { + free(deciphered_d); + return 0; + } memcpy(header, data, 32); if (_eet_image_words_bigendian) @@ -1429,10 +2176,16 @@ eet_data_image_header_decode_cipher(const void *data, al = header[3]; cp = header[4]; if ((iw < 1) || (ih < 1) || (iw > 8192) || (ih > 8192)) - return 0; + { + free(deciphered_d); + return 0; + } if ((cp == 0) && (size < ((iw * ih * 4) + 32))) - return 0; + { + free(deciphered_d); + return 0; + } if (w) *w = iw; @@ -1447,7 +2200,7 @@ eet_data_image_header_decode_cipher(const void *data, *comp = cp; if (lossy) - *lossy = 0; + *lossy = EET_IMAGE_LOSSLESS; if (quality) *quality = 100; @@ -1481,7 +2234,7 @@ eet_data_image_header_decode_cipher(const void *data, *comp = 0; if (lossy) - *lossy = 1; + *lossy = EET_IMAGE_JPEG; if (quality) *quality = 75; @@ -1489,6 +2242,38 @@ eet_data_image_header_decode_cipher(const void *data, return 1; } } + else if (!strncmp(data, "TGV1", 4)) + { + const char *m = data; + + if (w) *w = ntohl(*((unsigned int*) &(m[OFFSET_WIDTH]))); + if (h) *h = ntohl(*((unsigned int*) &(m[OFFSET_HEIGHT]))); + if (comp) *comp = m[OFFSET_OPTIONS] & 0x1; + switch (m[OFFSET_ALGORITHM] & 0xFF) + { + case 0: + if (lossy) *lossy = EET_IMAGE_ETC1; + if (alpha) *alpha = EINA_FALSE; + break; + case 1: + if (lossy) *lossy = EET_IMAGE_ETC2_RGB; + if (alpha) *alpha = EINA_FALSE; + break; + case 2: + if (alpha) *alpha = EINA_TRUE; + if (lossy) *lossy = EET_IMAGE_ETC2_RGBA; + break; + case 3: + if (alpha) *alpha = EINA_TRUE; + if (lossy) *lossy = EET_IMAGE_ETC1_ALPHA; + break; + default: + return 0; + } + if (quality) *quality = 50; + + return 1; + } else { unsigned int iw = 0, ih = 0; @@ -1510,7 +2295,7 @@ eet_data_image_header_decode_cipher(const void *data, *comp = 0; if (lossy) - *lossy = 1; + *lossy = EET_IMAGE_JPEG; if (quality) *quality = 75; @@ -1519,9 +2304,57 @@ eet_data_image_header_decode_cipher(const void *data, } } + free(deciphered_d); return 0; } +static const Eet_Colorspace _eet_etc1_colorspace[] = { + EET_COLORSPACE_ETC1, + EET_COLORSPACE_ARGB8888 +}; + +static const Eet_Colorspace _eet_etc1_alpha_colorspace[] = { + EET_COLORSPACE_ETC1_ALPHA, + EET_COLORSPACE_ARGB8888 +}; + +static const Eet_Colorspace _eet_etc2_rgb_colorspace[] = { + EET_COLORSPACE_RGB8_ETC2, + EET_COLORSPACE_ARGB8888 +}; + +static const Eet_Colorspace _eet_etc2_rgba_colorspace[] = { + EET_COLORSPACE_RGBA8_ETC2_EAC, + EET_COLORSPACE_ARGB8888 +}; + +EAPI int +eet_data_image_colorspace_get(Eet_File *ef, + const char *name, + const char *cipher_key, + const Eet_Colorspace **cspaces) +{ + Eet_Image_Encoding lossy; + int r; + + r = eet_data_image_header_read_cipher(ef, name, cipher_key, NULL, NULL, NULL, NULL, NULL, &lossy); + if (!r) return r; + + if (cspaces) + { + if (lossy == EET_IMAGE_ETC1) + *cspaces = _eet_etc1_colorspace; + else if (lossy == EET_IMAGE_ETC2_RGB) + *cspaces = _eet_etc2_rgb_colorspace; + else if (lossy == EET_IMAGE_ETC2_RGBA) + *cspaces = _eet_etc2_rgba_colorspace; + else if (lossy == EET_IMAGE_ETC1_ALPHA) + *cspaces = _eet_etc1_alpha_colorspace; + } + + return r; +} + EAPI int eet_data_image_header_decode(const void *data, int size, @@ -1530,7 +2363,7 @@ eet_data_image_header_decode(const void *data, int *alpha, int *comp, int *quality, - int *lossy) + Eet_Image_Encoding *lossy) { return eet_data_image_header_decode_cipher(data, NULL, @@ -1581,9 +2414,10 @@ _eet_data_image_decode_inside(const void *data, int alpha, int comp, int quality, - int lossy) + Eet_Image_Encoding lossy, + Eet_Colorspace cspace) { - if (lossy == 0 && quality == 100) + if (lossy == EET_IMAGE_LOSSLESS && quality == 100) { unsigned int *body; @@ -1670,7 +2504,7 @@ _eet_data_image_decode_inside(const void *data, for (x = 0; x < (w * h); x++) SWAP32(d[x]); } } - else if (comp == 0 && lossy == 1) + else if (comp == 0 && lossy == EET_IMAGE_JPEG) { if (alpha) { @@ -1704,6 +2538,15 @@ _eet_data_image_decode_inside(const void *data, h, row_stride)) return 0; } + else if ((lossy == EET_IMAGE_ETC1) || + (lossy == EET_IMAGE_ETC2_RGB) || + (lossy == EET_IMAGE_ETC2_RGBA) || + (lossy == EET_IMAGE_ETC1_ALPHA)) + { + return eet_data_image_etc2_decode(data, size, d, + src_x, src_y, src_w, src_h, + alpha, cspace, lossy); + } else abort(); @@ -1719,11 +2562,12 @@ eet_data_image_decode_cipher(const void *data, int *alpha, int *comp, int *quality, - int *lossy) + Eet_Image_Encoding *lossy) { unsigned int *d = NULL; unsigned int iw, ih; - int ialpha, icompress, iquality, ilossy; + int ialpha, icompress, iquality; + Eet_Image_Encoding ilossy; void *deciphered_d = NULL; unsigned int deciphered_sz = 0; @@ -1750,7 +2594,8 @@ eet_data_image_decode_cipher(const void *data, return NULL; if (!_eet_data_image_decode_inside(data, size, 0, 0, iw, ih, d, iw, ih, iw * - 4, ialpha, icompress, iquality, ilossy)) + 4, ialpha, icompress, iquality, ilossy, + EET_COLORSPACE_ARGB8888)) { free(d); return NULL; @@ -1785,29 +2630,31 @@ eet_data_image_decode(const void *data, int *alpha, int *comp, int *quality, - int *lossy) + Eet_Image_Encoding *lossy) { return eet_data_image_decode_cipher(data, NULL, size, w, h, alpha, comp, quality, lossy); } EAPI int -eet_data_image_decode_to_surface_cipher(const void *data, - const char *cipher_key, - int size, - unsigned int src_x, - unsigned int src_y, - unsigned int *d, - unsigned int w, - unsigned int h, - unsigned int row_stride, - int *alpha, - int *comp, - int *quality, - int *lossy) +eet_data_image_decode_to_cspace_surface_cipher(const void *data, + const char *cipher_key, + int size, + unsigned int src_x, + unsigned int src_y, + unsigned int *d, + unsigned int w, + unsigned int h, + unsigned int row_stride, + Eet_Colorspace cspace, + int *alpha, + int *comp, + int *quality, + Eet_Image_Encoding *lossy) { unsigned int iw, ih; - int ialpha, icompress, iquality, ilossy; + int ialpha, icompress, iquality; + Eet_Image_Encoding ilossy; void *deciphered_d = NULL; unsigned int deciphered_sz = 0; @@ -1832,7 +2679,24 @@ eet_data_image_decode_to_surface_cipher(const void *data, if (!d) return 0; - if (w * 4 > row_stride) + if (cspace == EET_COLORSPACE_ETC1 && + ilossy != EET_IMAGE_ETC1) + return 0; + + if (cspace == EET_COLORSPACE_RGB8_ETC2 && + ilossy != EET_IMAGE_ETC2_RGB) + return 0; + + if (cspace == EET_COLORSPACE_RGBA8_ETC2_EAC && + ilossy != EET_IMAGE_ETC2_RGBA) + return 0; + + if (cspace == EET_COLORSPACE_ETC1_ALPHA && + ilossy != EET_IMAGE_ETC1_ALPHA) + return 0; + + if (cspace == EET_COLORSPACE_ARGB8888 && + w * 4 > row_stride) return 0; if (w > iw || h > ih) @@ -1840,7 +2704,7 @@ eet_data_image_decode_to_surface_cipher(const void *data, if (!_eet_data_image_decode_inside(data, size, src_x, src_y, iw, ih, d, w, h, row_stride, ialpha, icompress, iquality, - ilossy)) + ilossy, cspace)) return 0; if (alpha) @@ -1859,6 +2723,24 @@ eet_data_image_decode_to_surface_cipher(const void *data, } EAPI int +eet_data_image_decode_to_surface_cipher(const void *data, + const char *cipher_key, + int size, + unsigned int src_x, + unsigned int src_y, + unsigned int *d, + unsigned int w, + unsigned int h, + unsigned int row_stride, + int *alpha, + int *comp, + int *quality, + Eet_Image_Encoding *lossy) +{ + return eet_data_image_decode_to_cspace_surface_cipher(data, cipher_key, size, src_x, src_y, d, w, h, row_stride, EET_COLORSPACE_ARGB8888, alpha, comp, quality, lossy); +} + +EAPI int eet_data_image_decode_to_surface(const void *data, int size, unsigned int src_x, @@ -1870,7 +2752,7 @@ eet_data_image_decode_to_surface(const void *data, int *alpha, int *comp, int *quality, - int *lossy) + Eet_Image_Encoding *lossy) { return eet_data_image_decode_to_surface_cipher(data, NULL, size, src_x, src_y, d, diff --git a/src/lib/eet_lib.c b/src/lib/eet_lib.c index bef99b3..792638b 100644 --- a/src/lib/eet_lib.c +++ b/src/lib/eet_lib.c @@ -675,6 +675,19 @@ eet_shutdown(void) eina_lock_free(&eet_cache_lock); #ifdef HAVE_GNUTLS + /* Note that gnutls has a leak where it doesnt free stuff it alloced + * on init. valgrind trace here: + * 21 bytes in 1 blocks are definitely lost in loss record 24 of 194 + * at 0x4C2B6CD: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) + * by 0x68AC801: strdup (strdup.c:43) + * by 0xD215B6A: p11_kit_registered_module_to_name (in /usr/lib/x86_64-linux-gnu/libp11-kit.so.0.0.0) + * by 0x9571574: gnutls_pkcs11_init (in /usr/lib/x86_64-linux-gnu/libgnutls.so.26.21.8) + * by 0x955B031: gnutls_global_init (in /usr/lib/x86_64-linux-gnu/libgnutls.so.26.21.8) + * by 0x6DFD6D0: eet_init (eet_lib.c:608) + * + * yes - i've tried calling gnutls_pkcs11_deinit() by hand but no luck. + * the leak is in there. + */ gnutls_global_deinit(); #endif /* ifdef HAVE_GNUTLS */ #ifdef HAVE_OPENSSL @@ -937,7 +950,7 @@ eet_internal_read2(Eet_File *ef) ef)) return NULL; - ef->ed = eet_dictionary_calloc(1); + ef->ed = eet_dictionary_add(); if (eet_test_close(!ef->ed, ef)) return NULL; @@ -1474,7 +1487,10 @@ open_error: else { if (mode != EET_FILE_MODE_WRITE) - return NULL; + { + UNLOCK_CACHE; + return NULL; + } size = 0; @@ -1582,6 +1598,9 @@ EAPI const void * eet_identity_x509(Eet_File *ef, int *der_length) { + if (eet_check_pointer(ef)) + return NULL; + if (!ef->x509_der) return NULL; @@ -1595,6 +1614,9 @@ EAPI const void * eet_identity_signature(Eet_File *ef, int *signature_length) { + if (eet_check_pointer(ef)) + return NULL; + if (!ef->signature) return NULL; @@ -1608,6 +1630,9 @@ EAPI const void * eet_identity_sha1(Eet_File *ef, int *sha1_length) { + if (eet_check_pointer(ef)) + return NULL; + if (!ef->sha1) ef->sha1 = eet_identity_compute_sha1(ef->data, ef->data_size, @@ -2032,7 +2057,10 @@ eet_alias_get(Eet_File *ef, } if (tmp[compr_size - 1] != '\0') - goto on_error; + { + free(tmp); + goto on_error; + } UNLOCK_FILE(ef); diff --git a/src/lib/eet_node.c b/src/lib/eet_node.c index faaa905..a9466ef 100644 --- a/src/lib/eet_node.c +++ b/src/lib/eet_node.c @@ -593,7 +593,7 @@ eet_node_dump_group_start(int level, int chnk_type; chnk_type = (group_type >= EET_G_UNKNOWN && group_type <= EET_G_HASH) ? - group_type : EET_G_LAST; + group_type : EET_G_UNKNOWN; eet_node_dump_level(level, dumpfunc, dumpdata); dumpfunc(dumpdata, "group \""); diff --git a/src/lib/lz4/README b/src/lib/lz4/README index 0abb857..718f773 100644 --- a/src/lib/lz4/README +++ b/src/lib/lz4/README @@ -4,4 +4,4 @@ by: yann.collet.73@gmail.com Copyright/licensing info in source files here. -this was from revsion 66. +this was from revision 84. diff --git a/src/lib/lz4/lz4.c b/src/lib/lz4/lz4.c index 06e2829..a651748 100644 --- a/src/lib/lz4/lz4.c +++ b/src/lib/lz4/lz4.c @@ -34,31 +34,24 @@ //************************************** // Tuning parameters //************************************** -// COMPRESSIONLEVEL : -// Increasing this value improves compression ratio -// Lowering this value reduces memory usage -// Reduced memory usage typically improves speed, due to cache effect (ex : L1 32KB for Intel, L1 64KB for AMD) -// Memory usage formula : N->2^(N+2) Bytes (examples : 12 -> 16KB ; 17 -> 512KB) -#define COMPRESSIONLEVEL 12 - -// NOTCOMPRESSIBLE_CONFIRMATION : +// MEMORY_USAGE : +// Memory usage formula : N->2^N Bytes (examples : 10 -> 1KB; 12 -> 4KB ; 16 -> 64KB; 20 -> 1MB; etc.) +// Increasing memory usage improves compression ratio +// Reduced memory usage can improve speed, due to cache effect +// Default value is 14, for 16KB, which nicely fits into Intel x86 L1 cache +#define MEMORY_USAGE 14 + +// NOTCOMPRESSIBLE_DETECTIONLEVEL : // Decreasing this value will make the algorithm skip faster data segments considered "incompressible" // This may decrease compression ratio dramatically, but will be faster on incompressible data // Increasing this value will make the algorithm search more before declaring a segment "incompressible" // This could improve compression a bit, but will be slower on incompressible data // The default value (6) is recommended -#define NOTCOMPRESSIBLE_CONFIRMATION 6 - -// LZ4_COMPRESSMIN : -// Compression function will *fail* if it is not successful at compressing input by at least LZ4_COMPRESSMIN bytes -// Since the compression function stops working prematurely, it results in a speed gain -// The output however is unusable. Compression function result will be zero. -// Default : 0 = disabled -#define LZ4_COMPRESSMIN 0 +#define NOTCOMPRESSIBLE_DETECTIONLEVEL 6 // BIG_ENDIAN_NATIVE_BUT_INCOMPATIBLE : -// This will provide a boost to performance for big endian cpu, but the resulting compressed stream will be incompatible with little-endian CPU. -// You can set this option to 1 in situations where data will stay within closed environment +// This will provide a small boost to performance for big endian cpu, but the resulting compressed stream will be incompatible with little-endian CPU. +// You can set this option to 1 in situations where data will remain within closed environment // This option is useless on Little_Endian CPU (such as x86) //#define BIG_ENDIAN_NATIVE_BUT_INCOMPATIBLE 1 @@ -98,7 +91,7 @@ //************************************** // Compiler Options //************************************** -#if __STDC_VERSION__ >= 199901L // C99 +#if __STDC_VERSION__ >= 199901L // C99 /* "restrict" is a known keyword */ #else # define restrict // Disable restrict @@ -107,7 +100,7 @@ #define GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__) #ifdef _MSC_VER // Visual Studio -# define inline __forceinline // Visual is not C99, but supports some kind of inline +# include // For Visual 2005 # if LZ4_ARCH64 // 64-bit # pragma intrinsic(_BitScanForward64) // For Visual 2005 # pragma intrinsic(_BitScanReverse64) // For Visual 2005 @@ -181,11 +174,11 @@ typedef struct _U64_S { U64 v; } U64_S; //************************************** #define MINMATCH 4 -#define HASH_LOG COMPRESSIONLEVEL +#define HASH_LOG (MEMORY_USAGE-2) #define HASHTABLESIZE (1 << HASH_LOG) #define HASH_MASK (HASHTABLESIZE - 1) -#define SKIPSTRENGTH (NOTCOMPRESSIBLE_CONFIRMATION>2?NOTCOMPRESSIBLE_CONFIRMATION:2) +#define SKIPSTRENGTH (NOTCOMPRESSIBLE_DETECTIONLEVEL>2?NOTCOMPRESSIBLE_DETECTIONLEVEL:2) #define STACKLIMIT 13 #define HEAPMODE (HASH_LOG>STACKLIMIT) // Defines if memory is allocated into the stack (local variable), or into the heap (malloc()). #define COPYLENGTH 8 @@ -196,8 +189,8 @@ typedef struct _U64_S { U64 v; } U64_S; #define MAXD_LOG 16 #define MAX_DISTANCE ((1 << MAXD_LOG) - 1) -#define ML_BITS 4 -#define ML_MASK ((1U<= 304) && !defined(LZ4_FORCE_SW_BITCOUNT) return (__builtin_clzll(val) >> 3); #else - int r; - if (!(val>>32)) { r=4; } else { r=0; val>>=32; } - if (!(val>>16)) { r+=2; val>>=8; } else { val>>=24; } - r += (!val); - return r; + int r; + if (!(val>>32)) { r=4; } else { r=0; val>>=32; } + if (!(val>>16)) { r+=2; val>>=8; } else { val>>=24; } + r += (!val); + return r; #endif #else #if defined(_MSC_VER) && !defined(LZ4_FORCE_SW_BITCOUNT) @@ -281,15 +274,15 @@ inline static int LZ4_NbCommonBytes (register U64 val) #elif defined(__GNUC__) && (GCC_VERSION >= 304) && !defined(LZ4_FORCE_SW_BITCOUNT) return (__builtin_ctzll(val) >> 3); #else - static const int DeBruijnBytePos[64] = { 0, 0, 0, 0, 0, 1, 1, 2, 0, 3, 1, 3, 1, 4, 2, 7, 0, 2, 3, 6, 1, 5, 3, 5, 1, 3, 4, 4, 2, 5, 6, 7, 7, 0, 1, 2, 3, 3, 4, 6, 2, 6, 5, 5, 3, 4, 5, 6, 7, 1, 2, 4, 6, 4, 4, 5, 7, 2, 6, 5, 7, 6, 7, 7 }; - return DeBruijnBytePos[((U64)((val & -val) * 0x0218A392CDABBD3F)) >> 58]; + static const int DeBruijnBytePos[64] = { 0, 0, 0, 0, 0, 1, 1, 2, 0, 3, 1, 3, 1, 4, 2, 7, 0, 2, 3, 6, 1, 5, 3, 5, 1, 3, 4, 4, 2, 5, 6, 7, 7, 0, 1, 2, 3, 3, 4, 6, 2, 6, 5, 5, 3, 4, 5, 6, 7, 1, 2, 4, 6, 4, 4, 5, 7, 2, 6, 5, 7, 6, 7, 7 }; + return DeBruijnBytePos[((U64)((val & -val) * 0x0218A392CDABBD3F)) >> 58]; #endif #endif } #else -inline static int LZ4_NbCommonBytes (register U32 val) +static inline int LZ4_NbCommonBytes (register U32 val) { #if defined(LZ4_BIG_ENDIAN) #if defined(_MSC_VER) && !defined(LZ4_FORCE_SW_BITCOUNT) @@ -299,10 +292,10 @@ inline static int LZ4_NbCommonBytes (register U32 val) #elif defined(__GNUC__) && (GCC_VERSION >= 304) && !defined(LZ4_FORCE_SW_BITCOUNT) return (__builtin_clz(val) >> 3); #else - int r; - if (!(val>>16)) { r=2; val>>=8; } else { r=0; val>>=24; } - r += (!val); - return r; + int r; + if (!(val>>16)) { r=2; val>>=8; } else { r=0; val>>=24; } + r += (!val); + return r; #endif #else #if defined(_MSC_VER) && !defined(LZ4_FORCE_SW_BITCOUNT) @@ -312,8 +305,8 @@ inline static int LZ4_NbCommonBytes (register U32 val) #elif defined(__GNUC__) && (GCC_VERSION >= 304) && !defined(LZ4_FORCE_SW_BITCOUNT) return (__builtin_ctz(val) >> 3); #else - static const int DeBruijnBytePos[32] = { 0, 0, 3, 0, 3, 1, 3, 0, 3, 2, 2, 1, 3, 2, 0, 1, 3, 3, 1, 2, 2, 2, 2, 0, 3, 1, 2, 0, 1, 0, 1, 1 }; - return DeBruijnBytePos[((U32)((val & -(S32)val) * 0x077CB531U)) >> 27]; + static const int DeBruijnBytePos[32] = { 0, 0, 3, 0, 3, 1, 3, 0, 3, 2, 2, 1, 3, 2, 0, 1, 3, 3, 1, 2, 2, 2, 2, 0, 3, 1, 2, 0, 1, 0, 1, 1 }; + return DeBruijnBytePos[((U32)((val & -(S32)val) * 0x077CB531U)) >> 27]; #endif #endif } @@ -321,154 +314,173 @@ inline static int LZ4_NbCommonBytes (register U32 val) #endif -//**************************** -// Public functions -//**************************** - -int LZ4_compressBound(int isize) -{ - return (isize + (isize/255) + 16); -} - - //****************************** // Compression functions //****************************** -int LZ4_compressCtx(void** ctx, - const char* source, - char* dest, - int isize) +// LZ4_compressCtx : +// ----------------- +// Compress 'isize' bytes from 'source' into an output buffer 'dest' of maximum size 'maxOutputSize'. +// If it cannot achieve it, compression will stop, and result of the function will be zero. +// return : the number of bytes written in buffer 'dest', or 0 if the compression fails + +static inline int LZ4_compressCtx(void** ctx, + const char* source, + char* dest, + int isize, + int maxOutputSize) { #if HEAPMODE - struct refTables *srt = (struct refTables *) (*ctx); - HTYPE* HashTable; + struct refTables *srt = (struct refTables *) (*ctx); + HTYPE* HashTable; #else - HTYPE HashTable[HASHTABLESIZE] = {0}; + HTYPE HashTable[HASHTABLESIZE] = {0}; #endif - const BYTE* ip = (BYTE*) source; - INITBASE(base); - const BYTE* anchor = ip; - const BYTE* const iend = ip + isize; - const BYTE* const mflimit = iend - MFLIMIT; + const BYTE* ip = (BYTE*) source; + INITBASE(base); + const BYTE* anchor = ip; + const BYTE* const iend = ip + isize; + const BYTE* const mflimit = iend - MFLIMIT; #define matchlimit (iend - LASTLITERALS) - BYTE* op = (BYTE*) dest; + BYTE* op = (BYTE*) dest; + BYTE* const oend = op + maxOutputSize; - int len, length; - const int skipStrength = SKIPSTRENGTH; - U32 forwardH; + int len, length; + const int skipStrength = SKIPSTRENGTH; + U32 forwardH; - // Init - if (isizehashTable); - memset((void*)HashTable, 0, sizeof(srt->hashTable)); + if (*ctx == NULL) + { + srt = (struct refTables *) malloc ( sizeof(struct refTables) ); + *ctx = (void*) srt; + } + HashTable = (HTYPE*)(srt->hashTable); + memset((void*)HashTable, 0, sizeof(srt->hashTable)); #else - (void) ctx; + (void) ctx; #endif - // First Byte - HashTable[LZ4_HASH_VALUE(ip)] = ip - base; - ip++; forwardH = LZ4_HASH_VALUE(ip); + // First Byte + HashTable[LZ4_HASH_VALUE(ip)] = ip - base; + ip++; forwardH = LZ4_HASH_VALUE(ip); - // Main Loop + // Main Loop for ( ; ; ) - { - int findMatchAttempts = (1U << skipStrength) + 3; - const BYTE* forwardIp = ip; - const BYTE* ref; - BYTE* token; - - // Find a match - do { - U32 h = forwardH; - int step = findMatchAttempts++ >> skipStrength; - ip = forwardIp; - forwardIp = ip + step; - - if unlikely(forwardIp > mflimit) { goto _last_literals; } - - forwardH = LZ4_HASH_VALUE(forwardIp); - ref = base + HashTable[h]; - HashTable[h] = ip - base; - - } while ((ref < ip - MAX_DISTANCE) || (A32(ref) != A32(ip))); - - // Catch up - while ((ip>anchor) && (ref>(BYTE*)source) && unlikely(ip[-1]==ref[-1])) { ip--; ref--; } - - // Encode Literal length - length = ip - anchor; - token = op++; - if (length>=(int)RUN_MASK) { *token=(RUN_MASK< 254 ; len-=255) *op++ = 255; *op++ = (BYTE)len; } - else *token = (length<> skipStrength; + ip = forwardIp; + forwardIp = ip + step; + + if unlikely(forwardIp > mflimit) { goto _last_literals; } + + forwardH = LZ4_HASH_VALUE(forwardIp); + ref = base + HashTable[h]; + HashTable[h] = ip - base; + + } while ((ref < ip - MAX_DISTANCE) || (A32(ref) != A32(ip))); + + // Catch up + while ((ip>anchor) && (ref>(BYTE*)source) && unlikely(ip[-1]==ref[-1])) { ip--; ref--; } + + // Encode Literal length + length = (int)(ip - anchor); + token = op++; + if unlikely(op + length + (2 + 1 + LASTLITERALS) + (length>>8) > oend) return 0; // Check output limit +#ifdef _MSC_VER + if (length>=(int)RUN_MASK) + { + int len = length-RUN_MASK; + *token=(RUN_MASK<254) + { + do { *op++ = 255; len -= 255; } while (len>254); + *op++ = (BYTE)len; + memcpy(op, anchor, length); + op += length; + goto _next_match; + } + else + *op++ = (BYTE)len; + } + else *token = (length<=(int)RUN_MASK) { *token=(RUN_MASK< 254 ; len-=255) *op++ = 255; *op++ = (BYTE)len; } + else *token = (length<=(int)ML_MASK) { *token+=ML_MASK; len-=ML_MASK; for(; len > 509 ; len-=510) { *op++ = 255; *op++ = 255; } if (len > 254) { len-=255; *op++ = 255; } *op++ = (BYTE)len; } - else *token += len; + // Encode MatchLength + len = (int)(ip - anchor); + if unlikely(op + (1 + LASTLITERALS) + (len>>8) > oend) return 0; // Check output limit + if (len>=(int)ML_MASK) { *token+=ML_MASK; len-=ML_MASK; for(; len > 509 ; len-=510) { *op++ = 255; *op++ = 255; } if (len > 254) { len-=255; *op++ = 255; } *op++ = (BYTE)len; } + else *token += len; - // Test end of chunk - if (ip > mflimit) { anchor = ip; break; } + // Test end of chunk + if (ip > mflimit) { anchor = ip; break; } - // Fill table - HashTable[LZ4_HASH_VALUE(ip-2)] = ip - 2 - base; + // Fill table + HashTable[LZ4_HASH_VALUE(ip-2)] = ip - 2 - base; - // Test next position - ref = base + HashTable[LZ4_HASH_VALUE(ip)]; - HashTable[LZ4_HASH_VALUE(ip)] = ip - base; - if ((ref > ip - (MAX_DISTANCE + 1)) && (A32(ref) == A32(ip))) { token = op++; *token=0; goto _next_match; } + // Test next position + ref = base + HashTable[LZ4_HASH_VALUE(ip)]; + HashTable[LZ4_HASH_VALUE(ip)] = ip - base; + if ((ref > ip - (MAX_DISTANCE + 1)) && (A32(ref) == A32(ip))) { token = op++; *token=0; goto _next_match; } - // Prepare next loop - anchor = ip++; - forwardH = LZ4_HASH_VALUE(ip); - } + // Prepare next loop + anchor = ip++; + forwardH = LZ4_HASH_VALUE(ip); + } _last_literals: - // Encode Last Literals - { - int lastRun = iend - anchor; - if ((LZ4_COMPRESSMIN>0) && (((op - (BYTE*)dest) + lastRun + 1 + ((lastRun-15)/255)) > isize - LZ4_COMPRESSMIN)) return 0; - if (lastRun>=(int)RUN_MASK) { *op++=(RUN_MASK< 254 ; lastRun-=255) *op++ = 255; *op++ = (BYTE) lastRun; } - else *op++ = (lastRun< (U32)maxOutputSize) return 0; + if (lastRun>=(int)RUN_MASK) { *op++=(RUN_MASK< 254 ; lastRun-=255) *op++ = 255; *op++ = (BYTE) lastRun; } + else *op++ = (lastRun<> ((MINMATCH*8)-HASHLOG64K)) #define LZ4_HASH64K_VALUE(p) LZ4_HASH64K_FUNCTION(A32(p)) -int LZ4_compress64kCtx(void** ctx, - const char* source, - char* dest, - int isize) +static inline int LZ4_compress64kCtx(void** ctx, + const char* source, + char* dest, + int isize, + int maxOutputSize) { #if HEAPMODE - struct refTables *srt = (struct refTables *) (*ctx); - U16* HashTable; + struct refTables *srt = (struct refTables *) (*ctx); + U16* HashTable; #else - U16 HashTable[HASH64KTABLESIZE] = {0}; + U16 HashTable[HASH64KTABLESIZE] = {0}; #endif - const BYTE* ip = (BYTE*) source; - const BYTE* anchor = ip; - const BYTE* const base = ip; - const BYTE* const iend = ip + isize; - const BYTE* const mflimit = iend - MFLIMIT; + const BYTE* ip = (BYTE*) source; + const BYTE* anchor = ip; + const BYTE* const base = ip; + const BYTE* const iend = ip + isize; + const BYTE* const mflimit = iend - MFLIMIT; #define matchlimit (iend - LASTLITERALS) - BYTE* op = (BYTE*) dest; + BYTE* op = (BYTE*) dest; + BYTE* const oend = op + maxOutputSize; - int len, length; - const int skipStrength = SKIPSTRENGTH; - U32 forwardH; + int len, length; + const int skipStrength = SKIPSTRENGTH; + U32 forwardH; - // Init - if (isizehashTable); - memset((void*)HashTable, 0, sizeof(srt->hashTable)); + if (*ctx == NULL) + { + srt = (struct refTables *) malloc ( sizeof(struct refTables) ); + *ctx = (void*) srt; + } + HashTable = (U16*)(srt->hashTable); + memset((void*)HashTable, 0, sizeof(srt->hashTable)); #else - (void) ctx; + (void) ctx; #endif - // First Byte - ip++; forwardH = LZ4_HASH64K_VALUE(ip); + // First Byte + ip++; forwardH = LZ4_HASH64K_VALUE(ip); - // Main Loop + // Main Loop for ( ; ; ) - { - int findMatchAttempts = (1U << skipStrength) + 3; - const BYTE* forwardIp = ip; - const BYTE* ref; - BYTE* token; - - // Find a match - do { - U32 h = forwardH; - int step = findMatchAttempts++ >> skipStrength; - ip = forwardIp; - forwardIp = ip + step; - - if (forwardIp > mflimit) { goto _last_literals; } - - forwardH = LZ4_HASH64K_VALUE(forwardIp); - ref = base + HashTable[h]; - HashTable[h] = ip - base; - - } while (A32(ref) != A32(ip)); - - // Catch up - while ((ip>anchor) && (ref>(BYTE*)source) && (ip[-1]==ref[-1])) { ip--; ref--; } - - // Encode Literal length - length = ip - anchor; - token = op++; - if (length>=(int)RUN_MASK) { *token=(RUN_MASK< 254 ; len-=255) *op++ = 255; *op++ = (BYTE)len; } - else *token = (length<> skipStrength; + ip = forwardIp; + forwardIp = ip + step; + + if (forwardIp > mflimit) { goto _last_literals; } + + forwardH = LZ4_HASH64K_VALUE(forwardIp); + ref = base + HashTable[h]; + HashTable[h] = (U16)(ip - base); + + } while (A32(ref) != A32(ip)); + + // Catch up + while ((ip>anchor) && (ref>(BYTE*)source) && (ip[-1]==ref[-1])) { ip--; ref--; } + + // Encode Literal length + length = (int)(ip - anchor); + token = op++; + if unlikely(op + length + (2 + 1 + LASTLITERALS) + (length>>8) > oend) return 0; // Check output limit +#ifdef _MSC_VER + if (length>=(int)RUN_MASK) + { + int len = length-RUN_MASK; + *token=(RUN_MASK<254) + { + do { *op++ = 255; len -= 255; } while (len>254); + *op++ = (BYTE)len; + memcpy(op, anchor, length); + op += length; + goto _next_match; + } + else + *op++ = (BYTE)len; + } + else *token = (length<=(int)RUN_MASK) { *token=(RUN_MASK< 254 ; len-=255) *op++ = 255; *op++ = (BYTE)len; } + else *token = (length<=(int)ML_MASK) { *token+=ML_MASK; len-=ML_MASK; for(; len > 509 ; len-=510) { *op++ = 255; *op++ = 255; } if (len > 254) { len-=255; *op++ = 255; } *op++ = (BYTE)len; } - else *token += len; + // Encode MatchLength + len = (int)(ip - anchor); + if unlikely(op + (1 + LASTLITERALS) + (len>>8) > oend) return 0; // Check output limit + if (len>=(int)ML_MASK) { *token+=ML_MASK; len-=ML_MASK; for(; len > 509 ; len-=510) { *op++ = 255; *op++ = 255; } if (len > 254) { len-=255; *op++ = 255; } *op++ = (BYTE)len; } + else *token += len; - // Test end of chunk - if (ip > mflimit) { anchor = ip; break; } + // Test end of chunk + if (ip > mflimit) { anchor = ip; break; } - // Fill table - HashTable[LZ4_HASH64K_VALUE(ip-2)] = ip - 2 - base; + // Fill table + HashTable[LZ4_HASH64K_VALUE(ip-2)] = (U16)(ip - 2 - base); - // Test next position - ref = base + HashTable[LZ4_HASH64K_VALUE(ip)]; - HashTable[LZ4_HASH64K_VALUE(ip)] = ip - base; - if (A32(ref) == A32(ip)) { token = op++; *token=0; goto _next_match; } + // Test next position + ref = base + HashTable[LZ4_HASH64K_VALUE(ip)]; + HashTable[LZ4_HASH64K_VALUE(ip)] = (U16)(ip - base); + if (A32(ref) == A32(ip)) { token = op++; *token=0; goto _next_match; } - // Prepare next loop - anchor = ip++; - forwardH = LZ4_HASH64K_VALUE(ip); - } + // Prepare next loop + anchor = ip++; + forwardH = LZ4_HASH64K_VALUE(ip); + } _last_literals: - // Encode Last Literals - { - int lastRun = iend - anchor; - if ((LZ4_COMPRESSMIN>0) && (((op - (BYTE*)dest) + lastRun + 1 + ((lastRun-15)/255)) > isize - LZ4_COMPRESSMIN)) return 0; - if (lastRun>=(int)RUN_MASK) { *op++=(RUN_MASK< 254 ; lastRun-=255) *op++ = 255; *op++ = (BYTE) lastRun; } - else *op++ = (lastRun< oend) return 0; + if (lastRun>=(int)RUN_MASK) { *op++=(RUN_MASK< 254 ; lastRun-=255) *op++ = 255; *op++ = (BYTE) lastRun; } + else *op++ = (lastRun<>ML_BITS)) == RUN_MASK) { for (;(len=*ip++)==255;length+=255){} length += len; } - - // copy literals - cpy = op+length; - if unlikely(cpy>oend-COPYLENGTH) - { - if (cpy > oend) goto _output_error; // Error : request to write beyond destination buffer - memcpy(op, ip, length); - ip += length; - break; // Necessarily EOF - } - LZ4_WILDCOPY(ip, op, cpy); ip -= (op-cpy); op = cpy; - - // get offset - LZ4_READ_LITTLEENDIAN_16(ref,cpy,ip); ip+=2; - if (ref < (BYTE* const)dest) goto _output_error; // Error : offset create reference outside destination buffer - - // get matchlength - if ((length=(token&ML_MASK)) == ML_MASK) { for (;*ip==255;length+=255) {ip++;} length += *ip++; } - - // copy repeated sequence - if unlikely(op-ref>ML_BITS)) == RUN_MASK) { for (;(len=*ip++)==255;length+=255){} length += len; } + + // copy literals + cpy = op+length; + if unlikely(cpy>oend-COPYLENGTH) + { + if (cpy != oend) goto _output_error; // Error : we must necessarily stand at EOF + memcpy(op, ip, length); + ip += length; + break; // EOF + } + LZ4_WILDCOPY(ip, op, cpy); ip -= (op-cpy); op = cpy; + + // get offset + LZ4_READ_LITTLEENDIAN_16(ref,cpy,ip); ip+=2; + if (ref < (BYTE* const)dest) goto _output_error; // Error : offset create reference outside destination buffer + + // get matchlength + if ((length=(token&ML_MASK)) == ML_MASK) { for (;*ip==255;length+=255) {ip++;} length += *ip++; } + + // copy repeated sequence + if unlikely(op-refoend-COPYLENGTH) - { - if (cpy > oend) goto _output_error; // Error : request to write beyond destination buffer - LZ4_SECURECOPY(ref, op, (oend-COPYLENGTH)); - while(opoend-COPYLENGTH) + { + if (cpy > oend) goto _output_error; // Error : request to write beyond destination buffer + LZ4_SECURECOPY(ref, op, (oend-COPYLENGTH)); + while(op>ML_BITS)) == RUN_MASK) { int s=255; while ((ipoend-COPYLENGTH) || (ip+length>iend-COPYLENGTH)) - { - if (cpy > oend) goto _output_error; // Error : request to write beyond destination buffer - if (ip+length > iend) goto _output_error; // Error : request to read beyond source buffer - memcpy(op, ip, length); - op += length; - ip += length; - if (ip>ML_BITS)) == RUN_MASK) { int s=255; while ((ipoend-COPYLENGTH) || (ip+length>iend-COPYLENGTH)) + { + if (cpy > oend) goto _output_error; // Error : writes beyond output buffer + if (ip+length != iend) goto _output_error; // Error : LZ4 format requires to consume all input at this stage + memcpy(op, ip, length); + op += length; + ip = iend; + break; // Necessarily EOF, due to parsing restrictions + } + LZ4_WILDCOPY(ip, op, cpy); ip -= (op-cpy); op = cpy; + + // get offset + LZ4_READ_LITTLEENDIAN_16(ref,cpy,ip); ip+=2; + if (ref < (BYTE* const)dest) goto _output_error; // Error : offset creates reference outside of destination buffer + + // get matchlength + if ((length=(token&ML_MASK)) == ML_MASK) { while (ipoend-COPYLENGTH) - { - if (cpy > oend) goto _output_error; // Error : request to write outside of destination buffer - LZ4_SECURECOPY(ref, op, (oend-COPYLENGTH)); - while(opoend-COPYLENGTH) + { + if (cpy > oend) goto _output_error; // Error : request to write outside of destination buffer + LZ4_SECURECOPY(ref, op, (oend-COPYLENGTH)); + while(op // For Visual 2005 +# if LZ4_ARCH64 // 64-bit +# pragma intrinsic(_BitScanForward64) // For Visual 2005 +# pragma intrinsic(_BitScanReverse64) // For Visual 2005 +# else +# pragma intrinsic(_BitScanForward) // For Visual 2005 +# pragma intrinsic(_BitScanReverse) // For Visual 2005 +# endif #endif #ifdef _MSC_VER // Visual Studio -#define bswap16(x) _byteswap_ushort(x) +#define lz4_bswap16(x) _byteswap_ushort(x) #else -#define bswap16(x) ((unsigned short int) ((((x) >> 8) & 0xffu) | (((x) & 0xffu) << 8))) +#define lz4_bswap16(x) ((unsigned short int) ((((x) >> 8) & 0xffu) | (((x) & 0xffu) << 8))) #endif @@ -174,8 +182,8 @@ typedef struct _U64_S { U64 v; } U64_S; #endif #if defined(LZ4_BIG_ENDIAN) -#define LZ4_READ_LITTLEENDIAN_16(d,s,p) { U16 v = A16(p); v = bswap16(v); d = (s) - v; } -#define LZ4_WRITE_LITTLEENDIAN_16(p,i) { U16 v = (U16)(i); v = bswap16(v); A16(p) = v; p+=2; } +#define LZ4_READ_LITTLEENDIAN_16(d,s,p) { U16 v = A16(p); v = lz4_bswap16(v); d = (s) - v; } +#define LZ4_WRITE_LITTLEENDIAN_16(p,i) { U16 v = (U16)(i); v = lz4_bswap16(v); A16(p) = v; p+=2; } #else // Little Endian #define LZ4_READ_LITTLEENDIAN_16(d,s,p) { d = (s) - A16(p); } #define LZ4_WRITE_LITTLEENDIAN_16(p,v) { A16(p) = v; p+=2; } @@ -329,7 +337,7 @@ inline static int LZ4HC_InsertAndFindBestMatch (LZ4HC_Data_Structure* hc4, const // HC4 match finder LZ4HC_Insert(hc4, ip); ref = HASH_POINTER(ip); - while ((ref > (ip-MAX_DISTANCE)) && (nbAttempts)) + while ((ref >= (ip-MAX_DISTANCE)) && (nbAttempts)) { nbAttempts--; if (*(ref+ml) == *(ip+ml)) @@ -350,7 +358,7 @@ inline static int LZ4HC_InsertAndFindBestMatch (LZ4HC_Data_Structure* hc4, const if ((ipt ml) { ml = ipt-ip; *matchpos = ref; } + if (ipt-ip > ml) { ml = (int)(ipt-ip); *matchpos = ref; } } ref = GETNEXT(ref); } @@ -366,13 +374,13 @@ inline static int LZ4HC_InsertAndGetWiderMatch (LZ4HC_Data_Structure* hc4, const INITBASE(base,hc4->base); const BYTE* ref; int nbAttempts = MAX_NB_ATTEMPTS; - int delta = ip-startLimit; + int delta = (int)(ip-startLimit); // First Match LZ4HC_Insert(hc4, ip); ref = HASH_POINTER(ip); - while ((ref > ip-MAX_DISTANCE) && (ref >= hc4->base) && (nbAttempts)) + while ((ref >= ip-MAX_DISTANCE) && (ref >= hc4->base) && (nbAttempts)) { nbAttempts--; if (*(startLimit + longest) == *(ref - delta + longest)) @@ -399,7 +407,7 @@ _endCount: if ((ipt-startt) > longest) { - longest = ipt-startt; + longest = (int)(ipt-startt); *matchpos = reft; *startpos = startt; } @@ -417,7 +425,7 @@ inline static int LZ4_encodeSequence(const BYTE** ip, BYTE** op, const BYTE** an BYTE* token; // Encode Literal length - length = *ip - *anchor; + length = (int)(*ip - *anchor); token = (*op)++; if (length>=(int)RUN_MASK) { *token=(RUN_MASK< 254 ; len-=255) *(*op)++ = 255; *(*op)++ = (BYTE)len; } else *token = (length< OPTIMAL_ML) new_ml = OPTIMAL_ML; - if (ip+new_ml > start2 + ml2 - MINMATCH) new_ml = start2 - ip + ml2 - MINMATCH; - correction = new_ml - (start2 - ip); + if (ip+new_ml > start2 + ml2 - MINMATCH) new_ml = (int)(start2 - ip) + ml2 - MINMATCH; + correction = new_ml - (int)(start2 - ip); if (correction > 0) { start2 += correction; @@ -543,8 +551,8 @@ _Search3: { int correction; if (ml > OPTIMAL_ML) ml = OPTIMAL_ML; - if (ip+ml > start2 + ml2 - MINMATCH) ml = start2 - ip + ml2 - MINMATCH; - correction = ml - (start2 - ip); + if (ip+ml > start2 + ml2 - MINMATCH) ml = (int)(start2 - ip) + ml2 - MINMATCH; + correction = ml - (int)(start2 - ip); if (correction > 0) { start2 += correction; @@ -554,7 +562,7 @@ _Search3: } else { - ml = start2 - ip; + ml = (int)(start2 - ip); } } // Now, encode 2 sequences @@ -570,7 +578,7 @@ _Search3: { if (start2 < ip+ml) { - int correction = (ip+ml) - start2; + int correction = (int)(ip+ml - start2); start2 += correction; ref2 += correction; ml2 -= correction; @@ -607,8 +615,8 @@ _Search3: { int correction; if (ml > OPTIMAL_ML) ml = OPTIMAL_ML; - if (ip + ml > start2 + ml2 - MINMATCH) ml = start2 - ip + ml2 - MINMATCH; - correction = ml - (start2 - ip); + if (ip + ml > start2 + ml2 - MINMATCH) ml = (int)(start2 - ip) + ml2 - MINMATCH; + correction = ml - (int)(start2 - ip); if (correction > 0) { start2 += correction; @@ -618,7 +626,7 @@ _Search3: } else { - ml = start2 - ip; + ml = (int)(start2 - ip); } } LZ4_encodeSequence(&ip, &op, &anchor, ml, ref); @@ -637,7 +645,7 @@ _Search3: // Encode Last Literals { - int lastRun = iend - anchor; + int lastRun = (int)(iend - anchor); if (lastRun>=(int)RUN_MASK) { *op++=(RUN_MASK< 254 ; lastRun-=255) *op++ = 255; *op++ = (BYTE) lastRun; } else *op++ = (lastRun< +in C++ and then converted to plain C by the EFL team to fit the rest of +the EFL. + +The original code comes from: +https://code.google.com/p/rg-etc1/ + +For the ZLIB license below, see http://opensource.org/licenses/Zlib + +Copyright (c) 2012 Rich Geldreich + +This software is provided 'as-is', without any express or implied +warranty. In no event will the authors be held liable for any damages +arising from the use of this software. + +Permission is granted to anyone to use this software for any purpose, +including commercial applications, and to alter it and redistribute it +freely, subject to the following restrictions: + +1. The origin of this software must not be misrepresented; you must not +claim that you wrote the original software. If you use this software +in a product, an acknowledgment in the product documentation would be +appreciated but is not required. + +2. Altered source versions must be plainly marked as such, and must not be +misrepresented as being the original software. + +3. This notice may not be removed or altered from any source distribution. + diff --git a/src/lib/rg_etc/etc2_encoder.c b/src/lib/rg_etc/etc2_encoder.c new file mode 100644 index 0000000..60f167a --- /dev/null +++ b/src/lib/rg_etc/etc2_encoder.c @@ -0,0 +1,1052 @@ +/* +Copyright (C) 2014 Jean-Philippe ANDRE + +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, +INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, +OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF +LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, +EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +#include +#include "rg_etc1.h" + +// Enable this flag when working on (quality) optimizations +//#define DEBUG + +#ifndef EINA_INLIST_FREE +/** + * @def EINA_INLIST_FREE + * @param list The list to free. + * @param it The pointer to the list item, i.e. a pointer to each item + * that is part of the list. + * + * NOTE: it is the duty of the body loop to properly remove the item from the + * inlist and free it. This function will turn into a infinite loop if you + * don't remove all items from the list. + * @since 1.8 + */ +#define EINA_INLIST_FREE(list, it) \ + for (it = (__typeof__(it)) list; list; it = (__typeof__(it)) list) +#endif + +// Weights for the distance (perceptual mode) - sum is ~1024 +static const int R_WEIGHT = 299 * 1024 / 1000; +static const int G_WEIGHT = 587 * 1024 / 1000; +static const int B_WEIGHT = 114 * 1024 / 1000; + +static const int kTargetError[3] = { + 5*5*16, // 34 dB + 2*2*16, // 42 dB + 0 // infinite dB +}; + +// For T and H modes +static const int kDistances[8] = { + 3, 6, 11, 16, 23, 32, 41, 64 +}; + +// For differential mode +static const int kSigned3bit[8] = { + 0, 1, 2, 3, -4, -3, -2, -1 +}; + +// For alpha support +static const int kAlphaModifiers[16][8] = { + { -3, -6, -9, -15, 2, 5, 8, 14}, + { -3, -7, -10, -13, 2, 6, 9, 12}, + { -2, -5, -8, -13, 1, 4, 7, 12}, + { -2, -4, -6, -13, 1, 3, 5, 12}, + { -3, -6, -8, -12, 2, 5, 7, 11}, + { -3, -7, -9, -11, 2, 6, 8, 10}, + { -4, -7, -8, -11, 3, 6, 7, 10}, + { -3, -5, -8, -11, 2, 4, 7, 10}, + { -2, -6, -8, -10, 1, 5, 7, 9}, + { -2, -5, -8, -10, 1, 4, 7, 9}, + { -2, -4, -8, -10, 1, 3, 7, 9}, + { -2, -5, -7, -10, 1, 4, 6, 9}, + { -3, -4, -7, -10, 2, 3, 6, 9}, + { -1, -2, -3, -10, 0, 1, 2, 9}, + { -4, -6, -8, -9, 3, 5, 7, 8}, + { -3, -5, -7, -9, 2, 4, 6, 8} +}; + +// Damn OpenGL people, why don't you just pack data as on a CPU??? +static const int kBlockWalk[16] = { + 0, 4, 8, 12, + 1, 5, 9, 13, + 2, 6, 10, 14, + 3, 7, 11, 15 +}; + +// Use with static constants so the compiler can optimize everything +#define BITS(byteval, lowbit, highbit) \ + (((byteval) >> (lowbit)) & ((1 << ((highbit) - (lowbit) + 1)) - 1)) + +#define BIT(byteval, bit) \ + (((byteval) >> (bit)) & 0x1) + +// Real clamp +#define CLAMP(a) ({ int _b = (a); (((_b) >= 0) ? (((_b) < 256) ? (_b) : 255) : 0); }) + +// Simple min +#define MIN(a,b) ({ int _z = (a), _y = (b); ((_z <= _y) ? _z : _y); }) + +// Simple max +#define MAX(a,b) ({ int __z = (a), __y = (b); ((__z > __y) ? __z : __y); }) + +// Simple clamp between two values +#define MINMAX(a,b,c) (MIN(c,MAX(a,b))) + +// Simple abs +#define ABS(a) ({ int _a = (a); ((_a >= 0) ? _a : (-_a)); }) + +// Write a BGRA value for output to Evas +#define BGRA(r,g,b,a) ((a << 24) | (r << 16) | (g << 8) | b) + +#ifndef WORDS_BIGENDIAN +/* x86 */ +#define A_VAL(p) (((uint8_t *)(p))[3]) +#define R_VAL(p) (((uint8_t *)(p))[2]) +#define G_VAL(p) (((uint8_t *)(p))[1]) +#define B_VAL(p) (((uint8_t *)(p))[0]) +#define R_IDX 2 +#define G_IDX 1 +#define B_IDX 0 +#else +/* ppc */ +#define A_VAL(p) (((uint8_t *)(p))[0]) +#define R_VAL(p) (((uint8_t *)(p))[1]) +#define G_VAL(p) (((uint8_t *)(p))[2]) +#define B_VAL(p) (((uint8_t *)(p))[3]) +#define R_IDX 1 +#define G_IDX 2 +#define B_IDX 3 +#endif + +#ifndef DBG +# ifdef DEBUG +# define DBG(fmt, ...) fprintf(stderr, "%s:%d: " fmt "\n", __FUNCTION__, __LINE__, ## __VA_ARGS__) +# else +# define DBG(...) +# endif +#endif + +/** Pack alpha block given a modifier table and a multiplier + * @returns Squared error + */ +static int +_etc2_alpha_block_pack(uint8_t *etc2_alpha, + const int base_codeword, + const int multiplier, + const int modifierIdx, + const uint32_t *bgra, + const Eina_Bool write) +{ + const int *alphaModifiers = kAlphaModifiers[modifierIdx]; + uint8_t alphaIndexes[16]; + int errAcc2 = 0; + + // Header + if (write) + { + etc2_alpha[0] = base_codeword & 0xFF; + etc2_alpha[1] = ((multiplier << 4) & 0xF0) | (modifierIdx & 0x0F); + } + + // Compute alphas now + for (int i = 0; i < 16; i++) + { + const int realA = A_VAL(bgra + kBlockWalk[i]); + int minErr = INT_MAX, idx = 0; + + // Brute force -- find modifier index + for (int k = 0; (k < 8) && minErr; k++) + { + int tryA = CLAMP(base_codeword + alphaModifiers[k] * multiplier); + int err = ABS(realA - tryA); + if (err < minErr) + { + minErr = err; + idx = k; + if (!minErr) break; + } + } + + alphaIndexes[i] = idx; + + // Keep some stats + errAcc2 += minErr * minErr; + } + + if (write) + for (int k = 0; k < 2; k++) + { + etc2_alpha[2 + 3 * k] = alphaIndexes[0 + 8 * k] << 5; // A + etc2_alpha[2 + 3 * k] |= alphaIndexes[1 + 8 * k] << 2; // B + etc2_alpha[2 + 3 * k] |= (alphaIndexes[2 + 8 * k] >> 1) & 0x3; // C01 + etc2_alpha[3 + 3 * k] = (alphaIndexes[2 + 8 * k] & 0x1) << 7; // C2 + etc2_alpha[3 + 3 * k] |= alphaIndexes[3 + 8 * k] << 4; // D + etc2_alpha[3 + 3 * k] |= alphaIndexes[4 + 8 * k] << 1; // E + etc2_alpha[3 + 3 * k] |= (alphaIndexes[5 + 8 * k] >> 2) & 0x1; // F0 + etc2_alpha[4 + 3 * k] = (alphaIndexes[5 + 8 * k] & 0x3) << 6; // F12 + etc2_alpha[4 + 3 * k] |= alphaIndexes[6 + 8 * k] << 3; // G + etc2_alpha[4 + 3 * k] |= alphaIndexes[7 + 8 * k]; // H + } + + return errAcc2; +} + +static int +_etc2_alpha_encode(uint8_t *etc2_alpha, const uint32_t *bgra, + const rg_etc1_pack_params *params) +{ + int alphas[16], avg = 0, diff = 0, maxDiff = 0, minErr = INT_MAX; + int base_codeword; + int multiplier, bestMult = 0; + int modifierIdx, bestIdx = 0, bestBase = 128; + int err, base_range, base_step = 1, max_error = 0; + + // Try to select the best alpha value (avg) + for (int i = 0; i < 16; i++) + { + alphas[i] = A_VAL(bgra + kBlockWalk[i]); + avg += alphas[i]; + } + avg /= 16; + + for (int i = 0; i < 16; i++) + { + int thisDiff = ABS(alphas[i] - avg); + maxDiff = MAX(thisDiff, maxDiff); + diff += thisDiff; + } + + base_codeword = alphas[0]; + if (!diff) + { + // All same alphas + etc2_alpha[0] = base_codeword; + memset(etc2_alpha + 1, 0, 7); + return 0; + } + + // Bruteforce -- try all tables and all multipliers, oh my god this will be slow. + max_error = kTargetError[params->m_quality]; + switch (params->m_quality) + { + // The follow parameters are completely arbitrary. + // Need some real testing. + case rg_etc1_high_quality: // exhaustive search + base_range = 255; + base_step = 1; + break; + case rg_etc1_medium_quality: // tweaked for "decent" results + base_range = 40; + base_step = 4; + break; + case rg_etc1_low_quality: // fast (not even fastest) + base_range = 8; + base_step = 4; + break; + } + + // for loop avg, avg-1, avg+1, avg-2, avg+2, ... + for (int step = 0; step < base_range; step += base_step) + for (base_codeword = CLAMP(avg - step); + base_codeword <= CLAMP(avg + step);) + { + for (modifierIdx = 0; modifierIdx < 16; modifierIdx++) + for (multiplier = 0; multiplier < 16; multiplier++) + { + if ((ABS(multiplier * kAlphaModifiers[modifierIdx][3]) + ABS(base_codeword - avg)) < maxDiff) + continue; + + err = _etc2_alpha_block_pack(etc2_alpha, base_codeword, + multiplier, modifierIdx, bgra, EINA_FALSE); + if (err < minErr) + { + minErr = err; + bestMult = multiplier; + bestIdx = modifierIdx; + bestBase = base_codeword; + if (err <= max_error) + goto pack_now; + + } + } + if (step <= 0) break; + if (base_codeword < 255) + base_codeword = CLAMP(base_codeword + 2 * step); + else + break; + } + +pack_now: + err = _etc2_alpha_block_pack(etc2_alpha, bestBase, + bestMult, bestIdx, bgra, EINA_TRUE); + return err; +} + +static Eina_Bool +_etc2_t_mode_header_pack(uint8_t *etc2, + uint32_t color1, uint32_t color2, int distance) +{ + // 4 bit colors + int r1_4 = R_VAL(&color1) >> 4; + int g1_4 = G_VAL(&color1) >> 4; + int b1_4 = B_VAL(&color1) >> 4; + int r2_4 = R_VAL(&color2) >> 4; + int g2_4 = G_VAL(&color2) >> 4; + int b2_4 = B_VAL(&color2) >> 4; + int distanceIdx, R, dR; + + for (distanceIdx = 0; distanceIdx < 8; distanceIdx++) + if (kDistances[distanceIdx] == distance) break; + + if (distanceIdx >= 8) + return EINA_FALSE; + + // R1. R + [dR] must be outside [0..31]. Scanning all values. Not smart. + R = r1_4 >> 2; + dR = r1_4 & 0x3; + for (int Rx = 0; Rx < 8; Rx++) + for (int dRx = 0; dRx < 2; dRx++) + { + int Rtry = R | (Rx << 2); + int dRtry = dR | (dRx << 2); + if ((Rtry + kSigned3bit[dRtry]) < 0 || (Rtry + kSigned3bit[dRtry] > 31)) + { + R = Rtry; + dR = dRtry; + break; + } + } + if ((R + kSigned3bit[dR]) >= 0 && (R + kSigned3bit[dR] <= 31)) + // this can't happen, should be an assert + return EINA_FALSE; + + etc2[0] = ((R & 0x1F) << 3) | (dR & 0x7); + + // G1, B1 + etc2[1] = (g1_4 << 4) | b1_4; + + // R2, G2 + etc2[2] = (r2_4 << 4) | g2_4; + + // B2, distance + etc2[3] = (b2_4 << 4) | ((distanceIdx >> 1) << 2) | (1 << 1) | (distanceIdx & 0x1); + + return EINA_TRUE; +} + +static Eina_Bool +_etc2_h_mode_header_pack(uint8_t *etc2, Eina_Bool *swap_colors, + uint32_t color1, uint32_t color2, int distance) +{ + int distanceIdx, R, dR, G, dG, distanceSpecialBit, da, db; + int r1_4, g1_4, b1_4, r2_4, g2_4, b2_4; + uint32_t c1, c2; + + for (distanceIdx = 0; distanceIdx < 8; distanceIdx++) + if (kDistances[distanceIdx] == distance) break; + + if (distanceIdx >= 8) + return EINA_FALSE; + + // The distance is coded in 3 bits. But in H mode, one bit is not coded + // in the header, as we use the comparison result between the two colors + // to select it. + distanceSpecialBit = BIT(distanceIdx, 0); + db = BIT(distanceIdx, 1); + da = BIT(distanceIdx, 2); + + // Note: if c1 == c2, no big deal because H is not the best choice of mode + if (distanceSpecialBit) + { + c1 = MAX(color1, color2); + c2 = MIN(color1, color2); + } + else + { + c1 = MIN(color1, color2); + c2 = MAX(color1, color2); + } + + // Return flag so we use the proper colors when packing the block + *swap_colors = (c1 != color1); + + // 4 bit colors + r1_4 = R_VAL(&c1) >> 4; + g1_4 = G_VAL(&c1) >> 4; + b1_4 = B_VAL(&c1) >> 4; + r2_4 = R_VAL(&c2) >> 4; + g2_4 = G_VAL(&c2) >> 4; + b2_4 = B_VAL(&c2) >> 4; + + // R1 + G1a. R + [dR] must be inside [0..31]. Scanning all values. Not smart. + R = r1_4; + dR = g1_4 >> 1; + if ((R + kSigned3bit[dR]) < 0 || (R + kSigned3bit[dR] > 31)) + R |= (1 << 4); + + if ((R + kSigned3bit[dR]) < 0 || (R + kSigned3bit[dR] > 31)) + return EINA_FALSE; // wtf? + + etc2[0] = ((R & 0x1F) << 3) | (dR & 0x7); + + // G1b + B1a + B1b[2 msb]. G + dG must be outside the range. + G = (g1_4 & 0x1) << 1; + G |= BIT(b1_4, 3); + dG = BITS(b1_4, 1, 2); + for (int Gx = 0; Gx < 8; Gx++) + for (int dGx = 0; dGx < 2; dGx++) + { + int Gtry = G | (Gx << 2); + int dGtry = dG | (dGx << 2); + if ((Gtry + kSigned3bit[dGtry]) < 0 || (Gtry + kSigned3bit[dGtry] > 31)) + { + G = Gtry; + dG = dGtry; + break; + } + } + + if ((G + kSigned3bit[dG]) >= 0 && (G + kSigned3bit[dG] <= 31)) + return EINA_FALSE; // wtf? + + etc2[1] = ((G & 0x1F) << 3) | (dG & 0x7); + + // B1[lsb] + R2 + G2 [3 msb] + etc2[2] = ((b1_4 & 0x1) << 7) | (r2_4 << 3) | (g2_4 >> 1); + + // G2[lsb] + B2 + distance + etc2[3] = ((g2_4 & 0x1) << 7) | (b2_4 << 3) + | (da << 2) | 0x2 | db; + + return EINA_TRUE; +} + +static inline int +_rgb_distance_percept(uint32_t color1, uint32_t color2) +{ + int R = R_VAL(&color1) - R_VAL(&color2); + int G = G_VAL(&color1) - G_VAL(&color2); + int B = B_VAL(&color1) - B_VAL(&color2); + return (R * R * R_WEIGHT) + (G * G * G_WEIGHT) + (B * B * B_WEIGHT); +} + +static inline int +_rgb_distance_euclid(uint32_t color1, uint32_t color2) +{ + int R = R_VAL(&color1) - R_VAL(&color2); + int G = G_VAL(&color1) - G_VAL(&color2); + int B = B_VAL(&color1) - B_VAL(&color2); + return (R * R) + (G * G) + (B * B); +} + +static unsigned int +_etc2_th_mode_block_pack(uint8_t *etc2, Eina_Bool h_mode, + uint32_t color1, uint32_t color2, int distance, + const uint32_t *bgra, Eina_Bool write, + Eina_Bool *swap_colors) +{ + uint8_t paint_colors[4][4]; + int errAcc = 0; + + if (write) + { + memset(etc2 + 4, 0, 4); + if (!h_mode) + { + if (!_etc2_t_mode_header_pack(etc2, color1, color2, distance)) + return INT_MAX; // assert + } + else + { + if (!_etc2_h_mode_header_pack(etc2, swap_colors, + color1, color2, distance)) + return INT_MAX; // assert + if (*swap_colors) + { + uint32_t tmp = color1; + color1 = color2; + color2 = tmp; + } + } + } + + for (int k = 0; k < 4; k++) + { + if (!h_mode) + { + paint_colors[0][k] = ((uint8_t *) &color1)[k]; + paint_colors[1][k] = CLAMP(((uint8_t *) &color2)[k] + distance); + paint_colors[2][k] = ((uint8_t *) &color2)[k]; + paint_colors[3][k] = CLAMP(((uint8_t *) &color2)[k] - distance); + } + else + { + paint_colors[0][k] = CLAMP(((uint8_t *) &color1)[k] + distance); + paint_colors[1][k] = CLAMP(((uint8_t *) &color1)[k] - distance); + paint_colors[2][k] = CLAMP(((uint8_t *) &color2)[k] + distance); + paint_colors[3][k] = CLAMP(((uint8_t *) &color2)[k] - distance); + } + } + + for (int k = 0; k < 16; k++) + { + uint32_t pixel = bgra[kBlockWalk[k]]; + int bestDist = INT_MAX; + int bestIdx = 0; + + for (int idx = 0; idx < 4; idx++) + { + int dist = _rgb_distance_euclid(pixel, *((uint32_t *) paint_colors[idx])); + if (dist < bestDist) + { + bestDist = dist; + bestIdx = idx; + if (!dist) break; + } + } + + errAcc += bestDist; + + if (write) + { + etc2[5 - (k >> 3)] |= ((bestIdx & 0x2) ? 1 : 0) << (k & 0x7); + etc2[7 - (k >> 3)] |= (bestIdx & 0x1) << (k & 0x7); + } + } + + return errAcc; +} + +static uint32_t +_color_reduce_444(uint32_t color) +{ + int R = R_VAL(&color); + int G = G_VAL(&color); + int B = B_VAL(&color); + int R1, R2, G1, G2, B1, B2; + + R1 = (R & 0xF0) | (R >> 4); + R2 = ((R & 0xF0) + 0x10) | ((R >> 4) + 1); + G1 = (G & 0xF0) | (G >> 4); + G2 = ((G & 0xF0) + 0x10) | ((G >> 4) + 1); + B1 = (B & 0xF0) | (B >> 4); + B2 = ((B & 0xF0) + 0x10) | ((B >> 4) + 1); + + R = (ABS(R - R1) <= ABS(R - R2)) ? R1 : R2; + G = (ABS(G - G1) <= ABS(G - G2)) ? G1 : G2; + B = (ABS(B - B1) <= ABS(B - B2)) ? B1 : B2; + + return BGRA(R, G, B, 255); +} + +static uint32_t +_color_reduce_676(uint32_t color) +{ + int R = R_VAL(&color); + int G = G_VAL(&color); + int B = B_VAL(&color); + int R1, G1, B1; + + // FIXME: Do we have better candidates to try? + R1 = (R & 0xFC) | (R >> 6); + G1 = (G & 0xFE) | (G >> 7); + B1 = (B & 0xFC) | (B >> 6); + + return BGRA(R1, G1, B1, 255); +} + +static int +_block_main_colors_find(uint32_t *color1_out, uint32_t *color2_out, + uint32_t color1, uint32_t color2, const uint32_t *bgra, + const rg_etc1_pack_params *params EINA_UNUSED) +{ + static const int kMaxIterations = 20; + + int errAcc; + + /* k-means complexity is O(n^(d.k+1) log n) + * In this case, n = 16, k = 2, d = 3 so 20 loops + */ + + if (color1 == color2) + { + // We should select another mode (planar) to encode flat colors + // We could also dither with two approximated colors + *color1_out = *color2_out = color1; + goto found; + } + + if (color1 == color2) + { + // We should dither... + *color1_out = *color2_out = color1; + goto found; + } + + for (int iter = 0; iter < kMaxIterations; iter++) + { + int r1 = 0, r2 = 0, g1 = 0, g2 = 0, b1 = 0, b2 = 0; + int cluster1_cnt = 0, cluster2_cnt = 0; + int cluster1[16], cluster2[16]; + int maxDist1 = 0, maxDist2 = 0; + uint32_t c1, c2; + + errAcc = 0; + memset(cluster1, 0, sizeof(cluster1)); + memset(cluster2, 0, sizeof(cluster2)); + + // k-means assignment step + for (int k = 0; k < 16; k++) + { + int dist1 = _rgb_distance_euclid(color1, bgra[k]); + int dist2 = _rgb_distance_euclid(color2, bgra[k]); + if (dist1 <= dist2) + { + cluster1[cluster1_cnt++] = k; + if (dist1 > maxDist1) + maxDist1 = dist1; + } + else + { + cluster2[cluster2_cnt++] = k; + if (dist2 > maxDist2) + maxDist2 = dist2; + } + } + + // k-means failed + if (!cluster1_cnt || !cluster2_cnt) + return -1; + + // k-means update step + for (int k = 0; k < cluster1_cnt; k++) + { + r1 += R_VAL(bgra + cluster1[k]); + g1 += G_VAL(bgra + cluster1[k]); + b1 += B_VAL(bgra + cluster1[k]); + } + + for (int k = 0; k < cluster2_cnt; k++) + { + r2 += R_VAL(bgra + cluster2[k]); + g2 += G_VAL(bgra + cluster2[k]); + b2 += B_VAL(bgra + cluster2[k]); + } + + r1 /= cluster1_cnt; + g1 /= cluster1_cnt; + b1 /= cluster1_cnt; + r2 /= cluster2_cnt; + g2 /= cluster2_cnt; + b2 /= cluster2_cnt; + + c1 = _color_reduce_444(BGRA(r1, g1, b1, 255)); + c2 = _color_reduce_444(BGRA(r2, g2, b2, 255)); + if (c1 == color1 && c2 == color2) + break; + + if (c1 != c2) + { + color1 = c1; + color2 = c2; + } + else if (_rgb_distance_euclid(c1, color1) > _rgb_distance_euclid(c2, color2)) + { + color1 = c1; + } + else + { + color2 = c2; + } + } + + *color1_out = color1; + *color2_out = color2; + +found: + errAcc = 0; + for (int k = 0; k < 16; k++) + errAcc += _rgb_distance_euclid(bgra[k], color2); + return errAcc; +} + +static unsigned int +_etc2_th_mode_block_encode(uint8_t *etc2, const uint32_t *bgra, + const rg_etc1_pack_params *params) +{ + int err, bestDist = kDistances[0]; + int minErr = INT_MAX, bestMode = 0; + uint32_t c1, c2, bestC1 = bgra[0], bestC2 = bgra[1]; + Eina_Bool swap_colors = EINA_FALSE; + + Eina_Inlist *tried_pairs = NULL; + struct ColorPair { + EINA_INLIST; + uint32_t low; + uint32_t high; + }; + struct ColorPair *pair; + + /* Bruteforce algo: + * Bootstrap k-means clustering with all possible pairs of colors + * from the 4x4 block. + * TODO: Don't retry the same rgb444 pairs again + */ + + for (int pix1 = 0; pix1 < 15; pix1++) + for (int pix2 = pix1 + 1; pix2 < 16; pix2++) + { + Eina_Bool already_tried = EINA_FALSE; + + // Bootstrap k-means. Find new pair of colors. + c1 = _color_reduce_444(bgra[pix1]); + c2 = _color_reduce_444(bgra[pix2]); + + if (c2 > c1) + { + uint32_t tmp = c2; + c2 = c1; + c1 = tmp; + } + + EINA_INLIST_FOREACH(tried_pairs, pair) + if (c1 == pair->high && c2 == pair->low) + { + already_tried = EINA_TRUE; + break; + } + + if (already_tried) + continue; + + pair = calloc(1, sizeof(*pair)); + if (pair) + { + pair->high = c1; + pair->low = c2; + tried_pairs = eina_inlist_append(tried_pairs, EINA_INLIST_GET(pair)); + } + + // Run k-means + err = _block_main_colors_find(&c1, &c2, c1, c2, bgra, params); + if (err < 0) + continue; + + for (int distIdx = 0; distIdx < 8; distIdx++) + { + for (int mode = 0; mode < 2; mode++) + { + + for (int swap = 0; swap < 2; swap++) + { + if (mode == 0 && swap) + { + uint32_t tmp = c2; + c2 = c1; + c1 = tmp; + } + err = _etc2_th_mode_block_pack(etc2, mode, c1, c2, + kDistances[distIdx], + bgra, EINA_FALSE, + &swap_colors); + if (err < minErr) + { + bestDist = kDistances[distIdx]; + minErr = err; + bestC1 = (!swap_colors) ? c1 : c2; + bestC2 = (!swap_colors) ? c2 : c1; + bestMode = mode; + if (err <= kTargetError[params->m_quality]) + goto found; + } + } + } + } + } + +found: + EINA_INLIST_FREE(tried_pairs, pair) + { + tried_pairs = eina_inlist_remove(tried_pairs, EINA_INLIST_GET(pair)); + free(pair); + } + + err = _etc2_th_mode_block_pack(etc2, bestMode, bestC1, bestC2, bestDist, + bgra, EINA_TRUE, &swap_colors); + + return err; +} + +static inline Eina_Bool +_etc2_planar_mode_header_pack(uint8_t *etc2, + uint32_t RO, uint32_t RH, uint32_t RV, + uint32_t GO, uint32_t GH, uint32_t GV, + uint32_t BO, uint32_t BH, uint32_t BV) +{ + int R, dR; + int G, dG; + int B, dB; + + // RO_6 [2..5] + R = BITS(RO >> 2, 2, 5); + // RO_6 [0..1] + GO_7[6] + dR = (BITS(RO >> 2, 0, 1) << 1) | BIT(GO >> 1, 6); + + if (!((R + kSigned3bit[dR] >= 0) && (R + kSigned3bit[dR] <= 31))) + R |= 1 << 4; + + // GO_7[2..5] + G = BITS(GO >> 1, 2, 5); + // GO_7[0..1] + BO_6[5] + dG = (BITS(GO >> 1, 0, 1) << 1) | BIT(BO >> 2, 5); + + if (!((G + kSigned3bit[dG] >= 0) && (G + kSigned3bit[dG] <= 31))) + G |= 1 << 4; + + // BO_6[3..4] + B = BITS(BO >> 2, 3, 4); + // BO_6[1..2] + dB = BITS(BO >> 2, 1, 2); + + // B + dB must be outside the range. + for (int Bx = 0; Bx < 8; Bx++) + for (int dBx = 0; dBx < 2; dBx++) + { + int Btry = B | (Bx << 2); + int dBtry = dB | (dBx << 2); + if ((Btry + kSigned3bit[dBtry]) < 0 || (Btry + kSigned3bit[dBtry] > 31)) + { + B = Btry; + dB = dBtry; + break; + } + } + + if (!((R + kSigned3bit[dR] >= 0) && (R + kSigned3bit[dR] <= 31))) + return EINA_FALSE; + + if (!((G + kSigned3bit[dG] >= 0) && (G + kSigned3bit[dG] <= 31))) + return EINA_FALSE; + + if ((B + kSigned3bit[dB] >= 0) && (B + kSigned3bit[dB] <= 31)) + return EINA_FALSE; + + // Write everything + etc2[0] = (R << 3) | dR; + etc2[1] = (G << 3) | dG; + etc2[2] = (B << 3) | dB; + etc2[3] = (BIT(BO >> 2, 0) << 7) | (BITS(RH >> 2, 1, 5) << 2) | 0x2 | BIT(RH >> 2, 0); + etc2[4] = ((GH >> 1) << 1) | BIT(BH >> 2, 5); + etc2[5] = (BITS(BH >> 2, 0, 4) << 3) | BITS(RV >> 2, 3, 5); + etc2[6] = (BITS(RV >> 2, 0, 2) << 5) | BITS(GV >> 1, 2, 6); + etc2[7] = (BITS(GV >> 1, 0, 1) << 6) | (BV >> 2); + + return EINA_TRUE; +} + +static unsigned int +_etc2_planar_mode_block_pack(uint8_t *etc2, + uint32_t Ocol, uint32_t Hcol, uint32_t Vcol, + const uint32_t *bgra, Eina_Bool write) +{ + unsigned int err = 0; + uint32_t RO, RH, RV, GO, GH, GV, BO, BH, BV; + + RO = R_VAL(&Ocol); + RH = R_VAL(&Hcol); + RV = R_VAL(&Vcol); + GO = G_VAL(&Ocol); + GH = G_VAL(&Hcol); + GV = G_VAL(&Vcol); + BO = B_VAL(&Ocol); + BH = B_VAL(&Hcol); + BV = B_VAL(&Vcol); + + if (write) + { + if (!_etc2_planar_mode_header_pack(etc2, RO, RH, RV, GO, GH, GV, BO, BH, BV)) + return INT_MAX; + } + + // Compute MSE, that's all we need to do + + for (int y = 0; y < 4; y++) + for (int x = 0; x < 4; x++) + { + const int R = CLAMP(((x * (RH - RO)) + y * (RV - RO) + 4 * RO + 2) >> 2); + const int G = CLAMP(((x * (GH - GO)) + y * (GV - GO) + 4 * GO + 2) >> 2); + const int B = CLAMP(((x * (BH - BO)) + y * (BV - BO) + 4 * BO + 2) >> 2); + uint32_t color = BGRA(R, G, B, 255); + + err += _rgb_distance_euclid(color, bgra[x + y * 4]); + } + + return err; +} + +static unsigned int +_etc2_planar_mode_block_encode(uint8_t *etc2, const uint32_t *bgra, + const rg_etc1_pack_params *params EINA_UNUSED) +{ + unsigned int err; + unsigned int Ocol, Hcol, Vcol, RO, GO, BO, Rx, Gx, Bx; + + // TODO: Scan a broader range to avoid artifacts when the + // points O, H or V are exceptions + + /* O is at (0,0) + * H is at (4,0) + * V is at (0,4) + * So, H and V are outside the block. + * We extrapolate the values from (0,3) and (3,0). + */ + + RO = R_VAL(&(bgra[0])); + GO = G_VAL(&(bgra[0])); + BO = B_VAL(&(bgra[0])); + Ocol = _color_reduce_676(bgra[0]); + + Rx = CLAMP(RO + (4 * (R_VAL(&(bgra[3])) - RO)) / 3); + Gx = CLAMP(GO + (4 * (G_VAL(&(bgra[3])) - GO)) / 3); + Bx = CLAMP(BO + (4 * (B_VAL(&(bgra[3])) - BO)) / 3); + Hcol = _color_reduce_676(BGRA(Rx, Gx, Bx, 0xFF)); + + Rx = CLAMP(RO + (4 * (R_VAL(&(bgra[12])) - RO)) / 3); + Gx = CLAMP(GO + (4 * (G_VAL(&(bgra[12])) - GO)) / 3); + Bx = CLAMP(BO + (4 * (B_VAL(&(bgra[12])) - BO)) / 3); + Vcol = _color_reduce_676(BGRA(Rx, Gx, Bx, 0xFF)); + + err = _etc2_planar_mode_block_pack(etc2, Ocol, Hcol, Vcol, bgra, EINA_TRUE); + + return err; +} + +#ifdef DEBUG +static unsigned int +_block_error_calc(const uint32_t *enc, const uint32_t *orig, Eina_Bool perceptual) +{ + unsigned int errAcc = 0; + + for (int k = 0; k < 16; k++) + { + if (perceptual) + errAcc += _rgb_distance_percept(enc[k], orig[k]); + else + errAcc += _rgb_distance_euclid(enc[k], orig[k]); + } + + return errAcc; +} +#endif + +unsigned int +etc2_rgba8_block_pack(unsigned char *etc2, const unsigned int *bgra, + rg_etc1_pack_params *params) +{ + rg_etc1_pack_params safe_params; + unsigned int errors[3] = { INT_MAX, INT_MAX, INT_MAX }; + unsigned int minErr = INT_MAX; + uint8_t etc2_try[3][8]; + int bestSolution = 0; + +#ifdef DEBUG + static int cnt [3] = {0}; +#endif + + safe_params.m_dithering = !!params->m_dithering; + safe_params.m_quality = MINMAX(params->m_quality, 0, 2); + + errors[0] = rg_etc1_pack_block(etc2_try[0], bgra, &safe_params); + errors[1] = _etc2_th_mode_block_encode(etc2_try[1], bgra, &safe_params); + errors[2] = _etc2_planar_mode_block_encode(etc2_try[2], bgra, &safe_params); + +#ifdef DEBUG + for (int i = 1; i < 3; i++) + { + const char *mode = (i == 1) ? "T or H" : "Planar"; + if (errors[i] < INT_MAX) + for (unsigned k = 0; k < sizeof(errors) / sizeof(*errors); k++) + { + uint32_t decoded[16]; + unsigned int real_errors[2]; + rg_etc2_rgb8_decode_block(etc2_try[i], decoded); + real_errors[0] = _block_error_calc(decoded, bgra, EINA_FALSE); + real_errors[1] = _block_error_calc(decoded, bgra, EINA_TRUE); + + if (real_errors[0] != errors[i]) + DBG("Invalid error calc in %s mode", mode); + } + } +#endif + + for (unsigned k = 0; k < sizeof(errors) / sizeof(*errors); k++) + if (errors[k] < minErr) + { + minErr = errors[k]; + bestSolution = k; + } + + memcpy(etc2 + 8, etc2_try[bestSolution], 8); + + minErr += _etc2_alpha_encode(etc2, bgra, &safe_params); + +#ifdef DEBUG + cnt[bestSolution]++; + DBG("Block count by mode: ETC1: %d T/H: %d Planar: %d. Err %d", + cnt[0], cnt[1], cnt[2], minErr); +#endif + + return minErr; +} + +unsigned int +etc2_rgb8_block_pack(unsigned char *etc2, const unsigned int *bgra, + rg_etc1_pack_params *params) +{ + rg_etc1_pack_params safe_params; + unsigned int errors[3] = { INT_MAX, INT_MAX, INT_MAX }; + unsigned int minErr = INT_MAX; + uint8_t etc2_try[3][8]; + int bestSolution = 0; + + safe_params.m_dithering = !!params->m_dithering; + safe_params.m_quality = MINMAX(params->m_quality, 0, 2); + + errors[0] = rg_etc1_pack_block(etc2_try[0], bgra, &safe_params); + errors[1] = _etc2_th_mode_block_encode(etc2_try[1], bgra, &safe_params); + errors[2] = _etc2_planar_mode_block_encode(etc2_try[2], bgra, &safe_params); + + for (unsigned k = 0; k < sizeof(errors) / sizeof(*errors); k++) + if (errors[k] < minErr) + { + minErr = errors[k]; + bestSolution = k; + } + + memcpy(etc2, etc2_try[bestSolution], 8); + + return minErr; +} diff --git a/src/lib/rg_etc/rg_etc1.c b/src/lib/rg_etc/rg_etc1.c new file mode 100644 index 0000000..59d79b4 --- /dev/null +++ b/src/lib/rg_etc/rg_etc1.c @@ -0,0 +1,2833 @@ +// File: rg_etc1.cpp - Fast, high quality ETC1 block packer/unpacker - Rich Geldreich +// Please see ZLIB license at the end of rg_etc1.h. +// +// For more information Ericsson Texture Compression (ETC/ETC1), see: +// http://www.khronos.org/registry/gles/extensions/OES/OES_compressed_ETC1_RGB8_texture.txt +// +// v1.04 - 5/15/14 - Fix signed vs. unsigned subtraction problem (noticed when compiled with gcc) in pack_etc1_block_init(). +// This issue would cause an assert when this func. was called in debug. (Note this module was developed/testing with MSVC, +// I still need to test it throughly when compiled with gcc.) +// +// v1.03 - 5/12/13 - Initial public release + +#include +#include +#include + +#include "Eina.h" + +#include "rg_etc1.h" + +#if defined(_DEBUG) || defined(DEBUG) +#define RG_ETC1_BUILD_DEBUG +#endif + +typedef unsigned long long uint64; +typedef unsigned char uint8; +typedef unsigned short uint16; +typedef unsigned int uint; +typedef unsigned int uint32; +typedef unsigned char DATA8; + +#define MIN(A, B) ((A < B) ? A : B) +#define MAX(A, B) ((A > B) ? A : B) +#define CLAMP(Value, Low, High) ((Value < Low) ? Low : ((Value > High) ? High : Value)) +#define SQUARE(Value) (Value * Value) + +#ifndef UINT_MAX +#define UINT_MAX 4294967295U +#endif + +#ifndef ULLONG_MAX +#define ULLONG_MAX 18446744073709551615ULL +#endif + +#define cUINT32_MAX UINT_MAX +#define cUINT64_MAX ULLONG_MAX +#define RG_ETC1_ARRAY_SIZE(X) (sizeof(X) / sizeof((X)[0])) + +// Some configuration defines + +// Disable this constrained function, it produces artifacts (in black areas mostly) +#define RG_ETC1_CONSTRAINED_SUBBLOCK 0 +// Disable dithering. It uses invalid RGBA order and isn't great visually +// Dithering should happen AFTER the color selection, not before +#define RG_ETC1_DITHERING 0 + +enum RG_Etc_Constants + { + cETC1BytesPerBlock = 8U, + + cETC1SelectorBits = 2U, + cETC1SelectorValues = 1U << cETC1SelectorBits, + cETC1SelectorMask = cETC1SelectorValues - 1U, + + cETC1BlockShift = 2U, + cETC1BlockSize = 1U << cETC1BlockShift, + + cETC1LSBSelectorIndicesBitOffset = 0, + cETC1MSBSelectorIndicesBitOffset = 16, + + cETC1FlipBitOffset = 32, + cETC1DiffBitOffset = 33, + + cETC1IntenModifierNumBits = 3, + cETC1IntenModifierValues = 1 << cETC1IntenModifierNumBits, + cETC1RightIntenModifierTableBitOffset = 34, + cETC1LeftIntenModifierTableBitOffset = 37, + + // Base+Delta encoding (5 bit bases, 3 bit delta) + cETC1BaseColorCompNumBits = 5, + cETC1BaseColorCompMax = 1 << cETC1BaseColorCompNumBits, + + cETC1DeltaColorCompNumBits = 3, + cETC1DeltaColorComp = 1 << cETC1DeltaColorCompNumBits, + cETC1DeltaColorCompMax = 1 << cETC1DeltaColorCompNumBits, + + cETC1BaseColor5RBitOffset = 59, + cETC1BaseColor5GBitOffset = 51, + cETC1BaseColor5BBitOffset = 43, + + cETC1DeltaColor3RBitOffset = 56, + cETC1DeltaColor3GBitOffset = 48, + cETC1DeltaColor3BBitOffset = 40, + + // Absolute (non-delta) encoding (two 4-bit per component bases) + cETC1AbsColorCompNumBits = 4, + cETC1AbsColorCompMax = 1 << cETC1AbsColorCompNumBits, + + cETC1AbsColor4R1BitOffset = 60, + cETC1AbsColor4G1BitOffset = 52, + cETC1AbsColor4B1BitOffset = 44, + + cETC1AbsColor4R2BitOffset = 56, + cETC1AbsColor4G2BitOffset = 48, + cETC1AbsColor4B2BitOffset = 40, + + cETC1ColorDeltaMin = -4, + cETC1ColorDeltaMax = 3, + + // Delta3: + // 0 1 2 3 4 5 6 7 + // 000 001 010 011 100 101 110 111 + // 0 1 2 3 -4 -3 -2 -1 + }; + +/* + * IMPORTANT NOTE: + * + * rg_etc1 originally works only on R,G,B,A data + * evas works on B,G,R,A data + * + * ARGB_JOIN() is used for unpacking, so it will directly produce BGRA. + * + * Upon packing, we convert BGRA to RGBA so we can use the precomputed tables, + * so we must use the X_VAL_GET() macros. + * Upon unpacking, we directly output BGRA data using ARGB_JOIN() and X_VAL_SET() + * + * Yes, this is a mess. Maybe a clear BGRA API is needed + */ + +#ifndef WORDS_BIGENDIAN +// BGRA +#define A_VAL_SET(p) (((DATA8 *)(p))[3]) +#define R_VAL_SET(p) (((DATA8 *)(p))[2]) +#define G_VAL_SET(p) (((DATA8 *)(p))[1]) +#define B_VAL_SET(p) (((DATA8 *)(p))[0]) +// RGBA +#define A_VAL_GET(p) (((DATA8 *)(p))[3]) +#define R_VAL_GET(p) (((DATA8 *)(p))[0]) +#define G_VAL_GET(p) (((DATA8 *)(p))[1]) +#define B_VAL_GET(p) (((DATA8 *)(p))[2]) +#else +// BIGENDIAN is untested +#define A_VAL_SET(p) (((DATA8 *)(p))[0]) +#define R_VAL_SET(p) (((DATA8 *)(p))[1]) +#define G_VAL_SET(p) (((DATA8 *)(p))[2]) +#define B_VAL_SET(p) (((DATA8 *)(p))[3]) +#define A_VAL_GET(p) (((DATA8 *)(p))[0]) +#define R_VAL_GET(p) (((DATA8 *)(p))[3]) +#define G_VAL_GET(p) (((DATA8 *)(p))[2]) +#define B_VAL_GET(p) (((DATA8 *)(p))[1]) +#endif + +#define A_MASK (0xFFul << 24) + +// For unpacking and writing BGRA output data +#define ARGB_JOIN(a,r,g,b) \ + (((a) << 24) + ((r) << 16) + ((g) << 8) + (b)) + +static unsigned char rg_etc_quant5_tab[256 + 16]; + +static const int rg_etc1_inten_tables[cETC1IntenModifierValues][cETC1SelectorValues] = { + { -8, -2, 2, 8 }, + { -17, -5, 5, 17 }, + { -29, -9, 9, 29 }, + { -42, -13, 13, 42 }, + { -60, -18, 18, 60 }, + { -80, -24, 24, 80 }, + { -106, -33, 33, 106 }, + { -183, -47, 47, 183 } +}; + +static const unsigned char rg_etc1_to_selector_index[cETC1SelectorValues] = { 2, 3, 1, 0 }; +static const unsigned char rg_etc_selector_index_to_etc1[cETC1SelectorValues] = { 3, 2, 0, 1 }; + +// Given an ETC1 diff/inten_table/selector, and an 8-bit desired color, this table encodes the best packed_color in the low byte, and the abs error in the high byte. +static unsigned short rg_etc1_inverse_lookup[2*8*4][256]; // [diff/inten_table/selector][desired_color] + +// rg_color8_to_etc_block_config[color][table_index] = Supplies for each 8-bit color value a list of packed ETC1 diff/intensity table/selectors/packed_colors that map to that color. +// To pack: diff | (inten << 1) | (selector << 4) | (packed_c << 8) +static const unsigned short rg_etc_color8_to_etc_block_config_0_255[2][33] = { + { + 0x0000, 0x0010, 0x0002, 0x0012, 0x0004, 0x0014, 0x0006, 0x0016, 0x0008, + 0x0018, 0x000A, 0x001A, 0x000C, 0x001C, 0x000E, 0x001E, 0x0001, 0x0011, + 0x0003, 0x0013, 0x0005, 0x0015, 0x0007, 0x0017, 0x0009, 0x0019, 0x000B, + 0x001B, 0x000D, 0x001D, 0x000F, 0x001F, 0xFFFF + }, + { + 0x0F20, 0x0F30, 0x0E32, 0x0F22, 0x0E34, 0x0F24, 0x0D36, 0x0F26, 0x0C38, + 0x0E28, 0x0B3A, 0x0E2A, 0x093C, 0x0E2C, 0x053E, 0x0D2E, 0x1E31, 0x1F21, + 0x1D33, 0x1F23, 0x1C35, 0x1E25, 0x1A37, 0x1E27, 0x1839, 0x1D29, 0x163B, + 0x1C2B, 0x133D, 0x1B2D, 0x093F, 0x1A2F, 0xFFFF + }, +}; + +// Really only [254][11]. +static const unsigned short rg_etc_color8_to_etc_block_config_1_to_254[254][12] = { + { 0x021C, 0x0D0D, 0xFFFF }, + { 0x0020, 0x0021, 0x0A0B, 0x061F, 0xFFFF }, + { 0x0113, 0x0217, 0xFFFF }, + { 0x0116, 0x031E, 0x0B0E, 0x0405, 0xFFFF }, + { 0x0022, 0x0204, 0x050A, 0x0023, 0xFFFF }, + { 0x0111, 0x0319, 0x0809, 0x170F, 0xFFFF }, + { 0x0303, 0x0215, 0x0607, 0xFFFF }, + { 0x0030, 0x0114, 0x0408, 0x0031, 0x0201, 0x051D, 0xFFFF }, + { 0x0100, 0x0024, 0x0306, 0x0025, 0x041B, 0x0E0D, 0xFFFF }, + { 0x021A, 0x0121, 0x0B0B, 0x071F, 0xFFFF }, + { 0x0213, 0x0317, 0xFFFF }, + { 0x0112, 0x0505, 0xFFFF }, + { 0x0026, 0x070C, 0x0123, 0x0027, 0xFFFF }, + { 0x0211, 0x0909, 0xFFFF }, + { 0x0110, 0x0315, 0x0707, 0x0419, 0x180F, 0xFFFF }, + { 0x0218, 0x0131, 0x0301, 0x0403, 0x061D, 0xFFFF }, + { 0x0032, 0x0202, 0x0033, 0x0125, 0x051B, 0x0F0D, 0xFFFF }, + { 0x0028, 0x031C, 0x0221, 0x0029, 0xFFFF }, + { 0x0120, 0x0313, 0x0C0B, 0x081F, 0xFFFF }, + { 0x0605, 0x0417, 0xFFFF }, + { 0x0216, 0x041E, 0x0C0E, 0x0223, 0x0127, 0xFFFF }, + { 0x0122, 0x0304, 0x060A, 0x0311, 0x0A09, 0xFFFF }, + { 0x0519, 0x190F, 0xFFFF }, + { 0x002A, 0x0231, 0x0503, 0x0415, 0x0807, 0x002B, 0x071D, 0xFFFF }, + { 0x0130, 0x0214, 0x0508, 0x0401, 0x0133, 0x0225, 0x061B, 0xFFFF }, + { 0x0200, 0x0124, 0x0406, 0x0321, 0x0129, 0x100D, 0xFFFF }, + { 0x031A, 0x0D0B, 0x091F, 0xFFFF }, + { 0x0413, 0x0705, 0x0517, 0xFFFF }, + { 0x0212, 0x0034, 0x0323, 0x0035, 0x0227, 0xFFFF }, + { 0x0126, 0x080C, 0x0B09, 0xFFFF }, + { 0x0411, 0x0619, 0x1A0F, 0xFFFF }, + { 0x0210, 0x0331, 0x0603, 0x0515, 0x0907, 0x012B, 0xFFFF }, + { 0x0318, 0x002C, 0x0501, 0x0233, 0x0325, 0x071B, 0x002D, 0x081D, 0xFFFF }, + { 0x0132, 0x0302, 0x0229, 0x110D, 0xFFFF }, + { 0x0128, 0x041C, 0x0421, 0x0E0B, 0x0A1F, 0xFFFF }, + { 0x0220, 0x0513, 0x0617, 0xFFFF }, + { 0x0135, 0x0805, 0x0327, 0xFFFF }, + { 0x0316, 0x051E, 0x0D0E, 0x0423, 0xFFFF }, + { 0x0222, 0x0404, 0x070A, 0x0511, 0x0719, 0x0C09, 0x1B0F, 0xFFFF }, + { 0x0703, 0x0615, 0x0A07, 0x022B, 0xFFFF }, + { 0x012A, 0x0431, 0x0601, 0x0333, 0x012D, 0x091D, 0xFFFF }, + { 0x0230, 0x0314, 0x0036, 0x0608, 0x0425, 0x0037, 0x0329, 0x081B, 0x120D, 0xFFFF }, + { 0x0300, 0x0224, 0x0506, 0x0521, 0x0F0B, 0x0B1F, 0xFFFF }, + { 0x041A, 0x0613, 0x0717, 0xFFFF }, + { 0x0235, 0x0905, 0xFFFF }, + { 0x0312, 0x0134, 0x0523, 0x0427, 0xFFFF }, + { 0x0226, 0x090C, 0x002E, 0x0611, 0x0D09, 0x002F, 0xFFFF }, + { 0x0715, 0x0B07, 0x0819, 0x032B, 0x1C0F, 0xFFFF }, + { 0x0310, 0x0531, 0x0701, 0x0803, 0x022D, 0x0A1D, 0xFFFF }, + { 0x0418, 0x012C, 0x0433, 0x0525, 0x0137, 0x091B, 0x130D, 0xFFFF }, + { 0x0232, 0x0402, 0x0621, 0x0429, 0xFFFF }, + { 0x0228, 0x051C, 0x0713, 0x100B, 0x0C1F, 0xFFFF }, + { 0x0320, 0x0335, 0x0A05, 0x0817, 0xFFFF }, + { 0x0623, 0x0527, 0xFFFF }, + { 0x0416, 0x061E, 0x0E0E, 0x0711, 0x0E09, 0x012F, 0xFFFF }, + { 0x0322, 0x0504, 0x080A, 0x0919, 0x1D0F, 0xFFFF }, + { 0x0631, 0x0903, 0x0815, 0x0C07, 0x042B, 0x032D, 0x0B1D, 0xFFFF }, + { 0x022A, 0x0801, 0x0533, 0x0625, 0x0237, 0x0A1B, 0xFFFF }, + { 0x0330, 0x0414, 0x0136, 0x0708, 0x0721, 0x0529, 0x140D, 0xFFFF }, + { 0x0400, 0x0324, 0x0606, 0x0038, 0x0039, 0x110B, 0x0D1F, 0xFFFF }, + { 0x051A, 0x0813, 0x0B05, 0x0917, 0xFFFF }, + { 0x0723, 0x0435, 0x0627, 0xFFFF }, + { 0x0412, 0x0234, 0x0F09, 0x022F, 0xFFFF }, + { 0x0326, 0x0A0C, 0x012E, 0x0811, 0x0A19, 0x1E0F, 0xFFFF }, + { 0x0731, 0x0A03, 0x0915, 0x0D07, 0x052B, 0xFFFF }, + { 0x0410, 0x0901, 0x0633, 0x0725, 0x0337, 0x0B1B, 0x042D, 0x0C1D, 0xFFFF }, + { 0x0518, 0x022C, 0x0629, 0x150D, 0xFFFF }, + { 0x0332, 0x0502, 0x0821, 0x0139, 0x120B, 0x0E1F, 0xFFFF }, + { 0x0328, 0x061C, 0x0913, 0x0A17, 0xFFFF }, + { 0x0420, 0x0535, 0x0C05, 0x0727, 0xFFFF }, + { 0x0823, 0x032F, 0xFFFF }, + { 0x0516, 0x071E, 0x0F0E, 0x0911, 0x0B19, 0x1009, 0x1F0F, 0xFFFF }, + { 0x0422, 0x0604, 0x090A, 0x0B03, 0x0A15, 0x0E07, 0x062B, 0xFFFF }, + { 0x0831, 0x0A01, 0x0733, 0x052D, 0x0D1D, 0xFFFF }, + { 0x032A, 0x0825, 0x0437, 0x0729, 0x0C1B, 0x160D, 0xFFFF }, + { 0x0430, 0x0514, 0x0236, 0x0808, 0x0921, 0x0239, 0x130B, 0x0F1F, 0xFFFF }, + { 0x0500, 0x0424, 0x0706, 0x0138, 0x0A13, 0x0B17, 0xFFFF }, + { 0x061A, 0x0635, 0x0D05, 0xFFFF }, + { 0x0923, 0x0827, 0xFFFF }, + { 0x0512, 0x0334, 0x003A, 0x0A11, 0x1109, 0x003B, 0x042F, 0xFFFF }, + { 0x0426, 0x0B0C, 0x022E, 0x0B15, 0x0F07, 0x0C19, 0x072B, 0xFFFF }, + { 0x0931, 0x0B01, 0x0C03, 0x062D, 0x0E1D, 0xFFFF }, + { 0x0510, 0x0833, 0x0925, 0x0537, 0x0D1B, 0x170D, 0xFFFF }, + { 0x0618, 0x032C, 0x0A21, 0x0339, 0x0829, 0xFFFF }, + { 0x0432, 0x0602, 0x0B13, 0x140B, 0x101F, 0xFFFF }, + { 0x0428, 0x071C, 0x0735, 0x0E05, 0x0C17, 0xFFFF }, + { 0x0520, 0x0A23, 0x0927, 0xFFFF }, + { 0x0B11, 0x1209, 0x013B, 0x052F, 0xFFFF }, + { 0x0616, 0x081E, 0x0D19, 0xFFFF }, + { 0x0522, 0x0704, 0x0A0A, 0x0A31, 0x0D03, 0x0C15, 0x1007, 0x082B, 0x072D, 0x0F1D, 0xFFFF }, + { 0x0C01, 0x0933, 0x0A25, 0x0637, 0x0E1B, 0xFFFF }, + { 0x042A, 0x0B21, 0x0929, 0x180D, 0xFFFF }, + { 0x0530, 0x0614, 0x0336, 0x0908, 0x0439, 0x150B, 0x111F, 0xFFFF }, + { 0x0600, 0x0524, 0x0806, 0x0238, 0x0C13, 0x0F05, 0x0D17, 0xFFFF }, + { 0x071A, 0x0B23, 0x0835, 0x0A27, 0xFFFF }, + { 0x1309, 0x023B, 0x062F, 0xFFFF }, + { 0x0612, 0x0434, 0x013A, 0x0C11, 0x0E19, 0xFFFF }, + { 0x0526, 0x0C0C, 0x032E, 0x0B31, 0x0E03, 0x0D15, 0x1107, 0x092B, 0xFFFF }, + { 0x0D01, 0x0A33, 0x0B25, 0x0737, 0x0F1B, 0x082D, 0x101D, 0xFFFF }, + { 0x0610, 0x0A29, 0x190D, 0xFFFF }, + { 0x0718, 0x042C, 0x0C21, 0x0539, 0x160B, 0x121F, 0xFFFF }, + { 0x0532, 0x0702, 0x0D13, 0x0E17, 0xFFFF }, + { 0x0528, 0x081C, 0x0935, 0x1005, 0x0B27, 0xFFFF }, + { 0x0620, 0x0C23, 0x033B, 0x072F, 0xFFFF }, + { 0x0D11, 0x0F19, 0x1409, 0xFFFF }, + { 0x0716, 0x003C, 0x091E, 0x0F03, 0x0E15, 0x1207, 0x0A2B, 0x003D, 0xFFFF }, + { 0x0622, 0x0804, 0x0B0A, 0x0C31, 0x0E01, 0x0B33, 0x092D, 0x111D, 0xFFFF }, + { 0x0C25, 0x0837, 0x0B29, 0x101B, 0x1A0D, 0xFFFF }, + { 0x052A, 0x0D21, 0x0639, 0x170B, 0x131F, 0xFFFF }, + { 0x0630, 0x0714, 0x0436, 0x0A08, 0x0E13, 0x0F17, 0xFFFF }, + { 0x0700, 0x0624, 0x0906, 0x0338, 0x0A35, 0x1105, 0xFFFF }, + { 0x081A, 0x0D23, 0x0C27, 0xFFFF }, + { 0x0E11, 0x1509, 0x043B, 0x082F, 0xFFFF }, + { 0x0712, 0x0534, 0x023A, 0x0F15, 0x1307, 0x1019, 0x0B2B, 0x013D, 0xFFFF }, + { 0x0626, 0x0D0C, 0x042E, 0x0D31, 0x0F01, 0x1003, 0x0A2D, 0x121D, 0xFFFF }, + { 0x0C33, 0x0D25, 0x0937, 0x111B, 0x1B0D, 0xFFFF }, + { 0x0710, 0x0E21, 0x0739, 0x0C29, 0xFFFF }, + { 0x0818, 0x052C, 0x0F13, 0x180B, 0x141F, 0xFFFF }, + { 0x0632, 0x0802, 0x0B35, 0x1205, 0x1017, 0xFFFF }, + { 0x0628, 0x091C, 0x0E23, 0x0D27, 0xFFFF }, + { 0x0720, 0x0F11, 0x1609, 0x053B, 0x092F, 0xFFFF }, + { 0x1119, 0x023D, 0xFFFF }, + { 0x0816, 0x013C, 0x0A1E, 0x0E31, 0x1103, 0x1015, 0x1407, 0x0C2B, 0x0B2D, 0x131D, 0xFFFF }, + { 0x0722, 0x0904, 0x0C0A, 0x1001, 0x0D33, 0x0E25, 0x0A37, 0x121B, 0xFFFF }, + { 0x0F21, 0x0D29, 0x1C0D, 0xFFFF }, + { 0x062A, 0x0839, 0x190B, 0x151F, 0xFFFF }, + { 0x0730, 0x0814, 0x0536, 0x0B08, 0x1013, 0x1305, 0x1117, 0xFFFF }, + { 0x0800, 0x0724, 0x0A06, 0x0438, 0x0F23, 0x0C35, 0x0E27, 0xFFFF }, + { 0x091A, 0x1709, 0x063B, 0x0A2F, 0xFFFF }, + { 0x1011, 0x1219, 0x033D, 0xFFFF }, + { 0x0812, 0x0634, 0x033A, 0x0F31, 0x1203, 0x1115, 0x1507, 0x0D2B, 0xFFFF }, + { 0x0726, 0x0E0C, 0x052E, 0x1101, 0x0E33, 0x0F25, 0x0B37, 0x131B, 0x0C2D, 0x141D, 0xFFFF }, + { 0x0E29, 0x1D0D, 0xFFFF }, + { 0x0810, 0x1021, 0x0939, 0x1A0B, 0x161F, 0xFFFF }, + { 0x0918, 0x062C, 0x1113, 0x1217, 0xFFFF }, + { 0x0732, 0x0902, 0x0D35, 0x1405, 0x0F27, 0xFFFF }, + { 0x0728, 0x0A1C, 0x1023, 0x073B, 0x0B2F, 0xFFFF }, + { 0x0820, 0x1111, 0x1319, 0x1809, 0xFFFF }, + { 0x1303, 0x1215, 0x1607, 0x0E2B, 0x043D, 0xFFFF }, + { 0x0916, 0x023C, 0x0B1E, 0x1031, 0x1201, 0x0F33, 0x0D2D, 0x151D, 0xFFFF }, + { 0x0822, 0x0A04, 0x0D0A, 0x1025, 0x0C37, 0x0F29, 0x141B, 0x1E0D, 0xFFFF }, + { 0x1121, 0x0A39, 0x1B0B, 0x171F, 0xFFFF }, + { 0x072A, 0x1213, 0x1317, 0xFFFF }, + { 0x0830, 0x0914, 0x0636, 0x0C08, 0x0E35, 0x1505, 0xFFFF }, + { 0x0900, 0x0824, 0x0B06, 0x0538, 0x1123, 0x1027, 0xFFFF }, + { 0x0A1A, 0x1211, 0x1909, 0x083B, 0x0C2F, 0xFFFF }, + { 0x1315, 0x1707, 0x1419, 0x0F2B, 0x053D, 0xFFFF }, + { 0x0912, 0x0734, 0x043A, 0x1131, 0x1301, 0x1403, 0x0E2D, 0x161D, 0xFFFF }, + { 0x0826, 0x0F0C, 0x062E, 0x1033, 0x1125, 0x0D37, 0x151B, 0x1F0D, 0xFFFF }, + { 0x1221, 0x0B39, 0x1029, 0xFFFF }, + { 0x0910, 0x1313, 0x1C0B, 0x181F, 0xFFFF }, + { 0x0A18, 0x072C, 0x0F35, 0x1605, 0x1417, 0xFFFF }, + { 0x0832, 0x0A02, 0x1223, 0x1127, 0xFFFF }, + { 0x0828, 0x0B1C, 0x1311, 0x1A09, 0x093B, 0x0D2F, 0xFFFF }, + { 0x0920, 0x1519, 0x063D, 0xFFFF }, + { 0x1231, 0x1503, 0x1415, 0x1807, 0x102B, 0x0F2D, 0x171D, 0xFFFF }, + { 0x0A16, 0x033C, 0x0C1E, 0x1401, 0x1133, 0x1225, 0x0E37, 0x161B, 0xFFFF }, + { 0x0922, 0x0B04, 0x0E0A, 0x1321, 0x1129, 0xFFFF }, + { 0x0C39, 0x1D0B, 0x191F, 0xFFFF }, + { 0x082A, 0x1413, 0x1705, 0x1517, 0xFFFF }, + { 0x0930, 0x0A14, 0x0736, 0x0D08, 0x1323, 0x1035, 0x1227, 0xFFFF }, + { 0x0A00, 0x0924, 0x0C06, 0x0638, 0x1B09, 0x0A3B, 0x0E2F, 0xFFFF }, + { 0x0B1A, 0x1411, 0x1619, 0x073D, 0xFFFF }, + { 0x1331, 0x1603, 0x1515, 0x1907, 0x112B, 0xFFFF }, + { 0x0A12, 0x0834, 0x053A, 0x1501, 0x1233, 0x1325, 0x0F37, 0x171B, 0x102D, 0x181D, 0xFFFF }, + { 0x0926, 0x072E, 0x1229, 0xFFFF }, + { 0x1421, 0x0D39, 0x1E0B, 0x1A1F, 0xFFFF }, + { 0x0A10, 0x1513, 0x1617, 0xFFFF }, + { 0x0B18, 0x082C, 0x1135, 0x1805, 0x1327, 0xFFFF }, + { 0x0932, 0x0B02, 0x1423, 0x0B3B, 0x0F2F, 0xFFFF }, + { 0x0928, 0x0C1C, 0x1511, 0x1719, 0x1C09, 0xFFFF }, + { 0x0A20, 0x1703, 0x1615, 0x1A07, 0x122B, 0x083D, 0xFFFF }, + { 0x1431, 0x1601, 0x1333, 0x112D, 0x191D, 0xFFFF }, + { 0x0B16, 0x043C, 0x0D1E, 0x1425, 0x1037, 0x1329, 0x181B, 0xFFFF }, + { 0x0A22, 0x0C04, 0x0F0A, 0x1521, 0x0E39, 0x1F0B, 0x1B1F, 0xFFFF }, + { 0x1613, 0x1717, 0xFFFF }, + { 0x092A, 0x1235, 0x1905, 0xFFFF }, + { 0x0A30, 0x0B14, 0x0836, 0x0E08, 0x1523, 0x1427, 0xFFFF }, + { 0x0B00, 0x0A24, 0x0D06, 0x0738, 0x1611, 0x1D09, 0x0C3B, 0x102F, 0xFFFF }, + { 0x0C1A, 0x1715, 0x1B07, 0x1819, 0x132B, 0x093D, 0xFFFF }, + { 0x1531, 0x1701, 0x1803, 0x122D, 0x1A1D, 0xFFFF }, + { 0x0B12, 0x0934, 0x063A, 0x1433, 0x1525, 0x1137, 0x191B, 0xFFFF }, + { 0x0A26, 0x003E, 0x082E, 0x1621, 0x0F39, 0x1429, 0x003F, 0xFFFF }, + { 0x1713, 0x1C1F, 0xFFFF }, + { 0x0B10, 0x1335, 0x1A05, 0x1817, 0xFFFF }, + { 0x0C18, 0x092C, 0x1623, 0x1527, 0xFFFF }, + { 0x0A32, 0x0C02, 0x1711, 0x1E09, 0x0D3B, 0x112F, 0xFFFF }, + { 0x0A28, 0x0D1C, 0x1919, 0x0A3D, 0xFFFF }, + { 0x0B20, 0x1631, 0x1903, 0x1815, 0x1C07, 0x142B, 0x132D, 0x1B1D, 0xFFFF }, + { 0x1801, 0x1533, 0x1625, 0x1237, 0x1A1B, 0xFFFF }, + { 0x0C16, 0x053C, 0x0E1E, 0x1721, 0x1529, 0x013F, 0xFFFF }, + { 0x0B22, 0x0D04, 0x1039, 0x1D1F, 0xFFFF }, + { 0x1813, 0x1B05, 0x1917, 0xFFFF }, + { 0x0A2A, 0x1723, 0x1435, 0x1627, 0xFFFF }, + { 0x0B30, 0x0C14, 0x0936, 0x0F08, 0x1F09, 0x0E3B, 0x122F, 0xFFFF }, + { 0x0C00, 0x0B24, 0x0E06, 0x0838, 0x1811, 0x1A19, 0x0B3D, 0xFFFF }, + { 0x0D1A, 0x1731, 0x1A03, 0x1915, 0x1D07, 0x152B, 0xFFFF }, + { 0x1901, 0x1633, 0x1725, 0x1337, 0x1B1B, 0x142D, 0x1C1D, 0xFFFF }, + { 0x0C12, 0x0A34, 0x073A, 0x1629, 0x023F, 0xFFFF }, + { 0x0B26, 0x013E, 0x092E, 0x1821, 0x1139, 0x1E1F, 0xFFFF }, + { 0x1913, 0x1A17, 0xFFFF }, + { 0x0C10, 0x1535, 0x1C05, 0x1727, 0xFFFF }, + { 0x0D18, 0x0A2C, 0x1823, 0x0F3B, 0x132F, 0xFFFF }, + { 0x0B32, 0x0D02, 0x1911, 0x1B19, 0xFFFF }, + { 0x0B28, 0x0E1C, 0x1B03, 0x1A15, 0x1E07, 0x162B, 0x0C3D, 0xFFFF }, + { 0x0C20, 0x1831, 0x1A01, 0x1733, 0x152D, 0x1D1D, 0xFFFF }, + { 0x1825, 0x1437, 0x1729, 0x1C1B, 0x033F, 0xFFFF }, + { 0x0D16, 0x063C, 0x0F1E, 0x1921, 0x1239, 0x1F1F, 0xFFFF }, + { 0x0C22, 0x0E04, 0x1A13, 0x1B17, 0xFFFF }, + { 0x1635, 0x1D05, 0xFFFF }, + { 0x0B2A, 0x1923, 0x1827, 0xFFFF }, + { 0x0C30, 0x0D14, 0x0A36, 0x1A11, 0x103B, 0x142F, 0xFFFF }, + { 0x0D00, 0x0C24, 0x0F06, 0x0938, 0x1B15, 0x1F07, 0x1C19, 0x172B, 0x0D3D, 0xFFFF }, + { 0x0E1A, 0x1931, 0x1B01, 0x1C03, 0x162D, 0x1E1D, 0xFFFF }, + { 0x1833, 0x1925, 0x1537, 0x1D1B, 0xFFFF }, + { 0x0D12, 0x0B34, 0x083A, 0x1A21, 0x1339, 0x1829, 0x043F, 0xFFFF }, + { 0x0C26, 0x023E, 0x0A2E, 0x1B13, 0xFFFF }, + { 0x1735, 0x1E05, 0x1C17, 0xFFFF }, + { 0x0D10, 0x1A23, 0x1927, 0xFFFF }, + { 0x0E18, 0x0B2C, 0x1B11, 0x113B, 0x152F, 0xFFFF }, + { 0x0C32, 0x0E02, 0x1D19, 0x0E3D, 0xFFFF }, + { 0x0C28, 0x0F1C, 0x1A31, 0x1D03, 0x1C15, 0x182B, 0x172D, 0x1F1D, 0xFFFF }, + { 0x0D20, 0x1C01, 0x1933, 0x1A25, 0x1637, 0x1E1B, 0xFFFF }, + { 0x1B21, 0x1929, 0x053F, 0xFFFF }, + { 0x0E16, 0x073C, 0x1439, 0xFFFF }, + { 0x0D22, 0x0F04, 0x1C13, 0x1F05, 0x1D17, 0xFFFF }, + { 0x1B23, 0x1835, 0x1A27, 0xFFFF }, + { 0x0C2A, 0x123B, 0x162F, 0xFFFF }, + { 0x0D30, 0x0E14, 0x0B36, 0x1C11, 0x1E19, 0x0F3D, 0xFFFF }, + { 0x0E00, 0x0D24, 0x0A38, 0x1B31, 0x1E03, 0x1D15, 0x192B, 0xFFFF }, + { 0x0F1A, 0x1D01, 0x1A33, 0x1B25, 0x1737, 0x1F1B, 0x182D, 0xFFFF }, + { 0x1A29, 0x063F, 0xFFFF }, + { 0x0E12, 0x0C34, 0x093A, 0x1C21, 0x1539, 0xFFFF }, + { 0x0D26, 0x033E, 0x0B2E, 0x1D13, 0x1E17, 0xFFFF }, + { 0x1935, 0x1B27, 0xFFFF }, + { 0x0E10, 0x1C23, 0x133B, 0x172F, 0xFFFF }, + { 0x0F18, 0x0C2C, 0x1D11, 0x1F19, 0xFFFF }, + { 0x0D32, 0x0F02, 0x1F03, 0x1E15, 0x1A2B, 0x103D, 0xFFFF }, + { 0x0D28, 0x1C31, 0x1E01, 0x1B33, 0x192D, 0xFFFF }, + { 0x0E20, 0x1C25, 0x1837, 0x1B29, 0x073F, 0xFFFF }, + { 0x1D21, 0x1639, 0xFFFF }, + { 0x0F16, 0x083C, 0x1E13, 0x1F17, 0xFFFF }, + { 0x0E22, 0x1A35, 0xFFFF }, + { 0x1D23, 0x1C27, 0xFFFF }, + { 0x0D2A, 0x1E11, 0x143B, 0x182F, 0xFFFF }, + { 0x0E30, 0x0F14, 0x0C36, 0x1F15, 0x1B2B, 0x113D, 0xFFFF }, + { 0x0F00, 0x0E24, 0x0B38, 0x1D31, 0x1F01, 0x1A2D, 0xFFFF }, + { 0x1C33, 0x1D25, 0x1937, 0xFFFF }, + { 0x1E21, 0x1739, 0x1C29, 0x083F, 0xFFFF }, + { 0x0F12, 0x0D34, 0x0A3A, 0x1F13, 0xFFFF }, + { 0x0E26, 0x043E, 0x0C2E, 0x1B35, 0xFFFF }, + { 0x1E23, 0x1D27, 0xFFFF }, + { 0x0F10, 0x1F11, 0x153B, 0x192F, 0xFFFF }, + { 0x0D2C, 0x123D, 0xFFFF }, +}; + +typedef union +{ + struct comp { + unsigned char r; + unsigned char g; + unsigned char b; + unsigned char a; + } comp; + + unsigned int m_u32; +} color_quad_u8; + +static inline int +rg_etc1_color_quad_u8_clamp(int v) +{ + /* FIXME: (From Wikipedia) + * "In C, the result of right-shifting a negative value is implementation-defined" + * The following code assumes right-shift will duplicate the sign bit. + */ + if (v & 0xFFFFFF00U) + v = ((~v) >> 31) & 0xFF; + return v; +} + +static inline void +rg_etc1_color_quad_u8_init(color_quad_u8 *color, int r, int g, int b, int alpha) +{ + color->comp.r = rg_etc1_color_quad_u8_clamp(r); + color->comp.g = rg_etc1_color_quad_u8_clamp(g); + color->comp.b = rg_etc1_color_quad_u8_clamp(b); + color->comp.a = rg_etc1_color_quad_u8_clamp(alpha); +} +static inline void +rg_etc1_color_quad_u8_copy(color_quad_u8 *dst, const color_quad_u8 *src) +{ + dst->m_u32 = src->m_u32; +} + +static inline void +rg_etc1_color_quad_u8_clear(color_quad_u8 *color) +{ + color->m_u32 = 0; +} + +static inline unsigned int +rg_etc1_color_quad_u8_rgb_squared_distance(color_quad_u8 color1, color_quad_u8 color2) +{ + return SQUARE((color1.comp.r - color2.comp.r)) + SQUARE((color1.comp.g - color2.comp.g)) + SQUARE((color1.comp.b - color2.comp.b)); +} + +#if RG_ETC1_CONSTRAINED_SUBBLOCK +static inline void +rg_etc1_color_quad_u8_component_set(color_quad_u8 *color, unsigned char idx, unsigned char value) +{ + switch (idx) + { + case 0: color->comp.r = value; break; + case 1: color->comp.g = value; break; + case 2: color->comp.b = value; break; + case 3: color->comp.a = value; break; + default: abort(); + } +} +#endif + +#if 0 +static inline unsigned int +rg_etc1_color_quad_duplicate_init(unsigned char y, unsigned char alpha) +{ + return ARGB_JOIN(alpha, y, y, y); +} +#endif + +static inline unsigned int +rg_etc1_color_quad_init(unsigned char r, unsigned char g, unsigned char b, unsigned char alpha) +{ + return ARGB_JOIN(alpha, r, g, b); +} + +static inline unsigned int +rg_etc1_color_quad_set(unsigned int old_color, unsigned int new_color) +{ + return (new_color & ~A_MASK) | (old_color & A_MASK); +} + +static inline void +rg_etc1_color_quad_get(unsigned int color, unsigned char *r, unsigned char *g, unsigned char *b, unsigned char *alpha) +{ + // Used for PACKING + if (r) *r = R_VAL_GET(&color); + if (g) *g = G_VAL_GET(&color); + if (b) *b = B_VAL_GET(&color); + if (alpha) *alpha = A_VAL_GET(&color); +} + +#if RG_ETC1_CONSTRAINED_SUBBLOCK +static inline unsigned char +rg_etc1_color_quad_component_get(unsigned int color, unsigned char idx) +{ + switch (idx) + { + // FIXME: Untested code (RGBA vs BGRA) + case 0: return R_VAL_GET(&color); + case 1: return G_VAL_GET(&color); + case 2: return B_VAL_GET(&color); + case 3: return A_VAL_GET(&color); + default: abort(); + } + return 0; +} +#endif + +#if 0 +static inline unsigned int +rg_etc1_color_quad_component_set(unsigned int color, unsigned char idx, unsigned char value) +{ + unsigned char r, g, b, a; + + rg_etc1_color_quad_get(color, &r, &g, &b, &a); + + switch (idx) + { + case 0: r = value; break; + case 1: g = value; break; + case 2: b = value; break; + case 3: a = value; break; + default: abort(); + } + + return rg_etc1_color_quad_init(r, g, b, a); +} + +static inline unsigned int +rg_etc1_color_quad_grayscale_set(unsigned int color, unsigned char l) +{ + unsigned char a; + + a = A_VAL_SET(&color); + + return rg_etc1_color_quad_init(l, l, l, a); +} + +static inline unsigned int +rg_etc1_color_quad_clamp(unsigned int color, unsigned int low, unsigned high) +{ + unsigned char *c = (unsigned char *)&color; + unsigned char *l = (unsigned char *)&low; + unsigned char *h = (unsigned char *)&high; + unsigned int i; + + for (i = 0; i < 4; i++) + c[i] = CLAMP(c[i], l[i], h[i]); + + return color; +} + +static inline unsigned int +rg_etc1_color_quad_component_clamp(unsigned int color, unsigned int low, unsigned high) +{ + unsigned char *c = (unsigned char *)&color; + unsigned int i; + + for (i = 0; i < 4; i++) + c[i] = CLAMP(c[i], low, high); + + return color; +} + +// Returns CCIR 601 luma (consistent with color_utils::RGB_To_Y). +static inline unsigned char +rg_etc1_color_quad_luma601_get(unsigned int color) +{ + unsigned char r, g, b; + rg_etc1_color_quad_get(color, &r, &g, &b, NULL); + return ((19595U * r + 38470U * g + 7471U * b + 32768U) >> 16U); +} + +// Returns REC 709 luma. +static inline unsigned char +rg_etc1_color_quad_luma709_get(unsigned int color) +{ + unsigned char r, g, b; + rg_etc1_color_quad_get(color, &r, &g, &b, NULL); + return ((13938U * r + 46869U * g + 4729U * b + 32768U) >> 16U); +} + +static inline unsigned int +rg_etc1_color_quad_rgb_squared_distance(unsigned int color1, unsigned int color2) +{ + unsigned char r1, g1, b1; + unsigned char r2, g2, b2; + + rg_etc1_color_quad_get(color1, &r1, &g1, &b1, NULL); + rg_etc1_color_quad_get(color2, &r2, &g2, &b2, NULL); + + return SQUARE(r1 - r2) + SQUARE(g1 - g2) + SQUARE(b1 - b2); +} + +static inline unsigned int +rg_etc1_color_quad_argb_squared_distance(unsigned int color1, unsigned int color2) +{ + unsigned char r1, g1, b1, a1; + unsigned char r2, g2, b2, a2; + + rg_etc1_color_quad_get(color1, &r1, &g1, &b1, &a1); + rg_etc1_color_quad_get(color2, &r2, &g2, &b2, &a2); + + return SQUARE(r1 - r2) + SQUARE(g1 - g2) + SQUARE(b1 - b2) + SQUARE(a1 - a1); +} + +static inline unsigned char +rg_etc1_color_quad_rgb_equals(unsigned int color1, unsigned int color2) +{ + A_VAL_SET(&color1) = 0; + A_VAL_SET(&color2) = 0; + + return color1 == color2; +} + +static inline unsigned int +rg_etc1_color_quad_add(unsigned int color1, unsigned int color2) +{ + unsigned char *c1 = (unsigned char *)&color1; + unsigned char *c2 = (unsigned char *)&color2; + unsigned int i; + + for (i = 0; i < 4; i++) + { + unsigned short t; + + t = c1[i] + c2[i]; + c1[i] = (unsigned char) (MIN(t, 255)); + } + + return color1; +} + +static inline unsigned int +rg_etc1_color_quad_del(unsigned int color1, unsigned int color2) +{ + unsigned char *c1 = (unsigned char *)&color1; + unsigned char *c2 = (unsigned char *)&color2; + unsigned int i; + + for (i = 0; i < 4; i++) + { + short t; + + t = c1[i] - c2[i]; + c1[i] = (unsigned char) (CLAMP(t, 0, 255)); + } + + return color1; +} +#endif + +static inline void +rg_etc1_vec_init(float v[3], float s) +{ + v[0] = s; v[1] = s; v[2] = s; +} + +static inline void +rg_etc1_vec_set(float v[3], float x, float y, float z) +{ + v[0] = x; v[1] = y; v[2] = z; +} + +static inline void +rg_etc1_vec_copy(float dst[3], float src[3]) +{ + dst[0] = src[0]; dst[1] = src[1]; dst[2] = src[2]; +} + +static inline void +rg_etc1_vec_add(float r[3], float a[3]) +{ + unsigned int i; + + //for (i = 0; i < RG_ETC1_ARRAY_SIZE(r); i++) + for (i = 0; i < 3; i++) + r[i] += a[i]; +} + +static inline void +rg_etc1_vec_scale(float r[3], float s) +{ + unsigned int i; + + //for (i = 0; i < RG_ETC1_ARRAY_SIZE(r); i++) + for (i = 0; i < 3; i++) + r[i] *= s; +} + +static inline unsigned char +rg_etc1_block_byte_bits_get(const unsigned char bytes[8], unsigned char offset, unsigned char num) +{ + const unsigned char byte_offset = 7 - (offset >> 3); + const unsigned char byte_bit_offset = offset & 7; + + return (bytes[byte_offset] >> byte_bit_offset) & ((1 << num) - 1); +} + +static inline void +rg_etc1_block_byte_bits_set(unsigned char bytes[8], unsigned char offset, unsigned char num, unsigned char bits) +{ + const unsigned char byte_offset = 7 - (offset >> 3); + const unsigned char byte_bit_offset = offset & 7; + const unsigned char mask = (1 << num) - 1; + bytes[byte_offset] &= ~(mask << byte_bit_offset); + bytes[byte_offset] |= (((!!bits) & 0x1) << byte_bit_offset); +} + +static inline unsigned char +rg_etc1_block_flip_bit_get(const unsigned char bytes[8]) +{ + return (bytes[3] & 1) != 0; +} + +static inline void +rg_etc1_block_flip_bit_set(unsigned char bytes[8], unsigned char flip) +{ + bytes[3] &= ~1; + bytes[3] |= (!!flip) & 0x1; +} + +static inline unsigned char +rg_etc1_block_diff_bit_get(const unsigned char bytes[8]) +{ + return (bytes[3] & 2) != 0; +} + +static inline void +rg_etc1_block_diff_bit_set(unsigned char bytes[8], unsigned char diff) +{ + bytes[3] &= ~2; + bytes[3] |= ((!!diff) & 0x1) << 1; +} + +static inline void +rg_etc1_block_clear(unsigned char bytes[8]) +{ + memset(bytes, 0, sizeof (unsigned char) * 8); +} + +// Returns intensity modifier table (0-7) used by subblock subblock_id. +// subblock_id=0 left/top (CW 1), 1=right/bottom (CW 2) +static inline unsigned char +rg_etc1_block_inten_table_get(const unsigned char bytes[8], unsigned char subblock_id) +{ + const unsigned char offset = subblock_id ? 2 : 5; + if (!(subblock_id < 2)) return 0; // ERROR CASE NO ASSERT IN EVAS CODE + return (bytes[3] >> offset) & 7; +} + +// Sets intensity modifier table (0-7) used by subblock subblock_id (0 or 1) +static inline void +rg_etc1_block_inten_table_set(unsigned char bytes[8], unsigned char subblock_id, unsigned char t) +{ + const unsigned char offset = subblock_id ? 2 : 5; + // ERROR CASE NO ASSERT IN EVAS CODE + if (!(subblock_id < 2) || !(t < 8)) return ; + bytes[3] &= ~(7 << offset); + bytes[3] |= (t << offset); +} + +// Returned selector value ranges from 0-3 and is a direct index into g_etc1_inten_tables. +static inline unsigned char +rg_etc1_block_selector_get(const unsigned char bytes[8], unsigned char x, unsigned char y) +{ + // ERROR CASE NO ASSERT IN EVAS CODE + if (!((x | y) < 4)) return 0; + + const unsigned char bit_index = x * 4 + y; + const unsigned char byte_bit_offset = bit_index & 7; + const unsigned char *p = &bytes[7 - (bit_index >> 3)]; + const unsigned char lsb = (p[0] >> byte_bit_offset) & 1; + const unsigned char msb = (p[-2] >> byte_bit_offset) & 1; + const unsigned char val = lsb | (msb << 1); + + return rg_etc1_to_selector_index[val]; +} + +// Selector "val" ranges from 0-3 and is a direct index into g_etc1_inten_tables. +static inline void +rg_etc1_block_selector_set(unsigned char bytes[8], unsigned char x, unsigned char y, unsigned char val) +{ + // ERROR CASE NO ASSERT IN EVAS CODE + if (!((x | y) < 4)) return ; + + const unsigned char bit_index = x * 4 + y; + + unsigned char *p = &bytes[7 - (bit_index >> 3)]; + + const unsigned char byte_bit_offsets = bit_index & 7; + const unsigned char mask = 1 << byte_bit_offsets; + const unsigned char etc1_val = rg_etc_selector_index_to_etc1[val]; + + const unsigned char lsb = etc1_val & 1; + const unsigned char msb = etc1_val >> 1; + + p[0] &= ~mask; + p[0] |= (lsb << byte_bit_offsets); + + p[-2] &= ~mask; + p[-2] |= (msb << byte_bit_offsets); +} + +static inline unsigned short +rg_etc_block_base4_color_get(const unsigned char bytes[8], unsigned char idx) +{ + unsigned short r, g, b; + + if (idx) + { + r = (bytes[0]) & ((1 << 4) - 1); + g = (bytes[1]) & ((1 << 4) - 1); + b = (bytes[2]) & ((1 << 4) - 1); + } + else + { + r = (bytes[0] >> 4) & ((1 << 4) - 1); + g = (bytes[1] >> 4) & ((1 << 4) - 1); + b = (bytes[2] >> 4) & ((1 << 4) - 1); + } + + return b | (g << 4) | (r << 8); +} + +static inline void +rg_etc1_block_base4_color_set(unsigned char bytes[8], unsigned char idx, unsigned short c) +{ + if (idx) + { + rg_etc1_block_byte_bits_set(bytes, cETC1AbsColor4R2BitOffset, 4, (c >> 8) & 15); + rg_etc1_block_byte_bits_set(bytes, cETC1AbsColor4G2BitOffset, 4, (c >> 4) & 15); + rg_etc1_block_byte_bits_set(bytes, cETC1AbsColor4B2BitOffset, 4, c & 15); + } + else + { + rg_etc1_block_byte_bits_set(bytes, cETC1AbsColor4R1BitOffset, 4, (c >> 8) & 15); + rg_etc1_block_byte_bits_set(bytes, cETC1AbsColor4G1BitOffset, 4, (c >> 4) & 15); + rg_etc1_block_byte_bits_set(bytes, cETC1AbsColor4B1BitOffset, 4, c & 15); + } +} + +static inline unsigned short +rg_etc1_block_base5_color_get(const unsigned char bytes[8]) +{ + unsigned short r, g, b; + + r = (bytes[0] >> 3) & ((1 << 5) - 1); + g = (bytes[1] >> 3) & ((1 << 5) - 1); + b = (bytes[2] >> 3) & ((1 << 5) - 1); + + return b | (g << 5) | (r << 10); +} + +static inline void +rg_etc1_block_base5_color_set(unsigned char bytes[8], unsigned short c) +{ + rg_etc1_block_byte_bits_set(bytes, cETC1BaseColor5RBitOffset, 5, (c >> 10) & 31); + rg_etc1_block_byte_bits_set(bytes, cETC1BaseColor5GBitOffset, 5, (c >> 5) & 31); + rg_etc1_block_byte_bits_set(bytes, cETC1BaseColor5BBitOffset, 5, c & 31); +} + +static inline unsigned short +rg_etc1_block_delta3_color_get(const unsigned char bytes[8]) +{ + unsigned short r, g, b; + + r = (bytes[0]) & ((1 << 3) - 1); + g = (bytes[1]) & ((1 << 3) - 1); + b = (bytes[2]) & ((1 << 3) - 1); + + return b | (g << 3) | (r << 6); +} + +static inline void +rg_etc1_block_delta3_color_set(unsigned char bytes[8], unsigned short c) +{ + rg_etc1_block_byte_bits_set(bytes, cETC1DeltaColor3RBitOffset, 3, (c >> 6) & 7); + rg_etc1_block_byte_bits_set(bytes, cETC1DeltaColor3GBitOffset, 3, (c >> 3) & 7); + rg_etc1_block_byte_bits_set(bytes, cETC1DeltaColor3BBitOffset, 3, c & 7); +} + +static inline unsigned short +rg_etc1_block_color5_component_pack(unsigned char r, unsigned char g, unsigned char b, + unsigned char scaled, unsigned char bias) +{ + if (scaled) + { + r = (r * 31 + bias) / 255U; + g = (g * 31 + bias) / 255U; + b = (b * 31 + bias) / 255U; + } + + r = MIN(r, 31); + g = MIN(g, 31); + b = MIN(b, 31); + + return b | (g << 5) | (r << 10); +} + +static inline unsigned short +rg_etc1_block_color5_pack(unsigned int c, unsigned char scaled, unsigned char bias) +{ + unsigned char r, g, b; + + rg_etc1_color_quad_get(c, &r, &g, &b, NULL); + return rg_etc1_block_color5_component_pack(r, g, b, scaled, bias); +} + +static inline void +rg_etc1_block_color5_component_unpack(unsigned char *r, unsigned char *g, unsigned char *b, + unsigned short packed_color5, unsigned scaled) +{ + *r = (packed_color5 >> 10) & 31; + *g = (packed_color5 >> 5) & 31; + *b = packed_color5 & 31; + + if (scaled) + { + *b = (*b << 3) | (*b >> 2); + *g = (*g << 3) | (*g >> 2); + *r = (*r << 3) | (*r >> 2); + } +} + +static inline unsigned int +rg_etc1_block_color5_unpack(unsigned short packed_color5, unsigned char scaled, unsigned char alpha) +{ + unsigned char r, g, b; + + rg_etc1_block_color5_component_unpack(&r, &g, &b, packed_color5, scaled); + + return rg_etc1_color_quad_init(r, g, b, MIN(alpha, 255)); +} + +// Inputs range from -4 to 3 (cETC1ColorDeltaMin to cETC1ColorDeltaMax) +static inline unsigned int +rg_etc1_block_delta3_pack(char r, char g, char b) +{ + // ERROR CASE NO ASSERT IN EVAS CODE + if (!((r >= cETC1ColorDeltaMin) && (r <= cETC1ColorDeltaMax))) return 0; + if (!((g >= cETC1ColorDeltaMin) && (g <= cETC1ColorDeltaMax))) return 0; + if (!((b >= cETC1ColorDeltaMin) && (b <= cETC1ColorDeltaMax))) return 0; + + if (r < 0) r += 8; + if (g < 0) g += 8; + if (b < 0) b += 8; + + return b | (g << 3) | (r << 6); +} + +// Results range from -4 to 3 (cETC1ColorDeltaMin to cETC1ColorDeltaMax) +static inline void +rg_etc1_block_delta3_unpack(char *r, char *g, char *b, unsigned short packed_delta3) +{ + *r = (packed_delta3 >> 6) & 7; + *g = (packed_delta3 >> 3) & 7; + *b = packed_delta3 & 7; + + if (*r >= 4) *r -= 8; + if (*g >= 4) *g -= 8; + if (*b >= 4) *b -= 8; +} + +static inline unsigned short +rg_etc1_block_color4_component_pack(unsigned char r, unsigned char g, unsigned char b, + unsigned char scaled, unsigned char bias) +{ + if (scaled) + { + r = (r * 15 + bias) / 255; + g = (g * 15 + bias) / 255; + b = (b * 15 + bias) / 255; + } + + r = MIN(r, 15); + g = MIN(g, 15); + b = MIN(b, 15); + + return b | (g << 4) | (r << 8); +} + +static inline unsigned short +rg_etc1_block_color4_pack(unsigned int color, unsigned char scaled, unsigned char bias) +{ + unsigned char r, g, b; + + rg_etc1_color_quad_get(color, &r, &g, &b, NULL); + return rg_etc1_block_color4_component_pack(r, g, b, scaled, bias); +} + +static inline void +rg_etc1_block_color4_component_unpack(unsigned char *r, unsigned char *g, unsigned char *b, + unsigned short packed_color4, bool scaled) +{ + *r = (packed_color4 >> 8) & 15; + *g = (packed_color4 >> 4) & 15; + *b = packed_color4 & 15; + + if (scaled) + { + *b = (*b << 4) | *b; + *g = (*g << 4) | *g; + *r = (*r << 4) | *r; + } +} + +static inline unsigned int +rg_etc1_block_color4_unpack(unsigned short packed_color4, unsigned char scaled, unsigned char alpha) +{ + unsigned char r, g, b; + + rg_etc1_block_color4_component_unpack(&r, &g, &b, packed_color4, scaled); + + return rg_etc1_color_quad_init(r, g, b, alpha); +} + + +static inline unsigned char +rg_etc1_block_color5_delta3_component_unpack(unsigned char *r, unsigned char *g, unsigned char *b, + unsigned short packed_color5, unsigned short packed_delta3, bool scaled) +{ + unsigned char success = 1; + char dc_r, dc_g, dc_b; + + rg_etc1_block_delta3_unpack(&dc_r, &dc_g, &dc_b, packed_delta3); + + *r = ((packed_color5 >> 10) & 31) + dc_r; + *g = ((packed_color5 >> 5) & 31) + dc_g; + *b = (packed_color5 & 31) + dc_b; + + if ((*r | *g | *b) > 31) + { + success = 0; + *r = MIN(*r, 31); + *g = MIN(*g, 31); + *b = MIN(*b, 31); + } + + if (scaled) + { + *r = (*r << 3) | (*r >> 2); + *g = (*g << 3) | (*g >> 2); + *b = (*b << 3) | (*b >> 2); + } + + return success; +} + + +static inline unsigned char +rg_etc1_block_color5_delta3_unpack(unsigned int *result, + unsigned short packed_color5, unsigned short packed_delta3, + bool scaled, unsigned char alpha) +{ + unsigned char success; + unsigned char r, g, b; + + success = rg_etc1_block_color5_delta3_component_unpack(&r, &g, &b, packed_color5, packed_delta3, scaled); + + *result = rg_etc1_color_quad_init(r, g, b, alpha); + return success; +} + +static inline void +rg_etc1_block_sublock_diff(unsigned int dst[4], const int *pInten_modifer_table, + unsigned char r, unsigned char g, unsigned char b) +{ + int i; + + for (i = 0; i < 4; i++) + { + int y = pInten_modifer_table[i]; + + dst[i] = rg_etc1_color_quad_init(CLAMP((int)r + y, 0, 255), + CLAMP((int) g + y,0, 255), + CLAMP((int) b + y,0, 255), + 255); + } +} + +static inline void +rg_etc1_block_subblock_color5_diff_get(unsigned int dst[4], unsigned short packed_color5, unsigned char table_idx) +{ + const int *pInten_modifer_table; + unsigned char r, g, b; + + // ERROR CASE NO ASSERT IN EVAS CODE + if (!(table_idx < cETC1IntenModifierValues)) return ; + + rg_etc1_block_color5_component_unpack(&r, &g, &b, packed_color5, 1); + + pInten_modifer_table = &rg_etc1_inten_tables[table_idx][0]; + rg_etc1_block_sublock_diff(dst, pInten_modifer_table, r, g, b); +} + +static inline unsigned char +rg_etc1_block_subblock_color5_delta3_diff_get(unsigned int dst[4], + unsigned short packed_color5, unsigned short packed_delta3, + unsigned char table_idx) +{ + const int *pInten_modifer_table; + unsigned char r, g, b; + unsigned char success; + + // ERROR CASE NO ASSERT IN EVAS CODE + if (!(table_idx < cETC1IntenModifierValues)) + { + fprintf(stderr, "table_idx %i < %i\n", table_idx, cETC1IntenModifierValues); + return 0; + } + + success = rg_etc1_block_color5_delta3_component_unpack(&r, &g, &b, packed_color5, packed_delta3, 1); + + pInten_modifer_table = &rg_etc1_inten_tables[table_idx][0]; + rg_etc1_block_sublock_diff(dst, pInten_modifer_table, r, g, b); + + return success; +} + +static inline void +rg_etc1_block_subblock_color4_abs_get(unsigned int dst[4], unsigned short packed_color4, unsigned char table_idx) +{ + const int *pInten_modifer_table; + unsigned char r, g, b; + + // ERROR CASE NO ASSERT IN EVAS CODE + if (!(table_idx < cETC1IntenModifierValues)) return ; + + rg_etc1_block_color4_component_unpack(&r, &g, &b, packed_color4, 1); + + pInten_modifer_table = &rg_etc1_inten_tables[table_idx][0]; + rg_etc1_block_sublock_diff(dst, pInten_modifer_table, r, g, b); +} + +// This is the exported function to unpack a block +bool +rg_etc1_unpack_block(const void *ETC1_block, unsigned int *pDst_pixels_BGRA, bool preserve_alpha) +{ + unsigned char diff_flag, flip_flag, table_index0, table_index1; + unsigned int subblock_colors0[4] = { 0 }; + unsigned int subblock_colors1[4] = { 0 }; + unsigned char success = 1; + const unsigned char *bytes; + bytes = (unsigned char *)ETC1_block; + + diff_flag = rg_etc1_block_diff_bit_get(ETC1_block); + flip_flag = rg_etc1_block_flip_bit_get(ETC1_block); + table_index0 = (bytes[3] >> 5) & 7; + table_index1 = (bytes[3] >> 2) & 7; + + if (diff_flag) + { + unsigned short base_color5, delta_color3; + + base_color5 = rg_etc1_block_base5_color_get(ETC1_block); + delta_color3 = rg_etc1_block_delta3_color_get(ETC1_block); + + rg_etc1_block_subblock_color5_diff_get(subblock_colors0, base_color5, table_index0); + success = rg_etc1_block_subblock_color5_delta3_diff_get(subblock_colors1, + base_color5, delta_color3, + table_index1); + } + else + { + unsigned short base_color4_0, base_color4_1; + + base_color4_0 = rg_etc_block_base4_color_get(ETC1_block, 0); + base_color4_1 = rg_etc_block_base4_color_get(ETC1_block, 1); + + rg_etc1_block_subblock_color4_abs_get(subblock_colors0, base_color4_0, table_index0); + rg_etc1_block_subblock_color4_abs_get(subblock_colors1, base_color4_1, table_index1); + } + + // FIXME: preserve_alpha and continue + // Block is either : + // 0000 + // 0000 + // 1111 + // 1111 + // or : + // 0011 + // 0011 + // 0011 + // 0011 + unsigned char val0 = (bytes[7] & 1) | ((bytes[5] & 1) << 1); + unsigned char val1 = ((bytes[7] >> 4) & 1) | (((bytes[5] >> 4) & 1) << 1); + unsigned char val2 = (bytes[6] & 1) | ((bytes[4] & 1) << 1); + unsigned char val3 = ((bytes[6] >> 4) & 1) | (((bytes[4] >> 4) & 1) << 1); + unsigned char val4 = ((bytes[7] >> 1) & 1) | (((bytes[5] >> 1) & 1) << 1); + unsigned char val5 = ((bytes[7] >> 5) & 1) | (((bytes[5] >> 5) & 1) << 1); + unsigned char val6 = ((bytes[6] >> 1) & 1) | (((bytes[4] >> 1) & 1) << 1); + unsigned char val7 = ((bytes[6] >> 5) & 1) | (((bytes[4] >> 5) & 1) << 1); + unsigned char val8 = ((bytes[7] >> 2) & 1) | (((bytes[5] >> 2) & 1) << 1); + unsigned char val9 = ((bytes[7] >> 6) & 1) | (((bytes[5] >> 6) & 1) << 1); + unsigned char val10 = ((bytes[6] >> 2) & 1) | (((bytes[4] >> 2) & 1) << 1); + unsigned char val11 = ((bytes[6] >> 6) & 1) | (((bytes[4] >> 6) & 1) << 1); + unsigned char val12 = ((bytes[7] >> 3) & 1) | (((bytes[5] >> 3) & 1) << 1); + unsigned char val13 = ((bytes[7] >> 7) & 1) | (((bytes[5] >> 7) & 1) << 1); + unsigned char val14 = ((bytes[6] >> 3) & 1) | (((bytes[4] >> 3) & 1) << 1); + unsigned char val15 = ((bytes[6] >> 7) & 1) | (((bytes[4] >> 7) & 1) << 1); + + if (preserve_alpha) // Depending on flip_flag. + { + if (flip_flag) + { + pDst_pixels_BGRA[0] = rg_etc1_color_quad_set(pDst_pixels_BGRA[0], + subblock_colors0[rg_etc1_to_selector_index[val0]]); + pDst_pixels_BGRA[1] = rg_etc1_color_quad_set(pDst_pixels_BGRA[1], + subblock_colors0[rg_etc1_to_selector_index[val1]]); + pDst_pixels_BGRA[2] = rg_etc1_color_quad_set(pDst_pixels_BGRA[2], + subblock_colors0[rg_etc1_to_selector_index[val2]]); + pDst_pixels_BGRA[3] = rg_etc1_color_quad_set(pDst_pixels_BGRA[3], + subblock_colors0[rg_etc1_to_selector_index[val3]]); + pDst_pixels_BGRA[4] = rg_etc1_color_quad_set(pDst_pixels_BGRA[4], + subblock_colors0[rg_etc1_to_selector_index[val4]]); + pDst_pixels_BGRA[5] = rg_etc1_color_quad_set(pDst_pixels_BGRA[5], + subblock_colors0[rg_etc1_to_selector_index[val5]]); + pDst_pixels_BGRA[6] = rg_etc1_color_quad_set(pDst_pixels_BGRA[6], + subblock_colors0[rg_etc1_to_selector_index[val6]]); + pDst_pixels_BGRA[7] = rg_etc1_color_quad_set(pDst_pixels_BGRA[7], + subblock_colors0[rg_etc1_to_selector_index[val7]]); + pDst_pixels_BGRA[8] = rg_etc1_color_quad_set(pDst_pixels_BGRA[8], + subblock_colors1[rg_etc1_to_selector_index[val8]]); + pDst_pixels_BGRA[9] = rg_etc1_color_quad_set(pDst_pixels_BGRA[9], + subblock_colors1[rg_etc1_to_selector_index[val9]]); + pDst_pixels_BGRA[10] = rg_etc1_color_quad_set(pDst_pixels_BGRA[10], + subblock_colors1[rg_etc1_to_selector_index[val10]]); + pDst_pixels_BGRA[11] = rg_etc1_color_quad_set(pDst_pixels_BGRA[11], + subblock_colors1[rg_etc1_to_selector_index[val11]]); + pDst_pixels_BGRA[12] = rg_etc1_color_quad_set(pDst_pixels_BGRA[12], + subblock_colors1[rg_etc1_to_selector_index[val12]]); + pDst_pixels_BGRA[13] = rg_etc1_color_quad_set(pDst_pixels_BGRA[13], + subblock_colors1[rg_etc1_to_selector_index[val13]]); + pDst_pixels_BGRA[14] = rg_etc1_color_quad_set(pDst_pixels_BGRA[14], + subblock_colors1[rg_etc1_to_selector_index[val14]]); + pDst_pixels_BGRA[15] = rg_etc1_color_quad_set(pDst_pixels_BGRA[15], + subblock_colors1[rg_etc1_to_selector_index[val15]]); + } + else + { + pDst_pixels_BGRA[0] = rg_etc1_color_quad_set(pDst_pixels_BGRA[0], + subblock_colors0[rg_etc1_to_selector_index[val0]]); + pDst_pixels_BGRA[1] = rg_etc1_color_quad_set(pDst_pixels_BGRA[1], + subblock_colors0[rg_etc1_to_selector_index[val1]]); + pDst_pixels_BGRA[2] = rg_etc1_color_quad_set(pDst_pixels_BGRA[2], + subblock_colors1[rg_etc1_to_selector_index[val2]]); + pDst_pixels_BGRA[3] = rg_etc1_color_quad_set(pDst_pixels_BGRA[3], + subblock_colors1[rg_etc1_to_selector_index[val3]]); + pDst_pixels_BGRA[4] = rg_etc1_color_quad_set(pDst_pixels_BGRA[4], + subblock_colors0[rg_etc1_to_selector_index[val4]]); + pDst_pixels_BGRA[5] = rg_etc1_color_quad_set(pDst_pixels_BGRA[5], + subblock_colors0[rg_etc1_to_selector_index[val5]]); + pDst_pixels_BGRA[6] = rg_etc1_color_quad_set(pDst_pixels_BGRA[6], + subblock_colors1[rg_etc1_to_selector_index[val6]]); + pDst_pixels_BGRA[7] = rg_etc1_color_quad_set(pDst_pixels_BGRA[7], + subblock_colors1[rg_etc1_to_selector_index[val7]]); + pDst_pixels_BGRA[8] = rg_etc1_color_quad_set(pDst_pixels_BGRA[8], + subblock_colors0[rg_etc1_to_selector_index[val8]]); + pDst_pixels_BGRA[9] = rg_etc1_color_quad_set(pDst_pixels_BGRA[9], + subblock_colors0[rg_etc1_to_selector_index[val9]]); + pDst_pixels_BGRA[10] = rg_etc1_color_quad_set(pDst_pixels_BGRA[10], + subblock_colors1[rg_etc1_to_selector_index[val10]]); + pDst_pixels_BGRA[11] = rg_etc1_color_quad_set(pDst_pixels_BGRA[11], + subblock_colors1[rg_etc1_to_selector_index[val11]]); + pDst_pixels_BGRA[12] = rg_etc1_color_quad_set(pDst_pixels_BGRA[12], + subblock_colors0[rg_etc1_to_selector_index[val12]]); + pDst_pixels_BGRA[13] = rg_etc1_color_quad_set(pDst_pixels_BGRA[13], + subblock_colors0[rg_etc1_to_selector_index[val13]]); + pDst_pixels_BGRA[14] = rg_etc1_color_quad_set(pDst_pixels_BGRA[14], + subblock_colors1[rg_etc1_to_selector_index[val14]]); + pDst_pixels_BGRA[15] = rg_etc1_color_quad_set(pDst_pixels_BGRA[15], + subblock_colors1[rg_etc1_to_selector_index[val15]]); + } + } + else + { + if (flip_flag) + { + pDst_pixels_BGRA[0] = subblock_colors0[rg_etc1_to_selector_index[val0]]; + pDst_pixels_BGRA[1] = subblock_colors0[rg_etc1_to_selector_index[val1]]; + pDst_pixels_BGRA[2] = subblock_colors0[rg_etc1_to_selector_index[val2]]; + pDst_pixels_BGRA[3] = subblock_colors0[rg_etc1_to_selector_index[val3]]; + pDst_pixels_BGRA[4] = subblock_colors0[rg_etc1_to_selector_index[val4]]; + pDst_pixels_BGRA[5] = subblock_colors0[rg_etc1_to_selector_index[val5]]; + pDst_pixels_BGRA[6] = subblock_colors0[rg_etc1_to_selector_index[val6]]; + pDst_pixels_BGRA[7] = subblock_colors0[rg_etc1_to_selector_index[val7]]; + pDst_pixels_BGRA[8] = subblock_colors1[rg_etc1_to_selector_index[val8]]; + pDst_pixels_BGRA[9] = subblock_colors1[rg_etc1_to_selector_index[val9]]; + pDst_pixels_BGRA[10] = subblock_colors1[rg_etc1_to_selector_index[val10]]; + pDst_pixels_BGRA[11] = subblock_colors1[rg_etc1_to_selector_index[val11]]; + pDst_pixels_BGRA[12] = subblock_colors1[rg_etc1_to_selector_index[val12]]; + pDst_pixels_BGRA[13] = subblock_colors1[rg_etc1_to_selector_index[val13]]; + pDst_pixels_BGRA[14] = subblock_colors1[rg_etc1_to_selector_index[val14]]; + pDst_pixels_BGRA[15] = subblock_colors1[rg_etc1_to_selector_index[val15]]; + } + else + { + pDst_pixels_BGRA[0] = subblock_colors0[rg_etc1_to_selector_index[val0]]; + pDst_pixels_BGRA[1] = subblock_colors0[rg_etc1_to_selector_index[val1]]; + pDst_pixels_BGRA[2] = subblock_colors1[rg_etc1_to_selector_index[val2]]; + pDst_pixels_BGRA[3] = subblock_colors1[rg_etc1_to_selector_index[val3]]; + pDst_pixels_BGRA[4] = subblock_colors0[rg_etc1_to_selector_index[val4]]; + pDst_pixels_BGRA[5] = subblock_colors0[rg_etc1_to_selector_index[val5]]; + pDst_pixels_BGRA[6] = subblock_colors1[rg_etc1_to_selector_index[val6]]; + pDst_pixels_BGRA[7] = subblock_colors1[rg_etc1_to_selector_index[val7]]; + pDst_pixels_BGRA[8] = subblock_colors0[rg_etc1_to_selector_index[val8]]; + pDst_pixels_BGRA[9] = subblock_colors0[rg_etc1_to_selector_index[val9]]; + pDst_pixels_BGRA[10] = subblock_colors1[rg_etc1_to_selector_index[val10]]; + pDst_pixels_BGRA[11] = subblock_colors1[rg_etc1_to_selector_index[val11]]; + pDst_pixels_BGRA[12] = subblock_colors0[rg_etc1_to_selector_index[val12]]; + pDst_pixels_BGRA[13] = subblock_colors0[rg_etc1_to_selector_index[val13]]; + pDst_pixels_BGRA[14] = subblock_colors1[rg_etc1_to_selector_index[val14]]; + pDst_pixels_BGRA[15] = subblock_colors1[rg_etc1_to_selector_index[val15]]; + } + } + + return success; +} + +// NOTE: Most of the following loop could be unrolled, but for sanity and readability, I did +// prefer to stick to simpler solution. +static inline unsigned int * +rg_etc1_indirect_radix_sort(unsigned int num_indices, unsigned int pIndices0[8], unsigned int pIndices1[8], + const unsigned short pKeys[8], unsigned int key_ofs, unsigned char key_size, + unsigned char init_indices) +{ + unsigned int hist[256 * 4]; + unsigned int *p; + unsigned int *q; + unsigned int *pCur; + unsigned int *pNew; + unsigned int key; + unsigned int pass; + + // ERROR CASE NO ASSERT IN EVAS CODE + if (!(key_ofs < sizeof(unsigned int) * 8)) return NULL; + if (!((key_size >= 1) && (key_size <= 4))) return NULL; + + if (init_indices) + { + unsigned int i; + + p = pIndices0; + for (i = 0; i < num_indices; p++, i++) + *p = i; + } + + memset(hist, 0, sizeof (hist)); + +#define RG_ETC1_GET_KEY(p) (*(const unsigned int*)((const unsigned char*)(pKeys + *(p)) + key_ofs)) +#define RG_ETC1_GET_KEY_FROM_INDEX(i) (*(const unsigned int*)((const unsigned char*)(pKeys + (i)) + key_ofs)) + + switch (key_size) + { + case 4: + p = pIndices0; + q = pIndices0 + num_indices; + + for (; p != q; p++) + { + key = RG_ETC1_GET_KEY(p); + + hist[ key & 0xFF]++; + hist[256 + ((key >> 8) & 0xFF)]++; + hist[512 + ((key >> 16) & 0xFF)]++; + hist[768 + ((key >> 24) & 0xFF)]++; + } + break; + case 3: + p = pIndices0; + q = pIndices0 + num_indices; + + for ( ; p != q; p++) + { + key = RG_ETC1_GET_KEY(p); + + hist[ key & 0xFF]++; + hist[256 + ((key >> 8) & 0xFF)]++; + hist[512 + ((key >> 16) & 0xFF)]++; + } + break; + case 2: + p = pIndices0; + q = pIndices0 + num_indices; + + for ( ; p != q; p++) + { + key = RG_ETC1_GET_KEY(p); + + hist[ key & 0xFF]++; + hist[256 + ((key >> 8) & 0xFF)]++; + } + break; + case 1: + p = pIndices0; + q = pIndices0 + num_indices; + + for ( ; p != q; p++) + { + key = RG_ETC1_GET_KEY(p); + + hist[key & 0xFF]++; + } + break; + default: + abort(); + } + + pCur = pIndices0; + pNew = pIndices1; + + for (pass = 0; pass < key_size; pass++) + { + unsigned int offsets[256]; + const unsigned int *pHist = &hist[pass << 8]; + unsigned int *t; + unsigned int cur_ofs = 0; + unsigned int i; + unsigned int pass_shift = pass << 3; + + for (i = 0; i < 256; i++) + { + offsets[i] = cur_ofs; + cur_ofs += pHist[i]; + } + + q = pCur + num_indices; + for (p = pCur; p != q; p++) + { + unsigned int dst_offset; + unsigned int idx = p[0]; + unsigned int c = (RG_ETC1_GET_KEY_FROM_INDEX(idx) >> pass_shift) & 0xFF; + + dst_offset = offsets[c]++; + pNew[dst_offset] = idx; + } + + t = pCur; + pCur = pNew; + pNew = t; + } + + return pCur; +} + +typedef struct _Etc1_Solution_Coordinates Etc1_Solution_Coordinates; +struct _Etc1_Solution_Coordinates +{ + color_quad_u8 m_unscaled_color; + unsigned int m_inten_table; + unsigned char m_color4; +}; + +static inline void +rg_etc1_solution_coordinates_component_set(Etc1_Solution_Coordinates *solution, + int r, int g, int b, + unsigned int inten_table, unsigned char color4) +{ + rg_etc1_color_quad_u8_init(&solution->m_unscaled_color, r, g, b, 255); + solution->m_inten_table = inten_table; + solution->m_color4 = color4; +} + +static inline void +rg_etc1_solution_coordinates_set(Etc1_Solution_Coordinates *solution, + color_quad_u8 unscaled_color, unsigned int inten_table, unsigned char color4) +{ + rg_etc1_color_quad_u8_copy(&solution->m_unscaled_color, &unscaled_color); + solution->m_inten_table = inten_table; + solution->m_color4 = color4; +} + +static inline void +rg_etc1_solution_coordinates_clear(Etc1_Solution_Coordinates *solution) +{ + memset(solution, 0, sizeof (Etc1_Solution_Coordinates)); +} + +static inline void +rg_etc1_solution_coordinates_component_get(const Etc1_Solution_Coordinates *solution, + unsigned char *r, unsigned char *g, unsigned char *b) +{ + *r = solution->m_unscaled_color.comp.r; + *g = solution->m_unscaled_color.comp.g; + *b = solution->m_unscaled_color.comp.b; + + if (solution->m_color4) + { + *r = *r | (*r << 4); + *g = *g | (*g << 4); + *b = *b | (*b << 4); + } + else + { + *r = (*r >> 2) | (*r << 3); + *g = (*g >> 2) | (*g << 3); + *b = (*b >> 2) | (*b << 3); + } +} + +static inline void +rg_etc1_solution_coordinates_get_scaled_color(color_quad_u8 *color, const Etc1_Solution_Coordinates *coords) +{ + unsigned char br, bg, bb; + + rg_etc1_solution_coordinates_component_get(coords, &br, &bg, &bb); + rg_etc1_color_quad_u8_init(color, br, bg, bb, 255); +} + +static inline void +rg_etc1_solution_coordinates_block_colors_get(const Etc1_Solution_Coordinates *coords, color_quad_u8 colors[4]) +{ + const int* pInten_table = rg_etc1_inten_tables[coords->m_inten_table]; + unsigned char i; + unsigned char br, bg, bb; + + rg_etc1_solution_coordinates_component_get(coords, &br, &bg, &bb); + + for (i = 0; i < 4; i++) + rg_etc1_color_quad_u8_init(&colors[i], br + pInten_table[i], bg + pInten_table[i], bb + pInten_table[i], 255); +} + +static inline void +rg_etc1_pack_params_clear(rg_etc1_pack_params *params) +{ + params->m_quality = rg_etc1_high_quality; + params->m_dithering = EINA_FALSE; +} + +static const int rg_etc1_default_scan_delta[] = { 0 }; + +typedef struct _rg_etc1_optimizer_params rg_etc1_optimizer_params; +struct _rg_etc1_optimizer_params +{ + rg_etc1_pack_params *base_params; + uint m_num_src_pixels; + const color_quad_u8* m_pSrc_pixels; + + bool m_use_color4; + const int* m_pScan_deltas; + uint m_scan_delta_size; + + color_quad_u8 m_base_color5; + bool m_constrain_against_base_color5; +}; + +static inline void +rg_etc1_optimizer_params_clean(rg_etc1_optimizer_params *params) +{ + params->m_num_src_pixels = 0; + params->m_pSrc_pixels = 0; + + params->m_use_color4 = EINA_FALSE; + params->m_pScan_deltas = rg_etc1_default_scan_delta; + params->m_scan_delta_size = 1; + + rg_etc1_color_quad_u8_clear(¶ms->m_base_color5); + params->m_constrain_against_base_color5 = EINA_FALSE; +} + +static inline void +rg_etc1_optimizer_params_base_clear(rg_etc1_optimizer_params *params) +{ + rg_etc1_pack_params_clear(params->base_params); + rg_etc1_optimizer_params_clean(params); +} + +typedef struct +{ + uint64 m_error; + color_quad_u8 m_block_color_unscaled; + uint m_block_inten_table; + uint m_n; + uint8* m_pSelectors; + bool m_block_color4; +} rg_etc1_optimizer_results; + +static inline void +rg_etc1_optimizer_results_duplicate(rg_etc1_optimizer_results *dst, const rg_etc1_optimizer_results *src) +{ + rg_etc1_color_quad_u8_copy(&dst->m_block_color_unscaled,&src->m_block_color_unscaled); + dst->m_block_color4 = src->m_block_color4; + dst->m_block_inten_table = src->m_block_inten_table; + dst->m_error = src->m_error; + // ERROR CASE NO ASSERT IN EVAS CODE + if (!(dst->m_n == src->m_n)) return ; + memcpy(dst->m_pSelectors, src->m_pSelectors, src->m_n); +} + +typedef struct +{ + Etc1_Solution_Coordinates m_coords; + uint8 m_selectors[8]; + uint64 m_error; + bool m_valid; +} rg_etc1_potential_solution; + +static inline void +rg_etc1_potential_solution_clear(rg_etc1_potential_solution *solution) +{ + rg_etc1_solution_coordinates_clear(&solution->m_coords); + solution->m_error = cUINT64_MAX; + solution->m_valid = EINA_FALSE; +} + +typedef struct +{ + const rg_etc1_optimizer_params* m_pParams; + rg_etc1_optimizer_results* m_pResult; + + int m_limit; + + float m_avg_color[3]; + int m_br, m_bg, m_bb; + uint16 m_luma[8]; + uint32 m_sorted_luma[2][8]; + const uint32* m_pSorted_luma_indices; + uint32* m_pSorted_luma; + + uint8 m_selectors[8]; + uint8 m_best_selectors[8]; + + rg_etc1_potential_solution m_best_solution; + rg_etc1_potential_solution m_trial_solution; + uint8 m_temp_selectors[8]; +}rg_etc1_optimizer; + +static inline void +rg_etc1_optimizer_clear(rg_etc1_optimizer *optimizer) +{ + optimizer->m_pParams = NULL; + optimizer->m_pResult = NULL; + optimizer->m_pSorted_luma = NULL; + optimizer->m_pSorted_luma_indices = NULL; + optimizer->m_br = optimizer->m_bg = optimizer->m_bb = 0; + rg_etc1_potential_solution_clear(&optimizer->m_best_solution); + rg_etc1_potential_solution_clear(&optimizer->m_trial_solution); +} + +static bool rg_etc1_optimizer_evaluate_solution(rg_etc1_optimizer *optimizer, const Etc1_Solution_Coordinates* coords, + rg_etc1_potential_solution* trial_solution, + rg_etc1_potential_solution* pBest_solution); +static bool rg_etc1_optimizer_evaluate_solution_fast(rg_etc1_optimizer *optimizer,const Etc1_Solution_Coordinates *coords, + rg_etc1_potential_solution *trial_solution, + rg_etc1_potential_solution *pBest_solution); + +static bool +rg_etc1_optimizer_compute(rg_etc1_optimizer *optimizer) +{ + const uint n = optimizer->m_pParams->m_num_src_pixels; + const int scan_delta_size = optimizer->m_pParams->m_scan_delta_size; + int zdi; + + // Scan through a subset of the 3D lattice centered around the avg block color trying each 3D (555 or 444) lattice point as a potential block color. + // Each time a better solution is found try to refine the current solution's block color based of the current selectors and intensity table index. + for (zdi = 0; zdi < scan_delta_size; zdi++) + { + const int zd = optimizer->m_pParams->m_pScan_deltas[zdi]; + const int mbb = optimizer->m_bb + zd; + int ydi; + if (mbb < 0) continue; else if (mbb > optimizer->m_limit) break; + + for (ydi = 0; ydi < scan_delta_size; ydi++) + { + const int yd = optimizer->m_pParams->m_pScan_deltas[ydi]; + const int mbg = optimizer->m_bg + yd; + int xdi; + + if (mbg < 0) continue; else if (mbg > optimizer->m_limit) break; + + for (xdi = 0; xdi < scan_delta_size; xdi++) + { + const int xd = optimizer->m_pParams->m_pScan_deltas[xdi]; + const int mbr = optimizer->m_br + xd; + Etc1_Solution_Coordinates coords; + uint max_refinement_trials; + uint refinement_trial; + + rg_etc1_solution_coordinates_component_set(&coords, mbr, mbg, mbb, 0, optimizer->m_pParams->m_use_color4); + if (mbr < 0) continue; else if (mbr > optimizer->m_limit) break; + + if (optimizer->m_pParams->base_params->m_quality == rg_etc1_high_quality) + { + if (!rg_etc1_optimizer_evaluate_solution(optimizer, &coords, &optimizer->m_trial_solution, &optimizer->m_best_solution)) + continue; + } + else + { + if (!rg_etc1_optimizer_evaluate_solution_fast(optimizer, &coords, &optimizer->m_trial_solution, &optimizer->m_best_solution)) + continue; + } + + // Now we have the input block, the avg. color of the input pixels, a set of trial selector indices, and the block color+intensity index. + // Now, for each component, attempt to refine the current solution by solving a simple linear equation. For example, for 4 colors: + // The goal is: + // pixel0 - (block_color+inten_table[selector0]) + pixel1 - (block_color+inten_table[selector1]) + pixel2 - (block_color+inten_table[selector2]) + pixel3 - (block_color+inten_table[selector3]) = 0 + // Rearranging this: + // (pixel0 + pixel1 + pixel2 + pixel3) - (block_color+inten_table[selector0]) - (block_color+inten_table[selector1]) - (block_color+inten_table[selector2]) - (block_color+inten_table[selector3]) = 0 + // (pixel0 + pixel1 + pixel2 + pixel3) - block_color - inten_table[selector0] - block_color-inten_table[selector1] - block_color-inten_table[selector2] - block_color-inten_table[selector3] = 0 + // (pixel0 + pixel1 + pixel2 + pixel3) - 4*block_color - inten_table[selector0] - inten_table[selector1] - inten_table[selector2] - inten_table[selector3] = 0 + // (pixel0 + pixel1 + pixel2 + pixel3) - 4*block_color - (inten_table[selector0] + inten_table[selector1] + inten_table[selector2] + inten_table[selector3]) = 0 + // (pixel0 + pixel1 + pixel2 + pixel3)/4 - block_color - (inten_table[selector0] + inten_table[selector1] + inten_table[selector2] + inten_table[selector3])/4 = 0 + // block_color = (pixel0 + pixel1 + pixel2 + pixel3)/4 - (inten_table[selector0] + inten_table[selector1] + inten_table[selector2] + inten_table[selector3])/4 + // So what this means: + // optimal_block_color = avg_input - avg_inten_delta + // So the optimal block color can be computed by taking the average block color and subtracting the current average of the intensity delta. + // Unfortunately, optimal_block_color must then be quantized to 555 or 444 so it's not always possible to improve matters using this formula. + // Also, the above formula is for unclamped intensity deltas. The actual implementation takes into account clamping. + + max_refinement_trials = (optimizer->m_pParams->base_params->m_quality == rg_etc1_low_quality) ? 2 : (((xd | yd | zd) == 0) ? 4 : 2); + for (refinement_trial = 0; refinement_trial < max_refinement_trials; refinement_trial++) + { + const uint8* pSelectors = optimizer->m_best_solution.m_selectors; + const int* pInten_table = rg_etc1_inten_tables[optimizer->m_best_solution.m_coords.m_inten_table]; + + int delta_sum_r = 0, delta_sum_g = 0, delta_sum_b = 0; + uint index; + color_quad_u8 base_color; + float avg_delta_r_f, avg_delta_g_f, avg_delta_b_f; + int br1, bg1, bb1; + bool skip; + Etc1_Solution_Coordinates coords1; + rg_etc1_solution_coordinates_get_scaled_color(&base_color, &optimizer->m_best_solution.m_coords); + + for (index = 0; index < n; index++) + { + const uint s = *pSelectors++; + const int yyd = pInten_table[s]; + // Compute actual delta being applied to each pixel, taking into account clamping. + delta_sum_r += CLAMP(base_color.comp.r + yyd, 0, 255) - base_color.comp.r; + delta_sum_g += CLAMP(base_color.comp.g + yyd, 0, 255) - base_color.comp.g; + delta_sum_b += CLAMP(base_color.comp.b + yyd, 0, 255) - base_color.comp.b; + } + if ((!delta_sum_r) && (!delta_sum_g) && (!delta_sum_b)) + break; + avg_delta_r_f = (float)(delta_sum_r) / n; + avg_delta_g_f = (float)(delta_sum_g) / n; + avg_delta_b_f = (float)(delta_sum_b) / n; + br1 = (uint)CLAMP(((optimizer->m_avg_color[0] - avg_delta_r_f) * optimizer->m_limit / 255.0f + .5f), + 0, optimizer->m_limit); + bg1 = (uint)CLAMP(((optimizer->m_avg_color[1] - avg_delta_g_f) * optimizer->m_limit / 255.0f + .5f), + 0, optimizer->m_limit); + bb1 = (uint)CLAMP(((optimizer->m_avg_color[2] - avg_delta_b_f) * optimizer->m_limit / 255.0f + .5f), + 0, optimizer->m_limit); + skip = EINA_FALSE; + + if ((mbr == br1) && (mbg == bg1) && (mbb == bb1)) { + skip = EINA_TRUE; + } else if ((br1 == optimizer->m_best_solution.m_coords.m_unscaled_color.comp.r) && + (bg1 == optimizer->m_best_solution.m_coords.m_unscaled_color.comp.g) && + (bb1 == optimizer->m_best_solution.m_coords.m_unscaled_color.comp.b)) { + skip = EINA_TRUE; + } else if ((optimizer->m_br == br1) && (optimizer->m_bg == bg1) && (optimizer->m_bb == bb1)) { + skip = EINA_TRUE; + } + + if (skip) + break; + + rg_etc1_solution_coordinates_component_set(&coords1, br1, bg1, bb1, 0, optimizer->m_pParams->m_use_color4); + if (optimizer->m_pParams->base_params->m_quality == rg_etc1_high_quality) + { + if (!rg_etc1_optimizer_evaluate_solution(optimizer, &coords1, &optimizer->m_trial_solution, &optimizer->m_best_solution)) + break; + } + else + { + if (!rg_etc1_optimizer_evaluate_solution_fast(optimizer, &coords1, &optimizer->m_trial_solution, &optimizer->m_best_solution)) + break; + } + + } // refinement_trial + + } // xdi + } // ydi + } // zdi + + if (!optimizer->m_best_solution.m_valid) + { + optimizer->m_pResult->m_error = cUINT32_MAX; + return EINA_FALSE; + } + +#ifdef RG_ETC1_BUILD_DEBUG + { + color_quad_u8 block_colors[4]; + const color_quad_u8* pSrc_pixels; + uint64 actual_error=0; + uint i; + const uint8* pSelectors = optimizer->m_best_solution.m_selectors; + + rg_etc1_solution_coordinates_block_colors_get(&optimizer->m_best_solution.m_coords, block_colors); + pSrc_pixels = optimizer->m_pParams->m_pSrc_pixels; + for (i = 0; i < n; i++) + actual_error += rg_etc1_color_quad_u8_rgb_squared_distance(pSrc_pixels[i], block_colors[pSelectors[i]]); + + // ERROR CASE NO ASSERT IN EVAS CODE + if (actual_error != optimizer->m_best_solution.m_error) + return EINA_FALSE; + } +#endif + + optimizer->m_pResult->m_error = optimizer->m_best_solution.m_error; + rg_etc1_color_quad_u8_copy(&optimizer->m_pResult->m_block_color_unscaled,&optimizer->m_best_solution.m_coords.m_unscaled_color); + optimizer->m_pResult->m_block_color4 = optimizer->m_best_solution.m_coords.m_color4; + optimizer->m_pResult->m_block_inten_table = optimizer->m_best_solution.m_coords.m_inten_table; + memcpy(optimizer->m_pResult->m_pSelectors, optimizer->m_best_solution.m_selectors, n); + optimizer->m_pResult->m_n = n; + + return EINA_TRUE; +} + +void +rg_etc1_optimizer_init(rg_etc1_optimizer *optimizer, const rg_etc1_optimizer_params *params, + rg_etc1_optimizer_results *result) +{ + // This version is hardcoded for 8 pixel subblocks. + // ERROR CASE NO ASSERT IN EVAS CODE + if (params->m_num_src_pixels != 8) return ; + + const uint n = 8; + uint i; + float avg_color[3]; + float fc[3]; + + optimizer->m_pParams = params; + optimizer->m_pResult = result; + + rg_etc1_vec_init(avg_color, 0.0f); + optimizer->m_limit = optimizer->m_pParams->m_use_color4 ? 15 : 31; + + for ( i = 0; i < n; i++) + { + const color_quad_u8 *c = &optimizer->m_pParams->m_pSrc_pixels[i]; + rg_etc1_vec_set(fc, c->comp.r, c->comp.g, c->comp.b); + + rg_etc1_vec_add(avg_color,fc); + + optimizer->m_luma[i] = (uint16)(c->comp.r + c->comp.g + c->comp.b); + optimizer->m_sorted_luma[0][i] = i; + } + rg_etc1_vec_scale(avg_color, (1.0f/(float)(n))); + rg_etc1_vec_copy(optimizer->m_avg_color,avg_color); + + optimizer->m_br = MIN((int)(optimizer->m_avg_color[0] * optimizer->m_limit / 255.0f + .5f), optimizer->m_limit); + optimizer->m_bg = MIN((int)(optimizer->m_avg_color[1] * optimizer->m_limit / 255.0f + .5f), optimizer->m_limit); + optimizer->m_bb = MIN((int)(optimizer->m_avg_color[2] * optimizer->m_limit / 255.0f + .5f), optimizer->m_limit); + + if (optimizer->m_pParams->base_params->m_quality <= rg_etc1_medium_quality) + { + optimizer->m_pSorted_luma_indices = rg_etc1_indirect_radix_sort(n, optimizer->m_sorted_luma[0], + optimizer->m_sorted_luma[1], optimizer->m_luma, + 0, sizeof(optimizer->m_luma[0]), EINA_FALSE); + optimizer->m_pSorted_luma = optimizer->m_sorted_luma[0]; + + if (optimizer->m_pSorted_luma_indices == optimizer->m_sorted_luma[0]) + optimizer->m_pSorted_luma = optimizer->m_sorted_luma[1]; + + for (i = 0; i < n; i++) + optimizer->m_pSorted_luma[i] = optimizer->m_luma[optimizer->m_pSorted_luma_indices[i]]; + } + + rg_etc1_solution_coordinates_clear(&optimizer->m_best_solution.m_coords); + optimizer->m_best_solution.m_valid = EINA_FALSE; + optimizer->m_best_solution.m_error = cUINT64_MAX; +} + +static bool +rg_etc1_optimizer_evaluate_solution(rg_etc1_optimizer *optimizer, const Etc1_Solution_Coordinates* coords, + rg_etc1_potential_solution* trial_solution, rg_etc1_potential_solution* pBest_solution) +{ + color_quad_u8 base_color; + const uint n = 8; + uint inten_table; + bool success = EINA_FALSE; + + trial_solution->m_valid = EINA_FALSE; + + if (optimizer->m_pParams->m_constrain_against_base_color5) + { + int dr, dg, db; + dr = coords->m_unscaled_color.comp.r - optimizer->m_pParams->m_base_color5.comp.r; + dg = coords->m_unscaled_color.comp.g - optimizer->m_pParams->m_base_color5.comp.g; + db = coords->m_unscaled_color.comp.b - optimizer->m_pParams->m_base_color5.comp.b; + + if ((MIN(MIN(dr,dg),db) < cETC1ColorDeltaMin) || (MAX(MAX(dr,dg),db) > cETC1ColorDeltaMax)) + return EINA_FALSE; + } + + rg_etc1_solution_coordinates_get_scaled_color(&base_color, coords); + trial_solution->m_error = cUINT64_MAX; + + for (inten_table = 0; inten_table < cETC1IntenModifierValues; inten_table++) + { + const int* pInten_table = rg_etc1_inten_tables[inten_table]; + uint64 total_error = 0; + color_quad_u8 block_colors[4]; + const color_quad_u8* pSrc_pixels = optimizer->m_pParams->m_pSrc_pixels; + + uint c; + for (c = 0; c < 4; c++) + { + const int yd = pInten_table[c]; + rg_etc1_color_quad_u8_init(&block_colors[c], base_color.comp.r+yd, base_color.comp.g+yd, base_color.comp.b+yd, 0); + } + + for (c = 0; c < n; c++) + { + uint best_selector_index = 0, best_error, trial_error; + const color_quad_u8* src_pixel = pSrc_pixels++; + + best_error = SQUARE((src_pixel->comp.r - block_colors[0].comp.r)) + + SQUARE((src_pixel->comp.g - block_colors[0].comp.g)) + + SQUARE((src_pixel->comp.b - block_colors[0].comp.b)); + trial_error = SQUARE((src_pixel->comp.r - block_colors[1].comp.r)) + + SQUARE((src_pixel->comp.g - block_colors[1].comp.g)) + + SQUARE((src_pixel->comp.b - block_colors[1].comp.b)); + if (trial_error < best_error) + { + best_error = trial_error; + best_selector_index = 1; + } + + trial_error = SQUARE((src_pixel->comp.r - block_colors[2].comp.r)) + + SQUARE((src_pixel->comp.g - block_colors[2].comp.g)) + + SQUARE((src_pixel->comp.b - block_colors[2].comp.b)); + if (trial_error < best_error) + { + best_error = trial_error; + best_selector_index = 2; + } + + trial_error = SQUARE((src_pixel->comp.r - block_colors[3].comp.r)) + + SQUARE((src_pixel->comp.g - block_colors[3].comp.g)) + + SQUARE((src_pixel->comp.b - block_colors[3].comp.b)); + if (trial_error < best_error) + { + best_error = trial_error; + best_selector_index = 3; + } + + optimizer->m_temp_selectors[c] = (uint8)(best_selector_index); + + total_error += best_error; + if (total_error >= trial_solution->m_error) + break; + } + + if (total_error < trial_solution->m_error) + { + trial_solution->m_error = total_error; + trial_solution->m_coords.m_inten_table = inten_table; + memcpy(trial_solution->m_selectors, optimizer->m_temp_selectors, 8); + trial_solution->m_valid = EINA_TRUE; + } + } + rg_etc1_color_quad_u8_copy(&trial_solution->m_coords.m_unscaled_color,&coords->m_unscaled_color); + trial_solution->m_coords.m_color4 = optimizer->m_pParams->m_use_color4; + + if (pBest_solution) + { + if (trial_solution->m_error < pBest_solution->m_error) + { + memcpy(pBest_solution,trial_solution,sizeof(rg_etc1_potential_solution)); + success = EINA_TRUE; + } + } + + return success; +} + +static bool +rg_etc1_optimizer_evaluate_solution_fast(rg_etc1_optimizer *optimizer, const Etc1_Solution_Coordinates *coords, + rg_etc1_potential_solution *trial_solution, rg_etc1_potential_solution *pBest_solution) +{ + color_quad_u8 base_color; + const uint n = 8; + int inten_table; + bool success = EINA_FALSE; + + if (optimizer->m_pParams->m_constrain_against_base_color5) + { + int dr, dg, db; + dr = coords->m_unscaled_color.comp.r - optimizer->m_pParams->m_base_color5.comp.r; + dg = coords->m_unscaled_color.comp.g - optimizer->m_pParams->m_base_color5.comp.g; + db = coords->m_unscaled_color.comp.b - optimizer->m_pParams->m_base_color5.comp.b; + + + if ((MIN(MIN(dr,dg),db) < cETC1ColorDeltaMin) || (MAX(MAX(dr,dg),db) > cETC1ColorDeltaMax)) + { + trial_solution->m_valid = EINA_FALSE; + return EINA_FALSE; + } + } + + rg_etc1_solution_coordinates_get_scaled_color(&base_color,coords); + + trial_solution->m_error = cUINT64_MAX; + + for (inten_table = cETC1IntenModifierValues - 1; inten_table >= 0; --inten_table) + { + const int* pInten_table = rg_etc1_inten_tables[inten_table]; + uint block_inten[4]; + color_quad_u8 block_colors[4]; + uint block_inten_midpoints[3]; + uint64 total_error = 0; + const color_quad_u8* pSrc_pixels = optimizer->m_pParams->m_pSrc_pixels; + uint s, c; + + for (s = 0; s < 4; s++) + { + const int yd = pInten_table[s]; + rg_etc1_color_quad_u8_init(&block_colors[s], base_color.comp.r+yd, base_color.comp.g+yd, base_color.comp.b+yd, 0); + block_inten[s] = block_colors[s].comp.r + block_colors[s].comp.g + block_colors[s].comp.b; + } + + // evaluate_solution_fast() enforces/assumesd a total ordering of the input colors along the intensity (1,1,1) axis to more quickly classify the inputs to selectors. + // The inputs colors have been presorted along the projection onto this axis, and ETC1 block colors are always ordered along the intensity axis, so this classification is fast. + // 0 1 2 3 + // 01 12 23 + block_inten_midpoints[0] = block_inten[0] + block_inten[1]; + block_inten_midpoints[1] = block_inten[1] + block_inten[2]; + block_inten_midpoints[2] = block_inten[2] + block_inten[3]; + + if ((optimizer->m_pSorted_luma[n - 1] * 2) < block_inten_midpoints[0]) + { + if (block_inten[0] > optimizer->m_pSorted_luma[n - 1]) + { + const uint min_error = labs(block_inten[0] - optimizer->m_pSorted_luma[n - 1]); + if (min_error >= trial_solution->m_error) + continue; + } + + memset(&optimizer->m_temp_selectors[0], 0, n); + + for (c = 0; c < n; c++) { + total_error += rg_etc1_color_quad_u8_rgb_squared_distance(block_colors[0], pSrc_pixels[c]); + } + } + else if ((optimizer->m_pSorted_luma[0] * 2) >= block_inten_midpoints[2]) + { + if (optimizer->m_pSorted_luma[0] > block_inten[3]) + { + const uint min_error = labs(optimizer->m_pSorted_luma[0] - block_inten[3]); + if (min_error >= trial_solution->m_error) + continue; + } + + memset(&optimizer->m_temp_selectors[0], 3, n); + + for (c = 0; c < n; c++) + total_error += rg_etc1_color_quad_u8_rgb_squared_distance(block_colors[3], pSrc_pixels[c]); + } + else + { + uint cur_selector = 0; + for (c = 0; c < n; c++) + { + const uint y = optimizer->m_pSorted_luma[c]; + while ((y * 2) >= block_inten_midpoints[cur_selector]) + if (++cur_selector > 2) + goto done; + const uint sorted_pixel_index = optimizer->m_pSorted_luma_indices[c]; + optimizer->m_temp_selectors[sorted_pixel_index] = (uint8)(cur_selector); + total_error += rg_etc1_color_quad_u8_rgb_squared_distance(block_colors[cur_selector], + pSrc_pixels[sorted_pixel_index]); + } + done: + while (c < n) + { + const uint sorted_pixel_index = optimizer->m_pSorted_luma_indices[c]; + optimizer->m_temp_selectors[sorted_pixel_index] = 3; + total_error += rg_etc1_color_quad_u8_rgb_squared_distance(block_colors[3], pSrc_pixels[sorted_pixel_index]); + ++c; + } + } + + if (total_error < trial_solution->m_error) + { + trial_solution->m_error = total_error; + trial_solution->m_coords.m_inten_table = inten_table; + memcpy(trial_solution->m_selectors, optimizer->m_temp_selectors, n); + trial_solution->m_valid = EINA_TRUE; + if (!total_error) + break; + } + } + rg_etc1_color_quad_u8_copy(&trial_solution->m_coords.m_unscaled_color,&coords->m_unscaled_color); + trial_solution->m_coords.m_color4 = optimizer->m_pParams->m_use_color4; + + if (pBest_solution) + { + if (trial_solution->m_error < pBest_solution->m_error) + { + memcpy(pBest_solution,trial_solution,sizeof(rg_etc1_potential_solution)); + success = EINA_TRUE; + } + } + + return success; +} + +static uint +etc1_decode_value(uint diff, uint inten, uint selector, uint packed_c) +{ + const uint limit = diff ? 32 : 16; + // ERROR CASE NO ASSERT IN EVAS CODE + if (!((diff < 2) && (inten < 8) && (selector < 4) && (packed_c < limit))) + return 0; + int c; + if (diff) + c = (packed_c >> 2) | (packed_c << 3); + else + c = packed_c | (packed_c << 4); + c += rg_etc1_inten_tables[inten][selector]; + c = CLAMP(c, 0, 255); + return c; +} + +static inline int mul_8bit(int a, int b) { int t = a*b + 128; return (t + (t >> 8)) >> 8; } + +void rg_etc1_pack_block_init() +{ + uint diff; + uint expand5[32]; + int i; + for (diff = 0; diff < 2; diff++) + { + const uint limit = diff ? 32 : 16; + uint inten; + + for (inten = 0; inten < 8; inten++) + { + uint selector; + for (selector = 0; selector < 4; selector++) + { + const uint inverse_table_index = diff + (inten << 1) + (selector << 4); + uint color; + for (color = 0; color < 256; color++) + { + uint best_error = cUINT32_MAX, best_packed_c = 0; + uint packed_c; + for (packed_c = 0; packed_c < limit; packed_c++) + { + int v = etc1_decode_value(diff, inten, selector, packed_c); + uint err = labs(v - (int)color); + if (err < best_error) + { + best_error = err; + best_packed_c = packed_c; + if (!best_error) + break; + } + } + if (!(best_error <= 255)) + { + fprintf(stderr, "ETC1: Failed to write the inverse lookup table!\n"); + return; + } + rg_etc1_inverse_lookup[inverse_table_index][color] = (uint16)(best_packed_c | (best_error << 8)); + } + } + } + } + + for(i = 0; i < 32; i++) + expand5[i] = (i << 3) | (i >> 2); + + for(i = 0; i < 256 + 16; i++) + { + int v = (int)CLAMP(i - 8, 0, 255); + rg_etc_quant5_tab[i] = (uint8)(expand5[mul_8bit(v,31)]); + } +} + +// Packs solid color blocks efficiently using a set of small precomputed tables. +// For random 888 inputs, MSE results are better than Erricson's ETC1 packer in "slow" mode ~9.5% of the time, is slightly worse only ~.01% of the time, and is equal the rest of the time. +static uint64 +rg_etc1_pack_block_solid_color(unsigned char *block, const color_quad_u8 *color, rg_etc1_pack_params *pack_params EINA_UNUSED) +{ + const uint8 *pColor = (uint8 *) &color->m_u32; + + if (!rg_etc1_inverse_lookup[0][255]) + rg_etc1_pack_block_init(); + + if (!rg_etc1_inverse_lookup[0][255]) + { + fprintf(stderr, "ETC1: Inverse lookup table not set (in %s)!\n", __FUNCTION__); + return 0; + } + + const uint s_next_comp[4] = { 1, 2, 0, 1 }; + uint best_error = cUINT32_MAX, best_i = 0; + int best_x = 0, best_packed_c1 = 0, best_packed_c2 = 0; + uint i; + + // For each possible 8-bit value, there is a precomputed list of diff/inten/selector configurations + // that allow that 8-bit value to be encoded with no error. + for (i = 0; i < 3; i++) + { + const int c0 = pColor[i]; + const int c1 = pColor[s_next_comp[i]]; + const int c2 = pColor[s_next_comp[i + 1]]; + + const int delta_range = 1; + int delta; + for (delta = -delta_range; delta <= delta_range; delta++) + { + const int c_plus_delta = CLAMP(c0 + delta, 0, 255); + + uint16* pTable; + if (!c_plus_delta) + pTable = (uint16 *)rg_etc_color8_to_etc_block_config_0_255[0]; + else if (c_plus_delta == 255) + pTable = (uint16 *)rg_etc_color8_to_etc_block_config_0_255[1]; + else + pTable = (uint16 *)rg_etc_color8_to_etc_block_config_1_to_254[c_plus_delta - 1]; + + do + { + const uint x = *pTable++; + uint16* pInverse_table; + uint16 p1, p2; + uint trial_error; + +#ifdef RG_ETC1_BUILD_DEBUG + const uint diff = x & 1; + const uint inten = (x >> 1) & 7; + const uint selector = (x >> 4) & 3; + const uint p0 = (x >> 8) & 255; + // ERROR CASE NO ASSERT IN EVAS CODE + if (etc1_decode_value(diff, inten, selector, p0) != (uint)c_plus_delta) return 0; +#endif + + pInverse_table = rg_etc1_inverse_lookup[x & 0xFF]; + p1 = pInverse_table[c1]; + p2 = pInverse_table[c2]; + trial_error = SQUARE((c_plus_delta - c0)) + SQUARE((p1 >> 8)) + SQUARE((p2 >> 8)); + if (trial_error < best_error) + { + best_error = trial_error; + best_x = x; + best_packed_c1 = p1 & 0xFF; + best_packed_c2 = p2 & 0xFF; + best_i = i; + if (!best_error) + goto found_perfect_match; + } + } while (*pTable != 0xFFFF); + } + } + found_perfect_match: + { + const uint diff = best_x & 1; + const uint inten = (best_x >> 1) & 7; + uint etc1_selector; + uint best_packed_c0; + uint selector_val; + + block[0] = block[1] = block[2] = 0; + block[3] = (uint8)(((inten | (inten << 3)) << 2) | (diff << 1)); + + etc1_selector = rg_etc_selector_index_to_etc1[(best_x >> 4) & 3]; + selector_val = (etc1_selector & 2) ? 0xFFFF : 0; + memcpy(&block[4], &selector_val, 2); + selector_val = (etc1_selector & 1) ? 0xFFFF : 0; + memcpy(&block[6], &selector_val, 2); + + best_packed_c0 = (best_x >> 8) & 255; + if (diff) + { + block[best_i] = (uint8)(best_packed_c0 << 3); + block[s_next_comp[best_i]] = (uint8)(best_packed_c1 << 3); + block[s_next_comp[best_i+1]] = (uint8)(best_packed_c2 << 3); + } + else + { + block[best_i] = (uint8)(best_packed_c0 | (best_packed_c0 << 4)); + block[s_next_comp[best_i]] = (uint8)(best_packed_c1 | (best_packed_c1 << 4)); + block[s_next_comp[best_i+1]] = (uint8)(best_packed_c2 | (best_packed_c2 << 4)); + } + } + return best_error; +} + +#if RG_ETC1_CONSTRAINED_SUBBLOCK +static uint +rg_etc1_pack_block_solid_color_constrained(rg_etc1_optimizer_results *results,uint num_colors, + const uint8* pColor, rg_etc1_pack_params *pack_params EINA_UNUSED, + bool use_diff, const color_quad_u8* pBase_color5_unscaled) +{ + static uint s_next_comp[4] = { 1, 2, 0, 1 }; + uint best_error = cUINT32_MAX, best_i = 0; + int best_x = 0, best_packed_c1 = 0, best_packed_c2 = 0; + uint i; + + if (!rg_etc1_inverse_lookup[0][255]) + rg_etc1_pack_block_init(); + + if (!rg_etc1_inverse_lookup[0][255]) + { + fprintf(stderr, "ETC1: Inverse lookup table not set (in %s)!\n", __FUNCTION__); + return 0; + } + + // For each possible 8-bit value, there is a precomputed list of diff/inten/selector configurations + // that allow that 8-bit value to be encoded with no error. + for (i = 0; i < 3; i++) + { + const uint c1 = pColor[s_next_comp[i]], c2 = pColor[s_next_comp[i + 1]]; + const int delta_range = 1; + int delta; + for (delta = -delta_range; delta <= delta_range; delta++) + { + const int c_plus_delta = CLAMP(pColor[i] + delta, 0, 255); + const uint16* pTable; + if (!c_plus_delta) + pTable = rg_etc_color8_to_etc_block_config_0_255[0]; + else if (c_plus_delta == 255) + pTable = rg_etc_color8_to_etc_block_config_0_255[1]; + else + pTable = rg_etc_color8_to_etc_block_config_1_to_254[c_plus_delta - 1]; + + do + { + const uint x = *pTable++; + const uint diff = x & 1; + if (((uint)use_diff) != diff) + { + if (*pTable == 0xFFFF) + break; + continue; + } + + if ((diff) && (pBase_color5_unscaled)) + { + const int p0 = (x >> 8) & 255; + unsigned char cc1 = rg_etc1_color_quad_component_get(pBase_color5_unscaled->m_u32, + i); + int delta1 = p0 - (int)(cc1); + if ((delta1 < cETC1ColorDeltaMin) || (delta1 > cETC1ColorDeltaMax)) + { + if (*pTable == 0xFFFF) + break; + continue; + } + } + +#ifdef RG_ETC1_BUILD_DEBUG + { + const uint inten = (x >> 1) & 7; + const uint selector = (x >> 4) & 3; + const uint p0 = (x >> 8) & 255; + // ERROR CASE NO ASSERT IN EVAS CODE + if (etc1_decode_value(diff, inten, selector, p0) != (uint)c_plus_delta) return 0; + } +#endif + + const uint16* pInverse_table = rg_etc1_inverse_lookup[x & 0xFF]; + uint16 p1 = pInverse_table[c1]; + uint16 p2 = pInverse_table[c2]; + uint trial_error; + + if ((diff) && (pBase_color5_unscaled)) + { + unsigned char cc1 = rg_etc1_color_quad_component_get(pBase_color5_unscaled->m_u32, + s_next_comp[i]); + int delta1 = (p1 & 0xFF) - (int)(cc1); + unsigned char cc2 = rg_etc1_color_quad_component_get(pBase_color5_unscaled->m_u32, + s_next_comp[i + 1]); + int delta2 = (p2 & 0xFF) - (int)(cc2); + if ((delta1 < cETC1ColorDeltaMin) || (delta1 > cETC1ColorDeltaMax) + || (delta2 < cETC1ColorDeltaMin) || (delta2 > cETC1ColorDeltaMax)) + { + if (*pTable == 0xFFFF) + break; + continue; + } + } + + trial_error = SQUARE((c_plus_delta - pColor[i])) + SQUARE((p1 >> 8)) + SQUARE((p2 >> 8)); + if (trial_error < best_error) + { + best_error = trial_error; + best_x = x; + best_packed_c1 = p1 & 0xFF; + best_packed_c2 = p2 & 0xFF; + best_i = i; + if (!best_error) + goto found_perfect_match; + } + } while (*pTable != 0xFFFF); + } + } + found_perfect_match: + + if (best_error == cUINT32_MAX) + return best_error; + + best_error *= num_colors; + + results->m_n = num_colors; + results->m_block_color4 = !(best_x & 1); + results->m_block_inten_table = (best_x >> 1) & 7; + memset(results->m_pSelectors, (best_x >> 4) & 3, num_colors); + { + const uint best_packed_c0 = (best_x >> 8) & 255; + rg_etc1_color_quad_u8_component_set(&results->m_block_color_unscaled, best_i, (uint8)best_packed_c0); + rg_etc1_color_quad_u8_component_set(&results->m_block_color_unscaled, s_next_comp[best_i], (uint8)best_packed_c1); + rg_etc1_color_quad_u8_component_set(&results->m_block_color_unscaled, s_next_comp[best_i+1], (uint8)best_packed_c2); + results->m_error = best_error; + } + return best_error; +} +#endif + +#if RG_ETC1_DITHERING +// Function originally from RYG's public domain real-time DXT1 compressor, modified for 555. +static void +rg_etc1_dither_block_555(color_quad_u8* dest, color_quad_u8* block) +{ + int err[8],*ep1 = err,*ep2 = err+4; + uint8 *quant = rg_etc_quant5_tab+8; + int ch; + + memset(dest, 0xFF, sizeof(color_quad_u8)*16); + + // process channels seperately + for(ch=0;ch<3;ch++) + { + uint8* bp = (uint8*)block; + uint8* dp = (uint8*)dest; + int y; + + bp += ch; dp += ch; + + memset(err,0, sizeof(err)); + for(y = 0; y < 4; y++) + { + int *tmp; + // pixel 0 + dp[ 0] = quant[bp[ 0] + ((3*ep2[1] + 5*ep2[0]) >> 4)]; + ep1[0] = bp[ 0] - dp[ 0]; + + // pixel 1 + dp[ 4] = quant[bp[ 4] + ((7*ep1[0] + 3*ep2[2] + 5*ep2[1] + ep2[0]) >> 4)]; + ep1[1] = bp[ 4] - dp[ 4]; + + // pixel 2 + dp[ 8] = quant[bp[ 8] + ((7*ep1[1] + 3*ep2[3] + 5*ep2[2] + ep2[1]) >> 4)]; + ep1[2] = bp[ 8] - dp[ 8]; + + // pixel 3 + dp[12] = quant[bp[12] + ((7*ep1[2] + 5*ep2[3] + ep2[2]) >> 4)]; + ep1[3] = bp[12] - dp[12]; + + // advance to next line + tmp = ep1; ep1 = ep2; ep2 = tmp; + bp += 16; + dp += 16; + } + } +} +#endif + +static inline unsigned int +_bgra_to_rgba(unsigned int val) +{ + //(((a) << 24) + ((r) << 16) + ((g) << 8) + (b)) + return ARGB_JOIN(A_VAL_GET(&val), R_VAL_GET(&val), G_VAL_GET(&val), B_VAL_GET(&val)); +} + +static void +_bgra_to_rgba_block(color_quad_u8 *output, const unsigned int *input, int len) +{ + for (int k = len; k; --k) + { + output->m_u32 = _bgra_to_rgba(*input++); + output++; + } +} + +unsigned int +rg_etc1_pack_block(void* pETC1_block, const unsigned int* pSrc_pixels_BGRA, rg_etc1_pack_params *pack_params) +{ + color_quad_u8 pSrc_pixels[16]; + unsigned char *dst_block = (unsigned char *)pETC1_block; + unsigned int first_pixel_u32; + int r; + color_quad_u8 subblock_pixels[8]; + uint64 best_error = cUINT64_MAX; + uint best_use_color4=EINA_FALSE; + uint best_flip=EINA_FALSE; + uint8 best_selectors[2][8]; + rg_etc1_optimizer optimizer = { 0 }; + rg_etc1_optimizer_results best_results[2] = { { 0 } }; + rg_etc1_optimizer_results results[3] = { { 0 } }; + rg_etc1_optimizer_params params; + uint i, flip; + uint8 selectors[3][8]; + int dr, dg, db; + uint selector0 = 0, selector1 = 0; + static const int s_scan_delta_0_to_4[] = { -4, -3, -2, -1, 0, 1, 2, 3, 4 }; + static const int s_scan_delta_0_to_1[] = { -1, 0, 1 }; + static const int s_scan_delta_0[] = { 0 }; + +#ifdef RG_ETC1_BUILD_DEBUG + // Ensure all alpha values are 0xFF. + for (i = 0; i < 16; i++) + { + // ERROR CASE NO ASSERT IN EVAS CODE + if (pSrc_pixels[i].comp.a != 255) return 0; + } +#endif + rg_etc1_optimizer_clear(&optimizer); + + // Convert evas BGRA to rg_etc1 RGBA + _bgra_to_rgba_block(pSrc_pixels, pSrc_pixels_BGRA, 16); + first_pixel_u32 = pSrc_pixels[0].m_u32; + + // Check for solid block. + for (r = 15; r >= 1; --r) + if (pSrc_pixels[r].m_u32 != first_pixel_u32) + break; + if (!r) + return (unsigned int)(16 * rg_etc1_pack_block_solid_color(dst_block, &pSrc_pixels[0], pack_params)); + +#if RG_ETC1_DITHERING + // Dithering gives mitigated results... It would be nice to know when to use it. + color_quad_u8 dithered_pixels[16]; + if (pack_params->m_dithering) + { + rg_etc1_dither_block_555(dithered_pixels, pSrc_pixels); + pSrc_pixels = dithered_pixels; + } +#endif + + for (i = 0; i < 2; i++) + { + best_results[i].m_n = 8; + best_results[i].m_pSelectors = best_selectors[i]; + } + + for (i = 0; i < 3; i++) + { + results[i].m_n = 8; + results[i].m_pSelectors = selectors[i]; + } + + rg_etc1_optimizer_params_clean(¶ms); + params.base_params = pack_params; + params.m_num_src_pixels = 8; + params.m_pSrc_pixels = subblock_pixels; + + // try horizontal VS. vertical split + for (flip = 0; flip < 2; flip++) + { + // try two color types: RGB555 + diff333 or RGB444 & RGB444 + uint use_color4; + for (use_color4 = 0; use_color4 < 2; use_color4++) + { + uint64 trial_error = 0; + + // subblock is either top/bottom or left/right + uint subblock; + for (subblock = 0; subblock < 2; subblock++) + { + results[2].m_error = cUINT64_MAX; + + if (flip) + // subblock is top or bottom, copy source + memcpy(subblock_pixels, pSrc_pixels + subblock * 8, sizeof(color_quad_u8) * 8); + else + { + // subblock = 1 : left, subblock = 2 : right, copy source + const color_quad_u8* pSrc_col = pSrc_pixels + subblock * 2; + rg_etc1_color_quad_u8_copy(&subblock_pixels[0], &pSrc_col[0]); + rg_etc1_color_quad_u8_copy(&subblock_pixels[1], &pSrc_col[4]); + rg_etc1_color_quad_u8_copy(&subblock_pixels[2], &pSrc_col[8]); + rg_etc1_color_quad_u8_copy(&subblock_pixels[3], &pSrc_col[12]); + rg_etc1_color_quad_u8_copy(&subblock_pixels[4], &pSrc_col[1]); + rg_etc1_color_quad_u8_copy(&subblock_pixels[5], &pSrc_col[5]); + rg_etc1_color_quad_u8_copy(&subblock_pixels[6], &pSrc_col[9]); + rg_etc1_color_quad_u8_copy(&subblock_pixels[7], &pSrc_col[13]); + } + +#if RG_ETC1_CONSTRAINED_SUBBLOCK + if ((params.base_params->m_quality >= rg_etc1_medium_quality) && ((subblock) || (use_color4))) + { + const uint32 subblock_pixel0_u32 = subblock_pixels[0].m_u32; + for (r = 7; r >= 1; --r) + if (subblock_pixels[r].m_u32 != subblock_pixel0_u32) + break; + if (!r) + { + // all pixels in subblock have the same color + rg_etc1_pack_block_solid_color_constrained(&results[2], 8, &subblock_pixels[0].comp.r, + pack_params, !use_color4, + (subblock && !use_color4) ? &results[0].m_block_color_unscaled : NULL); + } + } +#endif + + params.m_use_color4 = (use_color4 != 0); + params.m_constrain_against_base_color5 = EINA_FALSE; + + if ((!use_color4) && (subblock)) + { + params.m_constrain_against_base_color5 = EINA_TRUE; + rg_etc1_color_quad_u8_copy(¶ms.m_base_color5,&results[0].m_block_color_unscaled); + } + + if (params.base_params->m_quality == rg_etc1_high_quality) + { + params.m_scan_delta_size = RG_ETC1_ARRAY_SIZE(s_scan_delta_0_to_4); + params.m_pScan_deltas = s_scan_delta_0_to_4; + } + else if (params.base_params->m_quality == rg_etc1_medium_quality) + { + params.m_scan_delta_size = RG_ETC1_ARRAY_SIZE(s_scan_delta_0_to_1); + params.m_pScan_deltas = s_scan_delta_0_to_1; + } + else + { + params.m_scan_delta_size = RG_ETC1_ARRAY_SIZE(s_scan_delta_0); + params.m_pScan_deltas = s_scan_delta_0; + } + + rg_etc1_optimizer_init(&optimizer, ¶ms, &results[subblock]); + if (!rg_etc1_optimizer_compute(&optimizer)) + break; + + if (params.base_params->m_quality >= rg_etc1_medium_quality) + { + // TODO: Fix fairly arbitrary/unrefined thresholds that control how far away to scan for potentially better solutions. + const uint refinement_error_thresh0 = 3000; + const uint refinement_error_thresh1 = 6000; + if (results[subblock].m_error > refinement_error_thresh0) + { + if (params.base_params->m_quality == rg_etc1_medium_quality) + { + static const int s_scan_delta_2_to_3[] = { -3, -2, 2, 3 }; + params.m_scan_delta_size = RG_ETC1_ARRAY_SIZE(s_scan_delta_2_to_3); + params.m_pScan_deltas = s_scan_delta_2_to_3; + } + else + { + static const int s_scan_delta_5_to_5[] = { -5, 5 }; + static const int s_scan_delta_5_to_8[] = { -8, -7, -6, -5, 5, 6, 7, 8 }; + if (results[subblock].m_error > refinement_error_thresh1) + { + params.m_scan_delta_size = RG_ETC1_ARRAY_SIZE(s_scan_delta_5_to_8); + params.m_pScan_deltas = s_scan_delta_5_to_8; + } + else + { + params.m_scan_delta_size = RG_ETC1_ARRAY_SIZE(s_scan_delta_5_to_5); + params.m_pScan_deltas = s_scan_delta_5_to_5; + } + } + + if (!rg_etc1_optimizer_compute(&optimizer)) + break; + } + + if (results[2].m_error < results[subblock].m_error) + results[subblock] = results[2]; + } + + trial_error += results[subblock].m_error; + if (trial_error >= best_error) + break; + } + + if (subblock < 2) + continue; + + best_error = trial_error; + rg_etc1_optimizer_results_duplicate(&best_results[0], &results[0]); + rg_etc1_optimizer_results_duplicate(&best_results[1], &results[1]); + best_flip = flip; + best_use_color4 = use_color4; + } // use_color4 + } // flip + + dr = best_results[1].m_block_color_unscaled.comp.r - best_results[0].m_block_color_unscaled.comp.r; + dg = best_results[1].m_block_color_unscaled.comp.g - best_results[0].m_block_color_unscaled.comp.g; + db = best_results[1].m_block_color_unscaled.comp.b - best_results[0].m_block_color_unscaled.comp.b; + // ERROR CASE NO ASSERT IN EVAS CODE + if (!(best_use_color4 || ((MIN(MIN(dr, dg), db) >= cETC1ColorDeltaMin) && (MAX(MAX(dr, dg), db) <= cETC1ColorDeltaMax)))) return 0; + + if (best_use_color4) + { + dst_block[0] = (uint8)(best_results[1].m_block_color_unscaled.comp.r | + (best_results[0].m_block_color_unscaled.comp.r << 4)); + dst_block[1] = (uint8)(best_results[1].m_block_color_unscaled.comp.g | + (best_results[0].m_block_color_unscaled.comp.g << 4)); + dst_block[2] = (uint8)(best_results[1].m_block_color_unscaled.comp.b | + (best_results[0].m_block_color_unscaled.comp.b << 4)); + } + else + { + if (dr < 0) dr += 8; dst_block[0] = (uint8)((best_results[0].m_block_color_unscaled.comp.r << 3) | dr); + if (dg < 0) dg += 8; dst_block[1] = (uint8)((best_results[0].m_block_color_unscaled.comp.g << 3) | dg); + if (db < 0) db += 8; dst_block[2] = (uint8)((best_results[0].m_block_color_unscaled.comp.b << 3) | db); + } + + dst_block[3] = (uint8)((best_results[1].m_block_inten_table << 2) | + (best_results[0].m_block_inten_table << 5) | + ((~best_use_color4 & 1) << 1) | best_flip ); + + if (best_flip) + { + // flipped: + // { 0, 0 }, { 1, 0 }, { 2, 0 }, { 3, 0 }, + // { 0, 1 }, { 1, 1 }, { 2, 1 }, { 3, 1 } + // + // { 0, 2 }, { 1, 2 }, { 2, 2 }, { 3, 2 }, + // { 0, 3 }, { 1, 3 }, { 2, 3 }, { 3, 3 } + const uint8* pSelectors0 = best_results[0].m_pSelectors; + const uint8* pSelectors1 = best_results[1].m_pSelectors; + int x; + for (x = 3; x >= 0; --x) + { + uint b; + b = rg_etc_selector_index_to_etc1[pSelectors1[4 + x]]; + selector0 = (selector0 << 1) | (b & 1); selector1 = (selector1 << 1) | (b >> 1); + + b = rg_etc_selector_index_to_etc1[pSelectors1[x]]; + selector0 = (selector0 << 1) | (b & 1); selector1 = (selector1 << 1) | (b >> 1); + + b = rg_etc_selector_index_to_etc1[pSelectors0[4 + x]]; + selector0 = (selector0 << 1) | (b & 1); selector1 = (selector1 << 1) | (b >> 1); + + b = rg_etc_selector_index_to_etc1[pSelectors0[x]]; + selector0 = (selector0 << 1) | (b & 1); selector1 = (selector1 << 1) | (b >> 1); + } + } + else + { + // non-flipped: + // { 0, 0 }, { 0, 1 }, { 0, 2 }, { 0, 3 }, + // { 1, 0 }, { 1, 1 }, { 1, 2 }, { 1, 3 } + // + // { 2, 0 }, { 2, 1 }, { 2, 2 }, { 2, 3 }, + // { 3, 0 }, { 3, 1 }, { 3, 2 }, { 3, 3 } + int subblock; + for (subblock = 1; subblock >= 0; --subblock) + { + const uint8* pSelectors = best_results[subblock].m_pSelectors + 4; + uint j; + for (j = 0; j < 2; j++) + { + uint b; + b = rg_etc_selector_index_to_etc1[pSelectors[3]]; + selector0 = (selector0 << 1) | (b & 1); selector1 = (selector1 << 1) | (b >> 1); + + b = rg_etc_selector_index_to_etc1[pSelectors[2]]; + selector0 = (selector0 << 1) | (b & 1); selector1 = (selector1 << 1) | (b >> 1); + + b = rg_etc_selector_index_to_etc1[pSelectors[1]]; + selector0 = (selector0 << 1) | (b & 1); selector1 = (selector1 << 1) | (b >> 1); + + b = rg_etc_selector_index_to_etc1[pSelectors[0]]; + selector0 = (selector0 << 1) | (b & 1);selector1 = (selector1 << 1) | (b >> 1); + + pSelectors -= 4; + } + } + } + + dst_block[4] = (uint8)(selector1 >> 8); dst_block[5] = (uint8)(selector1 & 0xFF); + dst_block[6] = (uint8)(selector0 >> 8); dst_block[7] = (uint8)(selector0 & 0xFF); + return (unsigned int)(best_error); +} diff --git a/src/lib/rg_etc/rg_etc1.h b/src/lib/rg_etc/rg_etc1.h new file mode 100644 index 0000000..6f4b944 --- /dev/null +++ b/src/lib/rg_etc/rg_etc1.h @@ -0,0 +1,78 @@ +// File: rg_etc1.h - Fast, high quality ETC1 block packer/unpacker - Rich Geldreich +// Please see ZLIB license at the end of this file. +#ifndef __RG_ETC1_H__ +#define __RG_ETC1_H__ + +typedef unsigned char bool; + +// Unpacks an 8-byte ETC1 compressed block to a block of 4x4 32bpp RGBA pixels. +// Returns false if the block is invalid. Invalid blocks will still be unpacked with clamping. +// This function is thread safe, and does not dynamically allocate any memory. +// If preserve_alpha is true, the alpha channel of the destination pixels will not be overwritten. Otherwise, alpha will be set to 255. +bool rg_etc1_unpack_block(const void *pETC1_block, unsigned int* pDst_pixels_rgba, bool preserve_alpha); + +// Quality setting = the higher the quality, the slower. +// To pack large textures, it is highly recommended to call pack_etc1_block() in parallel, on different blocks, from multiple threads (particularly when using cHighQuality). +typedef enum { + rg_etc1_low_quality, + rg_etc1_medium_quality, + rg_etc1_high_quality +} rg_etc1_quality; + + +typedef struct +{ + rg_etc1_quality m_quality; + bool m_dithering; +} rg_etc1_pack_params; + +// pack_etc1_block_init() should be called before calling pack_etc1_block(), +// Otherwise rg_etc1_pack_block() will call rg_etc1_pack_block_init() for you. +void rg_etc1_pack_block_init(); + +// Packs a 4x4 block of 32bpp BGRA pixels to an 8-byte ETC1 block. +// 32-bit BGRA pixels must always be arranged as (B,G,R,A) (B first, A last) in memory, independent of platform endianness. A should always be 255. +// Returns squared error of result. +// This function is thread safe, and does not dynamically allocate any memory. +// pack_etc1_block() does not currently support "perceptual" colorspace metrics - it primarily optimizes for RGB RMSE. +unsigned int rg_etc1_pack_block(void* pETC1_block, const unsigned int* pSrc_pixels_BGRA, rg_etc1_pack_params *pack_params); + +// Pack a 4x4 block of 32bpp BGRA pixels to a 16-byte RGBA8_ETC2_EAC block (supports alpha). +unsigned int etc2_rgba8_block_pack(unsigned char *etc2, const unsigned int *bgra, rg_etc1_pack_params *params); + +// Pack a 4x4 block of 32bpp BGRA pixels to a 8-byte RGB8_ETC2 block (opaque). +unsigned int etc2_rgb8_block_pack(unsigned char *etc2, const unsigned int *bgra, rg_etc1_pack_params *params); + +// ETC2 support: RGB8_ETC2 +void rg_etc2_rgb8_decode_block(const unsigned char *etc_block, unsigned int *bgra); + +// ETC2 support: RGBA8_ETC2_EAC +void rg_etc2_rgba8_decode_block(const unsigned char *etc_block, unsigned int *bgra); + +//------------------------------------------------------------------------------ +// +// rg_etc1 uses the ZLIB license: +// http://opensource.org/licenses/Zlib +// +// Copyright (c) 2012 Rich Geldreich +// +// This software is provided 'as-is', without any express or implied +// warranty. In no event will the authors be held liable for any damages +// arising from the use of this software. +// +// Permission is granted to anyone to use this software for any purpose, +// including commercial applications, and to alter it and redistribute it +// freely, subject to the following restrictions: +// +// 1. The origin of this software must not be misrepresented; you must not +// claim that you wrote the original software. If you use this software +// in a product, an acknowledgment in the product documentation would be +// appreciated but is not required. +// +// 2. Altered source versions must be plainly marked as such, and must not be +// misrepresented as being the original software. +// +// 3. This notice may not be removed or altered from any source distribution. +// +//------------------------------------------------------------------------------ +#endif // __RG_ETC1_H__ diff --git a/src/lib/rg_etc/rg_etc2.c b/src/lib/rg_etc/rg_etc2.c new file mode 100644 index 0000000..18b3f4f --- /dev/null +++ b/src/lib/rg_etc/rg_etc2.c @@ -0,0 +1,345 @@ +/* +Copyright (C) 2014 Jean-Philippe ANDRE + +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, +INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, +OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF +LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, +EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +/* + * ETC2 decoding functions, reimplemented from scratch based on the + * OpenGL ES 3.0 spec (annex C.1). + * + * @author Jean-Philippe ANDRE + * @license BSD-2 with advertisement clause + */ + +#include +#include "rg_etc1.h" + +void rg_etc2_rgb8_decode_block(const unsigned char *etc_block, unsigned int *bgra); +void rg_etc2_rgba8_decode_block(const unsigned char *etc_block, unsigned int *bgra); + +typedef const uint8_t etc_block; + +// For T and H modes +static const int kDistances[8] = { + 3, 6, 11, 16, 23, 32, 41, 64 +}; + +// For differential mode +static const int kSigned3bit[8] = { + 0, 1, 2, 3, -4, -3, -2, -1 +}; + +// For alpha support +static const int kAlphaModifiers[16][8] = { + { -3, -6, -9, -15, 2, 5, 8, 14}, + { -3, -7, -10, -13, 2, 6, 9, 12}, + { -2, -5, -8, -13, 1, 4, 7, 12}, + { -2, -4, -6, -13, 1, 3, 5, 12}, + { -3, -6, -8, -12, 2, 5, 7, 11}, + { -3, -7, -9, -11, 2, 6, 8, 10}, + { -4, -7, -8, -11, 3, 6, 7, 10}, + { -3, -5, -8, -11, 2, 4, 7, 10}, + { -2, -6, -8, -10, 1, 5, 7, 9}, + { -2, -5, -8, -10, 1, 4, 7, 9}, + { -2, -4, -8, -10, 1, 3, 7, 9}, + { -2, -5, -7, -10, 1, 4, 6, 9}, + { -3, -4, -7, -10, 2, 3, 6, 9}, + { -1, -2, -3, -10, 0, 1, 2, 9}, + { -4, -6, -8, -9, 3, 5, 7, 8}, + { -3, -5, -7, -9, 2, 4, 6, 8} +}; + +// Use with static constants so the compiler can optimize everything +#define BITS(byteval, lowbit, highbit) \ + (((byteval) >> (lowbit)) & ((1 << ((highbit) - (lowbit) + 1)) - 1)) + +#define BIT(byteval, bit) \ + (((byteval) >> (bit)) & 0x1) + +// Clamps only if value is > 255 +#define CLAMPDOWN(a) ({ int _z = (a); ((_z <= 255) ? _z : 255); }) + +// Clamps only if value is < 0 +#define CLAMPUP(a) ({ int _z = (a); ((_z >= 0) ? _z : 0); }) + +// Real clamp +#define CLAMP(a) ({ int _b = (a); (((_b) >= 0) ? (((_b) < 256) ? (_b) : 255) : 0); }) + +// Simple min +#define MIN(a,b) ({ int _z = (a), _y = (b); ((_z <= _y) ? _z : _y); }) + +// Write a BGRA value for output to Evas +#define BGRA(r,g,b,a) ((a << 24) | (r << 16) | (g << 8) | b) + +#define _4to8(a) ({ int _a = (a) & ((1 << 4) - 1); ((_a << 4) | (_a & 0xf)); }) +#define _5to8(a) ({ int _a = (a) & ((1 << 5) - 1); ((_a << 3) | ((_a >> 2) & 0x7)); }) +#define _6to8(a) ({ int _a = (a) & ((1 << 6) - 1); ((_a << 2) | ((_a >> 4) & 0x3)); }) +#define _7to8(a) ({ int _a = (a) & ((1 << 7) - 1); ((_a << 1) | ((_a >> 6) & 0x1)); }) + +#ifndef WORDS_BIGENDIAN +/* x86 */ +#define A_VAL(p) (((uint8_t *)(p))[3]) +#define R_VAL(p) (((uint8_t *)(p))[2]) +#define G_VAL(p) (((uint8_t *)(p))[1]) +#define B_VAL(p) (((uint8_t *)(p))[0]) +#else +/* ppc */ +#define A_VAL(p) (((uint8_t *)(p))[0]) +#define R_VAL(p) (((uint8_t *)(p))[1]) +#define G_VAL(p) (((uint8_t *)(p))[2]) +#define B_VAL(p) (((uint8_t *)(p))[3]) +#endif + + +static inline void +_T_mode_color_read(const uint8_t *etc, uint32_t *paint_colors) +{ + // 4 bit colors + const int r1_4 = (BITS(etc[0], 3, 4) << 2) | BITS(etc[0], 0, 1); + const int g1_4 = BITS(etc[1], 4, 7); + const int b1_4 = BITS(etc[1], 0, 3); + const int r2_4 = BITS(etc[2], 4, 7); + const int g2_4 = BITS(etc[2], 0, 3); + const int b2_4 = BITS(etc[3], 4, 7); + + // Distance index + const int didx = (BITS(etc[3], 2, 3) << 1) | BIT(etc[3], 0); + const int d = kDistances[didx]; + + // Write out paint colors for T mode + paint_colors[0] = BGRA(_4to8(r1_4), _4to8(g1_4), _4to8(b1_4), 255); + paint_colors[1] = BGRA(CLAMPDOWN(_4to8(r2_4) + d), + CLAMPDOWN(_4to8(g2_4) + d), + CLAMPDOWN(_4to8(b2_4) + d), + 255); + paint_colors[2] = BGRA(_4to8(r2_4), _4to8(g2_4), _4to8(b2_4), 255); + paint_colors[3] = BGRA(CLAMPUP(_4to8(r2_4) - d), + CLAMPUP(_4to8(g2_4) - d), + CLAMPUP(_4to8(b2_4) - d), + 255); +} + +static inline void +_H_mode_color_read(const uint8_t *etc, uint32_t *paint_colors) +{ + // 4 bit colors + const int r1_4 = BITS(etc[0], 3, 6); + const int g1_4 = (BITS(etc[0], 0, 2) << 1) | (BIT(etc[1], 4)); + const int b1_4 = (BIT(etc[1], 3) << 3) | (BITS(etc[1], 0, 1) << 1) | (BIT(etc[2], 7)); + const int r2_4 = BITS(etc[2], 3, 6); + const int g2_4 = (BITS(etc[2], 0, 2) << 1) | (BIT(etc[3], 7)); + const int b2_4 = BITS(etc[3], 3, 6); + + // Distance index + const int basecol1 = (_4to8(r1_4) << 16) | (_4to8(g1_4) << 8) | _4to8(b1_4); + const int basecol2 = (_4to8(r2_4) << 16) | (_4to8(g2_4) << 8) | _4to8(b2_4); + const int didx = + (BIT(etc[3], 2) << 2) | + (BIT(etc[3], 0) << 1) | + ((basecol1 >= basecol2) ? 1 : 0); + const int d = kDistances[didx]; + + // Write out paint colors for H mode + paint_colors[0] = BGRA(CLAMPDOWN(_4to8(r1_4) + d), + CLAMPDOWN(_4to8(g1_4) + d), + CLAMPDOWN(_4to8(b1_4) + d), + 255); + paint_colors[1] = BGRA(CLAMPUP(_4to8(r1_4) - d), + CLAMPUP(_4to8(g1_4) - d), + CLAMPUP(_4to8(b1_4) - d), + 255); + paint_colors[2] = BGRA(CLAMPDOWN(_4to8(r2_4) + d), + CLAMPDOWN(_4to8(g2_4) + d), + CLAMPDOWN(_4to8(b2_4) + d), + 255); + paint_colors[3] = BGRA(CLAMPUP(_4to8(r2_4) - d), + CLAMPUP(_4to8(g2_4) - d), + CLAMPUP(_4to8(b2_4) - d), + 255); +} + +static inline void +_planar_mode_color_read(const uint8_t *etc, uint32_t *bgra) +{ + // RO: Bits 57-62 + const int RO = _6to8(BITS(etc[0], 1, 6)); + // GO: Bits 49-54,56 + const int GO = _7to8((BIT(etc[0], 0) << 6) | (BITS(etc[1], 1, 6))); + // BO: Bits 39,40-41,43-44,48 + const int BO = _6to8((BIT(etc[1], 0) << 5) | (BITS(etc[2], 3, 4) << 3) | (BITS(etc[2], 0, 1) << 1) | BIT(etc[3], 7)); + // RH: Bits 32,34-38 + const int RH = _6to8((BITS(etc[3], 2, 6) << 1) | BIT(etc[3], 0)); + // GH: Bits 25-31 + const int GH = _7to8(BITS(etc[4], 1, 7)); + // BH: Bits 19-23,24 + const int BH = _6to8((BIT(etc[4], 0) << 5) | (BITS(etc[5], 3, 7))); + // RV: Bits 13-15,16-18 + const int RV = _6to8((BITS(etc[5], 0, 2) << 3) | (BITS(etc[6], 5, 7))); + // GV: Bits 6-7,8-12 + const int GV = _7to8((BITS(etc[6], 0, 4) << 2) | (BITS(etc[7], 6, 7))); + // BV: Bits 0-5 + const int BV = _6to8(BITS(etc[7], 0, 5)); + + for (int y = 0; y < 4; y++) + for (int x = 0; x < 4; x++) + { + // Formulas straight from the spec + const int R = CLAMP(((x * (RH - RO)) + y * (RV - RO) + 4 * RO + 2) >> 2); + const int G = CLAMP(((x * (GH - GO)) + y * (GV - GO) + 4 * GO + 2) >> 2); + const int B = CLAMP(((x * (BH - BO)) + y * (BV - BO) + 4 * BO + 2) >> 2); + *bgra++ = BGRA(R, G, B, 255); + } +} + +static inline void +_TH_paint(const uint8_t *etc, uint32_t paint_colors[4], uint32_t *bgra) +{ + // Common code for modes T and H. + + // a,b,c,d + bgra[ 0] = paint_colors[(BIT(etc[5], 0) << 1) | (BIT(etc[7], 0))]; + bgra[ 4] = paint_colors[(BIT(etc[5], 1) << 1) | (BIT(etc[7], 1))]; + bgra[ 8] = paint_colors[(BIT(etc[5], 2) << 1) | (BIT(etc[7], 2))]; + bgra[12] = paint_colors[(BIT(etc[5], 3) << 1) | (BIT(etc[7], 3))]; + + // e,f,g,h + bgra[ 1] = paint_colors[(BIT(etc[5], 4) << 1) | (BIT(etc[7], 4))]; + bgra[ 5] = paint_colors[(BIT(etc[5], 5) << 1) | (BIT(etc[7], 5))]; + bgra[ 9] = paint_colors[(BIT(etc[5], 6) << 1) | (BIT(etc[7], 6))]; + bgra[13] = paint_colors[(BIT(etc[5], 7) << 1) | (BIT(etc[7], 7))]; + + // i,j,k,l + bgra[ 2] = paint_colors[(BIT(etc[4], 0) << 1) | (BIT(etc[6], 0))]; + bgra[ 6] = paint_colors[(BIT(etc[4], 1) << 1) | (BIT(etc[6], 1))]; + bgra[10] = paint_colors[(BIT(etc[4], 2) << 1) | (BIT(etc[6], 2))]; + bgra[14] = paint_colors[(BIT(etc[4], 3) << 1) | (BIT(etc[6], 3))]; + + // m,n,o,p + bgra[ 3] = paint_colors[(BIT(etc[4], 4) << 1) | (BIT(etc[6], 4))]; + bgra[ 7] = paint_colors[(BIT(etc[4], 5) << 1) | (BIT(etc[6], 5))]; + bgra[11] = paint_colors[(BIT(etc[4], 6) << 1) | (BIT(etc[6], 6))]; + bgra[15] = paint_colors[(BIT(etc[4], 7) << 1) | (BIT(etc[6], 7))]; +} + +void +rg_etc2_rgb8_decode_block(const unsigned char *etc, unsigned int *bgra) +{ + // Check differential mode bit + if ((etc[3] & 0x2) == 0) + goto etc1; + + // Read R,G,B + const int R = BITS(etc[0], 3, 7); + const int dR = kSigned3bit[BITS(etc[0], 0, 2)]; + const int G = BITS(etc[1], 3, 7); + const int dG = kSigned3bit[BITS(etc[1], 0, 2)]; + const int B = BITS(etc[2], 3, 7); + const int dB = kSigned3bit[BITS(etc[2], 0, 2)]; + uint32_t paint_colors[4]; + + if ((R + dR) < 0 || (R + dR) >= 32) + { + // T mode + _T_mode_color_read(etc, paint_colors); + _TH_paint(etc, paint_colors, bgra); + return; + } + if ((G + dG) < 0 || (G + dG) >= 32) + { + // H mode + _H_mode_color_read(etc, paint_colors); + _TH_paint(etc, paint_colors, bgra); + return; + } + if ((B + dB) < 0 || (B + dB) >= 32) + { + // Planar mode + _planar_mode_color_read(etc, bgra); + return; + } + +etc1: + // Valid differential mode or individual mode: ETC1 + if (!rg_etc1_unpack_block(etc, bgra, 0)) + fprintf(stderr, "ETC2: Something very strange is happening here!\n"); +} + +void +rg_etc2_rgba8_decode_block(const unsigned char *etc, unsigned int *bgra) +{ + const uint8_t zeros[7] = {0}; + uint32_t table_index; + int base_codeword; + int multiplier; + + base_codeword = etc[0]; + + // Fast path if alpha is the same for all pixels + if (!memcmp(etc + 1, zeros, 7)) + { + if (!base_codeword) + memset(bgra, 0, 64); + else + { + rg_etc2_rgb8_decode_block(etc + 8, bgra); + if (base_codeword != 255) + for (int k = 0; k < 16; k++) + { + const uint32_t rgb = *bgra; + const int R = MIN(R_VAL(&rgb), base_codeword); + const int G = MIN(G_VAL(&rgb), base_codeword); + const int B = MIN(B_VAL(&rgb), base_codeword); + *bgra++ = BGRA(R, G, B, base_codeword); + } + } + return; + } + + rg_etc2_rgb8_decode_block(etc + 8, bgra); + + multiplier = BITS(etc[1], 4, 7); + table_index = BITS(etc[1], 0, 3); + + for (int x = 0, k = 0; x < 4; x++) + for (int y = 0; y < 4; y++, k += 3) + { + const uint32_t byte = (k >> 3); // = [k/8] + const uint32_t bit = k - (byte << 3); // = k%8 + const uint32_t rgb = bgra[(y << 2) + x]; + uint32_t index, alpha, R, G, B; + + if (bit < 6) + index = BITS(etc[byte + 2], 5 - bit, 7 - bit); + else if (bit == 6) + index = (BITS(etc[byte + 2], 0, 1) << 1) | BIT(etc[byte + 3], 7); + else // bit == 7 + index = (BIT(etc[byte + 2], 0) << 2) | BITS(etc[byte + 3], 6, 7); + alpha = CLAMP(base_codeword + kAlphaModifiers[table_index][index] * multiplier); + R = MIN(R_VAL(&rgb), alpha); + G = MIN(G_VAL(&rgb), alpha); + B = MIN(B_VAL(&rgb), alpha); + bgra[(y << 2) + x] = BGRA(R, G, B, alpha); + } +} -- 2.7.4