Mercurial > hg > dmlib
comparison tools/lib64gfx.c @ 1505:3265175b24d2
Change the passing of RLE compression/decompression parameters to be in a dedicated struct.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Fri, 11 May 2018 20:18:14 +0300 |
parents | c7b9ef56319b |
children | 4fd4e7a00db4 |
comparison
equal
deleted
inserted
replaced
1504:15c77c6fbb5e | 1505:3265175b24d2 |
---|---|
246 { | 246 { |
247 | 247 |
248 } | 248 } |
249 | 249 |
250 | 250 |
251 int dmDecodeGenericRLE(DMGrowBuf *dst, const Uint8 *src, const Uint8 *srcEnd, | 251 int dmDecodeGenericRLE(DMGrowBuf *dst, const Uint8 *src, const Uint8 *srcEnd, const DMCompParams *cfg) |
252 const Uint8 rleMarker, const Uint8 rleMask1, const Uint8 rleMask2, const int rleType) | |
253 { | 252 { |
254 int res; | 253 int res; |
255 | 254 |
256 if ((res = dmGrowBufAlloc(dst, BUF_SIZE_INITIAL, BUF_SIZE_GROW)) != DMERR_OK) | 255 if ((res = dmGrowBufAlloc(dst, BUF_SIZE_INITIAL, BUF_SIZE_GROW)) != DMERR_OK) |
257 goto err; | 256 goto err; |
260 while (src < srcEnd) | 259 while (src < srcEnd) |
261 { | 260 { |
262 Uint8 data = *src++; | 261 Uint8 data = *src++; |
263 int count = 1; | 262 int count = 1; |
264 | 263 |
265 switch (rleType) | 264 switch (cfg->type) |
266 { | 265 { |
267 case DM_RLE_MARKER: | 266 case DM_COMP_RLE_MARKER: |
268 // A simple marker byte RLE variant: [Marker] [count] [data] | 267 // A simple marker byte RLE variant: [Marker] [count] [data] |
269 if (data == rleMarker) | 268 if (data == cfg->rleMarker) |
270 { | 269 { |
271 if (srcEnd - src < 2) | 270 if (srcEnd - src < 2) |
272 { | 271 { |
273 res = DMERR_INVALID_DATA; | 272 res = DMERR_INVALID_DATA; |
274 goto err; | 273 goto err; |
276 count = *src++; | 275 count = *src++; |
277 data = *src++; | 276 data = *src++; |
278 } | 277 } |
279 break; | 278 break; |
280 | 279 |
281 case DM_RLE_MASK: | 280 case DM_COMP_RLE_MASK: |
282 // Mask marker RLE: usually high bit(s) of byte mark RLE sequence | 281 // Mask marker RLE: usually high bit(s) of byte mark RLE sequence |
283 // and the lower bits contain the count: [Mask + count] [data] | 282 // and the lower bits contain the count: [Mask + count] [data] |
284 if ((data & rleMask1) == rleMarker) | 283 if ((data & cfg->rleMask1) == cfg->rleMarker) |
285 { | 284 { |
286 if (srcEnd - src < 1) | 285 if (srcEnd - src < 1) |
287 { | 286 { |
288 res = DMERR_INVALID_DATA; | 287 res = DMERR_INVALID_DATA; |
289 goto err; | 288 goto err; |
290 } | 289 } |
291 | 290 |
292 count = data & rleMask2; | 291 count = data & cfg->rleMask2; |
293 data = *src++; | 292 data = *src++; |
294 } | 293 } |
295 break; | 294 break; |
296 } | 295 } |
297 | 296 |
310 err: | 309 err: |
311 return res; | 310 return res; |
312 } | 311 } |
313 | 312 |
314 | 313 |
315 int dmDecodeGenericRLEAlloc(DMGrowBuf *dst, const Uint8 *src, const Uint8 *srcEnd, | 314 int dmDecodeGenericRLEAlloc(DMGrowBuf *dst, const Uint8 *src, const Uint8 *srcEnd, const DMCompParams *cfg) |
316 const Uint8 rleMarker, const Uint8 rleMask1, const Uint8 rleMask2, const int rleType) | |
317 { | 315 { |
318 int res; | 316 int res; |
319 if ((res = dmGrowBufAlloc(dst, BUF_SIZE_INITIAL, BUF_SIZE_GROW)) != DMERR_OK) | 317 if ((res = dmGrowBufAlloc(dst, BUF_SIZE_INITIAL, BUF_SIZE_GROW)) != DMERR_OK) |
320 return res; | 318 return res; |
321 | 319 |
322 return dmDecodeGenericRLE(dst, src, srcEnd, rleMarker, rleMask1, rleMask2, rleType); | 320 return dmDecodeGenericRLE(dst, src, srcEnd, cfg); |
323 } | 321 } |
324 | 322 |
325 | 323 |
326 static BOOL dmEncodeGenericRLESequence(DMGrowBuf *dst, const Uint8 data, Uint8 count, const Uint8 rleMarker, const Uint8 rleMinCount, const int rleType) | 324 static BOOL dmEncodeGenericRLESequence(DMGrowBuf *dst, const Uint8 data, Uint8 count, const DMCompParams *cfg) |
327 { | 325 { |
328 BOOL copyOnly = FALSE; | 326 BOOL copyOnly = FALSE; |
329 switch (rleType) | 327 |
330 { | 328 switch (cfg->type) |
331 case DM_RLE_MARKER: | 329 { |
332 if (count >= rleMinCount || data == rleMarker) | 330 case DM_COMP_RLE_MARKER: |
331 if (count >= cfg->rleMinCount || data == cfg->rleMarker) | |
333 { | 332 { |
334 // A simple marker byte RLE variant: [Marker] [count] [data] | 333 // A simple marker byte RLE variant: [Marker] [count] [data] |
335 if (!dmGrowBufPutU8(dst, rleMarker) || | 334 if (!dmGrowBufPutU8(dst, cfg->rleMarker) || |
336 !dmGrowBufPutU8(dst, count) || | 335 !dmGrowBufPutU8(dst, count) || |
337 !dmGrowBufPutU8(dst, data)) | 336 !dmGrowBufPutU8(dst, data)) |
338 return FALSE; | 337 return FALSE; |
339 } | 338 } |
340 else | 339 else |
341 copyOnly = TRUE; | 340 copyOnly = TRUE; |
342 break; | 341 break; |
343 | 342 |
344 case DM_RLE_MASK: | 343 case DM_COMP_RLE_MASK: |
345 if (count >= rleMinCount || (data & rleMarker) == rleMarker) | 344 if (count >= cfg->rleMinCount || (data & cfg->rleMarker) == cfg->rleMarker) |
346 { | 345 { |
347 // Mask marker RLE: usually high bit(s) of byte mark RLE sequence | 346 // Mask marker RLE: usually high bit(s) of byte mark RLE sequence |
348 // and the lower bits contain the count: [Mask + count] [data] | 347 // and the lower bits contain the count: [Mask + count] [data] |
349 if (!dmGrowBufPutU8(dst, rleMarker | count) || | 348 if (!dmGrowBufPutU8(dst, cfg->rleMarker | count) || |
350 !dmGrowBufPutU8(dst, data)) | 349 !dmGrowBufPutU8(dst, data)) |
351 return FALSE; | 350 return FALSE; |
352 } | 351 } |
353 else | 352 else |
354 copyOnly = TRUE; | 353 copyOnly = TRUE; |
366 | 365 |
367 return TRUE; | 366 return TRUE; |
368 } | 367 } |
369 | 368 |
370 | 369 |
371 int dmEncodeGenericRLE(DMGrowBuf *dst, const Uint8 *src, const Uint8 *srcEnd, | 370 int dmEncodeGenericRLE(DMGrowBuf *dst, const Uint8 *src, const Uint8 *srcEnd, const DMCompParams *cfg) |
372 const Uint8 rleMarker, const Uint8 rleMinCount, const Uint8 rleMaxCount, const int rleType) | |
373 { | 371 { |
374 int res; | 372 int res; |
373 | |
374 fprintf(stderr, "encrle: len=%d, rleMarker=%02x, rleMinCount=%d, rleMaxCount=%d, type=%d\n", | |
375 srcEnd - src, cfg->rleMarker, cfg->rleMinCount, cfg->rleMaxCount, cfg->type); | |
375 | 376 |
376 // Perform RLE encoding | 377 // Perform RLE encoding |
377 int count = 0, prev; | 378 int count = 0, prev; |
378 while (src < srcEnd) | 379 while (src < srcEnd) |
379 { | 380 { |
380 Uint8 data = *src++; | 381 Uint8 data = *src++; |
381 | 382 |
382 if (data != prev || count >= rleMaxCount) | 383 if (data != prev || count >= cfg->rleMaxCount) |
383 { | 384 { |
384 if (!dmEncodeGenericRLESequence(dst, prev, count, rleMarker, rleMinCount, rleType)) | 385 if (!dmEncodeGenericRLESequence(dst, prev, count, cfg)) |
385 { | 386 { |
386 res = dmError(DMERR_MALLOC, | 387 res = dmError(DMERR_MALLOC, |
387 "Could reallocate memory for RLE encoding buffer.\n"); | 388 "Could reallocate memory for RLE encoding buffer.\n"); |
388 goto err; | 389 goto err; |
389 } | 390 } |
401 err: | 402 err: |
402 return res; | 403 return res; |
403 } | 404 } |
404 | 405 |
405 | 406 |
406 int dmEncodeGenericRLEAlloc(DMGrowBuf *dst, const Uint8 *src, const Uint8 *srcEnd, | 407 int dmEncodeGenericRLEAlloc(DMGrowBuf *dst, const Uint8 *src, const Uint8 *srcEnd, const DMCompParams *cfg) |
407 const Uint8 rleMarker, const Uint8 rleMinCount, const Uint8 rleMaxCount, const int rleType) | |
408 { | 408 { |
409 int res; | 409 int res; |
410 if ((res = dmGrowBufAlloc(dst, BUF_SIZE_INITIAL, BUF_SIZE_GROW)) != DMERR_OK) | 410 if ((res = dmGrowBufAlloc(dst, BUF_SIZE_INITIAL, BUF_SIZE_GROW)) != DMERR_OK) |
411 return res; | 411 return res; |
412 | 412 |
413 return dmEncodeGenericRLE(dst, src, srcEnd, rleMarker, rleMinCount, rleMaxCount, rleType); | 413 return dmEncodeGenericRLE(dst, src, srcEnd, cfg); |
414 } | 414 } |
415 | 415 |
416 | 416 |
417 // Perform probing of the given data buffer, trying to determine | 417 // Perform probing of the given data buffer, trying to determine |
418 // if it contains a supported "C64" image format. Returns the | 418 // if it contains a supported "C64" image format. Returns the |