⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wm_bruyn_e.c

📁 包含了多个matlab编程在图像中加入水印的处理代码
💻 C
📖 第 1 页 / 共 2 页
字号:
    double mean_1A, mean_1B, mean_2A, mean_2B, mean_1, mean_2;    double mean__1A, mean__1B, mean__2A, mean__2B;    int n_1A, n_1B, n_2A, n_2B, n_1, n_2;    int var_1A, var_1B, var_2A, var_2B;    int zone1_ok, zone2_ok;    // find an unused block randomly, depending on seed    do {      xb = random() % (cols / blocksize);      yb = random() % (rows / blocksize);    } while (add_coord(coords, xb, yb) < 0);    // copy image block    copy_grays_to_block(block, image, xb * blocksize, yb * blocksize, blocksize, blocksize);        if (verbose > 0)      fprintf(stderr, "embedding bit #%d (= %d) in block at (%d/%d)\n", n, get_signature_bit(n), xb * blocksize, yb * blocksize);    if (verbose > 8) {      print_grays(image, xb * blocksize, yb * blocksize, blocksize, blocksize);      fprintf(stderr, "\n");    }    // sort luminance values in block to represent increasing function F    sort_grays(block[0], n_block);    if (verbose > 8) {      print_grays(block, 0, 0, blocksize, blocksize);      fprintf(stderr, "\n");    }    // calculate slopes of F and determine smax, the max. slope of F    // the index where smax occures is called alpha    alpha = 0;    smax = 0.0;    for (i = 0; i < n_block - 1; i++) {      slope[i] = block[0][i + 1] - block[0][i];      if (slope[i] > smax) {        smax = slope[i];        alpha = i;      }    }    slope[n_block - 1] = 0;    // block type classification    blocktype = BLOCKTYPE_UNKNOWN;    if (smax < threshold_noise) {      // block has noise contrast            blocktype = BLOCKTYPE_NOISE;      beta_minus = beta_plus = alpha;    }    else {      // block has progressive or hard contrast, let's find out...      beta_minus = alpha - 1;      while (beta_minus >= 0 && smax - slope[beta_minus] <= threshold_slope)        beta_minus--;      beta_plus = alpha + 1;      while (beta_plus < n_block && smax - slope[beta_plus] <= threshold_slope)        beta_plus++;      if (beta_minus + 1 == alpha && beta_plus - 1 == alpha)        blocktype = BLOCKTYPE_HARD;      else         blocktype = BLOCKTYPE_PROGRESSIVE;    }    if (verbose > 1) {      fprintf(stderr, "blocktype: %d\n", blocktype);       fprintf(stderr, "Smax = %lf, alpha = %d, beta- = %d, beta+ = %d\n", smax, alpha, beta_minus, beta_plus);    }    // block pixel classification    for (i = 0; i < blocksize; i++)      for (j = 0; j < blocksize; j++) {        gray pixel = image[yb * blocksize + j][xb * blocksize + i];        zone[j][i] = ZONE_VOID;        switch (blocktype) {          case BLOCKTYPE_PROGRESSIVE:          case BLOCKTYPE_HARD:            if (pixel < block[0][beta_minus])              zone[j][i] = ZONE_1;              else if (pixel > block[0][beta_plus])              zone[j][i] = ZONE_2;              break;          case BLOCKTYPE_NOISE:            if (pixel < block[0][n_block / 2])              zone[j][i] = ZONE_1;            else if (pixel > block[0][n_block / 2])              zone[j][i] = ZONE_2;            break;          default:            fprintf(stderr, "%s: invalid block type\n", progname);            break;        }      }     if (verbose > 8) {      print_grays(zone, 0, 0, blocksize, blocksize);      fprintf(stderr, "\n");    }    // calculate mean values for zone/categories    mean_1A = mean_1B = mean_2A = mean_2B = mean_1 = mean_2 = 0.0;    n_1A = n_1B = n_2A = n_2B = n_1 = n_2 = 0;    for (i = 0; i < blocksize; i++)      for (j = 0; j < blocksize; j++) {        gray pixel = image[yb * blocksize + j][xb * blocksize + i];        int pixel_zone = zone[j][i];        int pixel_category = CATEGORY_VOID;        if (pixel_zone == ZONE_1)          pixel_category = category1[j][i];        else if (pixel_zone == ZONE_2)          pixel_category = category2[j][i];        switch (pixel_zone | pixel_category) {          case CLASSIFICATION_1A:            n_1++;            n_1A++;            mean_1A += pixel;            mean_1 += pixel;            break;          case CLASSIFICATION_1B:            n_1++;            n_1B++;            mean_1B += pixel;            mean_1 += pixel;            break;          case CLASSIFICATION_2A:            n_2++;            n_2A++;            mean_2A += pixel;            mean_2 += pixel;            break;          case CLASSIFICATION_2B:            n_2++;            n_2B++;            mean_2B += pixel;            mean_2 += pixel;            break;        }      }    if (n_1 && n_1A && n_1B) {      mean_1 /= (double) n_1;      mean_1A /= (double) n_1A;      mean_1B /= (double) n_1B;      zone1_ok = 1;    }    else {      mean_1 = mean_1A = mean_1B = 0.0;      zone1_ok = 0;      if (verbose > 0)        fprintf(stderr, "zone 1 unusable\n");    }        if (n_2 && n_2A && n_2B) {      mean_2 /= (double) n_2;      mean_2A /= (double) n_2A;      mean_2B /= (double) n_2B;      zone2_ok = 1;    }    else {      mean_2 = mean_2A = mean_2B = 0.0;      zone2_ok = 0;      if (verbose > 0)        fprintf(stderr, "zone 2 unusable\n");    }    if (!skipping && !zone1_ok && !zone2_ok) {      // pathological case - can it ever happen?      if (verbose > 0)        fprintf(stderr, "block skipped\n");      continue;    }    if (verbose > 2) {      fprintf(stderr, "m_1 = %lf, m_1A = %lf, m_1B = %lf\n", mean_1, mean_1A, mean_1B);      fprintf(stderr, "m_2 = %lf, m_2A = %lf, m_2B = %lf\n", mean_2, mean_2A, mean_2B);    }    // calculate new mean values required by embedding rule    if (get_signature_bit(n)) {      if (zone1_ok) {        mean__1A = (mean_1 * (double) (n_1A + n_1B) + (double) n_1B * quality) / (double) (n_1A + n_1B);        mean__1B = mean__1A - quality;      }      if (zone2_ok) {        mean__2A = (mean_2 * (double) (n_2A + n_2B) + (double) n_2B * quality) /  (double) (n_2A + n_2B);        mean__2B = mean__2A - quality;      }    }    else {      if (zone1_ok) {        mean__1A = (mean_1 * (double) (n_1A + n_1B) - (double) n_1B * quality) / (double) (n_1A + n_1B);        mean__1B = mean__1A + quality;      }      if (zone2_ok) {        mean__2A = (mean_2 * (double) (n_2A + n_2B) - (double) n_2B * quality) / (double) (n_2A + n_2B);        mean__2B = mean__2A + quality;       }    }    // calculate luminance variations    if (zone1_ok) {      var_1A = rint(mean__1A - mean_1A);      var_1B = rint(mean__1B - mean_1B);    }    else var_1A = var_1B = 0;        if (zone2_ok) {      var_2A = rint(mean__2A - mean_2A);      var_2B = rint(mean__2B - mean_2B);    }    else var_2A = var_2B = 0;    if (verbose > 2) {      if (zone1_ok)        fprintf(stderr, "m*_1A = %lf, m*_1B = %lf\n", mean__1A, mean__1B);      if (zone2_ok)        fprintf(stderr, "m*_2A = %lf, m*_2B = %lf\n", mean__2A, mean__2B);      fprintf(stderr, "var %d %d %d %d\n", var_1A, var_1B, var_2A, var_2B);    }    // apply luminance variations to image pixels    for (i = 0; i < blocksize; i++)      for (j = 0; j < blocksize; j++) {        int pixel = image[yb * blocksize + j][xb * blocksize + i];        int pixel_zone = zone[j][i];        int pixel_category = CATEGORY_VOID;        if (pixel_zone == ZONE_1)          pixel_category = category1[j][i];        else if (pixel_zone == ZONE_2)          pixel_category = category2[j][i];        switch (pixel_zone | pixel_category) {          case CLASSIFICATION_1A:            pixel = GRAYRANGE(pixel + var_1A);            break;          case CLASSIFICATION_1B:            pixel = GRAYRANGE(pixel + var_1B);            break;          case CLASSIFICATION_2A:            pixel = GRAYRANGE(pixel + var_2A);            break;          case CLASSIFICATION_2B:            pixel = GRAYRANGE(pixel + var_2B);            break;        }        image[yb * blocksize + j][xb * blocksize + i] = pixel;      }    n++;        }  free_grays(category2);  free_grays(category1);  free_grays(zone);  free_grays(block);  // write output image dimensions to output file  pgm_writepgminit(out, cols, rows, maxval, 0);  // write output image  for (row = 0; row < rows; row++)    pgm_writepgmrow(out, image[row], cols, maxval, 0);  fclose(out);  pgm_freearray(image, rows);  exit(0);}

⌨️ 快捷键说明

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