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

📄 jpeg.c

📁 一个机器人开发的相关嵌入式开发源码
💻 C
📖 第 1 页 / 共 3 页
字号:
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *  jpeg.c - jpeg decompressor for ARM7
 *  Copyright (C) 2005-2006  Surveyor Corporation
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details (www.gnu.org/licenses)
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#include "jpeg.h"

#define JPEG_FUNCTION_END(NAME) static void NAME##End () { }

/* Converts left-to-right coefficient indices into zig-zagged indices. */
const unsigned char JPEG_ToZigZag [JPEG_DCTSIZE2] =
{
  0, 1, 8, 16, 9, 2, 3, 10,
  17, 24, 32, 25, 18, 11, 4, 5,
  12, 19, 26, 33, 40, 48, 41, 34,
  27, 20, 13, 6, 7, 14, 21, 28,
  35, 42, 49, 56, 57, 50, 43, 36,
  29, 22, 15, 23, 30, 37, 44, 51,
  58, 59, 52, 45, 38, 31, 39, 46,
  53, 60, 61, 54, 47, 55, 62, 63,
};

/* These macros are so that we can generate the AA&N multipliers at
 * compile-time, allowing configuration control of fixed point precision.
 */
#define JPEG_AAN_0 1.0
#define JPEG_AAN_1 1.387039845 
#define JPEG_AAN_2 1.306562965
#define JPEG_AAN_3 1.175875602
#define JPEG_AAN_4 1.0
#define JPEG_AAN_5 0.785694958
#define JPEG_AAN_6 0.541196100
#define JPEG_AAN_7 0.275899379

