tstcodec.c

来自「君正早期ucos系统(只有早期的才不没有打包成库),MPLAYER,文件系统,图」· C语言 代码 · 共 523 行 · 第 1/2 页

C
523
字号
/* *  A sequence of test procedures for this JBIG implementation *  *  Run this test sequence after each modification on the JBIG library. * *  Markus Kuhn -- http://www.cl.cam.ac.uk/~mgk25/ * *  $Id: tstcodec.c,v 1.1.1.1 2007/12/04 09:35:33 xliu Exp $ */#include <stdio.h>#include <stdlib.h>#include <stddef.h>#include <string.h>#include "jbig.h"#define TESTBUF_SIZE 400000L#define TESTPIC_SIZE 477995L#define FAILED "F\bFA\bAI\bIL\bLE\bED\bD"#define PASSED "PASSED"unsigned char *testbuf;unsigned char *testpic;long testbuf_len;static void *checkedmalloc(size_t n){  void *p;    if ((p = malloc(n)) == NULL) {    fprintf(stderr, "Sorry, not enough memory available!\n");    exit(1);  }    return p;}static void testbuf_write(int v, void *dummy){  if (testbuf_len < TESTBUF_SIZE)    testbuf[testbuf_len++] = v;  (void) dummy;  return;}static void testbuf_writel(unsigned char *start, size_t len, void *dummy){  if (testbuf_len < TESTBUF_SIZE) {    if (testbuf_len + len < TESTBUF_SIZE)      memcpy(testbuf + testbuf_len, start, len);    else      memcpy(testbuf + testbuf_len, start, TESTBUF_SIZE - testbuf_len);  }  testbuf_len += len;#ifdef DEBUG  {    unsigned char *p;    unsigned sum = 0;        for (p = start; p - start < (ptrdiff_t) len; sum = (sum ^ *p++) << 1);    printf("  testbuf_writel: %4d bytes, checksum %04x\n",	   len, sum & 0xffff);  }#endif  (void) dummy;  return;}/* * Store the artificial test image defined in T.82, clause 7.2.1 at * pic. The image requires 477995 bytes of memory, is 1960 x 1951 pixels * large and has one plane. */ static void testimage(unsigned char *pic){  unsigned long i, j, sum;  unsigned int prsg, repeat[8];  unsigned char *p;    memset(pic, 0, TESTPIC_SIZE);  p = pic;  prsg = 1;  for (j = 0; j < 1951; j++)    for (i = 0; i < 1960; i++) {      if (j >= 192) {	if (j < 1023 || ((i >> 3) & 3) == 0) {	  sum = (prsg & 1) + ((prsg >> 2) & 1) + ((prsg >> 11) & 1) +	    ((prsg >> 15) & 1);	  prsg = (prsg << 1) + (sum & 1);	  if ((prsg & 3) == 0) {	    *p |= 1 << (7 - (i & 7));	    repeat[i & 7] = 1;	  } else {	    repeat[i & 7] = 0;	  }	} else {	  if (repeat[i & 7])	    *p |= 1 << (7 - (i & 7));	}      }      if ((i & 7) == 7) ++p;    }  /* verify test image */  sum = 0;  for (i = 0; i < TESTPIC_SIZE; i++)    for (j = 0; j < 8; j++)      sum += (pic[i] >> j) & 1;  if (sum != 861965L)    printf("WARNING: Artificial test image has %lu (not 861965) "	   "foreground pixels!\n", sum);  return;}  /* * Perform a full test cycle with one set of parameters. Encode an image * and compare the length of the result with correct_length. Then decode * the image again both in one single chunk or byte by byte and compare * the results with the original input image. */static int test_cycle(unsigned char **orig_image, int width, int height,		      int options, int order, int layers, int planes,		      unsigned long l0, int mx, long correct_length,		      const char *test_id){  struct jbg_enc_state sje;  struct jbg_dec_state sjd;  int trouble = 0;  long l;  size_t plane_size;  int i, result;  unsigned char **image;  plane_size = ((width + 7) / 8) * height;  image = (unsigned char **) checkedmalloc(planes * sizeof(unsigned char *));  for (i = 0; i < planes; i++) {    image[i] = (unsigned char *) checkedmalloc(plane_size);    memcpy(image[i], orig_image[i], plane_size);  }  printf("\nTest %s.1: Encoding ...\n", test_id);  testbuf_len = 0;  jbg_enc_init(&sje, width, height, planes, image, testbuf_writel, NULL);  jbg_enc_layers(&sje, layers);  jbg_enc_options(&sje, order, options, l0, mx, 0);  jbg_enc_out(&sje);  jbg_enc_free(&sje);  for (i = 0; i < planes; i++)    free(image[i]);  free(image);  printf("Encoded BIE has %6ld bytes: ", testbuf_len);  if (correct_length >= 0)    if (testbuf_len == correct_length)      puts(PASSED);    else {      trouble++;      printf(FAILED ", correct would have been %ld\n", correct_length);    }  else    puts("");    printf("Test %s.2: Decoding whole chunk ...\n", test_id);  jbg_dec_init(&sjd);  result = jbg_dec_in(&sjd, testbuf, testbuf_len, NULL);  if (result != JBG_EOK) {    printf("Decoder complained with return value %d: " FAILED "\n"	   "Cause: '%s'\n", result, jbg_strerror(result, JBG_EN));    trouble++;  } else {    printf("Image comparison: ");    result = 1;    for (i = 0; i < planes; i++) {      if (memcmp(orig_image[i], sjd.lhp[layers & 1][i],		 ((width + 7) / 8) * height)) {	result = 0;	trouble++;	printf(FAILED " for plane %d\n", i);      }    }    if (result)      puts(PASSED);  }  jbg_dec_free(&sjd);  printf("Test %s.3: Decoding with single-byte feed ...\n", test_id);  jbg_dec_init(&sjd);  result = JBG_EAGAIN;  for (l = 0; l < testbuf_len; l++) {    result = jbg_dec_in(&sjd, testbuf + l, 1, NULL);    if (l < testbuf_len - 1 && result != JBG_EAGAIN) {      printf("Decoder complained with return value %d at byte %ld: " FAILED	     "\nCause: '%s'\n", result, l, jbg_strerror(result, JBG_EN));      trouble++;      break;    }  }  if (l == testbuf_len) {    if (result != JBG_EOK) {      printf("Decoder complained with return value %d at final byte: " FAILED	     "\nCause: '%s'\n", result, jbg_strerror(result, JBG_EN));      trouble++;    } else {      printf("Image comparison: ");      result = 1;      for (i = 0; i < planes; i++) {	if (memcmp(orig_image[i], sjd.lhp[layers & 1][i],		   ((width + 7) / 8) * height)) {	  result = 0;	  trouble++;	  printf(FAILED " for plane %d\n", i);	}      }      if (result)	puts(PASSED);    }  }  jbg_dec_free(&sjd);  puts("");    return trouble != 0;}int main(int argc, char **argv){  int trouble, problems = 0;  struct jbg_arenc_state *se;  struct jbg_ardec_state *sd;  long i;  int pix, order, layers;  char test[10];  size_t st;  unsigned char *pp;  unsigned char *ppp[4];  int t82pix[16] = {    0x05e0, 0x0000, 0x8b00, 0x01c4, 0x1700, 0x0034, 0x7fff, 0x1a3f,    0x951b, 0x05d8, 0x1d17, 0xe770, 0x0000, 0x0000, 0x0656, 0x0e6a  };  int t82cx[16] = {    0x0fe0, 0x0000, 0x0f00, 0x00f0, 0xff00, 0x0000, 0x0000, 0x0000,    0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000  };  unsigned char t82sde[32] = {    0x69, 0x89, 0x99, 0x5c, 0x32, 0xea, 0xfa, 0xa0,    0xd5, 0xff, 0x00, 0x52, 0x7f, 0xff, 0x00, 0xff,    0x00, 0xff, 0x00, 0xc0, 0x00, 0x00, 0x00, 0x3f,    0xff, 0x00, 0x2d, 0x20, 0x82, 0x91, 0xff, 0x02  };  /* three 23x5 pixel test images with the letters JBIG */

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?