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

📄 jpegtobmp.cpp

📁 一个可用于多平台的旋转180度 jpg的源代码。大家可以进一步的改进。
💻 CPP
📖 第 1 页 / 共 3 页
字号:
// JpegToBmp.cpp : Defines the entry point for the console application.
//
//#ifndef __JPEGDEC_H__
//#define __JPEGDEC_H__ 


//#include "stdafx.h"


////////////////////////////////////////////////


//BEGINNING OF HEADERS


/////////////////////////////////////////////////









#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "Coding.h"

#define BYTE unsigned char
#define WORD unsigned short int

#define DWORD unsigned int
#define SDWORD signed int

#define SBYTE signed char
#define SWORD signed short int

int load_JPEG_header(FILE *fp, DWORD *X_image, DWORD *Y_image);
void decode_JPEG_image();
int get_JPEG_buffer(WORD X_image,WORD Y_image, BYTE **address_dest_buffer);

//#endif




/////////////////////////////////////////////////
//BEGINNING OF PROGRAM SOURCE
//////////////////////////////////////////////////








//#include "jpegdec.h"
#include <conio.h>
#include <time.h>
char *FileName="E:\\Documents and Settings\\hong\\桌面\\13.jpg";
extern char error_string[90];

typedef struct s_BM_header {
			  WORD BMP_id ; // 'B''M'
			  DWORD size; // size in bytes of the BMP file
			  DWORD zero_res; // 0
			  DWORD offbits; // 54
			  DWORD biSize; // 0x28
			  DWORD Width;  // X
			  DWORD Height;  // Y
			  WORD  biPlanes; // 1
			  WORD  biBitCount ; // 24
			  DWORD biCompression; // 0 = BI_RGB
			  DWORD biSizeImage; // 0
			  DWORD biXPelsPerMeter; // 0xB40
			  DWORD biYPelsPerMeter; // 0xB40
			  DWORD biClrUsed; //0
			  DWORD biClrImportant; //0
} BM_header;
typedef struct s_RGB {
			 BYTE B;
		     BYTE G;
		     BYTE R;
} RGB;

void exitmessage(char *message)
{
 printf("%s\n",message);exit(0);
}

void write_buf_to_BMP(BYTE *im_buffer, WORD X_bitmap, WORD Y_bitmap, char *BMPname)
{
 SWORD x,y;
 RGB *pixel;
 BM_header BH;
 FILE *fp_bitmap;
 DWORD im_loc_bytes;
 BYTE nr_fillingbytes, i;
 BYTE zero_byte=0;

 fp_bitmap=fopen(BMPname,"wb");
 if (fp_bitmap==NULL) exitmessage("File cannot be created");

 if (X_bitmap%4!=0) nr_fillingbytes=4-((X_bitmap*3L)%4);
  else nr_fillingbytes=0;

 BH.BMP_id = 'M'*256+'B';     fwrite(&BH.BMP_id,2,1,fp_bitmap);
 BH.size=54+Y_bitmap*(X_bitmap*3+nr_fillingbytes);fwrite(&BH.size,4,1,fp_bitmap);
 BH.zero_res = 0;             fwrite(&BH.zero_res,4,1,fp_bitmap);
 BH.offbits = 54;             fwrite(&BH.offbits,4,1,fp_bitmap);
 BH.biSize = 0x28;            fwrite(&BH.biSize,4,1,fp_bitmap);
 BH.Width = X_bitmap;	      fwrite(&BH.Width,4,1,fp_bitmap);
 BH.Height = Y_bitmap;	      fwrite(&BH.Height,4,1,fp_bitmap);
 BH.biPlanes = 1;             fwrite(&BH.biPlanes,2,1,fp_bitmap);
 BH.biBitCount = 24;          fwrite(&BH.biBitCount,2,1,fp_bitmap);
 BH.biCompression = 0;        fwrite(&BH.biCompression,4,1,fp_bitmap);
 BH.biSizeImage = 0;          fwrite(&BH.biSizeImage,4,1,fp_bitmap);
 BH.biXPelsPerMeter = 0xB40;  fwrite(&BH.biXPelsPerMeter,4,1,fp_bitmap);
 BH.biYPelsPerMeter = 0xB40;  fwrite(&BH.biYPelsPerMeter,4,1,fp_bitmap);
 BH.biClrUsed = 0;	          fwrite(&BH.biClrUsed,4,1,fp_bitmap);
 BH.biClrImportant = 0;	      fwrite(&BH.biClrImportant,4,1,fp_bitmap);

 printf("Writing bitmap ...\n");
 im_loc_bytes=(DWORD)im_buffer+((DWORD)Y_bitmap-1)*X_bitmap*4;

 for (y=0;y<Y_bitmap;y++)
  {
   for (x=0;x<X_bitmap;x++)
	{
	 pixel=(RGB *)im_loc_bytes;
	 fwrite(pixel, 3, 1, fp_bitmap);
	 im_loc_bytes+=4;
	}
   for (i=0;i<nr_fillingbytes;i++)
	   fwrite(&zero_byte,1,1,fp_bitmap);
   im_loc_bytes-=2L*X_bitmap*4;
  }
 printf("Done.\n");
 fclose(fp_bitmap);
}



