예제 #1
0
static bool resize_image(void *img_data, int in_w, int in_h, const char *dir, const char *filename, int out_w, int out_h) {
    void *out_data = malloc(out_w * out_h * 4);
    double in_aspect = (double)in_w / (double)in_h;
    double out_aspect = (double)out_w / (double)out_h;
    if (fabs(in_aspect - out_aspect) < 0.05) {
        stbir_resize_uint8((const unsigned char*)img_data, in_w, in_h, 0, (unsigned char *)out_data, out_w, out_h, 0, 4);
    } else {
        uint32_t *pxl_data = (uint32_t*)out_data;
        uint32_t bg = ((uint32_t*)img_data)[0];
        for (int i = 0; i < out_w * out_h; i++) {
            pxl_data[i] = bg;
        }
        double scaling_x = (double)out_w / (double)in_w;
        double scaling_y = (double)out_h / (double)in_h;
        double scaling = fmin(scaling_x, scaling_y);
        int scaled_w = (int)floor((double)in_w * scaling);
        int scaled_h = (int)floor((double)in_h * scaling);
        int x_os = (out_w - scaled_w) / 2;
        int y_os = (out_h - scaled_h) / 2;
        void *tmp_data = malloc(scaled_w * scaled_h * 4);
        stbir_resize_uint8((const unsigned char*)img_data, in_w, in_h, 0, (unsigned char *)tmp_data, scaled_w, scaled_h, 0, 4);
        uint32_t *tmp_pxl_data = (uint32_t*)tmp_data;
        for (int i = 0; i < scaled_w; i++) {
            for (int j = 0; j < scaled_h; j++) {
                pxl_data[(i + x_os) + (j + y_os) * out_w] = tmp_pxl_data[i + j * scaled_w];
            }
        }
        free(tmp_data);
    }
    size_t len;
    void *png_data = tdefl_write_image_to_png_file_in_memory_ex(
        out_data, out_w, out_h, 4, &len, MZ_DEFAULT_LEVEL, 0);
    free(out_data);
    char *path = am_format("%s%c%s", dir, AM_PATH_SEP, filename);
    FILE *f = fopen(path, "wb");
    free(path);
    if (f == NULL) {
        fprintf(stderr, "Error: cannot open %s for writing\n", filename);
        return false;
    }
    fwrite(png_data, len, 1, f);
    fclose(f);
    free(png_data);
    return true;
}
예제 #2
0
파일: pattern.c 프로젝트: Mougatine/ocr
char ask_nn(struct net nwk, t_img_desc *img)
{
    assert(img->comp == 1);

    if (img->x != 20 || img->y != 20) {
        uchar *ptr = malloc(sizeof(char) * 20 * 20);
        stbir_resize_uint8(img->data, img->x, img->y, 0, ptr, 20, 20, 0, 1);

        img->data = ptr;
        img->x = 20;
        img->y = 20;
        img->comp = 1;
    }

    double in[400];
    gen_input(img, in);

    net_compute(nwk, in);
    double *out = net_output(nwk);
    return convert_output(out, 52);
}
int main() {
	int x,y,n;
	unsigned char *data = NULL;

#ifdef USESTB
	unsigned char * readImg = stbi_load("../../../../common/despicable-124_018.jpg", &x, &y, &n, 0);

	data = (unsigned char * )malloc( 1024 * 1080 * 3 * sizeof(unsigned char));

	stbir_resize_uint8(readImg, x, y, 0,
	                   data, 1024, 1080, 1024* 3, 3);
	x = 1024;

	FILE * fo = fopen("raw_img.dat","wb");
	fwrite( data, sizeof(char) , 3*1024*1080, fo);
	fclose(fo);
#else
#if 1
	x = 1024;
	y = 1080;
	n = 3;
	FILE * fi = fopen("raw_img.dat","rb");
	printf("F : %x\n", fi);
	data = (unsigned char*)malloc( x*y*n * sizeof(unsigned char));
	fread( data, sizeof(char) , n*x*y, fi);
	fclose(fi);
#endif


#endif



#define IMG_WIDTH    512

	cmpxDataIn   img[IMG_WIDTH];
	hls::stream<t_pyr_complex> pyrFilOut;


	for(int i=0;i<IMG_WIDTH;i++) {
		ap_fixed< 9,1> Val;
		unsigned char datIn = data[i*3 + x * 3 * 60];
		Val.range(8,0) = datIn;
		img[i].real() = Val;
		img[i].imag() = 0;
	}
	pyrcon_top(img, pyrFilOut,  512);

	FILE * fo = fopen("pyr_out.txt", " wb");
	for(int i=0;i<512;i++)
	//for(int i=0;i<2992;i++)
	//while(!pyrFilOut.empty())
	{
		t_pyr_complex val = pyrFilOut.read();
		//std::cout << val << std::endl;
		fprintf(fo, "%.8f %.8f\n", val.real().to_float(), val.imag().to_float());
		printf( "%.8f  %.8f\n", val.real().to_float(), val.imag().to_float());
	}
	fclose(fo);
#if 0

	fo = fopen("pyr_out.bin", " wb");
	for(int j=0;j<1080;j++) {
		printf("%d\n", j);
		for(int c=0;c<3;c++) {
			for(int i=0;i<IMG_WIDTH;i++) {
				ap_fixed< 9,1> Val;
				unsigned char datIn = data[i*3 + x * 3 * j + c];
				Val.range(8,0) = datIn;
				img[i].real() = Val;
				img[i].imag() = 0;
			}
			pyrconstuct_top(img, pyrFilOut, 512);
			//for(int i=0;i<1520;i++) {
			float tmp_out[2992*2];
			for(int i=0;i<2992;i++) {
				t_pyr_complex val = pyrFilOut.read();
				tmp_out[i*2] = val.real().to_float();
				tmp_out[i*2+1] = val.imag().to_float();
				//std::cout << val << std::endl;
//				fprintf(fo, "%.8f %.8f\n", val.real().to_float(), val.imag().to_float());

			}
			fwrite( tmp_out, 2992*2 , sizeof(float), fo);
		}
	}
	fclose(fo);
#endif


	return 0;
}
예제 #4
0
파일: kd_dxtcomp.c 프로젝트: h-s-c/libKD
KD_API KDImageATX KD_APIENTRY kdDXTCompressBufferATX(const void *buffer, KDint32 width, KDint32 height, KDint32 comptype, KDint32 levels)
{
    _KDImageATX *image = (_KDImageATX *)kdMalloc(sizeof(_KDImageATX));
    if(image == KD_NULL)
    {
        kdSetError(KD_ENOMEM);
        return KD_NULL;
    }
    image->levels = levels;
    image->width = width;
    image->height = height;

    switch(comptype)
    {
        case(KD_DXTCOMP_TYPE_DXT1_ATX):
        {
            image->format = KD_IMAGE_FORMAT_DXT1_ATX;
            image->alpha = KD_FALSE;
            break;
        }
        case(KD_DXTCOMP_TYPE_DXT1A_ATX):
        {
            kdFree(image);
            kdSetError(KD_EINVAL);
            return KD_NULL;
        }
        case(KD_DXTCOMP_TYPE_DXT3_ATX):
        {
            kdFree(image);
            kdSetError(KD_EINVAL);
            return KD_NULL;
        }
        case(KD_DXTCOMP_TYPE_DXT5_ATX):
        {
            image->format = KD_IMAGE_FORMAT_DXT5_ATX;
            image->alpha = KD_TRUE;
            break;
        }
        default:
        {
            kdFree(image);
            kdSetError(KD_EINVAL);
            return KD_NULL;
        }
    }
    image->bpp = image->alpha ? 16 : 8;
    image->size = (KDsize)image->width * (KDsize)image->height * (KDsize)(image->bpp / 8);

    KDint _width = image->width;
    KDint _height = image->height;
    for(KDint i = 0; i < image->levels; i++)
    {
        _width >>= 1;
        _height >>= 1;
        image->size += (KDsize)_width * (KDsize)_height * (KDsize)(image->bpp / 8);
    }

    image->buffer = kdMalloc(image->size);
    if(image->buffer == KD_NULL)
    {
        kdFree(image);
        kdSetError(KD_ENOMEM);
        return KD_NULL;
    }

    _width = image->width;
    _height = image->height;
    KDint channels = (image->alpha ? 4 : 3);
    for(KDint i = 0; i <= image->levels; i++)
    {
        KDsize size = (KDsize)_width * (KDsize)_height * (KDsize)channels;
        if(size)
        {
            void *tmp = kdMalloc(size);
            if((_width == image->width) && (_height == image->height))
            {
                kdMemcpy(tmp, buffer, size);
            }
            else
            {
                stbir_resize_uint8(buffer, image->width, image->height, 0, tmp, _width, _height, 0, channels);
            }
            for(KDint y = 0; y < _height; y += 4)
            {
                for(KDint x = 0; x < _width; x += 4)
                {
                    KDuint8 block[64];
                    __kdExtractBlock(tmp, x, y, _width, _height, block);
                    stb_compress_dxt_block(image->buffer, block, image->alpha, STB_DXT_NORMAL);
                    image->buffer += image->bpp;
                }
            }
            kdFree(tmp);
        }
        _width >>= 1;
        _height >>= 1;
    }

    return image;
}