1 /* ************************************************************************** */
2 /* * * */
3 /* * COPYRIGHT NOTICE: * */
4 /* * * */
5 /* * Copyright (c) 2000,2002 Gerard Juyn (gerard@libmng.com) * */
6 /* * [You may insert additional notices after this sentence if you modify * */
7 /* * this source] * */
8 /* * * */
9 /* * For the purposes of this copyright and license, "Contributing Authors" * */
10 /* * is defined as the following set of individuals: * */
11 /* * * */
12 /* * Gerard Juyn * */
13 /* * (hopefully some more to come...) * */
14 /* * * */
15 /* * The MNG Library is supplied "AS IS". The Contributing Authors * */
16 /* * disclaim all warranties, expressed or implied, including, without * */
17 /* * limitation, the warranties of merchantability and of fitness for any * */
18 /* * purpose. The Contributing Authors assume no liability for direct, * */
19 /* * indirect, incidental, special, exemplary, or consequential damages, * */
20 /* * which may result from the use of the MNG Library, even if advised of * */
21 /* * the possibility of such damage. * */
22 /* * * */
23 /* * Permission is hereby granted to use, copy, modify, and distribute this * */
24 /* * source code, or portions hereof, for any purpose, without fee, subject * */
25 /* * to the following restrictions: * */
26 /* * * */
27 /* * 1. The origin of this source code must not be misrepresented; * */
28 /* * you must not claim that you wrote the original software. * */
29 /* * * */
30 /* * 2. Altered versions must be plainly marked as such and must not be * */
31 /* * misrepresented as being the original source. * */
32 /* * * */
33 /* * 3. This Copyright notice may not be removed or altered from any source * */
34 /* * or altered source distribution. * */
35 /* * * */
36 /* * The Contributing Authors specifically permit, without fee, and * */
37 /* * encourage the use of this source code as a component to supporting * */
38 /* * the MNG and JNG file format in commercial products. If you use this * */
39 /* * source code in a product, acknowledgment would be highly appreciated. * */
40 /* * * */
41 /* ************************************************************************** */
42 /* * * */
43 /* * project : bogus * */
44 /* * file : bogus.cpp copyright (c) 2000,2002 G.Juyn * */
45 /* * version : 1.0.1 * */
46 /* * * */
47 /* * purpose : main project file * */
48 /* * * */
49 /* * author : G.Juyn * */
50 /* * web : http://www.3-t.com * */
51 /* * email : mailto:info@3-t.com * */
52 /* * * */
53 /* * comment : bogus is (quite literally) a bogus sample which creates and* */
54 /* * writes a totally valid, be it somewhat trivial, MNG-file * */
55 /* * * */
56 /* * changes : 0.5.3 - 06/26/2000 - G.Juyn * */
57 /* * - changed userdata variable to mng_ptr * */
58 /* * 0.5.3 - 06/28/2000 - G.Juyn * */
59 /* * - changed memory allocation size parameters to mng_size_t * */
60 /* * * */
61 /* * 1.0.1 - 10/07/2002 - G.Juyn * */
62 /* * - fixed copyright notice * */
63 /* * - updated MHDR simplicity flag * */
64 /* * * */
65 /* ************************************************************************** */
66
67 #pragma hdrstop
68 #include <condefs.h>
69 #include <stdio.h>
70 #include <stdlib.h>
71 #include <mem.h>
72
73 #include "libmng.h"
74
75 /* ************************************************************************** */
76
77 USERES("bogus.res");
78 USELIB("..\win32dll\libmng.lib");
79 //---------------------------------------------------------------------------
80 typedef struct user_struct {
81
82 FILE *hFile; /* file handle */
83
84 } userdata;
85
86 typedef userdata * userdatap;
87
88 /* ************************************************************************** */
89
90 #define MY_DECL __stdcall /* get the right callback convention */
91
92 /* ************************************************************************** */
93
myalloc(mng_size_t iSize)94 mng_ptr MY_DECL myalloc (mng_size_t iSize)
95 {
96 return (mng_ptr)calloc (1, iSize); /* duh! */
97 }
98
99 /* ************************************************************************** */
100
101 #pragma argsused
myfree(mng_ptr pPtr,mng_size_t iSize)102 void MY_DECL myfree (mng_ptr pPtr, mng_size_t iSize)
103 {
104 free (pPtr); /* duh! */
105 return;
106 }
107
108 /* ************************************************************************** */
109
110 #pragma argsused
myopenstream(mng_handle hMNG)111 mng_bool MY_DECL myopenstream (mng_handle hMNG)
112 {
113 return MNG_TRUE; /* already opened in main function */
114 }
115
116 /* ************************************************************************** */
117
118 #pragma argsused
myclosestream(mng_handle hMNG)119 mng_bool MY_DECL myclosestream (mng_handle hMNG)
120 {
121 return MNG_TRUE; /* gets closed in main function */
122 }
123
124 /* ************************************************************************** */
125
mywritedata(mng_handle hMNG,mng_ptr pBuf,mng_uint32 iSize,mng_uint32 * iWritten)126 mng_bool MY_DECL mywritedata (mng_handle hMNG,
127 mng_ptr pBuf,
128 mng_uint32 iSize,
129 mng_uint32 *iWritten)
130 { /* get to my file handle */
131 userdatap pMydata = (userdatap)mng_get_userdata (hMNG);
132 /* write it */
133 *iWritten = fwrite (pBuf, 1, iSize, pMydata->hFile);
134 /* iWritten will indicate errors */
135 return MNG_TRUE;
136 }
137
138 /* ************************************************************************** */
139
makeimage(char * zFilename)140 int makeimage (char * zFilename)
141 {
142 userdatap pMydata;
143 mng_handle hMNG;
144 mng_retcode iRC;
145 /* get a data buffer */
146 pMydata = (userdatap)calloc (1, sizeof (userdata));
147
148 if (pMydata == NULL) /* oke ? */
149 {
150 fprintf (stderr, "Cannot allocate a data buffer.\n");
151 return 1;
152 }
153 /* can we open the file ? */
154 if ((pMydata->hFile = fopen (zFilename, "wb")) == NULL)
155 { /* error out if we can't */
156 fprintf (stderr, "Cannot open output file %s.\n", zFilename);
157 return 1;
158 }
159 /* let's initialize the library */
160 hMNG = mng_initialize ((mng_ptr)pMydata, myalloc, myfree, MNG_NULL);
161
162 if (!hMNG) /* did that work out ? */
163 {
164 fprintf (stderr, "Cannot initialize libmng.\n");
165 iRC = 1;
166 }
167 else
168 { /* setup callbacks */
169 if ( ((iRC = mng_setcb_openstream (hMNG, myopenstream )) != 0) ||
170 ((iRC = mng_setcb_closestream (hMNG, myclosestream)) != 0) ||
171 ((iRC = mng_setcb_writedata (hMNG, mywritedata )) != 0) )
172 fprintf (stderr, "Cannot set callbacks for libmng.\n");
173 else
174 { /* create the file in memory */
175 if ( ((iRC = mng_create (hMNG) ) != 0) ||
176 ((iRC = mng_putchunk_mhdr (hMNG, 640, 480, 1000, 3, 1, 3, 0x0047) ) != 0) ||
177 ((iRC = mng_putchunk_basi (hMNG, 640, 160, 8, 2, 0, 0, 0, 0xFF, 0x00, 0x00, 0xFF, 1)) != 0) ||
178 ((iRC = mng_putchunk_iend (hMNG) ) != 0) ||
179 ((iRC = mng_putchunk_defi (hMNG, 0, 0, 0, MNG_TRUE, 0, 160, MNG_FALSE, 0, 0, 0, 0 )) != 0) ||
180 ((iRC = mng_putchunk_basi (hMNG, 640, 160, 8, 2, 0, 0, 0, 0xFF, 0xFF, 0xFF, 0xFF, 1)) != 0) ||
181 ((iRC = mng_putchunk_iend (hMNG) ) != 0) ||
182 ((iRC = mng_putchunk_defi (hMNG, 0, 0, 0, MNG_TRUE, 0, 320, MNG_FALSE, 0, 0, 0, 0 )) != 0) ||
183 ((iRC = mng_putchunk_basi (hMNG, 640, 160, 8, 2, 0, 0, 0, 0x00, 0x00, 0xFF, 0xFF, 1)) != 0) ||
184 ((iRC = mng_putchunk_iend (hMNG) ) != 0) ||
185 ((iRC = mng_putchunk_mend (hMNG) ) != 0) )
186 fprintf (stderr, "Cannot create the chunks for the image.\n");
187 else
188 {
189 if ((iRC = mng_write (hMNG)) != 0)
190 fprintf (stderr, "Cannot write the image.\n");
191
192 }
193 }
194
195 mng_cleanup (&hMNG); /* cleanup the library */
196 }
197
198 fclose (pMydata->hFile); /* cleanup */
199 free (pMydata);
200
201 return iRC;
202 }
203
204 /* ************************************************************************** */
205
206 #pragma argsused
main(int argc,char * argv[])207 int main (int argc, char *argv[])
208 {
209 return makeimage ("dutch.mng");
210 }
211
212 /* ************************************************************************** */
213