void Swap(BYTE *cm1, BYTE *cm2)
{
	BYTE kt;
	kt=*cm1;
	*cm1=*cm2;
	*cm2=kt;

}

//旋转180度的效果图片,不需要额外的内存空间 ,交换对称位置即可。
void ImageFlipC(BYTE *Begin,int m_height,int m_width)
{

 
	   int i,j;


		for( i=0 ; i < m_height;i++)
		{
			for(j=0; j < m_width/2;j++)
			{

				Swap(Begin+ (i*m_width+j)*4,Begin+(m_width*m_height)*4-(i*m_width+j)*4-4);
				Swap(Begin+ (i*m_width+j)*4+1,Begin+(m_width*m_height)*4-(i*m_width+j)*4-3);
				Swap(Begin+ (i*m_width+j)*4+2,Begin+(m_width*m_height)*4-(i*m_width+j)*4-2);				
				Swap(Begin+ (i*m_width+j)*4+3,Begin+(m_width*m_height)*4-(i*m_width+j)*4-1);
			}
		}


   
}

void main(int argc, char *argv[])
{
 FILE *fp;
 DWORD X_image, Y_image;
 BYTE *our_image_buffer;
 clock_t start_time, finish_time;
 float duration;

 if (argc<=1) fp=fopen(FileName,"rb");
  else fp=fopen(argv[1],"rb");
 if (fp==NULL) exitmessage("File not found ?");
 if (!load_JPEG_header(fp,&X_image,&Y_image)) {exitmessage(error_string);return;}
 fclose(fp);

 printf(" X_image = %d\n",X_image);
 printf(" Y_image = %d\n",Y_image);



 printf("Decoding JPEG image...\n");
 // main decoder
 start_time = clock();
 decode_JPEG_image();
 printf("Decoding finished.\n");
 
 finish_time = clock();
 duration = (double)(finish_time - start_time) / CLK_TCK;


 if (!get_JPEG_buffer(X_image,Y_image,&our_image_buffer)) {exitmessage(error_string);return;}
	ImageFlipC(our_image_buffer,X_image,Y_image);	
 
 write_buf_to_BMP(our_image_buffer,X_image,Y_image, "E:\\Documents and Settings\\hong\\桌面\\32.bmp");


 	EncodeToFile(FileName, "E:\\Documents and Settings\\hong\\桌面\\32.bmp", 100);

	
 printf( "Time elapsed: %2.1f seconds\n", duration );
 getch();
}



////////////////////////////////////////////////
//DECODER SOURCE CODE
////////////////////////////////////////////////














// JPEG decoder module
// Copyright 1999 Cristi Cuturicu


//#include "jpegdec.h"
// Used markers:
#define SOI 0xD8
#define EOI 0xD9
#define APP0 0xE0
#define SOF 0xC0
#define DQT 0xDB
#define DHT 0xC4
#define SOS 0xDA
#define DRI 0xDD
#define COM 0xFE

char error_string[90];
#define exit_func(err) { strcpy(error_string, err); return 0;}

static BYTE *buf; // the buffer we use for storing the entire JPG file

static BYTE bp; //current byte
static WORD wp; //current word

static DWORD byte_pos; // current byte position
#define BYTE_p(i) bp=buf[(i)++]
#define WORD_p(i) wp=(((WORD)(buf[(i)]))<<8) + buf[(i)+1]; (i)+=2

// WORD X_image_size,Y_image_size; // X,Y sizes of the image
static WORD X_round,Y_round; // The dimensions rounded to multiple of Hmax*8 (X_round)
			  // and Ymax*8 (Y_round)

static BYTE *im_buffer; // RGBA image buffer
static DWORD X_image_bytes; // size in bytes of 1 line of the image = X_round * 4
static DWORD y_inc_value ; // 32*X_round; // used by decode_MCU_1x2,2x1,2x2

BYTE YH,YV,CbH,CbV,CrH,CrV; // sampling factors (horizontal and vertical) for Y,Cb,Cr
static WORD Hmax,Vmax;


static BYTE zigzag[64]={ 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 };
typedef struct {
   BYTE Length[17];  // k =1-16 ; L[k] indicates the number of Huffman codes of length k
   WORD minor_code[17];  // indicates the value of the smallest Huffman code of length k
   WORD major_code[17];  // similar, but the highest code
   BYTE V[65536];  // V[k][j] = Value associated to the j-th Huffman code of length k
	// High nibble = nr of previous 0 coefficients
	// Low nibble = size (in bits) of the coefficient which will be taken from the data stream
} Huffman_table;

