{"id":729,"date":"2019-04-28T15:48:25","date_gmt":"2019-04-28T07:48:25","guid":{"rendered":"http:\/\/euhat.com\/wp\/?p=729"},"modified":"2019-04-28T15:48:25","modified_gmt":"2019-04-28T07:48:25","slug":"libjpeg-exit","status":"publish","type":"post","link":"http:\/\/euhat.com\/wp\/2019\/04\/28\/libjpeg-exit\/","title":{"rendered":"libjpeg exit"},"content":{"rendered":"<p>\u7f16\u8bd1jpeg-9c\u540e\u5f97\u5230libjpeg.lib\uff0c\u6309\u7167\u7f51\u4e0a\u7684\u4f8b\u7a0b\uff0c\u8bfbjpg\u6587\u4ef6\u65f6\uff0c\u53ea\u8981jpg\u6587\u4ef6\u6709\u9519\u8bef\uff0clibjpeg\u5e93\u7684\u51fd\u6570\u5185\u90e8\u5c31\u76f4\u63a5\u8c03\u7cfb\u7edf\u51fd\u6570exit\u9000\u51fa\u4e86\uff0c\u8fd9\u5728\u6574\u5408libjpeg\u5e93\u5230\u5927\u578b\u7a0b\u5e8f\u91cc\u65f6\u662f\u7edd\u5bf9\u4e0d\u80fd\u5141\u8bb8\u7684\u3002\u4ed4\u7ec6\u67e5\u770b\u5e93\u91cc\u7684libjpeg.txt\uff0c\u624d\u77e5\u9053\u539f\u6765libjpeg\u80fd\u81ea\u5b9a\u4e49\u9519\u8bef\u5904\u7406\uff0c\u4f8b\u7a0b\u5728example.c\u6587\u4ef6\u91cc\u7684read_JPEG_file\u3002<\/p>\n<p>\u4ee5\u4e0b\u662f\u6211\u6574\u7406\u597d\u7684\u96c6\u6210\u63a5\u53e3\uff1a<br \/>\nReadJpg.h<\/p>\n<pre lang=\"cpp\" line=\"0\">\n#pragma once\n\nHBITMAP readJpegAsBitmap(const char *fileName);\n<\/pre>\n<p><!--more--><br \/>\nReadJpg.cpp<\/p>\n<pre lang=\"cpp\" line=\"0\">\n#include <stdio.h>\n#include <setjmp.h>\n#include <windows.h>\nextern \"C\" {\n#include <jpeglib.h> \n#include <jerror.h>\n}\n\n#pragma comment(lib, \"libjpeg.lib\")\n\nstruct my_error_mgr {\n\tstruct jpeg_error_mgr pub;\n\n\tjmp_buf setjmp_buffer;\n};\n\ntypedef struct my_error_mgr *my_error_ptr;\n\nMETHODDEF(void)\nmy_error_exit(j_common_ptr cinfo)\n{\n\tmy_error_ptr myerr = (my_error_ptr)cinfo->err;\n\n\t(*cinfo->err->output_message) (cinfo);\n\n\tlongjmp(myerr->setjmp_buffer, 1);\n}\n\nHBITMAP readJpegAsBitmap(const char *fileName)\n{\n\tBITMAP bmp;\n\tBITMAPINFO bi = {};\n\tLPBYTE lpBuf, pb = NULL;\n\tHBITMAP hBmp = NULL;\n\tJSAMPARRAY buffer{};\n\tint row_stride;\n\tUINT row;\n\n\tFILE *fp = fopen(fileName, \"rb\");\n\tif (fp == NULL)\n\t{\n\t\treturn NULL;\n\t}\n\n\tunsigned char sign[4];\n\tfread(sign, 1, 4, fp);\n\tif (sign[0] != 0xff || sign[1] != 0xd8)\n\t{\n\t\tfclose(fp);\n\t\treturn NULL;\n\t}\n\tfseek(fp, 0, SEEK_SET);\n\n\tstruct jpeg_decompress_struct cinfo;\n\tstruct my_error_mgr jerr;\n\n\tcinfo.err = jpeg_std_error(&jerr.pub);\n\tjerr.pub.error_exit = my_error_exit;\n\n\tif (setjmp(jerr.setjmp_buffer))\n\t{\n\t\tgoto failout;\n\t}\n\n\tjpeg_create_decompress(&cinfo);\n\n\tjpeg_stdio_src(&cinfo, fp);\n\n\tjpeg_read_header(&cinfo, TRUE);\n\n\tjpeg_start_decompress(&cinfo);\n\n\t\/\/row = ((cinfo.output_width * 3 + 3) & ~3);\n\trow_stride = cinfo.output_width * cinfo.output_components;\n\tbuffer = (*cinfo.mem->alloc_sarray)\n\t\t((j_common_ptr)&cinfo, JPOOL_IMAGE, row_stride, 1);\n\n\tZeroMemory(&bi.bmiHeader, sizeof(BITMAPINFOHEADER));\n\tbi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);\n\tbi.bmiHeader.biWidth = cinfo.output_width;\n\tbi.bmiHeader.biHeight = cinfo.output_height;\n\tbi.bmiHeader.biPlanes = 1;\n\tbi.bmiHeader.biBitCount = 24;\n\tbi.bmiHeader.biCompression = BI_RGB;\n\/\/\tbi.bmiHeader.biSizeImage = row * cinfo.output_height;\n\n\thBmp = CreateDIBSection(NULL, &bi, DIB_RGB_COLORS, (void**)&lpBuf, NULL, 0);\n\tif (hBmp == NULL)\n\t\tgoto failout;\n\n\tGetObject(hBmp, sizeof(BITMAP), (void *)&bmp);\n\trow = bmp.bmWidthBytes;\n\n\tpb = lpBuf + row * cinfo.output_height;\n\twhile (cinfo.output_scanline < cinfo.output_height)\n\t{\n\t\tpb -= row;\n\t\tjpeg_read_scanlines(&cinfo, buffer, 1);\n\n\t\tif (cinfo.out_color_components == 1)\n\t\t{\n\t\t\tLPBYTE p = (LPBYTE)buffer[0];\n\t\t\tfor (UINT i = 0; i < cinfo.output_width; i++)\n\t\t\t{\n\t\t\t\tpb[3 * i + 0] = p[i];\n\t\t\t\tpb[3 * i + 1] = p[i];\n\t\t\t\tpb[3 * i + 2] = p[i];\n\t\t\t}\n\t\t}\n\t\telse if (cinfo.out_color_components == 3)\n\t\t{\n\t\t\tLPBYTE p = (LPBYTE)buffer[0];\n\t\t\tfor (UINT i = 0; i < cinfo.output_width; i++)\n\t\t\t{\n\t\t\t\tUINT j = i * 3;\n\t\t\t\tpb[j + 0] = p[j + 2]; \/\/ Blue\n\t\t\t\tpb[j + 1] = p[j + 1]; \/\/ Green\n\t\t\t\tpb[j + 2] = p[j + 0];  \/\/ Red\n\t\t\t}\n\t\t}\n\t\telse\n\t\t{\n\t\t\tgoto failout;\n\t\t}\n\t}\n\n\tSetDIBits(NULL, hBmp, 0, cinfo.output_height, lpBuf, &bi, DIB_RGB_COLORS);\n\n\tjpeg_finish_decompress(&cinfo);\n\nfailout:\n\n\tjpeg_destroy_decompress(&cinfo);\n\n\tfclose(fp);\n\n\treturn hBmp;\n}\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u7f16\u8bd1jpeg-9c\u540e\u5f97\u5230libjpeg.lib\uff0c\u6309\u7167\u7f51\u4e0a\u7684\u4f8b\u7a0b\uff0c\u8bfbjpg\u6587\u4ef6\u65f6\uff0c\u53ea\u8981jpg\u6587\u4ef6\u6709\u9519\u8bef\uff0clibjpeg\u5e93\u7684\u51fd\u6570\u5185\u90e8\u5c31\u76f4\u63a5\u8c03\u7cfb\u7edf\u51fd\u6570exit\u9000\u51fa\u4e86\uff0c\u8fd9\u5728\u6574\u5408libjpeg\u5e93\u5230\u5927\u578b\u7a0b\u5e8f\u91cc\u65f6\u662f\u7edd\u5bf9\u4e0d\u80fd\u5141\u8bb8\u7684\u3002\u4ed4\u7ec6\u67e5\u770b\u5e93\u91cc\u7684libjpeg.txt\uff0c\u624d\u77e5\u9053\u539f\u6765libjpeg\u80fd\u81ea\u5b9a\u4e49\u9519\u8bef\u5904\u7406\uff0c\u4f8b\u7a0b\u5728example.c\u6587\u4ef6\u91cc\u7684read_JPEG_file\u3002 \u4ee5\u4e0b\u662f\u6211\u6574\u7406\u597d\u7684\u96c6\u6210\u63a5\u53e3\uff1a ReadJpg.h #pragma once HBITMAP readJpegAsBitmap(const char *fileName);<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[91,123,153],"_links":{"self":[{"href":"http:\/\/euhat.com\/wp\/wp-json\/wp\/v2\/posts\/729"}],"collection":[{"href":"http:\/\/euhat.com\/wp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/euhat.com\/wp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/euhat.com\/wp\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/euhat.com\/wp\/wp-json\/wp\/v2\/comments?post=729"}],"version-history":[{"count":0,"href":"http:\/\/euhat.com\/wp\/wp-json\/wp\/v2\/posts\/729\/revisions"}],"wp:attachment":[{"href":"http:\/\/euhat.com\/wp\/wp-json\/wp\/v2\/media?parent=729"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/euhat.com\/wp\/wp-json\/wp\/v2\/categories?post=729"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/euhat.com\/wp\/wp-json\/wp\/v2\/tags?post=729"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}