#define JPEG_AAN_LINE(B) \
  JPEG_FTOFIX (JPEG_AAN_0 * JPEG_AAN_##B), \
  JPEG_FTOFIX (JPEG_AAN_1 * JPEG_AAN_##B), \
  JPEG_FTOFIX (JPEG_AAN_2 * JPEG_AAN_##B), \
  JPEG_FTOFIX (JPEG_AAN_3 * JPEG_AAN_##B), \
  JPEG_FTOFIX (JPEG_AAN_4 * JPEG_AAN_##B), \
  JPEG_FTOFIX (JPEG_AAN_5 * JPEG_AAN_##B), \
  JPEG_FTOFIX (JPEG_AAN_6 * JPEG_AAN_##B), \
  JPEG_FTOFIX (JPEG_AAN_7 * JPEG_AAN_##B)

/* The AA&N scaling factors.  These should be multiplied against quantization
 * coefficients to determine their real value.
 */
const JPEG_FIXED_TYPE JPEG_AANScaleFactor [JPEG_DCTSIZE2] =
{
  JPEG_AAN_LINE (0),
  JPEG_AAN_LINE (1),
  JPEG_AAN_LINE (2),
  JPEG_AAN_LINE (3),
  JPEG_AAN_LINE (4),
  JPEG_AAN_LINE (5),
  JPEG_AAN_LINE (6),
  JPEG_AAN_LINE (7),
};

/* This converts values in the range [-32 .. 32] to [0 .. 32] by clamping
 * values outside of that range.  To use it, add 32 to your input.
 */
const unsigned char JPEG_ComponentRange [32 * 3] =
{
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
  19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
  31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31
};

/* Compute the columns half of the IDCT. */
static void JPEG_IDCT_Columns (JPEG_FIXED_TYPE *zz)
{
  JPEG_FIXED_TYPE tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10, tmp11;
  JPEG_FIXED_TYPE *ez = zz + JPEG_DCTSIZE;

  /* The first column will always have a non-zero coefficient, the DC. */
  goto skipFirstCheckb;
  
  for ( ; zz < ez; zz ++)
  {
    /* A column containing only zeroes will output only zeroes.  Since we
     * output in-place, we don't need to do anything in that case.
     */
    if (!zz [0 * JPEG_DCTSIZE] && !zz [1 * JPEG_DCTSIZE]
    && !zz [2 * JPEG_DCTSIZE] && !zz [3 * JPEG_DCTSIZE]
    && !zz [4 * JPEG_DCTSIZE] && !zz [5 * JPEG_DCTSIZE]
    && !zz [6 * JPEG_DCTSIZE] && !zz [7 * JPEG_DCTSIZE])
      continue;
    
  skipFirstCheckb:
    tmp0 = zz [0 * JPEG_DCTSIZE];
    tmp1 = zz [2 * JPEG_DCTSIZE];
    tmp2 = zz [4 * JPEG_DCTSIZE];
    tmp3 = zz [6 * JPEG_DCTSIZE];
    
    tmp6 = tmp1 + tmp3;
    tmp7 = JPEG_FIXMUL (tmp1 - tmp3, JPEG_FTOFIX (1.414213562)) - tmp6;
    tmp1 = tmp0 - tmp2 + tmp7;
    tmp0 = tmp0 + tmp2 + tmp6;
    
    tmp3 = tmp0 - (tmp6 << 1);
    tmp2 = tmp1 - (tmp7 << 1);
    
    tmp4 = zz [1 * JPEG_DCTSIZE];
    tmp5 = zz [3 * JPEG_DCTSIZE];
    tmp6 = zz [5 * JPEG_DCTSIZE];
    tmp7 = zz [7 * JPEG_DCTSIZE];
    
    tmp10 = tmp4 - tmp7;
    
    tmp8 = tmp6 + tmp5;
    tmp9 = tmp4 + tmp7;
    tmp7 = tmp9 + tmp8;
    tmp11 = JPEG_FIXMUL (tmp9 - tmp8, JPEG_FTOFIX (1.414213562));
    
    tmp8 = tmp6 - tmp5;
    tmp9 = JPEG_FIXMUL (tmp8 + tmp10, JPEG_FTOFIX (1.847759065));
    
    tmp6 = JPEG_FIXMUL (JPEG_FTOFIX (-2.613125930), tmp8) + tmp9 - tmp7;
    tmp5 = tmp11 - tmp6;
    tmp4 = JPEG_FIXMUL (JPEG_FTOFIX (1.082392200), tmp10) - tmp9 + tmp5; 
    
    zz [0 * JPEG_DCTSIZE] = tmp0 + tmp7;
    zz [1 * JPEG_DCTSIZE] = tmp1 + tmp6;
    zz [2 * JPEG_DCTSIZE] = tmp2 + tmp5;
    zz [3 * JPEG_DCTSIZE] = tmp3 - tmp4;
    zz [4 * JPEG_DCTSIZE] = tmp3 + tmp4;
    zz [5 * JPEG_DCTSIZE] = tmp2 - tmp5;
    zz [6 * JPEG_DCTSIZE] = tmp1 - tmp6;
    zz [7 * JPEG_DCTSIZE] = tmp0 - tmp7;
  }
}
JPEG_FUNCTION_END (JPEG_IDCT_Columns)

/* Compute the rows half of the IDCT, loading the component information into
 * chunk as values in the range -64 to 64, although it can go somewhat outside
 * of that range.  chunkStride is the number of bytes in a row in chunk.
 */
 
static void JPEG_IDCT_Rows (const JPEG_FIXED_TYPE *zz, signed char *chunk, int chunkStride)
{
  JPEG_FIXED_TYPE tmp0, tmp1, tmp2, tmp3, tmp10, tmp11, tmp12, tmp13;
  JPEG_FIXED_TYPE tmp4, tmp5, tmp6, tmp7, z5, z10, z11, z12, z13;
  int row;
  
  for (row = 0; row < JPEG_DCTSIZE; row ++, zz += JPEG_DCTSIZE, chunk += chunkStride)
  {
    tmp10 = zz [0] + zz [4];
    tmp11 = zz [0] - zz [4];

    tmp13 = zz [2] + zz [6];
    tmp12 = JPEG_FIXMUL (zz [2] - zz [6], JPEG_FTOFIX (1.414213562)) - tmp13;

    tmp0 = tmp10 + tmp13;
    tmp3 = tmp10 - tmp13;
    tmp1 = tmp11 + tmp12;
    tmp2 = tmp11 - tmp12;
    
    z13 = zz [5] + zz [3];
    z10 = zz [5] - zz [3];
    z11 = zz [1] + zz [7];
    z12 = zz [1] - zz [7];

    tmp7 = z11 + z13;
    tmp11 = JPEG_FIXMUL (z11 - z13, JPEG_FTOFIX (1.414213562));

    z5 = JPEG_FIXMUL (z10 + z12, JPEG_FTOFIX (1.847759065));
    tmp10 = JPEG_FIXMUL (JPEG_FTOFIX (1.082392200), z12) - z5;
    tmp12 = JPEG_FIXMUL (JPEG_FTOFIX (-2.613125930), z10) + z5;
    
    tmp6 = tmp12 - tmp7;
    tmp5 = tmp11 - tmp6;
    tmp4 = tmp10 + tmp5;

    /* This shifts by an extra bit to remove the need for clamping at
     * this point.  Thus the normative samples are in the range -64 to 63.
     * This requires a later bit-shift, but that comes for free with the ARM
     * instruction set, and has an acceptable, likely imperceptible, loss
     * of quality.
     */
     
    chunk [0] = JPEG_FIXTOI (tmp0 + tmp7) >> 4;
    chunk [1] = JPEG_FIXTOI (tmp1 + tmp6) >> 4;
    chunk [2] = JPEG_FIXTOI (tmp2 + tmp5) >> 4;
    chunk [3] = JPEG_FIXTOI (tmp3 - tmp4) >> 4;
    chunk [4] = JPEG_FIXTOI (tmp3 + tmp4) >> 4;
    chunk [5] = JPEG_FIXTOI (tmp2 - tmp5) >> 4;
    chunk [6] = JPEG_FIXTOI (tmp1 - tmp6) >> 4;
    chunk [7] = JPEG_FIXTOI (tmp0 - tmp7) >> 4;
  }
}
JPEG_FUNCTION_END (JPEG_IDCT_Rows)

/* This function comes from jpeglib.  I feel all right about that since it comes from AA&N anyway. */
void JPEG_IDCT (JPEG_FIXED_TYPE *zz, signed char *chunk, int chunkStride)
{
  JPEG_IDCT_Columns (zz);
  JPEG_IDCT_Rows (zz, chunk, chunkStride);
}

/* Compute a signed value.  COUNT is the number of bits to read, and OUT is
 * where to store the result.
 */
 
#define JPEG_Value(COUNT, OUT) \
  do { \
    unsigned int value = JPEG_BITS_GET (COUNT); \
    \
    if (value < (unsigned int) (1 << ((unsigned int) (COUNT - 1)))) \
      value += (-1 << COUNT) + 1; \
    (OUT) = value; \
  } while (0)
 
/* Decode the coefficients from the input stream and do dequantization at the
 * same time.  dcLast is the previous block's DC value and is updated.  zz is
 * the output coefficients and will be all ready for an IDCT.  quant is the
 * quantization table to use, dcTable and acTable are the Huffman tables for
 * the DC and AC coefficients respectively, dataBase, bitsLeftBase, and
 * bitsDataBase are for input stream state, and toZigZag is a pointer to
 * JPEG_ToZigZag.
 */
 
static void JPEG_DecodeCoefficients (
  JPEG_FIXED_TYPE *dcLast, JPEG_FIXED_TYPE *zz, JPEG_FIXED_TYPE *quant,
  JPEG_HuffmanTable *dcTable, JPEG_HuffmanTable *acTable,
  const unsigned char **dataBase, unsigned int *bitsLeftBase,
  unsigned long int *bitsDataBase, const unsigned char *toZigZag)
{
  unsigned bits_left = *bitsLeftBase, bits_data = *bitsDataBase; /* Input stream state. */
  const unsigned char *data = *dataBase; /* Input stream state. */
  int r, s, diff; /* Various temporary data variables. */
  int index = 1; /* The current zig-zagged index. */
  
  /* Clear all coefficients to zero. */
  {
    JPEG_FIXED_TYPE *ez = zz + JPEG_DCTSIZE2;
    do *-- ez = 0;
    while (ez > zz);
  }
  
  /* Read the DC coefficient. */
  JPEG_BITS_CHECK ();
  JPEG_HuffmanTable_Decode (dcTable, s);
  JPEG_Value (s, diff);
  
  /* Store the DC coefficient. */
  *dcLast += diff;
  zz [toZigZag [0]] = *dcLast * quant [0];

  while (1)
  {
    /* Read a bits/run-length value. */
    JPEG_BITS_CHECK ();
    JPEG_HuffmanTable_Decode (acTable, s);
    r = s >> 4;
    s &= 15;
  
    /* If there is a value at this cell +r, then read it. */
    if (s)
    {
      index += r;
      JPEG_Value (s, r);
      zz [toZigZag [index]] = r * quant [index];
      if (index == JPEG_DCTSIZE2 - 1)
        break;
      index ++;
    }
    /* Otherwise we skip 16 cells or finish up. */
    else
    {
      if (r != 15)
        break;
      index += 16;
    }
  }
  
  /* Restore state for the caller. */
  *bitsDataBase = bits_data;
  *bitsLeftBase = bits_left;
  *dataBase = data;
}
JPEG_FUNCTION_END (JPEG_DecodeCoefficients)

/* Decode a Huffman table and initialize its data.  This expects to be called
 * after the DHT marker and the type/slot pair.
 */
int JPEG_HuffmanTable_Read (JPEG_HuffmanTable *huffmanTable, const unsigned char **dataBase)
{
  const unsigned char *data = *dataBase;
  const unsigned char *bits;
  int huffcode [256];
  unsigned char huffsize [256];
  int total = 0;
  int c;
  
  bits = data;
  for (c = 0; c < 16; c ++)
    total += *data ++;
  huffmanTable->huffval = data;
  data += total;
  
  /*void GenerateSizeTable ()*/
  {

⌨️ 快捷键说明

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