jcomapi.cpp 2.8 KB

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