jcomapi.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * jcomapi.c
  3. *
  4. * This file was part of the Independent JPEG Group's software:
  5. * Copyright (C) 1994-1997, Thomas G. Lane.0
  6. * It was modified by The libjpeg-turbo Project to include only code relevant
  7. * to libjpeg-turbo.
  8. * For conditions of distribution and use, see the accompanying README file.
  9. *
  10. * This file contains application interface routines that are used for both
  11. * compression and decompression.
  12. */
  13. #define JPEG_INTERNALS
  14. #include "jinclude.h"
  15. #include "jpeglib.h"
  16. /*
  17. * Abort processing of a JPEG compression or decompression operation,
  18. * but don't destroy the object itself.
  19. *
  20. * For this, we merely clean up all the nonpermanent memory pools.
  21. * Note that temp files (virtual arrays) are not allowed to belong to
  22. * the permanent pool, so we will be able to close all temp files here.
  23. * Closing a data source or destination, if necessary, is the application's
  24. * responsibility.
  25. */
  26. GLOBAL(void)
  27. jpeg_abort (j_common_ptr cinfo)
  28. {
  29. int pool;
  30. /* Do nothing if called on a not-initialized or destroyed JPEG object. */
  31. if (cinfo->mem == NULL)
  32. return;
  33. /* Releasing pools in reverse order might help avoid fragmentation
  34. * with some (brain-damaged) malloc libraries.
  35. */
  36. for (pool = JPOOL_NUMPOOLS-1; pool > JPOOL_PERMANENT; pool--) {
  37. (*cinfo->mem->free_pool) (cinfo, pool);
  38. }
  39. /* Reset overall state for possible reuse of object */
  40. if (cinfo->is_decompressor) {
  41. cinfo->global_state = DSTATE_START;
  42. /* Try to keep application from accessing now-deleted marker list.
  43. * A bit kludgy to do it here, but this is the most central place.
  44. */
  45. ((j_decompress_ptr) cinfo)->marker_list = NULL;
  46. } else {
  47. cinfo->global_state = CSTATE_START;
  48. }
  49. }
  50. /*
  51. * Destruction of a JPEG object.
  52. *
  53. * Everything gets deallocated except the master jpeg_compress_struct itself
  54. * and the error manager struct. Both of these are supplied by the application
  55. * and must be freed, if necessary, by the application. (Often they are on
  56. * the stack and so don't need to be freed anyway.)
  57. * Closing a data source or destination, if necessary, is the application's
  58. * responsibility.
  59. */
  60. GLOBAL(void)
  61. jpeg_destroy (j_common_ptr cinfo)
  62. {
  63. /* We need only tell the memory manager to release everything. */
  64. /* NB: mem pointer is NULL if memory mgr failed to initialize. */
  65. if (cinfo->mem != NULL)
  66. (*cinfo->mem->self_destruct) (cinfo);
  67. cinfo->mem = NULL; /* be safe if jpeg_destroy is called twice */
  68. cinfo->global_state = 0; /* mark it destroyed */
  69. }
  70. /*
  71. * Convenience routines for allocating quantization and Huffman tables.
  72. * (Would jutils.c be a more reasonable place to put these?)
  73. */
  74. GLOBAL(JQUANT_TBL *)
  75. jpeg_alloc_quant_table (j_common_ptr cinfo)
  76. {
  77. JQUANT_TBL *tbl;
  78. tbl = (JQUANT_TBL *)
  79. (*cinfo->mem->alloc_small) (cinfo, JPOOL_PERMANENT, sizeof(JQUANT_TBL));
  80. tbl->sent_table = FALSE; /* make sure this is false in any new table */
  81. return tbl;
  82. }
  83. GLOBAL(JHUFF_TBL *)
  84. jpeg_alloc_huff_table (j_common_ptr cinfo)
  85. {
  86. JHUFF_TBL *tbl;
  87. tbl = (JHUFF_TBL *)
  88. (*cinfo->mem->alloc_small) (cinfo, JPOOL_PERMANENT, sizeof(JHUFF_TBL));
  89. tbl->sent_table = FALSE; /* make sure this is false in any new table */
  90. return tbl;
  91. }