搜索
bottom↓
回复: 5

帮忙看下如何快速实现灰阶图像转Jpg格式。

[复制链接]

出25入84汤圆

发表于 2018-11-14 13:35:33 | 显示全部楼层 |阅读模式
灰阶图像大小为400*400
每个像素点是8bit。

想请教各位:如何将这样的data转换成jpg格式,而且速度要快,我自己做的,大概需要几秒钟才可以转换一幅。
代码如下:
  1. static struct APP0infotype {
  2.         unsigned short int marker;// = 0xFFE0
  3.         unsigned short int length; // = 16 for usual JPEG, no thumbnail
  4.         unsigned char JFIFsignature[5]; // = "JFIF",'\0'
  5.         unsigned char versionhi; // 1
  6.         unsigned char versionlo; // 1
  7.         unsigned char xyunits;   // 0 = no units, normal density
  8.         unsigned short int xdensity;  // 1
  9.         unsigned short int ydensity;  // 1
  10.         unsigned char thumbnwidth; // 0
  11.         unsigned char thumbnheight; // 0
  12. }APP0info = {0xFFE0,16,'J','F','I','F',0,1,1,0,1,1,0,0};

  13. static struct  SOF0infotype {
  14.         unsigned short int marker; // = 0xFFC0
  15.         unsigned short int length; // = 17 for a truecolor YCbCr JPG
  16.         unsigned char precision ;// Should be 8: 8 bits/sample
  17.         unsigned short int height ;
  18.         unsigned short int width;
  19.         unsigned char nrofcomponents;//Should be 3: We encode a truecolor JPG
  20.         unsigned char IdY;  // = 1
  21.         unsigned char HVY; // sampling factors for Y (bit 0-3 vert., 4-7 hor.)
  22.         unsigned char QTY;  // Quantization Table number for Y = 0
  23.         unsigned char IdCb; // = 2
  24.         unsigned char HVCb;
  25.         unsigned char QTCb; // 1
  26.         unsigned char IdCr; // = 3
  27.         unsigned char HVCr;
  28.         unsigned char QTCr; // Normally equal to QTCb = 1
  29. }SOF0info = { 0xFFC0,17,8,0,0,3,1,0x11,0,2,0x11,1,3,0x11,1};
  30. // Default sampling factors are 1,1 for every image component: No downsampling


  31. //DQT说明:Define Quantization Table
  32. static struct DQTinfotype
  33. {
  34.     unsigned short int marker;  // = 0xFFDB
  35.     unsigned short int length;  // = 132
  36.     unsigned char QTYinfo;// = 0:  bit 0..3: number of QT = 0 (table for Y)
  37.                   //       bit 4..7: precision of QT, 0 = 8 bit
  38.     unsigned char Ytable[64];
  39.     unsigned char QTCbinfo; // = 1 (quantization table for Cb,Cr)
  40.     unsigned char Cbtable[64];
  41. }DQTinfo;

  42. // Ytable from DQTinfo should be equal to a scaled and zizag reordered version
  43. // of the table which can be found in "tables.h": std_luminance_qt
  44. // Cbtable , similar = std_chrominance_qt
  45. // We'll init them in the program using set_DQTinfo function
  46. //说明DHT:Define Huffman Table
  47. /*
  48.                                             哈夫曼编码表说明(是 "熵编码" 的一种形式)
  49.     哈夫曼(Huffman)编码是一种常用的压缩编码方法,是Huffman于1952年为压缩文本文件建立的。它的基本原理是频繁使用的数据用较短的代码
  50. 代替,较少使用的数据用较长的代码代替,每个数据的代码各不相同。这些代码都是二进制码,且码的长度是可变的。举个例子:假设一个文件中
  51. 出现了8种符号S0,S1,S2,S3,S4,S5,S6,S7,那么每种符号要编码,至少需要3比特。假设编码成000,001,010,011,100,101,110,111(称做码字)。
  52. 那么符号序列S0S1S7S0S1S6S2S2S3S4S5S0S0S1编码后变成000001111000001110010010011100101000000001,共用了42比特。我们发现S0,S1,S2
  53. 这三个符号出现的频率比较大,其它符号出现的频率比较小,如果我们采用一种编码方案使得S0,S1,S2的码字短,其它符号的码字长,这样就
  54. 能够减少占用的比特数。例如,我们采用这样的编码方案:S0到S7的码字分别01,11,101,0000,0001,0010,0011,100,那么上述符号序列变成
  55. 011110001110011101101000000010010010111,共用了39比特,尽管有些码字如S3,S4,S5,S6变长了(由3位变成4位),但使用频繁的几个码字
  56. 如S0,S1变短了,所以实现了压缩。
  57.     上述的编码是如何得到的呢?随意乱写是不行的。编码必须保证不能出现一个码字和另一个的前几位相同的情况,比如说,如果S0的码字为01,
  58. S2的码字为011,那么当序列中出现011时,你不知道是S0的码字后面跟了个1,还是完整的一个S2的码字。我们给出的编码能够保证这一点。

  59. 下面给出具体的Huffman编码算法
  60. (1)首先统计出每个符号出现的频率,上例S0到S7的出现频率分别为4/14,3/14,2/14,1/14,1/14,1/14,1/14,1/14
  61. (2)从左到右把上述频率按从小到大的顺序排列
  62. (3)每一次选出最小的两个值,作为二叉树的两个叶子节点,将和作为它们的根节点,这两个叶子节点不再参与比较,新的根节点参与比较
  63. (4)重复(3),直到最后得到和为1的根节点
  64. (5)将形成的二叉树的左节点标0,右节点标1。把从最上面的根节点到最下面的叶子节点途中遇到的0,1序列串起来,就得到了各个符号的编码

  65.     产生Huffman编码需要对原始数据扫描两遍。第一遍扫描要精确地统计出原始数据中,每个值出现的频率,第二遍是建立Huffman树并进行编码。
  66. 由于需要建立二叉树并遍历二叉树生成编码,因此数据压缩和还原速度都较慢,但简单有效,因而得到广泛的应用

  67. */
  68. static struct DHTinfotype
  69. {
  70.     unsigned short int marker;                // = 0xFFC4
  71.     unsigned short int length;                //0x01A2
  72.     unsigned char HTYDCinfo;             //bit 0..3: number of HT (0..3), for Y =0
  73.                                 //bit 4  :type of HT, 0 = DC table,1 = AC table
  74.                                 //bit 5..7: not used, must be 0
  75.     unsigned char YDC_nrcodes[16];       //at index i = nr of codes with length i
  76.     unsigned char YDC_values[12];
  77.     unsigned char HTYACinfo;             // = 0x10
  78.     unsigned char YAC_nrcodes[16];
  79.     unsigned char YAC_values[162];       //we'll use the standard Huffman tables
  80.     unsigned char HTCbDCinfo;            // = 1
  81.     unsigned char CbDC_nrcodes[16];
  82.     unsigned char CbDC_values[12];
  83.     unsigned char HTCbACinfo;            // = 0x11
  84.     unsigned char CbAC_nrcodes[16];
  85.     unsigned char CbAC_values[162];
  86. }DHTinfo;

  87. static struct SOSinfotype {
  88.          unsigned short int marker;  // = 0xFFDA
  89.          unsigned short int length; // = 12
  90.          unsigned char nrofcomponents; // Should be 3: truecolor JPG
  91.          unsigned char IdY; //1
  92.          unsigned char HTY; //0 // bits 0..3: AC table (0..3)
  93.                    // bits 4..7: DC table (0..3)
  94.          unsigned char IdCb; //2
  95.          unsigned char HTCb; //0x11
  96.          unsigned char IdCr; //3
  97.          unsigned char HTCr; //0x11
  98.          unsigned char Ss,Se,Bf; // not interesting, they should be 0,63,0
  99. }SOSinfo={0xFFDA,12,3,1,0,2,0x11,3,0x11,0,0x3F,0};

  100. typedef struct
  101. {
  102.     unsigned char B,G,R;
  103. }colorRGB;

  104. typedef struct
  105. {
  106.     unsigned char length;
  107.     unsigned short int value;
  108. }bitstring;

  109. //extern unsigned char all_jpeg_data[IMAGE_WIDE*IMAGE_HIGH];
  110. extern unsigned int all_jpeg_data_length;
  111. unsigned int data2jpg(int image_w,int image_h,unsigned char *ImageData);
