1 /*________________________________ epson-escpr-mem.c ________________________________*/
\r
3 /* 1 2 3 4 5 6 7 8 */
\r
4 /*34567890123456789012345678901234567890123456789012345678901234567890123456789012345678*/
\r
5 /*******************************************|********************************************/
\r
7 * Copyright (c) 2009 Seiko Epson Corporation All rights reserved.
\r
9 * Copyright protection claimed includes all forms and matters of copyrightable
\r
10 * material and information now allowed by statutory or judicial law or hereinafter
\r
11 * granted, including without limitation, material generated from the software
\r
12 * programs which are displayed on the screen such as icons, screen display looks,
\r
15 /*******************************************|********************************************/
\r
19 /* Public Function Calls */
\r
20 /* -------------------------- */
\r
21 /* void* memRealloc (buffer, oldSizem, newSize ); */
\r
22 /* EPS_INT8* memStrStr (strSrc, strFind, gotoEnd ); */
\r
23 /* EPS_INT8* memStrStrWithLen (strSrc, nSrcLen, strFind ); */
\r
24 /* void memSetEndian (Endianess, byteSize, endTag, value, array); */
\r
25 /* void memInspectEndian ( ); */
\r
26 /* EPS_INT16 memHtoN16 (nSrc ); */
\r
27 /* EPS_INT32 memHtoN32 (nSrc ); */
\r
28 /* EPS_INT32 memGetBitCount (bitfield ); */
\r
30 /*******************************************|********************************************/
\r
32 /*------------------------------------ Includes -------------------------------------*/
\r
33 /*******************************************|********************************************/
\r
34 #include "epson-escpr-def.h"
\r
35 #include "epson-escpr-err.h"
\r
36 #include "epson-escpr-mem.h"
\r
38 /*---------------------------- ESC/P-R Lib Global Variables --------------------------*/
\r
39 /*******************************************|********************************************/
\r
41 /*** Extern Function */
\r
42 /*** -------------------------------------------------------------------------------*/
\r
43 extern EPS_CMN_FUNC epsCmnFnc;
\r
45 EPS_INT16 cpuEndian; /* Endian-ness */
\r
48 /*-------------------------------- Local Definition ---------------------------------*/
\r
49 /*******************************************|********************************************/
\r
52 /*-------------------------------- Local Functions ----------------------------------*/
\r
53 /*******************************************|********************************************/
\r
57 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%|%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
\r
58 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%|%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
\r
59 /*%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%*/
\r
60 /*-------------------- Public Functions ---------------------*/
\r
61 /*%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%*/
\r
62 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%|%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
\r
63 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%|%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
\r
65 /*******************************************|********************************************/
\r
67 /* Function name: memRealloc() */
\r
71 /* Name: Type: Description: */
\r
72 /* buffer void* I/O: pointer of buffer */
\r
73 /* oldSize EPS_UINT32 I: original buffer size */
\r
74 /* newSize EPS_UINT32 I: new buffer size */
\r
77 /* EPS_INT8* pointer to finded string */
\r
80 /* Realocate buffer. */
\r
82 /*******************************************|********************************************/
\r
86 EPS_UINT32 oldSize,
\r
90 /* Create a temporary pointer to a new buffer of the desired size */
\r
91 void* newBuffer = EPS_ALLOC(newSize);
\r
92 if( NULL == newBuffer ){
\r
93 EPS_SAFE_RELEASE( buffer );
\r
97 memset(newBuffer, 0, newSize);
\r
99 /* Copy the data from the old buffer to the new one */
\r
100 if(oldSize < newSize)
\r
102 memcpy(newBuffer, buffer, oldSize);
\r
106 memcpy(newBuffer, buffer, newSize);
\r
108 /* Free the original buffer */
\r
109 EPS_SAFE_RELEASE( buffer );
\r
111 /* Return a pointer to the new block of memory */
\r
116 /*******************************************|********************************************/
\r
118 /* Function name: memStrStr() */
\r
122 /* Name: Type: Description: */
\r
123 /* strSrc EPS_INT8* I: Source string */
\r
124 /* strFind EPS_INT8* I: Find string. Size MUST be less than 64 byte. */
\r
125 /* gotoEnd EPS_BOOL I: If TRUE, the return value indicates */
\r
126 /* the end of found string. */
\r
128 /* Return value: */
\r
129 /* EPS_INT8* pointer to finded string */
\r
132 /* Finds the first occurrence of a substring within a string. */
\r
133 /* The comparison is NOT case sensitive. */
\r
135 /*******************************************|********************************************/
\r
136 EPS_INT8* memStrStr (
\r
139 const EPS_INT8* strFind,
\r
143 EPS_INT8 strShadow[64];
\r
144 EPS_INT32 nSrcPos = 0;
\r
145 EPS_INT32 nFindPos = 0;
\r
146 EPS_INT32 nFindedTop = -1;
\r
147 EPS_UINT32 nFindStrLen = 0;
\r
149 if(NULL == strSrc || 64 <= strlen(strFind)){
\r
153 memset(strShadow, 0, sizeof(strShadow));
\r
155 /* create shadow string */
\r
157 if( 0x41 <= strFind[nFindPos] && strFind[nFindPos] <= 0x5A){
\r
158 strShadow[nFindPos] = (EPS_INT8)(strFind[nFindPos] + 0x20);
\r
159 } else if( 0x61 <= strFind[nFindPos] && strFind[nFindPos] <= 0x7A){
\r
160 strShadow[nFindPos] = (EPS_INT8)(strFind[nFindPos] - 0x20);
\r
162 strShadow[nFindPos] = strFind[nFindPos];
\r
164 }while('\0' != strFind[nFindPos++]);
\r
166 /* search by strFind & strShadow */
\r
168 while( '\0' != strSrc[nSrcPos] && '\0' != strFind[nFindPos]){
\r
169 if( strSrc[nSrcPos] == strFind[nFindPos]
\r
170 || strSrc[nSrcPos] == strShadow[nFindPos] ){
\r
171 if(-1 == nFindedTop){
\r
172 nFindedTop = nSrcPos;
\r
179 if(-1 == nFindedTop){
\r
183 nSrcPos = nFindedTop + 1;
\r
189 nFindStrLen = (EPS_UINT32)strlen(strFind);
\r
190 if(0 <= nFindedTop && (EPS_INT32)strlen(strFind) == nFindPos){
\r
192 return &strSrc[nFindedTop];
\r
194 return &strSrc[nFindedTop] + nFindStrLen;
\r
202 /*******************************************|********************************************/
\r
204 /* Function name: memStrStrWithLen() */
\r
208 /* Name: Type: Description: */
\r
209 /* strSrc EPS_INT8* I: Source string */
\r
210 /* nSrcLen EPS_UINT32 I: Source string length */
\r
211 /* strFind EPS_INT8* I: Find string. Size MUST be less than 64 byte. */
\r
213 /* Return value: */
\r
214 /* EPS_INT8* pointer to finded string */
\r
217 /* Finds the first occurrence of a substring within a string. */
\r
218 /* The comparison is NOT case sensitive. */
\r
219 /* Limit nSrcLen characters. */
\r
221 /*******************************************|********************************************/
\r
222 EPS_INT8* memStrStrWithLen (
\r
225 EPS_UINT32 nSrcLen,
\r
226 const EPS_INT8* strFind
\r
229 EPS_INT8 strShadow[64];
\r
230 EPS_INT32 nSrcPos = 0;
\r
231 EPS_INT32 nFindPos = 0;
\r
232 EPS_INT32 nFindedTop = -1;
\r
234 if(NULL == strSrc || 64 <= strlen(strFind)){
\r
238 memset(strShadow, 0, sizeof(strShadow));
\r
240 /* create shadow string */
\r
242 if( 0x41 <= strFind[nFindPos] && strFind[nFindPos] <= 0x5A){
\r
243 strShadow[nFindPos] = (EPS_INT8)(strFind[nFindPos] + 0x20);
\r
244 } else if( 0x61 <= strFind[nFindPos] && strFind[nFindPos] <= 0x7A){
\r
245 strShadow[nFindPos] = (EPS_INT8)(strFind[nFindPos] - 0x20);
\r
247 strShadow[nFindPos] = strFind[nFindPos];
\r
249 }while('\0' != strFind[nFindPos++]);
\r
251 /* search by strFind & strShadow */
\r
253 while( (EPS_INT32)nSrcLen > nSrcPos && '\0' != strFind[nFindPos]){
\r
254 if( strSrc[nSrcPos] == strFind[nFindPos]
\r
255 || strSrc[nSrcPos] == strShadow[nFindPos] ){
\r
256 if(-1 == nFindedTop){
\r
257 nFindedTop = nSrcPos;
\r
263 if(-1 == nFindedTop){
\r
266 nSrcPos = nFindedTop + 1;
\r
273 if(0 <= nFindedTop && (EPS_INT32)strlen(strFind) == nFindPos){
\r
274 return &strSrc[nFindedTop];
\r
281 /*******************************************|********************************************/
\r
282 /* Function name: memSetEndian() */
\r
286 /* Name: Type: Description: */
\r
287 /* Endianess EPS_ENDIAN I: Desired Endianess (Big/Little) */
\r
288 /* byteSize EPS_BYTE_SIZE I: 2-byte or 4 bytes to convert */
\r
289 /* value EPS_UINT32 I: 4 Bytes to be swapped if necesaray */
\r
290 /* array EPS_UINT8* O: Correct endian-ness bytes */
\r
292 /* Return value: */
\r
296 /* Swap data depending on endian-ness. */
\r
298 /*******************************************|********************************************/
\r
299 void memSetEndian (
\r
301 EPS_ENDIAN Endianess, /* Desired Endianess (Big/Little) */
\r
302 EPS_BYTE_SIZE byteSize, /* 2-byte or 4 bytes to convert */
\r
303 EPS_UINT32 value, /* 4 Bytes to be swapped if necesaray */
\r
304 EPS_UINT8* array /* Correct endian-ness bytes */
\r
308 /*** Declare Variable Local to Routine */
\r
309 EPS_UINT16 value2byte;
\r
310 EPS_UINT32 value4byte;
\r
312 /*** Initialize Local Variables */
\r
314 /*** Based on desired Eniandess - Perform test and swap, if necessary */
\r
315 switch (byteSize + Endianess) {
\r
316 /*** Change 2 bytes value to the little endianness */
\r
317 case (EPS_2_BYTES + EPS_ENDIAN_LITTLE):
\r
318 #if 0 /* Not Used */
\r
319 value2byte = (EPS_UINT16)value;
\r
320 array[0] = (EPS_UINT8)((value2byte ) & 0x00ff);
\r
321 array[1] = (EPS_UINT8)((value2byte >> 8) & 0x00ff);
\r
324 /*** Change 2 bytes value to the big endianness */
\r
325 case (EPS_2_BYTES + EPS_ENDIAN_BIG):
\r
326 value2byte = (EPS_UINT16)value;
\r
327 array[0] = (EPS_UINT8)((value2byte >> 8) & 0x00ff);
\r
328 array[1] = (EPS_UINT8)((value2byte ) & 0x00ff);
\r
330 /*** Change 4 bytes value to the little endianness */
\r
331 case (EPS_4_BYTES + EPS_ENDIAN_LITTLE):
\r
332 value4byte = (EPS_UINT32)value;
\r
333 array[0] = (EPS_UINT8)((value4byte ) & 0x000000ff);
\r
334 array[1] = (EPS_UINT8)((value4byte >> 8) & 0x000000ff);
\r
335 array[2] = (EPS_UINT8)((value4byte >> 16) & 0x000000ff);
\r
336 array[3] = (EPS_UINT8)((value4byte >> 24) & 0x000000ff);
\r
338 /*** Change 4 bytes value to the big endianness */
\r
339 case (EPS_4_BYTES + EPS_ENDIAN_BIG):
\r
340 value4byte = (EPS_UINT32)value;
\r
341 array[0] = (EPS_UINT8)((value4byte >> 24) & 0x000000ff);
\r
342 array[1] = (EPS_UINT8)((value4byte >> 16) & 0x000000ff);
\r
343 array[2] = (EPS_UINT8)((value4byte >> 8) & 0x000000ff);
\r
344 array[3] = (EPS_UINT8)((value4byte ) & 0x000000ff);
\r
352 /*******************************************|********************************************/
\r
353 /* Function name: memInspectEndian() */
\r
357 /* Name: Type: Description: */
\r
360 /* Return value: */
\r
364 /* Determine "Endian-ness" for the current cpu. */
\r
366 /*******************************************|********************************************/
\r
367 void memInspectEndian (
\r
373 EPS_INT8 array[2]; /* Endian-ness test array */
\r
374 EPS_INT16 chars; /* Endian-ness test string */
\r
377 EndianTest.array[0] = 'a';
\r
378 EndianTest.array[1] = 'b';
\r
379 if (EndianTest.chars == 0x6162) {
\r
380 cpuEndian = EPS_ENDIAN_BIG;
\r
382 cpuEndian = EPS_ENDIAN_LITTLE;
\r
387 EPS_INT32 memGetBitCount(
\r
395 for(i = 0; i < sizeof(bitfield)*8; i++){
\r
396 if( (bitfield >> i) & 1 ){
\r
405 EPS_UINT8 memSearchWhiteColorVal(
\r
407 EPS_UINT8 colorPlane,
\r
408 EPS_UINT8* paletteData,
\r
409 EPS_UINT16 paletteSize
\r
412 EPS_UINT8 whiteColorValue = 255; /* Set default index for white to negative one */
\r
413 EPS_UINT16 jdx; /* General indes/loop variable */
\r
414 EPS_UINT8* bufPtr = NULL; /* Temporary buffer pointer */
\r
416 if( colorPlane == EPS_CP_256COLOR ){
\r
417 bufPtr = paletteData;
\r
418 for ( jdx = 0; jdx < paletteSize/3; jdx++ ) {
\r
419 if (*bufPtr == 255 && *(bufPtr+1) == 255 && *(bufPtr+2) == 255) {
\r
420 whiteColorValue = (EPS_UINT8)jdx;
\r
425 } else /* if(EPS_CP_FULLCOLOR) */{
\r
426 whiteColorValue = 0xFF;
\r
429 return whiteColorValue;
\r
432 /*________________________________ epson-escpr-mem.c _________________________________*/
\r
434 /*34567890123456789012345678901234567890123456789012345678901234567890123456789012345678*/
\r
435 /* 1 2 3 4 5 6 7 8 */
\r
436 /*******************************************|********************************************/
\r
437 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%|%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
\r
438 /***** End of File *** End of File *** End of File *** End of File *** End of File ******/
\r
439 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%|%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
\r