@@ -295,6 +295,41 @@ static void convert8888to32(int width, int height,
295
295
static void convert8888to32_crop (int width , int height , int * outWidth , int * outHeight ,
296
296
unsigned char * inbuffer ,
297
297
unsigned char * outbuffer )
298
+ {
299
+ unsigned int i ;
300
+ int minX = 160 + 32 ;
301
+ int maxX = 1119 - 32 ;
302
+ int minY = 0 + 24 ;
303
+ int maxY = 719 - 24 ;
304
+ int pixels = 0 ;
305
+
306
+ for (i = 0 ; i < (unsigned int ) height * width ; i ++ )
307
+ {
308
+ int x = i %width ;
309
+ int y = i /width ;
310
+ if ((minX >= 0 && x >= minX ) && (maxX < 0 || x <= maxX ) && (minY >= 0 && y >= minY ) && (maxY < 0 || y <= maxY ))
311
+ {
312
+ /* BLUE = 0 */
313
+ outbuffer [(pixels <<2 )+ Blue ] = inbuffer [i * 4 + srcBlue ];
314
+ /* GREEN = 1 */
315
+ outbuffer [(pixels <<2 )+ Green ] = inbuffer [i * 4 + srcGreen ];
316
+ /* RED = 2 */
317
+ outbuffer [(pixels <<2 )+ Red ] = inbuffer [i * 4 + srcRed ];
318
+ /* ALPHA */
319
+ outbuffer [(pixels <<2 )+ Alpha ] = /*srcAlpha >= 0 ? inbuffer[i*4+srcAlpha] :*/ 0 ;
320
+ pixels ++ ;
321
+ }
322
+ }
323
+
324
+ * outWidth = maxX - minX + 1 ;
325
+ * outHeight = maxY - minY + 1 ;
326
+ fprintf (stderr , "minx=%d maxx=%d miny=%d maxy=%d\n" , minX , maxX , minY , maxY );
327
+ fprintf (stderr , "width=%d height=%d\n" , * outWidth , * outHeight );
328
+ }
329
+
330
+ static void convert8888to32_crop_auto (int width , int height , int * outWidth , int * outHeight ,
331
+ unsigned char * inbuffer ,
332
+ unsigned char * outbuffer )
298
333
{
299
334
unsigned int i ;
300
335
int minX = -1 ;
@@ -339,8 +374,14 @@ static void convert8888to32_crop(int width, int height, int* outWidth, int* outH
339
374
* outHeight = maxY - minY + 1 ;
340
375
fprintf (stderr , "minx=%d maxx=%d miny=%d maxy=%d\n" , minX , maxX , minY , maxY );
341
376
fprintf (stderr , "width=%d height=%d\n" , * outWidth , * outHeight );
377
+ if (* outWidth <= 2 || * outHeight <= 2 )
378
+ {
379
+ fprintf (stderr , "invalid size, trying other cropping mode\n" );
380
+ convert8888to32_crop (width , height , outWidth , outHeight , inbuffer , outbuffer );
381
+ }
342
382
}
343
383
384
+
344
385
static void write_PNG (unsigned char * outbuffer , char * filename ,
345
386
int width , int height , int interlace , int compression )
346
387
{
@@ -381,7 +422,7 @@ static void write_PNG(unsigned char *outbuffer, char *filename,
381
422
382
423
png_init_io (png_ptr , outfile );
383
424
384
- png_set_compression_level (png_ptr , Z_BEST_COMPRESSION );
425
+ png_set_compression_level (png_ptr , compression );
385
426
386
427
bit_depth = 8 ;
387
428
color_type = PNG_COLOR_TYPE_RGB_ALPHA ;
@@ -440,7 +481,7 @@ static void convert_and_write(unsigned char *inbuffer, char *filename,
440
481
break ;
441
482
case 32 :
442
483
// convert8888to32(width, height, inbuffer, outbuffer);
443
- convert8888to32_crop (width , height , & outWidth , & outHeight , inbuffer , outbuffer );
484
+ convert8888to32_crop_auto (width , height , & outWidth , & outHeight , inbuffer , outbuffer );
444
485
write_PNG (outbuffer , filename , outWidth , outHeight , interlace , compression );
445
486
break ;
446
487
default :
0 commit comments