复制代码

  1. //通过查表,将RGB变成YCrBr
  2. //因为进行DCT变换的数据必须在-128 -- 127之间,所以要减掉128;但从公式可见,只有R不是范围内,所以只有R减128
  3. #define  Y(R,G,B) ((unsigned char)( (YRtab[(R)]+YGtab[(G)]+YBtab[(B)])>>16 ) - 128)
  4. #define Cb(R,G,B) ((unsigned char)( (CbRtab[(R)]+CbGtab[(G)]+CbBtab[(B)])>>16 ))
  5. #define Cr(R,G,B) ((unsigned char)( (CrRtab[(R)]+CrGtab[(G)]+CrBtab[(B)])>>16 ))

  6. #define writeword(w) writebyte((w)/256);writebyte((w)%256);

  7. static unsigned char bytenew=0; // The byte that will be written in the JPG file
  8. static signed char bytepos=7; //bit position in the byte we write (bytenew)
  9. static unsigned short int mask[16]={1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768};

  10. //下面是我们要用到的哈夫曼表数据(两个直流分量,两个交流分量)
  11. static bitstring YDC_HT[12];
  12. static bitstring CbDC_HT[12];
  13. static bitstring YAC_HT[256];
  14. static bitstring CbAC_HT[256];

  15. static unsigned char *category_alloc;
  16. static unsigned char *category;              //Here we'll keep the category of the numbers in range: -32767..32767
  17. static bitstring *bitcode_alloc;
  18. static bitstring *bitcode;          // their bitcoded representation

  19. //Precalculated tables for a faster YCbCr->RGB transformation
  20. // We use a signed long int table because we'll scale values by 2^16 and work with integers
  21. static signed long int YRtab[256],YGtab[256],YBtab[256];
  22. static signed long int CbRtab[256],CbGtab[256],CbBtab[256];
  23. static signed long int CrRtab[256],CrGtab[256],CrBtab[256];
  24. static float fdtbl_Y[64];
  25. static float fdtbl_Cb[64]; //the same with the fdtbl_Cr[64]

  26. colorRGB *RGB_buffer; //image to be encoded
  27. unsigned short int Ximage,Yimage;// image dimensions divisible by 8
  28. static signed char YDU[64]; // This is the Data Unit of Y after YCbCr->RGB transformation
  29. static signed char CbDU[64];
  30. static signed char CrDU[64];
  31. static signed short int DU_DCT[64]; // Current DU (after DCT and quantization) which we'll zigzag
  32. static signed short int DU[64]; //zigzag reordered DU which will be Huffman coded

  33. //说明:按'之'字形量化DCT系数的序号,可以增加连续的0系数的个数
  34. static unsigned char zigzag[64] =
  35. {
  36.     0, 1, 5, 6,14,15,27,28,
  37.     2, 4, 7,13,16,26,29,42,
  38.     3, 8,12,17,25,30,41,43,
  39.     9,11,18,24,31,40,44,53,
  40.     10,19,23,32,39,45,52,54,
  41.     20,22,33,38,46,51,55,60,
  42.     21,34,37,47,50,56,59,61,
  43.     35,36,48,49,57,58,62,63
  44. };

  45. //说明:下面是亮度与色度的量化表,本量化表经测试在电视图像是最佳的(除二后也可得到不错的效果)
  46. //luminance:亮度      chrominance:色度
  47. static unsigned char std_luminance_qt[64] =
  48. {
  49.     16,  11,  10,  16,  24,  40,  51,  61,
  50.     12,  12,  14,  19,  26,  58,  60,  55,
  51.     14,  13,  16,  24,  40,  57,  69,  56,
  52.     14,  17,  22,  29,  51,  87,  80,  62,
  53.     18,  22,  37,  56,  68, 109, 103,  77,
  54.     24,  35,  55,  64,  81, 104, 113,  92,
  55.     49,  64,  78,  87, 103, 121, 120, 101,
  56.     72,  92,  95,  98, 112, 100, 103,  99
  57. };
  58. static unsigned char std_chrominance_qt[64] =
  59. {
  60.     17,  18,  24,  47,  99,  99,  99,  99,
  61.     18,  21,  26,  66,  99,  99,  99,  99,
  62.     24,  26,  56,  99,  99,  99,  99,  99,
  63.     47,  66,  99,  99,  99,  99,  99,  99,
  64.     99,  99,  99,  99,  99,  99,  99,  99,
  65.     99,  99,  99,  99,  99,  99,  99,  99,
  66.     99,  99,  99,  99,  99,  99,  99,  99,
  67.     99,  99,  99,  99,  99,  99,  99,  99
  68. };
  69. //Standard Huffman tables (cf. JPEG standard section K.3) */

  70. //直流系数的编码(固定值)
  71. static unsigned char std_dc_luminance_nrcodes[17] = {0, 0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0};
  72. static unsigned char std_dc_luminance_values[12]  = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};

  73. static unsigned char std_dc_chrominance_nrcodes[17] = {0,0,3,1,1,1,1,1,1,1,1,1,0,0,0,0,0};
  74. static unsigned char std_dc_chrominance_values[12]  = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};

  75. //交流系数的编码(固定值)
  76. static unsigned char std_ac_luminance_nrcodes[17] = {0,0,2,1,3,3,2,4,3,5,5,4,4,0,0,1,0x7d};
  77. static unsigned char std_ac_luminance_values[162] =
  78. {
  79.       0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12,
  80.       0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07,
  81.       0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08,
  82.       0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0,
  83.       0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16,
  84.       0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28,
  85.       0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
  86.       0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
  87.       0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
  88.       0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
  89.       0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
  90.       0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
  91.       0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
  92.       0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
  93.       0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6,
  94.       0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5,
  95.       0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4,
  96.       0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2,
  97.       0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea,
  98.       0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
  99.       0xf9, 0xfa
  100. };

  101. static unsigned char std_ac_chrominance_nrcodes[17] = {0,0,2,1,2,4,4,3,4,7,5,4,4,0,1,2,0x77};
  102. static unsigned char std_ac_chrominance_values[162] =
  103. {
  104.       0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21,
  105.       0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71,
  106.       0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91,
  107.       0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0,
  108.       0x15, 0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34,
  109.       0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26,
  110.       0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38,
  111.       0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
  112.       0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
  113.       0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
  114.       0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
  115.       0x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
  116.       0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96,
  117.       0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5,
  118.       0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4,
  119.       0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3,
  120.       0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2,
  121.       0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda,
  122.       0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9,
  123.       0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
  124.       0xf9, 0xfa
  125. };

  126. #define QUA          50

  127. #define MAIN_BUFFER_LENGTH        358752
  128. #define CATEGORY_ALLOC        40000
  129. #define BITCODE_ALLOC        (40000*1)                                //长为65535*3
  130. #define SENDOR_SOURCE        (1280 * 1 * 8)                //sensor每次取8行数据,表示最多支持每行1280点,每点最多三字节
  131. #define SENDOR_DEST                (1280 * 1 * 8)                //sensor采样转后后数据,必须是24位的
  132. unsigned char g8main_bigbuffer[MAIN_BUFFER_LENGTH];
  133. unsigned char *gp8main_category = g8main_bigbuffer;
  134. bitstring *gp8main_bitcode  = (bitstring *)(g8main_bigbuffer + CATEGORY_ALLOC);
  135. unsigned char *gp8main_sensor_source = g8main_bigbuffer + CATEGORY_ALLOC + BITCODE_ALLOC;
  136. unsigned char *gp8main_sensor_dest   = g8main_bigbuffer + CATEGORY_ALLOC + BITCODE_ALLOC + SENDOR_SOURCE;

  137. unsigned char all_jpeg_data[500*400];
  138. unsigned int all_jpeg_data_length=0;

  139. unsigned int ImageWdith=0;
  140. unsigned int ImageHeight=0;
  141. unsigned long int bytes_perline;

  142. void writebyte(unsigned long int data)
  143. {
  144.         all_jpeg_data[all_jpeg_data_length] = data;
  145.         all_jpeg_data_length++;
  146. }

  147. //Nothing to overwrite for APP0info
  148. void write_APP0info()
  149. {
  150.         writeword(APP0info.marker);
  151.         writeword(APP0info.length);
  152.         writebyte('J');
  153.         writebyte('F');
  154.         writebyte('I');
  155.         writebyte('F');
  156.         writebyte(0);
  157.         writebyte(APP0info.versionhi);
  158.         writebyte(APP0info.versionlo);
  159.         writebyte(APP0info.xyunits);
  160.         writeword(APP0info.xdensity);
  161.         writeword(APP0info.ydensity);
  162.         writebyte(APP0info.thumbnwidth);
  163.         writebyte(APP0info.thumbnheight);
  164. }

  165. // We should overwrite width and height
  166. void write_SOF0info()
  167. {
  168.         writeword(SOF0info.marker);
  169.         writeword(SOF0info.length);
  170.         writebyte(SOF0info.precision);
  171.         writeword(SOF0info.height);
  172.         writeword(SOF0info.width);
  173.         writebyte(SOF0info.nrofcomponents);
  174.         writebyte(SOF0info.IdY);
  175.         writebyte(SOF0info.HVY);
  176.         writebyte(SOF0info.QTY);
  177.         writebyte(SOF0info.IdCb);
  178.         writebyte(SOF0info.HVCb);
  179.         writebyte(SOF0info.QTCb);
  180.         writebyte(SOF0info.IdCr);
  181.         writebyte(SOF0info.HVCr);
  182.         writebyte(SOF0info.QTCr);
  183. }

  184. void write_DQTinfo()
  185. {
  186.         unsigned char i;
  187.         writeword(DQTinfo.marker);
  188.         writeword(DQTinfo.length);
  189.         writebyte(DQTinfo.QTYinfo);
  190.         for (i=0;i<64;i++)
  191.                 writebyte(DQTinfo.Ytable[i]);
  192.         writebyte(DQTinfo.QTCbinfo);
  193.         for (i=0;i<64;i++)
  194.                 writebyte(DQTinfo.Cbtable[i]);
  195. }

  196. // Set quantization table and zigzag reorder it
  197. void set_quant_table(unsigned char *basic_table,unsigned char scale_factor,unsigned char *newtable)
  198. {
  199.         unsigned char i;
  200.         long temp;
  201.         for (i = 0; i < 64; i++)
  202.         {
  203.                 temp = ((long) basic_table[i] * scale_factor + 50L) / 100L;
  204.                 /* limit the values to the valid range */
  205.                 if (temp <= 0L)
  206.                         temp = 1L;
  207.                 if (temp > 255L)
  208.                         temp = 255L; /* limit to baseline range if requested */
  209.                 newtable[zigzag[i]] = (unsigned char) temp;
  210.         }
  211. }

  212. //DQT说明:Define Quantization Table
  213. //功能:初始化量化表
  214. void set_DQTinfo()
  215. {
  216.         unsigned char scalefactor = QUA;       
  217.         DQTinfo.marker=0xFFDB;
  218.         DQTinfo.length=132;
  219.         DQTinfo.QTYinfo=0;
  220.         DQTinfo.QTCbinfo=1;
  221.         set_quant_table(std_luminance_qt,scalefactor,DQTinfo.Ytable);                //将亮度表写入Y表
  222.         set_quant_table(std_chrominance_qt,scalefactor,DQTinfo.Cbtable);        //将色度表写入C表
  223. }

  224. //DHT说明:Define Huffman Table
  225. //功能:初始化哈夫曼表
  226. void write_DHTinfo()
  227. {
  228.         unsigned char i;
  229.         writeword(DHTinfo.marker);
  230.         writeword(DHTinfo.length);
  231.         writebyte(DHTinfo.HTYDCinfo);
  232.         for (i=0;i<16;i++)
  233.                 writebyte(DHTinfo.YDC_nrcodes[i]);
  234.         for (i=0;i<=11;i++)
  235.                 writebyte(DHTinfo.YDC_values[i]);
  236.         writebyte(DHTinfo.HTYACinfo);
  237.         for (i=0;i<16;i++)
  238.                 writebyte(DHTinfo.YAC_nrcodes[i]);
  239.         for (i=0;i<=161;i++)
  240.                 writebyte(DHTinfo.YAC_values[i]);
  241.         writebyte(DHTinfo.HTCbDCinfo);
  242.         for (i=0;i<16;i++)
  243.                 writebyte(DHTinfo.CbDC_nrcodes[i]);
  244.         for (i=0;i<=11;i++)
  245.                 writebyte(DHTinfo.CbDC_values[i]);
  246.         writebyte(DHTinfo.HTCbACinfo);
  247.         for (i=0;i<16;i++)
  248.                 writebyte(DHTinfo.CbAC_nrcodes[i]);
  249.         for (i=0;i<=161;i++)
  250.                 writebyte(DHTinfo.CbAC_values[i]);
  251. }


  252. //DHT:Define Huffman Table
  253. //说明:将表数据填入DHT数据结构
  254. void set_DHTinfo()
  255. {
  256.         unsigned char i;
  257.         DHTinfo.marker=0xFFC4;
  258.         DHTinfo.length=0x01A2;
  259.         DHTinfo.HTYDCinfo=0;
  260.         for (i=0; i<16; i++)
  261.                 DHTinfo.YDC_nrcodes[i] = std_dc_luminance_nrcodes[i+1];
  262.         for (i=0; i<=11; i++)
  263.                 DHTinfo.YDC_values[i] = std_dc_luminance_values[i];
  264.         DHTinfo.HTYACinfo = 0x10;
  265.         for (i=0; i<16; i++)
  266.                 DHTinfo.YAC_nrcodes[i] = std_ac_luminance_nrcodes[i+1];
  267.         for (i=0;i<=161;i++)
  268.                 DHTinfo.YAC_values[i] = std_ac_luminance_values[i];
  269.         DHTinfo.HTCbDCinfo = 1;
  270.         for (i=0;i<16;i++)
  271.                 DHTinfo.CbDC_nrcodes[i] = std_dc_chrominance_nrcodes[i+1];
  272.         for (i=0;i<=11;i++)
  273.                 DHTinfo.CbDC_values[i] = std_dc_chrominance_values[i];
  274.         DHTinfo.HTCbACinfo = 0x11;
  275.         for (i=0;i<16;i++)
  276.                 DHTinfo.CbAC_nrcodes[i] = std_ac_chrominance_nrcodes[i+1];
  277.         for (i=0; i<=161; i++)
  278.                 DHTinfo.CbAC_values[i] = std_ac_chrominance_values[i];
  279. }

  280. //Nothing to overwrite for SOSinfo
  281. //说明SOS:Start of Scan
  282. void write_SOSinfo()
  283. {
  284.         writeword(SOSinfo.marker);
  285.         writeword(SOSinfo.length);
  286.         writebyte(SOSinfo.nrofcomponents);
  287.         writebyte(SOSinfo.IdY);
  288.         writebyte(SOSinfo.HTY);
  289.         writebyte(SOSinfo.IdCb);
  290.         writebyte(SOSinfo.HTCb);
  291.         writebyte(SOSinfo.IdCr);
  292.         writebyte(SOSinfo.HTCr);
  293.         writebyte(SOSinfo.Ss);
  294.         writebyte(SOSinfo.Se);
  295.         writebyte(SOSinfo.Bf);
  296. }

  297. //在文件数据内加入注释的,可以不用
  298. void write_comment(char *comment)
  299. {
  300.         unsigned short int i,length;
  301.         writeword(0xFFFE); //The COM marker
  302.         length = strlen((const char *)comment);
  303.         writeword(length+2);
  304.         for (i=0; i<length; i++)
  305.                 writebyte(comment[i]);
  306. }

  307. // A portable version; it should be done in assembler
  308. void writebits(bitstring bs)
  309. {
  310.         unsigned short int value;
  311.         signed char posval;//bit position in the bitstring we read, should be<=15 and >=0
  312.         value=bs.value;
  313.         posval=bs.length-1;
  314.         while (posval>=0)
  315.         {
  316.                 if (value & mask[posval])
  317.                         bytenew|=mask[bytepos];
  318.                
  319.                 posval--;
  320.                 bytepos--;
  321.                 if (bytepos<0)
  322.                 {
  323.                         if (bytenew==0xFF)
  324.                         {
  325.                                 writebyte(0xFF);
  326.                                 writebyte(0);
  327.                         }
  328.                         else
  329.                         {
  330.                                 writebyte(bytenew);
  331.                         }
  332.                     bytepos=7;
  333.                         bytenew=0;
  334.                 }
  335.         }
  336. }

  337. //哈夫曼表的生成原理分析
  338. //用常量表就可以生成哈夫曼表(两个常量表生成一个哈夫曼表)
  339. void compute_Huffman_table(unsigned char *nrcodes,unsigned char *std_table,bitstring *HT)
  340. {
  341.         unsigned char k,j;
  342.         unsigned char pos_in_table;
  343.         unsigned short int codevalue;
  344.         codevalue = 0;
  345.         pos_in_table = 0;

  346.         for (k=1; k<=16; k++)
  347.         {
  348.                 for (j=1; j<=nrcodes[k]; j++)
  349.                 {
  350.                         HT[std_table[pos_in_table]].value = codevalue;
  351.                         HT[std_table[pos_in_table]].length= k;
  352.                         pos_in_table ++;
  353.                         codevalue ++;
  354.                 }
  355.                 codevalue *= 2;
  356.         }
  357. }

  358. //初始化哈夫曼表:只有四个表YDC,YAC,CbDC,CbAC(Cr的与Cb的相同)
  359. void init_Huffman_tables()
  360. {
  361.         compute_Huffman_table(std_dc_luminance_nrcodes,std_dc_luminance_values,YDC_HT);
  362.         compute_Huffman_table(std_dc_chrominance_nrcodes,std_dc_chrominance_values,CbDC_HT);
  363.         compute_Huffman_table(std_ac_luminance_nrcodes,std_ac_luminance_values,YAC_HT);
  364.         compute_Huffman_table(std_ac_chrominance_nrcodes,std_ac_chrominance_values,CbAC_HT);
  365. }

  366. //category:种类
  367. //功能:生成category / bitcode两个数组,供压缩时使用
  368. void set_numbers_category_and_bitcode()
  369. {
  370.         signed long int nr;
  371.         signed long int nrlower,nrupper;
  372.         unsigned char cat;

  373.         category_alloc = gp8main_category;
  374.         category = category_alloc+32767;

  375.         bitcode_alloc = gp8main_bitcode;
  376.         bitcode = bitcode_alloc+32767;
  377.         nrlower = 1;
  378.         nrupper = 2;

  379.         //从数组的中间向两头写数据
  380.         for (cat=1; cat<=15; cat++)
  381.         {
  382.                 //Positive numbers
  383.                 for (nr=nrlower; nr<nrupper; nr++)
  384.                 {
  385.                         category[nr] = cat;
  386.                         bitcode[nr].length = cat;
  387.                         bitcode[nr].value = (unsigned short int)nr;
  388.                 }

  389.                 //Negative numbers
  390.                 for (nr=-(nrupper-1); nr<=-nrlower; nr++)
  391.                 {
  392.                         category[nr] = cat;
  393.                         bitcode[nr].length = cat;
  394.                         bitcode[nr].value = (unsigned short int)(nrupper-1+nr);
  395.                 }
  396.                 nrlower <<= 1;
  397.                 nrupper <<= 1;
  398.         }
  399. }

  400. //说明:生成YCbCr表,供RGB转化时的查表
  401. //Y(n)=0.114B(n)+0.587G(n)+0.299R(n)
  402. //Cb(n)=0.5B(n)-0.3313G(n)-0.1687R(n)
  403. //Cr(n)=0.0813B(n)-0.14187G(n)+0.5R(n)
  404. void precalculate_YCbCr_tables()
  405. {
  406.         unsigned short int R,G,B;
  407.         for (R=0;R<=255;R++)
  408.         {
  409.                 YRtab[R]=(signed long int)(65536*0.299+0.5)*R;
  410.                 CbRtab[R]=(signed long int)(65536*-0.16874+0.5)*R;
  411.                 CrRtab[R]=(signed long int)(32768)*R;
  412.         }
  413.         for (G=0;G<=255;G++)
  414.         {
  415.                 YGtab[G]=(signed long int)(65536*0.587+0.5)*G;
  416.                 CbGtab[G]=(signed long int)(65536*-0.33126+0.5)*G;
  417.                 CrGtab[G]=(signed long int)(65536*-0.41869+0.5)*G;
  418.         }
  419.         for (B=0;B<=255;B++)
  420.         {
  421.                 YBtab[B]=(signed long int)(65536*0.114+0.5)*B;
  422.                 CbBtab[B]=(signed long int)(32768)*B;
  423.                 CrBtab[B]=(signed long int)(65536*-0.08131+0.5)*B;
  424.         }
  425. }

  426. //说明:8X8的区域进行DCT变换表的准备,即量化表fdtbl_Y[64] / fdtbl_Cb[64]
  427. //生成本量化表时,只用生成一次,注重与ARM环境的差别
  428. void prepare_quant_tables()
  429. {
  430.         double aanscalefactor[8] =
  431.         {
  432.                 1.0,
  433.                 1.387039845,
  434.                 1.306562965,
  435.                 1.175875602,
  436.                 1.0,
  437.                 0.785694958,
  438.                 0.541196100,
  439.                 0.275899379
  440.         };
  441.         unsigned char row, col;
  442.         unsigned char i = 0;
  443.         for (row = 0; row < 8; row++)
  444.         {
  445.                 for (col = 0; col < 8; col++)
  446.                 {
  447.                         fdtbl_Y[i] = (float) (1.0 / ((double) DQTinfo.Ytable[zigzag[i]] *
  448.                                 aanscalefactor[row] * aanscalefactor[col] * 8.0));
  449.                         fdtbl_Cb[i] = (float) (1.0 / ((double) DQTinfo.Cbtable[zigzag[i]] *
  450.                                 aanscalefactor[row] * aanscalefactor[col] * 8.0));

  451.                         i++;
  452.                 }
  453.         }
  454. }

  455. //说明:前向DCT转换,并且进行量化
  456. void fdct_and_quantization(signed char *data,float *fdtbl,signed short int *outdata)
  457. {
  458.         float tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7;
  459.         float tmp10, tmp11, tmp12, tmp13;
  460.         float z1, z2, z3, z4, z5, z11, z13;
  461.         float *dataptr;
  462.         float datafloat[64];
  463.         float temp;
  464.         signed char ctr;
  465.         unsigned char i;
  466.         for (i=0; i<64; i++)
  467.                 datafloat[i] = data[i];

  468.         //DCT转换第一步:按行处理
  469.         dataptr=datafloat;
  470.         for (ctr = 7; ctr >= 0; ctr--)
  471.         {
  472.                 tmp0 = dataptr[0] + dataptr[7];
  473.                 tmp7 = dataptr[0] - dataptr[7];
  474.                 tmp1 = dataptr[1] + dataptr[6];
  475.                 tmp6 = dataptr[1] - dataptr[6];
  476.                 tmp2 = dataptr[2] + dataptr[5];
  477.                 tmp5 = dataptr[2] - dataptr[5];
  478.                 tmp3 = dataptr[3] + dataptr[4];
  479.                 tmp4 = dataptr[3] - dataptr[4];

  480.                 /* Even part */

  481.                 tmp10 = tmp0 + tmp3;        /* phase 2 */
  482.                 tmp13 = tmp0 - tmp3;
  483.                 tmp11 = tmp1 + tmp2;
  484.                 tmp12 = tmp1 - tmp2;

  485.                 dataptr[0] = tmp10 + tmp11; /* phase 3 */
  486.                 dataptr[4] = tmp10 - tmp11;

  487.                 z1 = (tmp12 + tmp13) * ((float) 0.707106781); /* c4 */
  488.                 dataptr[2] = tmp13 + z1;        /* phase 5 */
  489.                 dataptr[6] = tmp13 - z1;

  490.                 /* Odd part */

  491.                 tmp10 = tmp4 + tmp5;        /* phase 2 */
  492.                 tmp11 = tmp5 + tmp6;
  493.                 tmp12 = tmp6 + tmp7;

  494.                 /* The rotator is modified from fig 4-8 to avoid extra negations. */
  495.                 z5 = (tmp10 - tmp12) * ((float) 0.382683433); /* c6 */
  496.                 z2 = ((float) 0.541196100) * tmp10 + z5; /* c2-c6 */
  497.                 z4 = ((float) 1.306562965) * tmp12 + z5; /* c2+c6 */
  498.                 z3 = tmp11 * ((float) 0.707106781); /* c4 */

  499.                 z11 = tmp7 + z3;                /* phase 5 */
  500.                 z13 = tmp7 - z3;

  501.                 dataptr[5] = z13 + z2;        /* phase 6 */
  502.                 dataptr[3] = z13 - z2;
  503.                 dataptr[1] = z11 + z4;
  504.                 dataptr[7] = z11 - z4;

  505.                 dataptr += 8;                /* advance pointer to next row */
  506.         }

  507.         //DCT转换第二步:按列处理
  508.         dataptr = datafloat;
  509.         for (ctr = 7; ctr >= 0; ctr--)
  510.         {
  511.                 tmp0 = dataptr[0] + dataptr[56];
  512.                 tmp7 = dataptr[0] - dataptr[56];
  513.                 tmp1 = dataptr[8] + dataptr[48];
  514.                 tmp6 = dataptr[8] - dataptr[48];
  515.                 tmp2 = dataptr[16] + dataptr[40];
  516.                 tmp5 = dataptr[16] - dataptr[40];
  517.                 tmp3 = dataptr[24] + dataptr[32];
  518.                 tmp4 = dataptr[24] - dataptr[32];

  519.                 /* Even part */

  520.                 tmp10 = tmp0 + tmp3;        /* phase 2 */
  521.                 tmp13 = tmp0 - tmp3;
  522.                 tmp11 = tmp1 + tmp2;
  523.                 tmp12 = tmp1 - tmp2;

  524.                 dataptr[0] = tmp10 + tmp11; /* phase 3 */
  525.                 dataptr[32] = tmp10 - tmp11;

  526.                 z1 = (tmp12 + tmp13) * ((float) 0.707106781); /* c4 */
  527.                 dataptr[16] = tmp13 + z1; /* phase 5 */
  528.                 dataptr[48] = tmp13 - z1;

  529.                 /* Odd part */

  530.                 tmp10 = tmp4 + tmp5;        /* phase 2 */
  531.                 tmp11 = tmp5 + tmp6;
  532.                 tmp12 = tmp6 + tmp7;

  533.                 /* The rotator is modified from fig 4-8 to avoid extra negations. */
  534.                 z5 = (tmp10 - tmp12) * ((float) 0.382683433); /* c6 */
  535.                 z2 = ((float) 0.541196100) * tmp10 + z5; /* c2-c6 */
  536.                 z4 = ((float) 1.306562965) * tmp12 + z5; /* c2+c6 */
  537.                 z3 = tmp11 * ((float) 0.707106781); /* c4 */

  538.                 z11 = tmp7 + z3;                /* phase 5 */
  539.                 z13 = tmp7 - z3;

  540.                 dataptr[40] = z13 + z2; /* phase 6 */
  541.                 dataptr[24] = z13 - z2;
  542.                 dataptr[8] = z11 + z4;
  543.                 dataptr[56] = z11 - z4;

  544.                 dataptr++;                        /* advance pointer to next column */
  545.         }

  546.         //DCT转换的结果存在datafloat[64]

  547.         //对DCT转换的结果进行量化处理,结果存在outdata[]内
  548.         for (i = 0; i < 64; i++)
  549.         {
  550.                 /* Apply the quantization and scaling factor */
  551.                 temp = datafloat[i] * fdtbl[i];
  552.                 outdata[i] = (signed short int) ((signed short int)(temp + 16384.5) - 16384);
  553.         }
  554. }

  555. void process_DU(signed char *ComponentDU, float *fdtbl,signed short int *DC, bitstring *HTDC, bitstring *HTAC)
  556. {
  557.         bitstring EOB=HTAC[0x00];
  558.         bitstring M16zeroes=HTAC[0xF0];
  559.         unsigned char i;
  560.         unsigned char startpos;
  561.         unsigned char end0pos;
  562.         unsigned char nrzeroes;
  563.         unsigned char nrmarker;
  564.         signed short int Diff;

  565.         //要函数进行DCT变换处理,并且进行量化,量化的结果存在DU_DCT内
  566.         fdct_and_quantization(ComponentDU,fdtbl,DU_DCT);
  567.         //==============================以下对量化的结果进行压缩==============================
  568.         //对DU_DCT[]按 '之' 字重新排列,存在DU[]内
  569.         for (i=0; i<=63; i++)
  570.                 DU[zigzag[i]] = DU_DCT[i];

  571.         Diff = DU[0] - *DC;                        //这里的*DC不一定是0
  572.         *DC = DU[0];                                //因为这里会修改局部参数*DC

  573.         //Encode DC
  574.         if (Diff==0)
  575.                 writebits(HTDC[0]);                        //Diff might be 0
  576.         else
  577.         {
  578.                 writebits(HTDC[category[Diff]]);
  579.                 writebits(bitcode[Diff]);
  580.         }

  581.         //Encode ACs
  582.         //先将后面的0全找出来,真正要处理的是头部的非零部分
  583.         for (end0pos=63; (end0pos>0)&&(DU[end0pos]==0); end0pos--);

  584.         //end0pos = first element in reverse order !=0
  585.         if (end0pos==0)
  586.         {
  587.                 writebits(EOB);
  588.                 return;
  589.         }

  590.         //非零部分的处理方法
  591.         //但是非零部分的内部,可能还有单个或连续的零,也要过滤掉,不处理
  592.         //所以真正处理的,只有非零部分内的非零的数据
  593.         i=1;
  594.         while (i <= end0pos)
  595.         {
  596.                 startpos=i;

  597.                 //过滤掉里面的0
  598.                 for (; (DU[i]==0)&&(i<=end0pos);i++) ;

  599.                 nrzeroes = i-startpos;
  600.                 //如果内部连续0个数超过16个要做以下处理
  601.                 if (nrzeroes>=16)
  602.                 {
  603.                         for (nrmarker=1; nrmarker<=nrzeroes/16; nrmarker++)
  604.                                 writebits(M16zeroes);
  605.                         nrzeroes = nrzeroes%16;
  606.                 }

  607.                 writebits(HTAC[nrzeroes*16+category[DU[i]]]);
  608.                 writebits(bitcode[DU[i]]);
  609.                 i++;
  610.         }
  611.         //EOB是每帧数据(8*8)的结束村记
  612.         if (end0pos!=63)
  613.                 writebits(EOB);
  614. }


  615. /*=============================================================================
  616. 功能说明:
  617.                 将8行的sensor数据加载到gp8main_sensor_source,并且变成24bit数据后放到
  618.                 gp8main_sensor_dest
  619. 返回:
  620.                 NULL
  621. 备注:
  622.                 本函数是摸拟ARM来做,因为真正的数据来自于sensor
  623. //===========================================================================*/
  624. void main_load8line_form_sensor(unsigned short int linenum,unsigned char *data)
  625. {
  626.         unsigned long int tmp01,tmp02;       

  627.         unsigned long int         perline_dest = bytes_perline * 3 ;

  628.         gp8main_sensor_source = (unsigned char*)  data+(linenum*bytes_perline);
  629.        
  630.         //16bit的数据转成24bit
  631.         for(tmp01 = 0; tmp01 < 8; tmp01 ++)
  632.         {
  633.                 for(tmp02 = 0; tmp02 < ImageWdith; tmp02 ++)
  634.                 {
  635.                         gp8main_sensor_dest[(tmp01)*perline_dest + tmp02*3 + 2] = gp8main_sensor_source[tmp01*bytes_perline + tmp02 ];
  636.                         gp8main_sensor_dest[(tmp01)*perline_dest + tmp02*3 + 0] = gp8main_sensor_source[tmp01*bytes_perline + tmp02 ];
  637.                         gp8main_sensor_dest[(tmp01)*perline_dest + tmp02*3 + 1] = gp8main_sensor_source[tmp01*bytes_perline + tmp02 ];
  638.                 }
  639.         }

  640.         tmp01 = 0;
  641. }

  642. //将8X8单元RGB数据从RGB_buffer[]内取出,转成YCbCr,同时存储到YDU[]/CbDU[]/CrDU[]内去
  643. void load_data_units_from_RGB_buffer(unsigned short int xpos,unsigned short int ypos)
  644. {
  645.         unsigned char x,y;
  646.         unsigned char pos=0;
  647.         unsigned long int location;
  648.         unsigned char R,G,B;
  649.         location = xpos;
  650.         for (y=0; y<8; y++)
  651.         {
  652.                 for (x=0; x<8; x++)
  653.                 {
  654.                         R = ((colorRGB *)gp8main_sensor_dest)[location].R;
  655.                         G = ((colorRGB *)gp8main_sensor_dest)[location].G;
  656.                         B = ((colorRGB *)gp8main_sensor_dest)[location].B;
  657.                         YDU[pos]=Y(R,G,B);
  658.                         CbDU[pos]=Cb(R,G,B);
  659.                         CrDU[pos]=Cr(R,G,B);
  660.                         location++;
  661.                         pos++;
  662.                 }
  663.                 location += ImageWdith-8;                                //换行
  664.         }
  665. }

  666. void main_encoder(unsigned char *image)
  667. {
  668.         signed short int DCY=0,DCCb=0,DCCr=0; //DC coefficients used for differential encoding
  669.         unsigned short int xpos,ypos;
  670.         for(ypos=0;ypos<ImageHeight;ypos += 8)
  671.         {
  672.                 main_load8line_form_sensor(ypos,image);
  673.                 for(xpos=0;xpos<ImageWdith;xpos += 8)
  674.                 {
  675.                         load_data_units_from_RGB_buffer(xpos,ypos);
  676.                         //对Y,Cb,Cr各自进行量化(Y量化表,Cb与Cr表相同)
  677.                         process_DU(YDU,fdtbl_Y,&DCY,YDC_HT,YAC_HT);
  678.                         process_DU(CbDU,fdtbl_Cb,&DCCb,CbDC_HT,CbAC_HT);
  679.                         process_DU(CrDU,fdtbl_Cb,&DCCr,CbDC_HT,CbAC_HT);
  680.                 }
  681.         }
  682. }

  683. void init_all()
  684. {
  685.         set_DQTinfo();
  686.         set_DHTinfo();
  687.         init_Huffman_tables();
  688.         set_numbers_category_and_bitcode();
  689.         precalculate_YCbCr_tables();
  690.         prepare_quant_tables();
  691. }

  692. //        入口函数
  693. //        image_w 和image_h为图像的宽和高,一定要为8的倍数
  694. //  ImageData 为原始图像数据
  695. //        图像输出地址在JPG_filename中

  696. unsigned int data2jpg(int image_w,int image_h,unsigned char *ImageData)
  697. {
  698. //        unsigned int i;
  699. //        char JPG_filename[64] = "c:/test.jpg";
  700.         bitstring fillbits;                                                                        //filling bitstring for the bit alignment of the EOI marker

  701.         all_jpeg_data_length=0;

  702.         ImageWdith = image_w;
  703.         ImageHeight = image_h;

  704.         bytes_perline = ((ImageWdith * 1 + 3) / 4) * 4;
  705.         init_all();
  706.         SOF0info.width = (unsigned short int)ImageWdith;                //Ximage_original;
  707.         SOF0info.height = (unsigned short int)ImageHeight;                //Yimage_original;
  708.         writeword(0xFFD8); //SOI
  709.         write_APP0info();
  710.         write_DQTinfo();
  711.         write_SOF0info();
  712.         write_DHTinfo();
  713.         write_SOSinfo();

  714.         bytenew = 0;
  715.         bytepos = 7;
  716.         main_encoder(ImageData);
  717.         //Do the bit alignment of the EOI marker
  718.         if (bytepos>=0)
  719.         {
  720.                 fillbits.length=bytepos+1;
  721.                 fillbits.value=(1<<(bytepos+1))-1;
  722.                 writebits(fillbits);
  723.         }
  724.         writeword(0xFFD9); //EOI
  725.     return all_jpeg_data_length;
  726. }
