image.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. /* VIPS image class.
  2. *
  3. * 7/7/09
  4. * - from vips.h
  5. * 2/3/11
  6. * - move to GObject
  7. */
  8. /*
  9. This file is part of VIPS.
  10. VIPS is free software; you can redistribute it and/or modify
  11. it under the terms of the GNU Lesser General Public License as published by
  12. the Free Software Foundation; either version 2 of the License, or
  13. (at your option) any later version.
  14. This program is distributed in the hope that it will be useful,
  15. but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. GNU Lesser General Public License for more details.
  18. You should have received a copy of the GNU Lesser General Public License
  19. along with this program; if not, write to the Free Software
  20. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  21. 02110-1301 USA
  22. */
  23. /*
  24. These files are distributed with VIPS - http://www.vips.ecs.soton.ac.uk
  25. */
  26. #ifndef VIPS_IMAGE_H
  27. #define VIPS_IMAGE_H
  28. #ifdef __cplusplus
  29. extern "C" {
  30. #endif /*__cplusplus*/
  31. /* If you read MSB first, you get these two values.
  32. * intel order: byte 0 = b6
  33. * SPARC order: byte 0 = 08
  34. */
  35. #define VIPS_MAGIC_INTEL (0xb6a6f208U)
  36. #define VIPS_MAGIC_SPARC (0x08f2a6b6U)
  37. /* We have a maximum value for a coordinate at various points for sanity
  38. * checking. For example, vips_black() has a max with and height. We use int
  39. * for width/height so we could go up to 2bn, but it's good to have a lower
  40. * value set so we can see crazy numbers early.
  41. *
  42. * This was 1m for a while, but someone found a use for a 4m wide image.
  43. */
  44. #define VIPS_MAX_COORD (10000000)
  45. typedef enum {
  46. VIPS_DEMAND_STYLE_ERROR = -1,
  47. VIPS_DEMAND_STYLE_SMALLTILE,
  48. VIPS_DEMAND_STYLE_FATSTRIP,
  49. VIPS_DEMAND_STYLE_THINSTRIP,
  50. VIPS_DEMAND_STYLE_ANY
  51. } VipsDemandStyle;
  52. /* Types of image descriptor we may have. The type field is advisory only: it
  53. * does not imply that any fields in IMAGE have valid data.
  54. */
  55. typedef enum {
  56. VIPS_IMAGE_ERROR = -1,
  57. VIPS_IMAGE_NONE, /* no type set */
  58. VIPS_IMAGE_SETBUF, /* malloced memory array */
  59. VIPS_IMAGE_SETBUF_FOREIGN, /* memory array, don't free on close */
  60. VIPS_IMAGE_OPENIN, /* input from fd with a window */
  61. VIPS_IMAGE_MMAPIN, /* memory mapped input file */
  62. VIPS_IMAGE_MMAPINRW, /* memory mapped read/write file */
  63. VIPS_IMAGE_OPENOUT, /* output to fd */
  64. VIPS_IMAGE_PARTIAL /* partial image */
  65. } VipsImageType;
  66. typedef enum {
  67. VIPS_INTERPRETATION_ERROR = -1,
  68. VIPS_INTERPRETATION_MULTIBAND = 0,
  69. VIPS_INTERPRETATION_B_W = 1,
  70. VIPS_INTERPRETATION_HISTOGRAM = 10,
  71. VIPS_INTERPRETATION_XYZ = 12,
  72. VIPS_INTERPRETATION_LAB = 13,
  73. VIPS_INTERPRETATION_CMYK = 15,
  74. VIPS_INTERPRETATION_LABQ = 16,
  75. VIPS_INTERPRETATION_RGB = 17,
  76. VIPS_INTERPRETATION_CMC = 18,
  77. VIPS_INTERPRETATION_LCH = 19,
  78. VIPS_INTERPRETATION_LABS = 21,
  79. VIPS_INTERPRETATION_sRGB = 22,
  80. VIPS_INTERPRETATION_YXY = 23,
  81. VIPS_INTERPRETATION_FOURIER = 24,
  82. VIPS_INTERPRETATION_RGB16 = 25,
  83. VIPS_INTERPRETATION_GREY16 = 26,
  84. VIPS_INTERPRETATION_MATRIX = 27,
  85. VIPS_INTERPRETATION_scRGB = 28,
  86. VIPS_INTERPRETATION_HSV = 29,
  87. VIPS_INTERPRETATION_LAST = 30
  88. } VipsInterpretation;
  89. typedef enum {
  90. VIPS_FORMAT_NOTSET = -1,
  91. VIPS_FORMAT_UCHAR = 0,
  92. VIPS_FORMAT_CHAR = 1,
  93. VIPS_FORMAT_USHORT = 2,
  94. VIPS_FORMAT_SHORT = 3,
  95. VIPS_FORMAT_UINT = 4,
  96. VIPS_FORMAT_INT = 5,
  97. VIPS_FORMAT_FLOAT = 6,
  98. VIPS_FORMAT_COMPLEX = 7,
  99. VIPS_FORMAT_DOUBLE = 8,
  100. VIPS_FORMAT_DPCOMPLEX = 9,
  101. VIPS_FORMAT_LAST = 10
  102. } VipsBandFormat;
  103. typedef enum {
  104. VIPS_CODING_ERROR = -1,
  105. VIPS_CODING_NONE = 0,
  106. VIPS_CODING_LABQ = 2,
  107. VIPS_CODING_RAD = 6,
  108. VIPS_CODING_LAST = 7
  109. } VipsCoding;
  110. typedef enum {
  111. VIPS_ACCESS_RANDOM,
  112. VIPS_ACCESS_SEQUENTIAL,
  113. VIPS_ACCESS_SEQUENTIAL_UNBUFFERED,
  114. VIPS_ACCESS_LAST
  115. } VipsAccess;
  116. typedef void *(*VipsStartFn)( struct _VipsImage *out, void *a, void *b );
  117. typedef int (*VipsGenerateFn)( struct _VipsRegion *out,
  118. void *seq, void *a, void *b, gboolean *stop );
  119. typedef int (*VipsStopFn)( void *seq, void *a, void *b );
  120. /* Struct we keep a record of execution time in. Passed to eval signal so
  121. * it can assess progress.
  122. */
  123. typedef struct _VipsProgress {
  124. /*< private >*/
  125. struct _VipsImage *im; /* Image we are part of */
  126. /*< public >*/
  127. int run; /* Time we have been running */
  128. int eta; /* Estimated seconds of computation left */
  129. gint64 tpels; /* Number of pels we expect to calculate */
  130. gint64 npels; /* Number of pels calculated so far */
  131. int percent; /* Percent complete */
  132. GTimer *start; /* Start time */
  133. } VipsProgress;
  134. #define VIPS_TYPE_IMAGE (vips_image_get_type())
  135. #define VIPS_IMAGE( obj ) \
  136. (G_TYPE_CHECK_INSTANCE_CAST( (obj), \
  137. VIPS_TYPE_IMAGE, VipsImage ))
  138. #define VIPS_IMAGE_CLASS( klass ) \
  139. (G_TYPE_CHECK_CLASS_CAST( (klass), \
  140. VIPS_TYPE_IMAGE, VipsImageClass))
  141. #define VIPS_IS_IMAGE( obj ) \
  142. (G_TYPE_CHECK_INSTANCE_TYPE( (obj), VIPS_TYPE_IMAGE ))
  143. #define VIPS_IS_IMAGE_CLASS( klass ) \
  144. (G_TYPE_CHECK_CLASS_TYPE( (klass), VIPS_TYPE_IMAGE ))
  145. #define VIPS_IMAGE_GET_CLASS( obj ) \
  146. (G_TYPE_INSTANCE_GET_CLASS( (obj), \
  147. VIPS_TYPE_IMAGE, VipsImageClass ))
  148. typedef struct _VipsImage {
  149. VipsObject parent_instance;
  150. /*< private >*/
  151. /* We have to keep these names for compatibility with the old API.
  152. * Don't use them though, use vips_image_get_width() and friends.
  153. */
  154. int Xsize; /* image width, in pixels */
  155. int Ysize; /* image height, in pixels */
  156. int Bands; /* number of image bands */
  157. VipsBandFormat BandFmt; /* pixel format */
  158. VipsCoding Coding; /* pixel coding */
  159. VipsInterpretation Type;/* pixel interpretation */
  160. double Xres; /* horizontal pixels per millimetre */
  161. double Yres; /* vertical pixels per millimetre */
  162. int Xoffset; /* image origin hint */
  163. int Yoffset; /* image origin hint */
  164. /* No longer used, the names are here for compat with very, very old
  165. * code.
  166. */
  167. int Length;
  168. short Compression;
  169. short Level;
  170. int Bbits; /* was number of bits in this format */
  171. /* Old code expects to see this member, newer code has a param on
  172. * eval().
  173. */
  174. VipsProgress *time;
  175. /* Derived fields that some code can fiddle with. New code should use
  176. * vips_image_get_history() and friends.
  177. */
  178. char *Hist; /* don't use, see vips_image_get_history() */
  179. char *filename; /* pointer to copy of filename */
  180. VipsPel *data; /* start of image data for WIO */
  181. int kill; /* set to non-zero to block eval */
  182. /* Everything below this private and only used internally by
  183. * VipsImage.
  184. */
  185. /* During vips image read and write we need temporary float-sized
  186. * fields in the struct for staging xres/yres. Don't use these any
  187. * other time.
  188. */
  189. float Xres_float;
  190. float Yres_float;
  191. char *mode; /* mode string passed to _new() */
  192. VipsImageType dtype; /* descriptor type */
  193. int fd; /* file descriptor */
  194. void *baseaddr; /* pointer to the start of an mmap file */
  195. size_t length; /* size of mmap area */
  196. guint32 magic; /* magic from header, endian-ness of image */
  197. /* Partial image stuff. All these fields are initialised
  198. * to NULL and ignored unless set by vips_image_generate() etc.
  199. */
  200. VipsStartFn start_fn;
  201. VipsGenerateFn generate_fn;
  202. VipsStopFn stop_fn;
  203. void *client1; /* user arguments */
  204. void *client2;
  205. GMutex *sslock; /* start-stop lock */
  206. GSList *regions; /* list of regions current for this image */
  207. VipsDemandStyle dhint; /* demand style hint */
  208. /* Extra user-defined fields ... see vips_image_get() etc.
  209. */
  210. GHashTable *meta; /* GhashTable of GValue */
  211. GSList *meta_traverse; /* traverse order for Meta */
  212. /* Part of mmap() read ... the sizeof() the header we skip from the
  213. * file start. Usually VIPS_SIZEOF_HEADER, but can be something else
  214. * for binary file read.
  215. *
  216. * guint64 so that we can guarantee to work even on systems with
  217. * strange ideas about large files.
  218. */
  219. gint64 sizeof_header;
  220. /* If this is a large disc image, don't map the whole thing, instead
  221. * have a set of windows shared between the regions active on the
  222. * image. List of VipsWindow.
  223. */
  224. GSList *windows;
  225. /* Upstream/downstream relationships, built from args to
  226. * vips_demand_hint().
  227. *
  228. * We use these to invalidate downstream pixel buffers.
  229. * Use 'serial' to spot circular dependencies.
  230. *
  231. * See also hint_set below.
  232. */
  233. GSList *upstream;
  234. GSList *downstream;
  235. int serial;
  236. /* Keep a list of recounted GValue strings so we can share hist
  237. * efficiently.
  238. */
  239. GSList *history_list;
  240. /* The VipsImage (if any) we should signal eval progress on.
  241. */
  242. struct _VipsImage *progress_signal;
  243. /* Record the file length here. We use this to stop ourselves mapping
  244. * things beyond the end of the file in the case that the file has
  245. * been truncated.
  246. *
  247. * gint64 so that we can guarantee to work even on systems with
  248. * strange ideas about large files.
  249. */
  250. gint64 file_length;
  251. /* Set this when vips_demand_hint_array() is called, and check in any
  252. * operation that will demand pixels from the image.
  253. *
  254. * We use vips_demand_hint_array() to build the tree of
  255. * upstream/downstream relationships, so it's a mandatory thing.
  256. */
  257. gboolean hint_set;
  258. /* Delete-on-close is hard to do with signals and callbacks since we
  259. * really need to do this in finalize after the fd has been closed,
  260. * but you can't emit signals then.
  261. *
  262. * Also keep a private copy of the filename string to be deleted,
  263. * since image->filename will be freed in _dispose().
  264. */
  265. gboolean delete_on_close;
  266. char *delete_on_close_filename;
  267. } VipsImage;
  268. typedef struct _VipsImageClass {
  269. VipsObjectClass parent_class;
  270. /* Signals we emit.
  271. */
  272. /* Evaluation is starting.
  273. */
  274. void (*preeval)( VipsImage *image, VipsProgress *progress );
  275. /* Evaluation progress.
  276. */
  277. void (*eval)( VipsImage *image, VipsProgress *progress );
  278. /* Evaluation is ending.
  279. */
  280. void (*posteval)( VipsImage *image, VipsProgress *progress );
  281. /* An image has been written to.
  282. * Used by eg. vips_image_new_mode("x.jpg", "w") to do the
  283. * final write to jpeg.
  284. * Set *result to non-zero to indicate an error on write.
  285. */
  286. void (*written)( VipsImage *image, int *result );
  287. /* An image has been modified in some way and all caches
  288. * need dropping.
  289. */
  290. void (*invalidate)( VipsImage *image );
  291. /* Minimise this pipeline.
  292. *
  293. * This is triggered (sometimes) at the end of eval to signal that
  294. * we're probably done and that operations involved should try to
  295. * minimise memory use by, for example, dropping caches.
  296. *
  297. * See vips_tilecache().
  298. */
  299. void (*minimise)( VipsImage *image );
  300. } VipsImageClass;
  301. /* Don't put spaces around void here, it breaks gtk-doc.
  302. */
  303. GType vips_image_get_type(void);
  304. /* Has to be guint64 and not size_t/off_t since we have to be able to address
  305. * huge images on platforms with 32-bit files.
  306. */
  307. /* Pixel address calculation macros.
  308. */
  309. #define VIPS_IMAGE_SIZEOF_ELEMENT( I ) \
  310. (vips_format_sizeof_unsafe((I)->BandFmt))
  311. #define VIPS_IMAGE_SIZEOF_PEL( I ) \
  312. (VIPS_IMAGE_SIZEOF_ELEMENT( I ) * (I)->Bands)
  313. #define VIPS_IMAGE_SIZEOF_LINE( I ) \
  314. (VIPS_IMAGE_SIZEOF_PEL( I ) * (I)->Xsize)
  315. #define VIPS_IMAGE_SIZEOF_IMAGE( I ) \
  316. (VIPS_IMAGE_SIZEOF_LINE( I ) * (I)->Ysize)
  317. #define VIPS_IMAGE_N_ELEMENTS( I ) \
  318. ((I)->Bands * (I)->Xsize)
  319. #define VIPS_IMAGE_N_PELS( I ) \
  320. ((guint64) (I)->Xsize * (I)->Ysize)
  321. /* If VIPS_DEBUG is defined, add bounds checking.
  322. */
  323. #ifdef VIPS_DEBUG
  324. #define VIPS_IMAGE_ADDR( I, X, Y ) \
  325. ( ((X) >= 0 && (X) < VIPS_IMAGE( I )->Xsize && \
  326. (Y) >= 0 && (Y) < VIPS_IMAGE( I )->Ysize && \
  327. VIPS_IMAGE( I )->data) ? \
  328. (VIPS_IMAGE( I )->data + \
  329. (Y) * VIPS_IMAGE_SIZEOF_LINE( I ) + \
  330. (X) * VIPS_IMAGE_SIZEOF_PEL( I )) : \
  331. (fprintf( stderr, \
  332. "VIPS_IMAGE_ADDR: point out of bounds, " \
  333. "file \"%s\", line %d\n" \
  334. "(point x=%d, y=%d\n" \
  335. " should have been within VipsRect left=%d, top=%d, " \
  336. "width=%d, height=%d)\n", \
  337. __FILE__, __LINE__, \
  338. (X), (Y), \
  339. 0, 0, \
  340. VIPS_IMAGE( I )->Xsize, \
  341. VIPS_IMAGE( I )->Ysize ), (VipsPel *) NULL) \
  342. )
  343. #else /*!VIPS_DEBUG*/
  344. #define VIPS_IMAGE_ADDR( I, X, Y ) \
  345. ((I)->data + \
  346. (Y) * VIPS_IMAGE_SIZEOF_LINE( I ) + \
  347. (X) * VIPS_IMAGE_SIZEOF_PEL( I ))
  348. #endif /*VIPS_DEBUG*/
  349. #ifdef VIPS_DEBUG
  350. #define VIPS_MATRIX( I, X, Y ) \
  351. ((VIPS_IMAGE( I )->BandFmt == VIPS_FORMAT_DOUBLE && \
  352. VIPS_IMAGE( I )->Bands == 1) ? \
  353. ((double *) VIPS_IMAGE_ADDR( I, X, Y )) : \
  354. (fprintf( stderr, "VIPS_MATRIX: not a matrix image\n" ), \
  355. (double *) NULL))
  356. #else /*!VIPS_DEBUG*/
  357. #define VIPS_MATRIX( I, X, Y ) \
  358. ((double *) VIPS_IMAGE_ADDR( I, X, Y ))
  359. #endif /*VIPS_DEBUG*/
  360. void vips_progress_set( gboolean progress );
  361. void vips_image_invalidate_all( VipsImage *image );
  362. void vips_image_minimise_all( VipsImage *image );
  363. gboolean vips_image_is_sequential( VipsImage *image );
  364. void vips_image_set_progress( VipsImage *image, gboolean progress );
  365. gboolean vips_image_iskilled( VipsImage *image );
  366. void vips_image_set_kill( VipsImage *image, gboolean kill );
  367. char *vips_filename_get_filename( const char *vips_filename );
  368. char *vips_filename_get_options( const char *vips_filename );
  369. VipsImage *vips_image_new( void );
  370. VipsImage *vips_image_new_memory( void );
  371. VipsImage *vips_image_memory( void );
  372. VipsImage *vips_image_new_from_file( const char *name, ... )
  373. __attribute__((sentinel));
  374. VipsImage *vips_image_new_from_file_RW( const char *filename );
  375. VipsImage *vips_image_new_from_file_raw( const char *filename,
  376. int xsize, int ysize, int bands, guint64 offset );
  377. VipsImage *vips_image_new_from_memory( const void *data, size_t size,
  378. int width, int height, int bands, VipsBandFormat format );
  379. VipsImage *vips_image_new_from_memory_copy( const void *data, size_t size,
  380. int width, int height, int bands, VipsBandFormat format );
  381. VipsImage *vips_image_new_from_buffer( const void *buf, size_t len,
  382. const char *option_string, ... )
  383. __attribute__((sentinel));
  384. VipsImage *vips_image_new_from_source( VipsSource *source,
  385. const char *option_string, ... ) __attribute__((sentinel));
  386. VipsImage *vips_image_new_matrix( int width, int height );
  387. VipsImage *vips_image_new_matrixv( int width, int height, ... );
  388. VipsImage *vips_image_new_matrix_from_array( int width, int height,
  389. const double *array, int size );
  390. VipsImage *vips_image_matrix_from_array( int width, int height,
  391. const double *array, int size );
  392. VipsImage *vips_image_new_from_image( VipsImage *image,
  393. const double *c, int n );
  394. VipsImage *vips_image_new_from_image1( VipsImage *image, double c );
  395. void vips_image_set_delete_on_close( VipsImage *image,
  396. gboolean delete_on_close );
  397. guint64 vips_get_disc_threshold( void );
  398. VipsImage *vips_image_new_temp_file( const char *format );
  399. int vips_image_write( VipsImage *image, VipsImage *out );
  400. int vips_image_write_to_file( VipsImage *image, const char *name, ... )
  401. __attribute__((sentinel));
  402. int vips_image_write_to_buffer( VipsImage *in,
  403. const char *suffix, void **buf, size_t *size, ... )
  404. __attribute__((sentinel));
  405. int vips_image_write_to_target( VipsImage *in,
  406. const char *suffix, VipsTarget *target, ... )
  407. __attribute__((sentinel));
  408. void *vips_image_write_to_memory( VipsImage *in, size_t *size );
  409. int vips_image_decode_predict( VipsImage *in,
  410. int *bands, VipsBandFormat *format );
  411. int vips_image_decode( VipsImage *in, VipsImage **out );
  412. int vips_image_encode( VipsImage *in, VipsImage **out, VipsCoding coding );
  413. gboolean vips_image_isMSBfirst( VipsImage *image );
  414. gboolean vips_image_isfile( VipsImage *image );
  415. gboolean vips_image_ispartial( VipsImage *image );
  416. gboolean vips_image_hasalpha( VipsImage *image );
  417. VipsImage *vips_image_copy_memory( VipsImage *image );
  418. int vips_image_wio_input( VipsImage *image );
  419. int vips_image_pio_input( VipsImage *image );
  420. int vips_image_pio_output( VipsImage *image );
  421. int vips_image_inplace( VipsImage *image );
  422. int vips_image_write_prepare( VipsImage *image );
  423. int vips_image_write_line( VipsImage *image, int ypos, VipsPel *linebuffer );
  424. gboolean vips_band_format_isint( VipsBandFormat format );
  425. gboolean vips_band_format_isuint( VipsBandFormat format );
  426. gboolean vips_band_format_is8bit( VipsBandFormat format );
  427. gboolean vips_band_format_isfloat( VipsBandFormat format );
  428. gboolean vips_band_format_iscomplex( VipsBandFormat format );
  429. int vips_system( const char *cmd_format, ... )
  430. __attribute__((sentinel));
  431. /* Defined in type.c but declared here, since they use VipsImage.
  432. */
  433. VipsArrayImage *vips_array_image_new( VipsImage **array, int n );
  434. VipsArrayImage *vips_array_image_newv( int n, ... );
  435. VipsArrayImage *vips_array_image_new_from_string( const char *string,
  436. VipsAccess flags );
  437. VipsArrayImage *vips_array_image_empty( void );
  438. VipsArrayImage *vips_array_image_append( VipsArrayImage *array,
  439. VipsImage *image );
  440. VipsImage **vips_array_image_get( VipsArrayImage *array, int *n );
  441. VipsImage **vips_value_get_array_image( const GValue *value, int *n );
  442. void vips_value_set_array_image( GValue *value, int n );
  443. /* Defined in reorder.c, but really a function on image.
  444. */
  445. int vips_reorder_prepare_many( VipsImage *image,
  446. struct _VipsRegion **regions, VipsRect *r );
  447. void vips_reorder_margin_hint( VipsImage *image, int margin );
  448. #ifdef __cplusplus
  449. }
  450. #endif /*__cplusplus*/
  451. #endif /*VIPS_IMAGE_H*/