⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 simple_example_d.cpp

📁 该源码是JPEG2000的c++源代码,希望对研究JPEG2000标准以及编解码的朋友们有用.
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/*****************************************************************************/// File: simple_example_d.cpp [scope = APPS/SIMPLE_EXAMPLE]// Version: Kakadu, V2.2// Author: David Taubman// Last Revised: 20 June, 2001/*****************************************************************************/// Copyright 2001, David Taubman, The University of New South Wales (UNSW)// The copyright owner is Unisearch Ltd, Australia (commercial arm of UNSW)// Neither this copyright statement, nor the licensing details below// may be removed from this file or dissociated from its contents./*****************************************************************************/// Licensee: Book Owner// License number: 99999// The Licensee has been granted a NON-COMMERCIAL license to the contents of// this source file, said Licensee being the owner of a copy of the book,// "JPEG2000: Image Compression Fundamentals, Standards and Practice," by// Taubman and Marcellin (Kluwer Academic Publishers, 2001).  A brief summary// of the license appears below.  This summary is not to be relied upon in// preference to the full text of the license agreement, which was accepted// upon breaking the seal of the compact disc accompanying the above-mentioned// book.// 1. The Licensee has the right to Non-Commercial Use of the Kakadu software,//    Version 2.2, including distribution of one or more Applications built//    using the software, provided such distribution is not for financial//    return.// 2. The Licensee has the right to personal use of the Kakadu software,//    Version 2.2.// 3. The Licensee has the right to distribute Reusable Code (including//    source code and dynamically or statically linked libraries) to a Third//    Party, provided the Third Party possesses a license to use the Kakadu//    software, Version 2.2, and provided such distribution is not for//    financial return./******************************************************************************Description:   Simple example showing decompression into an intermediate buffer for theimage samples.  This is, of course, a great waste of memory and the moresophisticated "kdu_expand" application does no such thing.  This is also farfrom the recommended procedure for interactive applications, for two reasons:1) a responsive interactive application should incrementally update thedisplay as it goes, processing samples in order of their relevance to theportion of the image actually being displayed; and 2) interactive applicationsusually maintain a viewing portal into a potentially much larger image, soit is preferable to decompress only the image regions which are visible,thereby avoiding the cost of buffering the entire decompressed image.  Afar superior approach for interactive applications is demonstrated by the"kdu_show" utility and the "kd_region_decompressor" object used in thatutility should provide a versatile and useful interface tool for manyinteractive applications.   Although the current example is far from the recommended way of usingKakadu in many professional applications, developers may find this type ofintroductory example helpful as a first exercise in understanding how to useKakadu.  The vast majority of Kakadu's interesting features are not utilized bythis example, but it may be enough to satisfy the initial needs of somedevelopers who are working with moderately large images, already in an internalbuffer, especially when interfacing with existing image processing packageswhich are buffer-oriented.   The emphasis here is on showing a minimal number of instructions requiredto open a codestream object, attach a decompression engine and recover samplesfrom the engine.  Decompression is necessarily a little more complex thancompression, since we have no control over the compressed code-stream whichmay be sent to us.  The present application should be able to cope with anycode-stream at all, although it may decompress only the first imagecomponent, if multiple components have differing sizes so that they areincompatible with the simple interleaved buffer structure used here.   By far the largest function here is that used to convert decompressedsamples to an 8-bit representation for the buffer.  Although this may soundtrivial, it is complicated by the fact that the original samples whichwere compressed may have had any bit-depth at all, and they may have beencompressed by either the reversible or the irreversible processing pathoffered by JPEG2000, each of which has fundamentally different implicationsfor the interpretation of the decompressed sample values.  There are avariety of reasons for regarding sample representation conversions as thetask of the application, rather than the core system.  This will becomeeven more obvious as we add support for JP2 and perhaps other file formatsin subsequent releases of the Kakadu system.******************************************************************************/// System includes#include <assert.h>#include <stdlib.h>#include <string.h>#include <fstream>// Kakadu core includes#include "kdu_elementary.h"#include "kdu_messaging.h"#include "kdu_params.h"#include "kdu_compressed.h"#include "kdu_sample_processing.h"// Application level includes#include "kdu_file_io.h"/*****************************************************************************//* STATIC                         write_image                                *//*****************************************************************************/static void  write_image(char *fname, kdu_byte *buffer,              int num_components, int height, int width)  /* Simple PGM/PPM file writer.  The `buffer' array contains interleaved     image samples (there are 1 or 3 components) in unsigned bytes,     appearing in scan-line order. */{  std::ofstream out(fname,std::ios::out|std::ios::binary);  if (!out)    { kdu_error e;      e << "Unable to open output image file, \"" << fname << "\"."; }  if (num_components == 1)    out << "P5\n" << width << " " << height << "\n255\n";  else if (num_components == 3)    out << "P6\n" << width << " " << height << "\n255\n";  else    assert(0); // The caller makes sure only 1 or 3 components are decompressed  if (!out.write((char *) buffer,num_components*width*height))    { kdu_error e;      e << "Unable to write to output image file, \"" << fname << "\"."; }}/******************************************************************************//*                              transfer_bytes                                *//******************************************************************************/void  transfer_bytes(kdu_byte *dest, kdu_line_buf &src, int gap, int precision)  /* Transfers source samples from the supplied line buffer into the output     byte buffer, spacing successive output samples apart by `gap' bytes     (to allow for interleaving of colour components).  The function performs     all necessary level shifting, type conversion, rounding and truncation. */{  int width = src.get_width();  if (src.get_buf32() != NULL)    { // Decompressed samples have a 32-bit representation (integer or float)      assert(precision >= 8); // Else would have used 16 bit representation      kdu_sample32 *sp = src.get_buf32();      if (!src.is_absolute())        { // Transferring normalized floating point data.          float scale16 = (float)(1<<16);          kdu_int32 val;          for (; width > 0; width--, sp++, dest+=gap)            {              val = (kdu_int32)(sp->fval*scale16);              val = (val+128)>>8; // May be faster than true rounding              val += 128;              if (val & ((-1)<<8))                val = (val<0)?0:255;              *dest = (kdu_byte) val;            }        }      else        { // Transferring 32-bit absolute integers.          kdu_int32 val;          kdu_int32 downshift = precision-8;          kdu_int32 offset = (1<<downshift)>>1;                        for (; width > 0; width--, sp++, dest+=gap)            {              val = sp->ival;              val = (val+offset)>>downshift;              val += 128;              if (val & ((-1)<<8))                val = (val<0)?0:255;              *dest = (kdu_byte) val;            }        }    }  else    { // Source data is 16 bits.      kdu_sample16 *sp = src.get_buf16();      if (!src.is_absolute())        { // Transferring 16-bit fixed point quantities          kdu_int16 val;          if (precision >= 8)            { // Can essentially ignore the bit-depth.              for (; width > 0; width--, sp++, dest+=gap)                {                  val = sp->ival;                  val += (1<<(KDU_FIX_POINT-8))>>1;                  val >>= (KDU_FIX_POINT-8);                  val += 128;                  if (val & ((-1)<<8))                    val = (val<0)?0:255;                  *dest = (kdu_byte) val;                }            }          else            { // Need to force zeros into one or more least significant bits.              kdu_int16 downshift = KDU_FIX_POINT-precision;              kdu_int16 upshift = 8-precision;              kdu_int16 offset = 1<<(downshift-1);              for (; width > 0; width--, sp++, dest+=gap)                {                  val = sp->ival;                  val = (val+offset)>>downshift;                  val <<= upshift;                  val += 128;                  if (val & ((-1)<<8))                    val = (val<0)?0:(256-(1<<upshift));                  *dest = (kdu_byte) val;                }            }        }      else        { // Transferring 16-bit absolute integers.          kdu_int16 val;          if (precision >= 8)            {              kdu_int16 downshift = precision-8;              kdu_int16 offset = (1<<downshift)>>1;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -