#include <Windows.h>
#include <stdio.h> 

extern  "C" {
#include <jpeglib.h>
} 

#define WIDTH 352
#define HEIGHT 288
#define QUALITY 80
#define BUFFER_SZIE (WIDTH*HEIGHT*2) 

/* The following declarations and 5 functions are jpeg related
 * functions used by put_jpeg_grey_memory and put_jpeg_yuv420p_memory
 */
typedef struct {
    struct jpeg_destination_mgr pub;
    JOCTET *buf;
    size_t bufsize;
    size_t jpegsize;
} mem_destination_mgr; 

typedef mem_destination_mgr *mem_dest_ptr; 

METHODDEF(void) init_destination(j_compress_ptr cinfo)
{
    mem_dest_ptr dest = (mem_dest_ptr) cinfo->dest;
    dest->pub.next_output_byte = dest->buf;
    dest->pub.free_in_buffer = dest->bufsize;
    dest->jpegsize = ;
} 

METHODDEF(boolean) empty_output_buffer(j_compress_ptr cinfo)
{
    mem_dest_ptr dest = (mem_dest_ptr) cinfo->dest;
    dest->pub.next_output_byte = dest->buf;
    dest->pub.free_in_buffer = dest->bufsize; 

    return FALSE;
} 

METHODDEF(void) term_destination(j_compress_ptr cinfo)
{
    mem_dest_ptr dest = (mem_dest_ptr) cinfo->dest;
    dest->jpegsize = dest->bufsize - dest->pub.free_in_buffer;
} 

static GLOBAL(void) jpeg_mem_dest(j_compress_ptr cinfo, JOCTET* buf, size_t bufsize)
{
    mem_dest_ptr dest; 

    if (cinfo->dest == NULL) {
        cinfo->dest = (struct jpeg_destination_mgr *)
            (*cinfo->mem->alloc_small)((j_common_ptr)cinfo, JPOOL_PERMANENT,
            sizeof(mem_destination_mgr));
    } 

    dest = (mem_dest_ptr) cinfo->dest; 

    dest->pub.init_destination    = init_destination;
    dest->pub.empty_output_buffer = empty_output_buffer;
    dest->pub.term_destination    = term_destination; 

    dest->buf      = buf;
    dest->bufsize  = bufsize;
    dest->jpegsize = ;
} 

static GLOBAL(int) jpeg_mem_size(j_compress_ptr cinfo)
{
    mem_dest_ptr dest = (mem_dest_ptr) cinfo->dest;
    return dest->jpegsize;
} 

/* put_jpeg_yuv420p_memory converts an input image in the YUV420P format into a jpeg image and puts
 * it in a memory buffer.
 * Inputs:
 * - input_image is the image in YUV420P format.
 * - width and height are the dimensions of the image
 * Output:
 * - dest_image is a pointer to the jpeg image buffer
 * Returns buffer size of jpeg image
 */
static int put_jpeg_yuv420p_memory(unsigned char *dest_image,
                                   unsigned char *input_image, int width, int height)
{
    int i, j, jpeg_image_size; 

    JSAMPROW y[],cb[],cr[]; // y[2][5] = color sample of row 2 and pixel column 5; (one plane)
    JSAMPARRAY data[]; // t[0][2][5] = color sample 0 of row 2 and column 5 

    struct jpeg_compress_struct cinfo;
    struct jpeg_error_mgr jerr; 

    data[] = y;
    data[] = cb;
    data[] = cr; 

    cinfo.err = jpeg_std_error(&jerr);  // errors get written to stderr  

    jpeg_create_compress(&cinfo);
    cinfo.image_width = width;
    cinfo.image_height = height;
    cinfo.input_components = ;
    jpeg_set_defaults (&cinfo); 

    jpeg_set_colorspace(&cinfo, JCS_YCbCr); 

    cinfo.raw_data_in = TRUE;                  // supply downsampled data
    cinfo.do_fancy_downsampling = FALSE;       // fix segfaulst with v7
    cinfo.comp_info[].h_samp_factor = ;
    cinfo.comp_info[].v_samp_factor = ;
    cinfo.comp_info[].h_samp_factor = ;
    cinfo.comp_info[].v_samp_factor = ;
    cinfo.comp_info[].h_samp_factor = ;
    cinfo.comp_info[].v_samp_factor = ; 

    jpeg_set_quality(&cinfo, QUALITY, TRUE);
    cinfo.dct_method = JDCT_FASTEST; 

    jpeg_mem_dest(&cinfo, dest_image, BUFFER_SZIE);    // data written to mem 

    jpeg_start_compress (&cinfo, TRUE); 

    ; j < height; j += ) {
        ; i < ; i++) {
            y[i] = input_image + width * (i + j);
             == ) {
                cb[i/] = input_image + width * height + width /  * ((i + j) / );
                cr[i/] = input_image + width * height + width * height /  + width /  * ((i + j) / );
            }
        }
        jpeg_write_raw_data(&cinfo, data, );
    } 

    jpeg_finish_compress(&cinfo);
    jpeg_image_size = jpeg_mem_size(&cinfo);
    jpeg_destroy_compress(&cinfo); 

    return jpeg_image_size;
} 

int main( int argc, TCHAR * argv[], TCHAR * envp[] )
{
    HANDLE fyuv,fyuvjpg;
    BYTE *pSrc ,*pDst;
    LONG  lSize = ;
    DWORD  readsize;
    DWORD  writesize; 

    pSrc = new BYTE[BUFFER_SZIE];
    fyuv = CreateFile(L, NULL, OPEN_EXISTING, , NULL);
    ReadFile(fyuv , pSrc ,BUFFER_SZIE ,&readsize,NULL); 

    pDst = new BYTE[BUFFER_SZIE];
    lSize = put_jpeg_yuv420p_memory(pDst,pSrc , WIDTH ,HEIGHT);
    fyuvjpg = CreateFile(L, NULL, CREATE_ALWAYS, , NULL);
    WriteFile(fyuvjpg, pDst, lSize, &writesize, NULL); 

    CloseHandle(fyuv);
    CloseHandle(fyuvjpg);
    ;
} 