复制代码

阿莫论坛20周年了!感谢大家的支持与爱护!!

一只鸟敢站在脆弱的枝条上歇脚,它依仗的不是枝条不会断,而是自己有翅膀,会飞。

出200入2554汤圆

发表于 2018-11-14 19:54:49 | 显示全部楼层
运行平台呢?打算跑 FPGA 么?这个代码看不出 HDL 的影子啊。

Windows 下倒是可以用 Gdiplus 做,系统标准的图像库,可以创建/保存 BMP/JPG/PNG/GIF/TIFF 格式图片。
流程大致是:创建 Gdiplus::Bitmap 实例 -> 获取图像数据区指针并访问(写入你的图像) -> Gdiplus::Bitmap::Save 指定使用 JPG 编码器。

当然 JPG 编出来是有损的,推荐使用 PNG。

出0入0汤圆

发表于 2018-11-15 14:39:16 | 显示全部楼层
1:如果是灰度图的话,只用处理Y分量
2:代码里面大量的浮点运算,而且有一次RGB到YUV的转换。看看输入能不能改为YUV的数据;
3:为什么不直接使用libjpeg等开源库
4:代码要是在ARM上运行,建议使用NEON进行优化
5:各个模块做成异步的,例如,RGB2YUV和DCT变换可以做成流水线的

另外代码可能有问题,没有跑通,例如:“//16bit的数据转成24bit”的部分;

出25入84汤圆

 楼主| 发表于 2018-11-15 16:21:16 | 显示全部楼层
t3486784401 发表于 2018-11-14 19:54
运行平台呢?打算跑 FPGA 么?这个代码看不出 HDL 的影子啊。

Windows 下倒是可以用 Gdiplus 做,系统标准 ...

fpga内核搞  ,相当于纯C语言做jpg

出25入84汤圆

 楼主| 发表于 2018-11-15 16:22:25 | 显示全部楼层
jm2011 发表于 2018-11-15 14:39
1:如果是灰度图的话,只用处理Y分量
2:代码里面大量的浮点运算,而且有一次RGB到YUV的转换。看看输入能不 ...

是啊 ,标准的库中,大量的float运算,各种嵌套循环,100M的内核跑起来生不如死。

出25入84汤圆

 楼主| 发表于 2018-11-15 16:23:56 | 显示全部楼层
另外 这个问题已经解决了,用的是自己写的行程编码方式。
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-3-29 20:53

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

快速回复 返回顶部 返回列表