// mesh_3a.c
#include <stdio.h>
#include <stdlib.h>
#define X_SIZE 32
#define Y_SIZE 32
#define HIGH 255
typedef unsigned char u_char;
void save_image_data(char* f_name);
void normalize(void);
void disp_img(u_char img[][X_SIZE]);
void cut_img(int n);
void load_image_data(char* f_name);
u_char image[Y_SIZE][X_SIZE * 10];
u_char img_a[Y_SIZE][X_SIZE];
u_char img_b[Y_SIZE][X_SIZE];
int main()
{
char f_name[256];
int i;
load_image_data("number2.pgm");
for (i = 0; i < 10; i++) {
cut_img(i);
disp_img(img_a);
normalize();
disp_img(img_b);
sprintf_s(f_name, _countof(f_name), "num%d.pgm", i);
save_image_data(f_name);
}
return 0;
}
void save_image_data(char* f_name)
{
FILE* fp;
errno_t err;
int ix, iy;
err = fopen_s(&fp, f_name, "wb");
if (err) {
return;
}
fputs("# Created by Image Processing\n", fp
); fprintf(fp
, "%d %d\n", X_SIZE
, Y_SIZE
);
for (iy = 0; iy < Y_SIZE; iy++) {
for (ix = 0; ix < X_SIZE; ix++) {
fputc(img_b
[iy
][ix
], fp
); }
}
}
void normalize(void)
{
int xs = X_SIZE;
int xe = -1;
int ys = Y_SIZE;
int ye = -1;
int ix, iy;
int iw, ih;
int ax, ay;
int max;
// Y走査
for (iy = 0; iy < Y_SIZE; iy++) {
for (ix = 0; ix < X_SIZE; ix++) {
if (img_a[iy][ix]) continue;
if (iy < ys) ys = iy;
if (ye < iy) ye = iy;
}
}
// X走査
for (ix = 0; ix < X_SIZE; ix++) {
for (iy = 0; iy < Y_SIZE; iy++) {
if (img_a[iy][ix]) continue;
if (ix < xs) xs = ix;
if (xe < ix) xe = ix;
}
}
iw = xe - xs + 1;
ih = ye - ys + 1;
printf("xs=%d xe=%d ys=%d ye=%d, w=%d h=%d\n", xs
, xe
, ys
, ye
, iw
, ih
);
if (iw < ih) {
max = ih;
xs += (iw - max) / 2;
} else {
max = iw;
ys += (ih - max) / 2;
}
for (iy = 0; iy < Y_SIZE; iy++) {
for (ix = 0; ix < X_SIZE; ix++) {
ax = xs + ix * max / X_SIZE;
ay = ys + iy * max / Y_SIZE;
if (ax < 0 || X_SIZE <= ax || ay < 0 || Y_SIZE <= ay) {
img_b[iy][ix] = 0xff;
} else {
img_b[iy][ix] = img_a[ay][ax];
}
}
}
}
void disp_img(u_char img[][X_SIZE])
{
int ix, iy;
for (iy = 0; iy < Y_SIZE; iy++) {
for (ix = 0; ix < X_SIZE; ix++) {
}
}
}
void cut_img(int n)
{
int ix, iy;
for (iy = 0; iy < Y_SIZE; iy++) {
for (ix = 0; ix < X_SIZE; ix++) {
img_a[iy][ix] = image[iy][X_SIZE * n + ix];
}
}
}
void load_image_data(char* f_name)
{
FILE* fp;
char buf[640];
errno_t err;
int x_size, y_size;
int max_gray;
int ix, iy;
err = fopen_s(&fp, f_name, "rb");
if (err) {
return;
}
// P5
fgets(buf
, sizeof buf
, fp
); if (buf[0] != 'P' || buf[1] != '5') {
return;
}
// x y
x_size = y_size = 0;
while (x_size == 0) {
fgets(buf
, sizeof buf
, fp
); if (buf[0] == '#') continue;
sscanf_s(buf, "%d%d", &x_size, &y_size);
}
// max
max_gray = 0;
while (max_gray == 0) {
fgets(buf
, sizeof buf
, fp
); if (buf[0] == '#') continue;
sscanf_s(buf, "%d", &max_gray);
}
for (iy = 0; iy < y_size; iy++) {
for (ix = 0; ix < x_size; ix++) {
image
[iy
][ix
] = fgetc(fp
); }
}
}
/*
img_a:9
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
ffffffffff0000000000ffffffffffffffffffffffffffffffffffffffffffff
ffffff0000ffffffffff00ffffffffffffffffffffffffffffffffffffffffff
ffff0000ffffffffffffff00ffffffffffffffffffffffffffffffffffffffff
ffff00ffffffffffffffffff00ffffffffffffffffffffffffffffffffffffff
ff0000ffffffffffffffffff00ffffffffffffffffffffffffffffffffffffff
ff0000ffffffffffffffffff0000ffffffffffffffffffffffffffffffffffff
ff0000ffffffffffffffffff0000ffffffffffffffffffffffffffffffffffff
ff0000ffffffffffffffffff0000ffffffffffffffffffffffffffffffffffff
ffff0000ffffffffffffff000000ffffffffffffffffffffffffffffffffffff
ffffff0000ffffffffff00ff0000ffffffffffffffffffffffffffffffffffff
ffffffff000000000000ffff0000ffffffffffffffffffffffffffffffffffff
ffffffffffffffffffffffff0000ffffffffffffffffffffffffffffffffffff
ffffffffffffffffffffffff0000ffffffffffffffffffffffffffffffffffff
ffffffffffffffffffffffff00ffffffffffffffffffffffffffffffffffffff
ffff000000ffffffffffff0000ffffffffffffffffffffffffffffffffffffff
ffff000000ffffffffffff00ffffffffffffffffffffffffffffffffffffffff
ffff0000ffffffffffff0000ffffffffffffffffffffffffffffffffffffffff
ffffff00ffffffffff0000ffffffffffffffffffffffffffffffffffffffffff
ffffffff0000000000ffffffffffffffffffffffffffffffffffffffffffffff
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
xs=1 xe=13 ys=8 ye=26, w=13 h=19
img_b:9
ffffffffffffffffffffffff000000000000000000ffffffffffffffffffffff
ffffffffffffffffffffffff000000000000000000ffffffffffffffffffffff
ffffffffffffffffff000000ffffffffffffffffff00ffffffffffffffffffff
ffffffffffffffffff000000ffffffffffffffffff00ffffffffffffffffffff
ffffffffffffff00000000ffffffffffffffffffffff0000ffffffffffffffff
ffffffffffffff00000000ffffffffffffffffffffff0000ffffffffffffffff
ffffffffffffff0000ffffffffffffffffffffffffffffff0000ffffffffffff
ffffffffffff000000ffffffffffffffffffffffffffffff0000ffffffffffff
ffffffffffff000000ffffffffffffffffffffffffffffff0000ffffffffffff
ffffffffffff000000ffffffffffffffffffffffffffffff000000ffffffffff
ffffffffffff000000ffffffffffffffffffffffffffffff000000ffffffffff
ffffffffffff000000ffffffffffffffffffffffffffffff000000ffffffffff
ffffffffffff000000ffffffffffffffffffffffffffffff000000ffffffffff
ffffffffffff000000ffffffffffffffffffffffffffffff000000ffffffffff
ffffffffffffff00000000ffffffffffffffffffffff0000000000ffffffffff
ffffffffffffff00000000ffffffffffffffffffffff0000000000ffffffffff
ffffffffffffffffff000000ffffffffffffffffff00ffff000000ffffffffff
ffffffffffffffffffffff00000000000000000000ffffff000000ffffffffff
ffffffffffffffffffffff00000000000000000000ffffff000000ffffffffff
ffffffffffffffffffffffffffffffffffffffffffffffff000000ffffffffff
ffffffffffffffffffffffffffffffffffffffffffffffff000000ffffffffff
ffffffffffffffffffffffffffffffffffffffffffffffff000000ffffffffff
ffffffffffffffffffffffffffffffffffffffffffffffff0000ffffffffffff
ffffffffffffffffffffffffffffffffffffffffffffffff0000ffffffffffff
ffffffffffffff0000000000ffffffffffffffffffff00000000ffffffffffff
ffffffffffffff0000000000ffffffffffffffffffff00000000ffffffffffff
ffffffffffffff0000000000ffffffffffffffffffff0000ffffffffffffffff
ffffffffffffff00000000ffffffffffffffffffff000000ffffffffffffffff
ffffffffffffff00000000ffffffffffffffffffff000000ffffffffffffffff
ffffffffffffffffff0000ffffffffffffffff000000ffffffffffffffffffff
ffffffffffffffffff0000ffffffffffffffff000000ffffffffffffffffffff
ffffffffffffffffffffff0000000000000000ffffffffffffffffffffffffff
*/