]> mj.ucw.cz Git - libucw.git/blobdiff - images/color.h
Added the local copy of the regex library back.
[libucw.git] / images / color.h
index 28daa822141bff1cef63e27655b8a6d272aff866..a148e6c3c062b38e1e4ec5159fc133ec6ef7e167 100644 (file)
@@ -8,6 +8,8 @@
  *
  *
  *     References:
+ *     - A Review of RGB Color Spaces, Danny Pascale (2003)
+ *     - http://www.adobe.com/digitalimag/pdfs/AdobeRGB1998.pdf
  *     - http://www.tecgraf.puc-rio.br/~mgattass/color/ColorIndex.html
  *
  *     FIXME:
  *     - SIMD should help to speed up conversion of large arrays
  *     - maybe try to generate a long switch in color_conv_pixel()
  *       with optimized entries instead of access to interpolation table
+ *     - most of multiplications in srgb_to_luv_pixels can be replaced
+ *       with tables lookup... tests shows almost the same speed for random
+ *       input and cca 40% gain when input colors fit in CPU chache
  */
 
 #ifndef _IMAGES_COLOR_H
 #define _IMAGES_COLOR_H
 
+#include "images/images.h"
+
+/* Basic color spaces */
+enum {
+  COLOR_SPACE_UNKNOWN = 0,
+  COLOR_SPACE_UNKNOWN_1 = 1,   /* unknown 1-channel color space */
+  COLOR_SPACE_UNKNOWN_2 = 2,   /* unknown 2-channels color space */
+  COLOR_SPACE_UNKNOWN_3 = 3,   /* unknown 3-channels color space */
+  COLOR_SPACE_UNKNOWN_4 = 4,   /* unknown 4-channels color space */
+  COLOR_SPACE_UNKNOWN_MAX = 4,
+  COLOR_SPACE_GRAYSCALE,
+  COLOR_SPACE_RGB,
+  COLOR_SPACE_XYZ,
+  COLOR_SPACE_LAB,
+  COLOR_SPACE_LUV,
+  COLOR_SPACE_YCBCR,
+  COLOR_SPACE_CMYK,
+  COLOR_SPACE_YCCK,
+  COLOR_SPACE_MAX
+};
+
+extern uns color_space_channels[COLOR_SPACE_MAX];
+extern byte *color_space_name[COLOR_SPACE_MAX];
+
+/* Color space ID <-> name conversions */
+byte *color_space_id_to_name(uns id);
+uns color_space_name_to_id(byte *name);
+
+/* Struct color manipulation */
+int color_get(struct color *color, byte *src, uns src_space);
+int color_put(struct image_context *ctx, struct color *color, byte *dest, uns dest_space);
+
+static inline void
+color_make_gray(struct color *color, uns gray)
+{
+  color->c[0] = gray;
+  color->color_space = COLOR_SPACE_GRAYSCALE;
+}
+
+static inline void
+color_make_rgb(struct color *color, uns r, uns g, uns b)
+{
+  color->c[0] = r;
+  color->c[1] = g;
+  color->c[2] = b;
+  color->color_space = COLOR_SPACE_RGB;
+}
+
+extern struct color color_black, color_white;
+
+/* Conversion between various pixel formats */
+
+enum {
+  IMAGE_CONV_FILL_ALPHA = 1,
+  IMAGE_CONV_COPY_ALPHA = 2,
+  IMAGE_CONV_APPLY_ALPHA = 4,
+};
+
+struct image_conv_options {
+  uns flags;
+  struct color background;
+};
+
+extern struct image_conv_options image_conv_defaults;
+
+int image_conv(struct image_context *ctx, struct image *dest, struct image *src, struct image_conv_options *opt);
+
+/* Color spaces in the CIE 1931 chromacity diagram */
+
+struct color_space_chromacity_info {
+  double prim1[2];
+  double prim2[2];
+  double prim3[2];
+  double white[2];
+};
+
+struct color_space_gamma_info {
+  double simple_gamma;
+  double detailed_gamma;
+  double offset;
+  double transition;
+  double slope;
+};
+
+struct color_space_info {
+  byte *name;
+  struct color_space_chromacity_info chromacity;
+  struct color_space_gamma_info gamma;
+};
+
+extern const double
+  color_illuminant_d50[2],
+  color_illuminant_d65[2],
+  color_illuminant_e[2];
+
+extern const struct color_space_info
+  color_adobe_rgb_info,                /* Adobe RGB (1998) */
+  color_apple_rgb_info,                /* Apple RGB */
+  color_cie_rgb_info,          /* CIE RGB */
+  color_color_match_rgb_info,  /* ColorMatch RGB */
+  color_srgb_info;             /* sRGB */
+
+/* These routines do not check numeric errors! */
+void color_compute_color_space_to_xyz_matrix(double matrix[9], const struct color_space_chromacity_info *space);
+void color_compute_bradford_matrix(double matrix[9], const double src[2], const double dest[2]);
+void color_compute_color_spaces_conversion_matrix(double matrix[9], const struct color_space_chromacity_info *src, const struct color_space_chromacity_info *dest);
+void color_invert_matrix(double dest[9], double matrix[9]);
+
+static inline uns
+rgb_to_gray_func(uns r, uns g, uns b)
+{
+  return (r * 19660 + g * 38666 + b * 7210) >> 16;
+}
+
 /* Exact slow conversion routines */
-void srgb_to_xyz_slow(double dest[3], double src[3]);
-void xyz_to_luv_slow(double dest[3], double src[3]);
+void srgb_to_xyz_exact(double dest[3], double src[3]);
+void xyz_to_srgb_exact(double dest[3], double src[3]);
+void xyz_to_luv_exact(double dest[3], double src[3]);
+void luv_to_xyz_exact(double dest[3], double src[3]);
+void rgb_to_cmyk_exact(double dest[4], double src[3]);
+void cmyk_to_rgb_exact(double dest[3], double src[4]);
 
 /* Reference white */
 #define REF_WHITE_X 0.96422
@@ -56,6 +179,7 @@ extern u32 srgb_to_luv_tab3[20 << SRGB_TO_LUV_TAB3_SIZE];
 void srgb_to_luv_init(void);
 void srgb_to_luv_pixels(byte *dest, byte *src, uns count);
 
+/* L covers the interval [0..255]; u and v are centered to 128 and scaled by 1/4 in respect of L */
 static inline void
 srgb_to_luv_pixel(byte *dest, byte *src)
 {