static float *QT[4]; // quantization tables, no more than 4 quantization tables (QT[0..3])
static Huffman_table HTDC[4]; //DC huffman tables , no more than 4 (0..3)
static Huffman_table HTAC[4]; //AC huffman tables                  (0..3)

static BYTE YQ_nr,CbQ_nr,CrQ_nr; // quantization table number for Y, Cb, Cr
static BYTE YDC_nr,CbDC_nr,CrDC_nr; // DC Huffman table number for Y,Cb, Cr
static BYTE YAC_nr,CbAC_nr,CrAC_nr; // AC Huffman table number for Y,Cb, Cr

static BYTE Restart_markers; // if 1 => Restart markers on , 0 => no restart markers
static WORD MCU_restart; //Restart markers appears every MCU_restart MCU blocks
typedef void (*decode_MCU_func)(DWORD);


static SWORD DCY, DCCb, DCCr; // Coeficientii DC pentru Y,Cb,Cr
static SWORD DCT_coeff[64]; // Current DCT_coefficients
static BYTE Y[64],Cb[64],Cr[64]; // Y, Cb, Cr of the current 8x8 block for the 1x1 case
static BYTE Y_1[64],Y_2[64],Y_3[64],Y_4[64];
static BYTE tab_1[64],tab_2[64],tab_3[64],tab_4[64]; // tabelele de supraesantionare pt cele 4 blocuri

static SWORD Cr_tab[256],Cb_tab[256]; // Precalculated Cr, Cb tables
static SWORD Cr_Cb_green_tab[65536];

// Initial conditions:
// byte_pos = start position in the Huffman coded segment
// WORD_get(w1); WORD_get(w2);wordval=w1;

static BYTE d_k=0;  // Bit displacement in memory, relative to the offset of w1
			 // it's always <16
static WORD w1,w2; // w1 = First word in memory; w2 = Second word
static DWORD wordval ; // the actual used value in Huffman decoding.
static DWORD mask[17];
static SWORD neg_pow2[17]={0,-1,-3,-7,-15,-31,-63,-127,-255,-511,-1023,-2047,-4095,-8191,-16383,-32767};
static DWORD start_neg_pow2=(DWORD)neg_pow2;

static int shift_temp;
#define RIGHT_SHIFT(x,shft)  \
	((shift_temp = (x)) < 0 ? \
	 (shift_temp >> (shft)) | ((~(0L)) << (32-(shft))) : \
	 (shift_temp >> (shft)))
#define DESCALE(x,n)  RIGHT_SHIFT((x) + (1L << ((n)-1)), n)
#define RANGE_MASK 1023L
static BYTE *rlimit_table;
void prepare_range_limit_table()
/* Allocate and fill in the sample_range_limit table */
{
  int j;
  rlimit_table = (BYTE *)malloc(5 * 256L + 128) ;
  /* First segment of "simple" table: limit[x] = 0 for x < 0 */
  memset((void *)rlimit_table,0,256);
  rlimit_table += 256;	/* allow negative subscripts of simple table */
  /* Main part of "simple" table: limit[x] = x */
  for (j = 0; j < 256; j++) rlimit_table[j] = j;
  /* End of simple table, rest of first half of post-IDCT table */
  for (j = 256; j < 640; j++) rlimit_table[j] = 255;
  /* Second half of post-IDCT table */
  memset((void *)(rlimit_table + 640),0,384);
  for (j = 0; j < 128 ; j++) rlimit_table[j+1024] = j;
}

#ifdef _MSC_VER
WORD lookKbits(BYTE k)
{
 _asm {
	 mov dl, k
	 mov cl, 16
	 sub cl, dl
	 mov eax, [wordval]
	 shr eax, cl
 }
}

WORD WORD_hi_lo(BYTE byte_high,BYTE byte_low)
{
	_asm {
		  mov ah,byte_high
		  mov al,byte_low
		 }
}
SWORD get_svalue(BYTE k)
// k>0 always
// Takes k bits out of the BIT stream (wordval), and makes them a signed value
{
	_asm {
		   xor ecx, ecx
		   mov cl,k
		   mov eax,[wordval]
		   shl eax,cl
		   shr eax, 16
		   dec cl
		   bt eax,ecx
		   jc end_macro
	signed_value:inc cl
		   mov ebx,[start_neg_pow2]
		   add ax,word ptr [ebx+ecx*2]
		 end_macro:
	}
}
#endif

#ifdef __WATCOMC__

WORD lookKbits(BYTE k);

⌨️ 快捷键说明

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