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

📄 jutility.c

📁 这是一个JPEG编码源代码
💻 C
字号:
#include <math.h>
#include <stdio.h>

#include <assert.h>

#include "jinclude.h"

void 
jutl_zigzag_sort (DCTBLOCK dptr) {
  int i;
  static const char zz_order[DCTSIZE2] = {
     0,  1,  5,  6, 14, 15, 27, 28,
     2,  4,  7, 13, 16, 26, 29, 42,
     3,  8, 12, 17, 25, 30, 41, 43,
     9, 11, 18, 24, 31, 40, 44, 53,
    10, 19, 23, 32, 39, 45, 52, 54,
    20, 22, 33, 38, 46, 51, 55, 60,
    21, 34, 37, 47, 50, 56, 59, 61,
    35, 36, 48, 49, 57, 58, 62, 63
  };
  DCTBLOCK dtmp;
  memcpy(&dtmp, dptr, sizeof(DCTBLOCK));
  for (i=0; i<DCTSIZE2; i++) {
    dptr[zz_order[i]] = dtmp[i];
  }
}

void
jutl_cc_rgb2ycc(JSAMPLE *data, int num) {
  int i;
  float a, b, c;
  struct three_component_color {
    JSAMPLE a, b, c;
  } *pcolor;

  pcolor = (struct three_component_color *)data;
  for (i=0; i<num; i++) {
    a = pcolor[i].a;
    b = pcolor[i].b;
    c = pcolor[i].c;
    /* Y  color */
    pcolor[i].a = ( 0.29900f * a + 0.58700f * b + 0.11400f * c);
    /* Cb color */
    pcolor[i].b = (-0.16874f * a - 0.33126f * b + 0.50000f * c + 128); 
    /* Cr color */
    pcolor[i].c = ( 0.50000f * a - 0.41869f * b - 0.08131f * c + 128); 
  }
}

/* Read a microsoft windows bitmap image file, and return a JSAMPLE-ARRAY.
 * WARNING!!!
 *   DON'T forget free the JSAMPLE-ARRAY when you exit.
 */
JSAMPLE * 
jutl_read_bitmap(FILE *src, int *width, int *height)
{
  UINT16 bfType;
  struct BITMAPHEADER {
    UINT32 bfSize;
    UINT16 bfRes1;
    UINT16 bfRes2;
    UINT32 bfOffBits;
    UINT32 biSize;
    UINT32 biWidth;
    UINT32 biHeight;
    UINT16 biPlanes;
    UINT16 biBitCount;
    UINT32 biCompression;
    UINT32 biSizeImage;
    UINT32 biXPeIsPerMeter;
    UINT32 biYPeIsPerMeter;
    UINT32 biClrUsed;
    UINT32 biClrImportant;
  } bmphead;
  JSAMPLE *tar;
  int i, pixnum, pos, loop;
 
  fread(&bfType, sizeof(bfType), 1, src);
  if (bfType != 0x4D42) {
    printf("error in BM\n");
    exit(1);
  }
  fread(&bmphead, sizeof(bmphead), 1, src);  
  if (bmphead.biBitCount != 24) {
    printf("error in BitCount\n");
    exit(1);
  }
  if (bmphead.biCompression != 0) {
    printf("error in Compress\n");
    exit(1);
  }
  loop = bmphead.biWidth % 4;
  pixnum = bmphead.biWidth*bmphead.biHeight;
  tar = (JSAMPLE *)malloc(sizeof(JSAMPLE)*3*pixnum);
  pos = pixnum - bmphead.biWidth; 
  for (i=0; i<pixnum; i++) {
    UINT8 b, g, r;
    fread(&b, sizeof(b), 1, src);
    fread(&g, sizeof(g), 1, src);
    fread(&r, sizeof(r), 1, src);
    tar[pos*3+0] = r;
    tar[pos*3+1] = g;
    tar[pos*3+2] = b;
    pos++;
    if (pos%bmphead.biWidth == 0) {
      int j;
      UINT8 t;
		pos -= 2*bmphead.biWidth;
      /* 这里很奇怪,BMP文件宽度必须是4的倍数如果不是,填充0
       */
      
      for (j=0; j<loop; j++)
        fread(&t, sizeof(t), 1, src);
    }
  }
  *width  = bmphead.biWidth;
  *height = bmphead.biHeight;

  
 
  
  return tar;
}

void 
jutl_write_byte(UINT8 c, FILE *f) {
  assert(f);
  fwrite(&c, sizeof(UINT8), 1, f);
}

⌨️ 快捷键说明

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