使用libjpeg 压缩yuv420到jpg (内存方式)的更多相关文章

  1. C语言中的联合体union所占内存方式

     当多个数据需要共享内存或者多个数据每次只取其一时,可以利用联合体(union).在C Programming Language 一书中对于联合体是这么描述的:      1)联合体是一个结构:    ...

  2. 史林枫:sqlserver数据库中数据日志的压缩及sqlserver占用内存管理设置

    使用sqlserver和IIS开发.net B/S程序时,数据量逐渐增多,用户也逐渐增多,那么服务器的稳定性就需要维护了.数据库如何占用更小内存,无用的日志如何瞬间清空? 今天在给一个客户维护网站的时 ...

  3. CxImage内存方式转换图像

    最近,处于项目需要,需要将Bmp转换为JPEG格式.以前做过,采用的是GDI+的方式,该方式有一个极大地缺陷为无法实现跨平台处理.闲话少说,进入正题. CxImage cxImageBmp(pRGBB ...

  4. AFNetworking上传一张或多张图片,并压缩图片节约占用内存

    最近在做APP的时候,遇到了难题:根据公司需求,在用户评论并上传图片的时候,有的手机像素比较高拍的照片高清的,但是每张图片占用的内存太大,或者上传照片的时候,相册的部分照片本身就占很大内存空间,后台数 ...

  5. .net中清理内存,清理占用内存方式方法

    #region 内存回收 [DllImport("kernel32.dll", EntryPoint = "SetProcessWorkingSetSize") ...

  6. SAP SMARTFORMS-基于内存方式在report程序中传递数据到smartforms显示

    一.准备工作 1.新建include程序 1> include程序名字:ZDD_INC_0104 2> ZDD_INC_0104  程序中的内容为 2.使用T-CODE :SE11新建两个 ...

  7. Eclipse中设置JDK内存方式

    (1) 打开Eclipse,双击Serveers进入到servers编辑画面 (2) 点击 Open launch configuration 选项 (3) 选项中找到Arguments 的选项卡(t ...

  8. 008-对象—— 对象$this self parent 内存方式及使用方法讲解

    <?php /** * */ /*class Web{ private $webname; private $weburl; function __construct($webname,$web ...

  9. <转>libjpeg解码内存中的jpeg数据

    转自http://my.unix-center.net/~Simon_fu/?p=565 熟悉libjpeg的朋友都知道libjpeg是一个开源的库.Linux和Android都是用libjpeg来 ...

随机推荐

  1. CSS:CSS+DIV布局网页

    现代网页布局:CSS+DIV: 一般的网页都是顺序布局的,很难达到我们需要的网页布局格式,此时使用DIV进行分层布局,类似于盒子,对每一部分内容进行设计.这是现在主流的网页布局方式,使用DIV+CSS ...

  2. 利用WMI检测电脑硬件信息,没办法显示cpu的信息

    但你要给某些系统或软件加密时,需要了解到服务器的硬件信息时,系统和软件会利用WMI检测硬件信息, 而有时我们会遇到检测不到CPU的型号信息,如图 此时的解决方法: 1.确定“服务”里启动了WMI 2. ...

  3. 使用OctreeQuantizer提高gdi+绘图质量

    .net中gdi+绘制的图形质量很少,原因是gdi+使用的是256色的. 为了提高绘制图片的质量,可以使用是“Octree“ 算法.“Octree“ 算法允许我们插入自己的算法来量子化我们的图像. 一 ...

  4. java设计模式5--原型模式(Prototype)

    本文地址:http://www.cnblogs.com/archimedes/p/java-prototype-pattern.html,转载请注明源地址. 原型模式 用原型实例指定创建对象的种类,并 ...

  5. Linux中crontab无法执行java程序的问题

    Linux中crontab无法执行java程序的问题 jdk环境变量配置问题:可以用全路径来解决: 竟然是kill -9之后,后面的脚本都不运行,这个还没有找到原因啊: 即便是系统级别的 ls 命令, ...

  6. Rust 格式输出

    格式输出由一系列定义在 std::fmt 中的宏提供. 包含: format! : 输出格式化的字符串. print!  : 输出格式化的字符串到控制台(终端)println!: 添加一个换行,输出格 ...

  7. ExplorerControls的显示问题

    我们都知道ArcGIS桌面版本"添加数据"的功能是弹出一选择对话框,如下图所示,但我们总想搞自己的,以便融入自己的风格.下图左边是ArcGIS自带的"添加数据" ...

  8. 首都医科大学附属北京安贞医院全院级PACS系统采购项目[转]

    项目名称:首都医科大学附属北京安贞医院全院级PACS系统采购项目 项目编号:TC140VCF0 采购人名称:首都医科大学附属北京安贞医院 采购人地址:北京市朝阳区安贞里 采购人联系方式:010-644 ...

  9. 流编辑器sed

    sed与grep一样,都起源于老式的ed编辑器,因其是一个流编辑器(stream editor)而得名.与vi等编辑器不同,sed是一种非交互式编辑器(即用户不必参与编辑过程),它使用预先设定好的编辑 ...

  10. 简单测试Demo:如何用Java压缩文件夹和文件

    一.直接贴出测试代码 package com.jason.zip; import java.io.File; import java.io.FileInputStream; import java.i ...