Click here to Skip to main content
Click here to Skip to main content
 
Add your own
alternative version

ImageStone - A Powerful C++ Class Library for Image Manipulation

, 6 Dec 2011 Zlib
An article on a library for image manipulation
imagestonesrc.zip
ImageStone
example
001
src
001.dsp
001.dsw
Makefile
002
src
002.dsp
002.dsw
002.ico
bk.tga
small.ico
003
src
003.dsp
003.dsw
003.ico
rgn.TGA
small.ico
004
src
004.dsp
004.dsw
005
src
005.dsp
005.dsw
Makefile
006
src
006.dsp
006.dsw
Makefile
007
src
007.dsp
007.dsw
Makefile
008
src
008.dsp
008.dsw
res
008.exe.manifest
008.ico
008Doc.ico
arc_grad.bmp
tex_canvas.BMP
Toolbar.bmp
009
src
009.dsp
009.dsw
009.ico
pattern.jpg
small.ico
010
src
010.dsp
010.dsw
010.plg
res
010.ico
include
command
imagefile
pixelprocessor
lib
FreeImage
Dist
Source
gdiplus
Include
lib
imagestone_chm.zip
doc.chm
imagestone_demo.zip
002_vs2005.exe
imagestone_demo008.zip
008.exe
imagestone_demo_008.zip
008.exe
imagestone_docchm.zip
Help.chm
imagestone_doc_chm.zip
index.chm
imagestone_doc_html.zip
doc_html
doxygen.png
class_f_c_image_handle.png
class_f_c_image_handle___bmp.png
class_f_c_image_handle___free_image.png
class_f_c_image_handle___gdiplus.png
class_f_c_image_handle___i_picture.png
class_f_c_image_handle___tga.png
class_f_c_image_handle_base.png
class_f_c_image_handle_factory.png
class_f_c_image_handle_factory___i_picture.png
class_f_c_image_handle_factory___mini.png
class_f_c_interface___pixel_process.png
class_f_c_object.png
class_f_c_obj_graph.png
class_f_c_obj_image.png
class_f_c_obj_multi_frame.png
class_f_c_pixel3_d_grid.png
class_f_c_pixel_add_random_noise.png
class_f_c_pixel_adjust_r_g_b.png
class_f_c_pixel_auto_color_enhance.png
class_f_c_pixel_auto_contrast.png
class_f_c_pixel_bilinear_distord.png
class_f_c_pixel_blinds.png
class_f_c_pixel_brightness.png
class_f_c_pixel_color_balance.png
class_f_c_pixel_color_level.png
class_f_c_pixel_colors_count.png
class_f_c_pixel_color_tone.png
class_f_c_pixel_combine_channel___r_g_b.png
class_f_c_pixel_contrast.png
class_f_c_pixel_convert_quantize.png
class_f_c_pixel_convert_to16_bit.png
class_f_c_pixel_convert_to8_bit_gray.png
class_f_c_pixel_convolute.png
class_f_c_pixel_cylinder.png
class_f_c_pixel_deinterlace.png
class_f_c_pixel_detect_edges.png
class_f_c_pixel_emboss.png
class_f_c_pixel_export_asc_i_i.png
class_f_c_pixel_fill3_d_solid_frame.png
class_f_c_pixel_fill_color.png
class_f_c_pixel_fill_grid.png
class_f_c_pixel_flip.png
class_f_c_pixel_fractal_trace.png
class_f_c_pixel_gamma.png
class_f_c_pixel_gaussian_blur3x3.png
class_f_c_pixel_gaussian_blur5x5.png
class_f_c_pixel_get_key_color.png
class_f_c_pixel_gradient_base.png
class_f_c_pixel_gradient_bi_line.png
class_f_c_pixel_gradient_conical_a_sym.png
class_f_c_pixel_gradient_conical_sym.png
class_f_c_pixel_gradient_line.png
class_f_c_pixel_gradient_radial.png
class_f_c_pixel_gradient_rect.png
class_f_c_pixel_grayscale.png
class_f_c_pixel_halftone_m3.png
class_f_c_pixel_hue_saturation.png
class_f_c_pixel_illusion.png
class_f_c_pixel_invert.png
class_f_c_pixel_lens.png
class_f_c_pixel_l_u_t_routine.png
class_f_c_pixel_median_filter.png
class_f_c_pixel_mirror.png
class_f_c_pixel_mosaic.png
class_f_c_pixel_noisify.png
class_f_c_pixel_oil_paint.png
class_f_c_pixel_perspective.png
class_f_c_pixel_posterize.png
class_f_c_pixel_ribbon.png
class_f_c_pixel_ripple.png
class_f_c_pixel_rotate.png
class_f_c_pixel_rotate270.png
class_f_c_pixel_rotate90.png
class_f_c_pixel_sharp.png
class_f_c_pixel_shift.png
class_f_c_pixel_skew.png
class_f_c_pixel_small_tile.png
class_f_c_pixel_solarize.png
class_f_c_pixel_splash.png
class_f_c_pixel_splite_channel___r_g_b.png
class_f_c_pixel_threshold.png
class_f_c_pixel_video.png
class_f_c_pixel_wave.png
class_f_c_pixel_whirl_pinch.png
class_f_c_single_pixel_process_base.png
imagestone_src.zip
image_stone
doc
doc.rar
Doxyfile
example
001
Makefile
Readme_MinGW
002
dlg_effect_blinds.inl
dlg_effect_color_balance.inl
dlg_effect_color_level.inl
dlg_effect_color_tone.inl
dlg_effect_custom.inl
dlg_effect_fillgrid.inl
dlg_effect_lensflare.inl
dlg_effect_supernova.inl
dlg_image_size.inl
res
002.ico
002Doc.ico
bar_top.png
example.jpg
lang.ini
menu_icon
adjustrgb.png
brightness_contrast.png
color_balance.png
color_level.png
color_tone.png
effect_fillgrid.png
effect_invert.png
effect_lensflare.png
effect_oil.png
effect_pencil.png
effect_splash.png
effect_supernova.png
effect_twist.png
flip.png
grayscale.png
hue_saturation.png
mirror.png
open.png
print.png
quit.png
resize.png
rotate270.png
rotate90.png
save_as.png
smooth_edge.png
to_ascii.png
shadow.png
tex_bricks.png
tex_canvas_1.png
tex_canvas_2.png
tex_dot.png
tex_grid.png
tex_leather.png
tex_mat.png
tex_sandstone.png
tex_weave.png
uistone
include
effect
image_codec
image_effect.inl
image_property.inl
image_resize_filter.inl
image_win.inl
linux_define.inl
post_implement.inl
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1">
<title>ImageStone: ImageHandle_Tga.h Source File</title>
<link href="doxygen.css" rel="stylesheet" type="text/css">
</head><body>
<!-- Generated by Doxygen 1.4.1 -->
<div class="qindex"><a class="qindex" href="index.html">Main&nbsp;Page</a> | <a class="qindex" href="hierarchy.html">Class&nbsp;Hierarchy</a> | <a class="qindex" href="annotated.html">Class&nbsp;List</a> | <a class="qindex" href="files.html">File&nbsp;List</a> | <a class="qindex" href="functions.html">Class&nbsp;Members</a></div>
<h1>ImageHandle_Tga.h</h1><div class="fragment"><pre class="fragment">00001 <span class="comment">/*</span>
00002 <span class="comment"> *   Copyright (C) =USTC= Fu Li</span>
00003 <span class="comment"> *</span>
00004 <span class="comment"> *   Author   :  Fu Li</span>
00005 <span class="comment"> *   Create   :  2004-4-9</span>
00006 <span class="comment"> *   Home     :  http://www.crazy-bit.com/</span>
00007 <span class="comment"> *   Mail     :  crazybit@263.net</span>
00008 <span class="comment"> *   History  :  </span>
00009 <span class="comment"> */</span>
00010 <span class="preprocessor">#ifndef __PCL_IMAGE_HANDLE_TGA__2004_04_09__H__</span>
00011 <span class="preprocessor"></span><span class="preprocessor">#define __PCL_IMAGE_HANDLE_TGA__2004_04_09__H__</span>
00012 <span class="preprocessor"></span><span class="preprocessor">#include "ImageHandleBase.h"</span>
00013 <span class="preprocessor">#include "../compress/Rle.h"</span>
00014 
00015 <span class="comment">//class FCImageHandle ;</span>
00016     <span class="keyword">class </span><a class="code" href="class_f_c_image_handle___tga.html">FCImageHandle_Tga</a> ;
00017 
00018 <span class="comment">//=============================================================================</span>
<a name="l00022"></a><a class="code" href="class_f_c_image_handle___tga.html">00022</a> <span class="comment"></span><span class="keyword">class </span><a class="code" href="class_f_c_image_handle___tga.html">FCImageHandle_Tga</a> : <span class="keyword">public</span> <a class="code" href="class_f_c_image_handle.html">FCImageHandle</a>
00023 {
00024     <span class="keyword">virtual</span> <span class="keywordtype">bool</span> <a class="code" href="class_f_c_image_handle_base.html#a1">LoadImageMemory</a> (<span class="keyword">const</span> BYTE* pStart, <span class="keywordtype">int</span> nFileSize) ;
00025 
00029     <span class="keyword">virtual</span> <span class="keywordtype">bool</span> <a class="code" href="class_f_c_image_handle_base.html#a2">SaveImage</a> (<span class="keyword">const</span> <span class="keywordtype">char</span>* szFileName, <span class="keywordtype">int</span> nFlag = -1) ;
00030 
00031     <span class="comment">// Definitions for TGA image types.</span>
00032     <span class="keyword">enum</span>
00033     {
00034         TGA_NULL    = 0,
00035         TGA_UCPAL   = 1,
00036         TGA_UCRGB   = 2,
00037         TGA_UCMONO  = 3,
00038         TGA_RLEPAL  = 9,
00039         TGA_RLERGB  = 10,
00040         TGA_RLEMONO = 11,
00041     };
00042 
00043     <span class="comment">// TGA file header (same as BMP format, pixel store from left-bottom)</span>
00044 <span class="preprocessor">#pragma pack(1)</span>
00045 <span class="preprocessor"></span>    <span class="keyword">struct </span>TGAHEAD
00046     {
00047         BYTE    byID_Length ;   <span class="comment">// ͼ��ʶ����Ϣ��С</span>
00048         BYTE    byPalType ;     <span class="comment">// 00 : no-palette     01 : has-palette</span>
00049         BYTE    byImageType ;   <span class="comment">// ����</span>
00050         WORD    wPalFirstNdx ;  <span class="comment">// ��ɫ����ʼ����</span>
00051         WORD    wPalLength ;    <span class="comment">// ��ɫ�峤��</span>
00052         BYTE    byPalBits ;     <span class="comment">// ��ɫ����ÿһ��ɫ��ռλ��</span>
00053         WORD    wLeft ;         <span class="comment">// �������Ļ���½�X����</span>
00054         WORD    wBottom ;       <span class="comment">// �������Ļ���½�Y����</span>
00055         WORD    wWidth ;        <span class="comment">// width</span>
00056         WORD    wHeight ;       <span class="comment">// height</span>
00057         BYTE    byColorBits ;   <span class="comment">// bpp</span>
00058         <span class="keyword">struct</span>
00059 <span class="keyword">        </span>{
00060             BYTE    AlphaBits : 4 ; <span class="comment">// ÿ����Alpha Channelλ��</span>
00061             BYTE    HorMirror : 1 ; <span class="comment">// Ϊ1��ʾͼ���������ҵߵ��洢</span>
00062             BYTE    VerMirror : 1 ; <span class="comment">// Ϊ1��ʾͼ���������µߵ��洢</span>
00063             BYTE    Reserved : 2 ;
00064         } Descriptor ;
00065     }; <span class="comment">// 18 - Bytes</span>
00066 <span class="preprocessor">#pragma pack()</span>
00067 <span class="preprocessor"></span>};
00068 
00069 <span class="comment">//=============================================================================</span>
00070 <span class="comment">// inline Implement</span>
00071 <span class="comment">//=============================================================================</span>
00072 <span class="keyword">inline</span> <span class="keywordtype">bool</span> <a class="code" href="class_f_c_image_handle_base.html#a1">FCImageHandle_Tga::LoadImageMemory</a> (<span class="keyword">const</span> BYTE* pStart, <span class="keywordtype">int</span> nFileSize)
00073 {
00074     <span class="keyword">const</span> TGAHEAD   * pTga = (TGAHEAD*)pStart ;
00075     <span class="keywordflow">if</span> (!pStart)
00076         return false ;
00077     if ((pTga-&gt;byPalBits == 15) || (pTga-&gt;byPalBits == 16))
00078         return false ; <span class="comment">// not support 15 or 16 bit palette</span>
00079 
00080     <span class="comment">// create image</span>
00081     <a class="code" href="class_f_c_obj_image.html">FCObjImage</a>   * pImg = new <a class="code" href="class_f_c_obj_image.html">FCObjImage</a> ;
00082     if (!pImg-&gt;Create (pTga-&gt;wWidth, pTga-&gt;wHeight, (pTga-&gt;byColorBits == 15) ? 16 : pTga-&gt;byColorBits))
00083     {
00084         <span class="keyword">delete</span> pImg; <span class="keywordflow">return</span> false ;
00085     }
00086 
00087     <span class="comment">// set palette</span>
00088     <span class="keyword">const</span> BYTE     * pCurr = pStart + <span class="keyword">sizeof</span>(TGAHEAD) + pTga-&gt;byID_Length ;
00089     <span class="keywordflow">if</span> (pTga-&gt;byPalType == 1)
00090     {
00091         <span class="keywordflow">if</span> (pTga-&gt;wPalFirstNdx + pTga-&gt;wPalLength &gt; 256)
00092         {
00093             <span class="keyword">delete</span> pImg; <span class="keywordflow">return</span> false ;
00094         }
00095 
00096         RGBQUAD     pPal[256] = {0} ;
00097         <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i=0 ; i &lt; pTga-&gt;wPalLength ; i++)
00098         {
00099             PCL_B(&amp;pPal[pTga-&gt;wPalFirstNdx + i]) = *pCurr++ ;
00100             PCL_G(&amp;pPal[pTga-&gt;wPalFirstNdx + i]) = *pCurr++ ;
00101             PCL_R(&amp;pPal[pTga-&gt;wPalFirstNdx + i]) = *pCurr++ ;
00102             <span class="keywordflow">if</span> (pTga-&gt;byPalBits == 32)
00103                 PCL_A(&amp;pPal[pTga-&gt;wPalFirstNdx + i]) = *pCurr++ ;
00104         }
00105         pImg-&gt;SetColorTable (0, 256, pPal) ;
00106     }
00107 
00108     <span class="comment">// start decode</span>
00109     for (<span class="keywordtype">int</span> i=0 ; i &lt; pImg-&gt;Height() ; i++)
00110     {
00111         BYTE     * pDest ;
00112         <span class="keywordflow">if</span> (pStart[17] &amp; 0x20)
00113             pDest = pImg-&gt;GetBits(i) ; <span class="comment">// top to bottom</span>
00114         else
00115             pDest = pImg-&gt;GetBits(pImg-&gt;Height() - 1 - i) ; <span class="comment">// bottom to top</span>
00116 
00117         if ((pTga-&gt;byImageType == TGA_RLEPAL) || (pTga-&gt;byImageType == TGA_RLERGB) || (pTga-&gt;byImageType == TGA_RLEMONO)) <span class="comment">// ѹ��</span>
00118         {
00119             pCurr = <a class="code" href="class_f_c_compress.html#e3">FCCompress::RLE_TGA_DecodeLine</a> (pCurr, pImg-&gt;ColorBits(), pImg-&gt;Width(), pDest) ;
00120         }
00121         <span class="keywordflow">else</span> <span class="comment">// not-compressed</span>
00122         {
00123             <span class="keywordtype">int</span>   nPitch = pImg-&gt;Width() * pImg-&gt;ColorBits() / 8 ;
00124             memcpy (pDest, pCurr, nPitch) ;
00125             pCurr += nPitch ;
00126         }
00127     }
00128     <a class="code" href="class_p_c_l___interface___composite.html#a2">PCL_PushObject</a> (pImg) ; <span class="comment">// pImg's ownership</span>
00129     <span class="keywordflow">return</span> true ;
00130 }
00131 <span class="comment">//-----------------------------------------------------------------------------</span>
00132 <span class="keyword">inline</span> <span class="keywordtype">bool</span> <a class="code" href="class_f_c_image_handle_base.html#a2">FCImageHandle_Tga::SaveImage</a> (<span class="keyword">const</span> <span class="keywordtype">char</span>* szFileName, <span class="keywordtype">int</span> nFlag)
00133 {
00134     <span class="keywordflow">if</span> (<a class="code" href="class_f_c_image_handle_base.html#p0">m_SaveImg</a>.empty())
00135         <span class="keywordflow">return</span> false ;
00136     <span class="keyword">const</span> <a class="code" href="class_f_c_obj_image.html">FCObjImage</a>   &amp;img = *<a class="code" href="class_f_c_image_handle_base.html#p0">m_SaveImg</a>[0] ;
00137 
00138     <span class="comment">// validate</span>
00139     <span class="keywordflow">if</span> ((img.<a class="code" href="class_f_c_obj_image.html#z29_6">ColorBits</a>() != 8) &amp;&amp; (img.<a class="code" href="class_f_c_obj_image.html#z29_6">ColorBits</a>() != 16) &amp;&amp; (img.<a class="code" href="class_f_c_obj_image.html#z29_6">ColorBits</a>() != 24) &amp;&amp; (img.<a class="code" href="class_f_c_obj_image.html#z29_6">ColorBits</a>() != 32))
00140         <span class="keywordflow">return</span> false ;
00141 
00142     <span class="comment">// create image file</span>
00143     std::ofstream    outFile (szFileName, std::ios::out|std::ios::binary|std::ios::trunc) ;
00144     <span class="keywordflow">if</span> (!outFile.is_open())
00145         <span class="keywordflow">return</span> false ;
00146 
00147     <span class="comment">// Initialize TGA Header</span>
00148     <span class="keyword">const</span> BYTE   fTgaInfo[] = {<span class="stringliteral">"PhoXo -- TGA"</span>} ;
00149     TGAHEAD      TgaHead ;
00150     memset (&amp;TgaHead, 0, <span class="keyword">sizeof</span>(TgaHead)) ;
00151     TgaHead.byID_Length     = <span class="keyword">sizeof</span>(fTgaInfo) - 1 ; <span class="comment">// tga size</span>
00152     TgaHead.byPalType       = ((img.<a class="code" href="class_f_c_obj_image.html#z29_6">ColorBits</a>() == 8) ? 1 : 0) ;
00153 
00154     <span class="keywordflow">if</span> (nFlag == -1)
00155         TgaHead.byImageType = ((img.ColorBits() == 8) ? TGA_UCPAL : TGA_UCRGB) ;
00156     else if (nFlag == 1)
00157         TgaHead.byImageType = ((img.ColorBits() == 8) ? TGA_RLEPAL : TGA_RLERGB) ;
00158 
00159     TgaHead.wPalFirstNdx    = 0 ;
00160     TgaHead.wPalLength      = 256 ;
00161     TgaHead.byPalBits       = 24 ; <span class="comment">// palette's bit</span>
00162     TgaHead.wWidth          = img.Width() ;
00163     TgaHead.wHeight         = img.Height() ;
00164     TgaHead.byColorBits     = (BYTE)img.ColorBits() ;
00165     ((BYTE*)&amp;TgaHead)[17]   = 0x20 ; <span class="comment">// top to bottom</span>
00166     outFile.write ((<span class="keywordtype">char</span>*)&amp;TgaHead, sizeof(TGAHEAD)) ;
00167     outFile.write ((<span class="keywordtype">char</span>*)fTgaInfo, TgaHead.byID_Length) ;
00168 
00169     <span class="comment">// write palette</span>
00170     if (img.ColorBits() == 8)
00171     {
00172         RGBQUAD   pPal[256] ;
00173         img.<a class="code" href="class_f_c_obj_image.html#z33_0">GetColorTable</a> (0, 256, pPal) ;
00174         <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i=0 ; i &lt; 256 ; i++)
00175             outFile.write ((<span class="keywordtype">char</span>*)&amp;pPal[i], 3) ;
00176     }
00177 
00178     <span class="comment">// write pixels</span>
00179     const <span class="keywordtype">int</span>     nLineByte = img.ColorBits() * img.Width() / 8 ;
00180     if (nFlag == -1)
00181     {
00182         <span class="comment">// not compress</span>
00183         <span class="keywordflow">for</span> (<span class="keywordtype">int</span> y=0 ; y &lt; img.<a class="code" href="class_f_c_obj_image.html#z29_5">Height</a>() ; y++)
00184             outFile.write ((<span class="keywordtype">char</span>*)img.<a class="code" href="class_f_c_obj_image.html#z29_1">GetBits</a>(y), nLineByte) ;
00185     }
00186     <span class="keywordflow">else</span> <span class="keywordflow">if</span> (nFlag == 1)
00187     {
00188         <span class="comment">// RLE compress</span>
00189         <a class="code" href="class_p_c_l__array.html">PCL_array&lt;BYTE&gt;</a>   pStart (nLineByte * 2 + 4096) ;
00190         <span class="keywordflow">for</span> (<span class="keywordtype">int</span> y=0 ; y &lt; img.<a class="code" href="class_f_c_obj_image.html#z29_5">Height</a>() ; y++)
00191         {
00192             BYTE   * pEn = <a class="code" href="class_f_c_compress.html#e2">FCCompress::RLE_TGA_EncodeLine</a> (img.<a class="code" href="class_f_c_obj_image.html#z29_1">GetBits</a>(y), img.<a class="code" href="class_f_c_obj_image.html#z29_6">ColorBits</a>(), img.<a class="code" href="class_f_c_obj_image.html#z29_4">Width</a>(), pStart.get()) ;
00193             outFile.write ((<span class="keywordtype">char</span>*)pStart.get(), (int)(pEn - pStart.get())) ;
00194         }
00195     }
00196     <span class="keywordflow">return</span> true ;
00197 }
00198 
00199 <span class="preprocessor">#endif</span>
</pre></div><hr size="1"><address style="align: right;"><small>Generated on Wed Mar 8 11:12:46 2006 for ImageStone by&nbsp;
<a href="http://www.doxygen.org/index.html">
<img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.4.1 </small></address>
</body>
</html>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The zlib/libpng License

Share

About the Author

crazybit
Team Leader PhoXo
China China
graduate from University of Science and Technology of China at 2002.
 
Now I work at www.phoxo.com.

| Advertise | Privacy | Mobile
Web04 | 2.8.141015.1 | Last Updated 6 Dec 2011
Article Copyright 2006 by crazybit
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid