C++ 创造大形象

C++ 创造大形象,c++,c,image,bitmap,C++,C,Image,Bitmap,首先,你好 然后,有一点背景: 我实际上在做一个“迷宫发生器”。现在,我可以生成18500宽18500高的迷宫(这让我有点高兴!!) 所以,这一代人没有问题 然后,当然,我想将它们写入.jpeg/.bmp或其他文件 所以,对于一个18k5*18k5的迷宫,我需要一个37k*37k的图像 我第一次尝试使用.jpeg。它工作了,但是输出文件是1.2GB大。那些需要很长时间才能打开。。。但是我可以打开它们 所以我尝试使用BMP格式,因为它允许每个像素编码1位。输出文件现在真的更小了。。。。但是我不能打

首先,你好

然后,有一点背景:

我实际上在做一个“迷宫发生器”。现在,我可以生成18500宽18500高的迷宫(这让我有点高兴!!)

所以,这一代人没有问题

然后,当然,我想将它们写入.jpeg/.bmp或其他文件

所以,对于一个18k5*18k5的迷宫,我需要一个37k*37k的图像

我第一次尝试使用.jpeg。它工作了,但是输出文件是1.2GB大。那些需要很长时间才能打开。。。但是我可以打开它们

所以我尝试使用BMP格式,因为它允许每个像素编码1位。输出文件现在真的更小了。。。。但是我不能打开它们。。。我被告知这些文件“已损坏或太大”

我不认为它们被破坏了,因为它与一个10k*10k迷宫(20k*20k图像)一起工作,但它无法用相同的代码打开一个13k*13k迷宫(26k*26k图像)

下面是代码(我使用的是EasyBMP库):

intmain()
{
HandyMaze labz(迷宫侧,迷宫侧);//迷宫生成器
Labz*l;//self
BMP-img;
if(labz.Generate()==false)
{

std::cout我对使用easybmp-lib的问题不太了解,但是您也可以尝试使用libpng

您可以检查下面的代码(使用libpng)来编写更大尺寸的图像文件,libpng也会生成更多的压缩文件。您可以根据需要修改代码。希望对您有所帮助

/***********************************************************
This is a modification of the original code taken from 
http://www.lemoda.net/c/write-png/
***********************************************************/

#include "png.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>

/* A coloured pixel. */

typedef struct {
    char color;
} pixel_t;

/* A picture. */

typedef struct  {
    pixel_t *pixels;
    size_t width;
    size_t height;
} bitmap_t;

/* Given "bitmap", this returns the pixel of bitmap at the point 
   ("x", "y"). */

static pixel_t * pixel_at (bitmap_t * bitmap, int x, int y)
{
    return bitmap->pixels + bitmap->width * y + x;
}

/* Write "bitmap" to a PNG file specified by "path"; returns 0 on
   success, non-zero on error. */

static int save_png_to_file (bitmap_t *bitmap, const char *path)
{
    FILE * fp;
    png_structp png_ptr = NULL;
    png_infop info_ptr = NULL;
    size_t x, y;
    png_byte ** row_pointers = NULL;
    png_byte *row = NULL;
    /* "status" contains the return value of this function. At first
       it is set to a value which means 'failure'. When the routine
       has finished its work, it is set to a value which means
       'success'. */
    int status = -1;
    /* The following number is set by trial and error only. I cannot
       see where it it is documented in the libpng manual.
    */
    int pixel_size = 1;
    int depth = 8;

    fp = fopen (path, "wb");
    if (! fp) {
        goto fopen_failed;
    }

    png_ptr = png_create_write_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
    if (png_ptr == NULL) {
        goto png_create_write_struct_failed;
    }

    info_ptr = png_create_info_struct (png_ptr);
    if (info_ptr == NULL) {
        goto png_create_info_struct_failed;
    }

    /* Set up error handling. */

    if (setjmp (png_jmpbuf (png_ptr))) {
        goto png_failure;
    }

    /* Set image attributes. */

    png_set_IHDR (png_ptr,
                  info_ptr,
                  bitmap->width,
                  bitmap->height,
                  depth,
                  PNG_COLOR_TYPE_GRAY,
                  PNG_INTERLACE_NONE,
                  PNG_COMPRESSION_TYPE_DEFAULT,
                  PNG_FILTER_TYPE_DEFAULT);

    /* Initialize rows of PNG. */
    printf("Filling image...\n");
    png_init_io (png_ptr, fp);
    png_write_info(png_ptr, info_ptr);

    row_pointers = png_malloc (png_ptr, sizeof (pixel_t *));
    row = png_malloc (png_ptr, sizeof (pixel_t) * bitmap->width * pixel_size);
    for (y = 0; y < bitmap->height; ++y) {
        for (x = 0; x < bitmap->width; ++x) {
            pixel_t * pixel = pixel_at (bitmap, x, y);
            row[x-1] = pixel->color;
        }
        png_write_row(png_ptr, row);
    }
    png_free (png_ptr, row_pointers);

    /* Write the image data to "fp". */
    printf("Writing image...\n");
    png_write_end(png_ptr, info_ptr);

    /* The routine has successfully written the file, so we set
       "status" to a value which indicates success. */

    status = 0;
    /*
    for (y = 0; y < bitmap->height; y++) {
        png_free (png_ptr, row_pointers[y]);
    }
    png_free (png_ptr, row_pointers);
    */
 png_failure:
 png_create_info_struct_failed:
    png_destroy_write_struct (&png_ptr, &info_ptr);
 png_create_write_struct_failed:
    fclose (fp);
 fopen_failed:
    return status;
}

/* Given "value" and "max", the maximum value which we expect "value"
   to take, this returns an integer between 0 and 255 proportional to
   "value" divided by "max". */

static int pix (int value, int max)
{
    if (value < 0)
        return 0;
    return (int) (256.0 *((double) (value)/(double) max));
}

int main ()
{
    bitmap_t fruit;
    int x;
    int y;

    /* Create an image. */

    fruit.width = 37000;
    fruit.height = 37000;

    fruit.pixels = calloc (sizeof (pixel_t), fruit.width * fruit.height);
    printf("creating image...\n");
    for (y = 0; y < fruit.height; y++) {
        for (x = 0; x < fruit.width; x++) {
            pixel_t * pixel = pixel_at (& fruit, x, y);
            if((x+y)%2)
            {
                pixel->color = 0;
            }
            else
            {
                pixel->color = 255;
            }
        }
    }

    /* Write the image to a file 'fruit.png'. */

    save_png_to_file (& fruit, "fruit.png");

    return 0;
}
/***********************************************************
这是对源代码的修改
http://www.lemoda.net/c/write-png/
***********************************************************/
#包括“png.h”
#包括
#包括
#包括
/*彩色像素*/
类型定义结构{
炭色;
}像素t;
/*一幅画*/
类型定义结构{
像素_t*像素;
尺寸和宽度;
尺寸和高度;
}位图;
/*给定“位图”,返回该点位图的像素
(“x”,“y”)*/
静态像素\u t*像素\u at(位图\u t*位图,整数x,整数y)
{
返回位图->像素+位图->宽度*y+x;
}
/*将“位图”写入由“路径”指定的PNG文件;在
成功,错误不为零*/
静态整型保存到文件(位图*位图,常量字符*路径)
{
文件*fp;
png_structp png_ptr=NULL;
png_infop info_ptr=NULL;
尺寸x,y;
png_字节**行指针=NULL;
png_字节*行=空;
/*“status”包含此函数的返回值。首先
它被设置为一个表示“失败”的值
完成其工作后,将其设置为一个值,表示
“成功”*/
int status=-1;
/*以下数字仅通过尝试和错误设置。我无法更改
请参阅libpng手册中的文档位置。
*/
int像素_大小=1;
int深度=8;
fp=fopen(路径,“wb”);
如果(!fp){
goto fopen_失败;
}
png_ptr=png_create_write_struct(png_LIBPNG_VER_STRING,NULL,NULL);
如果(png_ptr==NULL){
转到png\u创建\u写入\u结构\u失败;
}
info_ptr=png_create_info_struct(png_ptr);
if(info_ptr==NULL){
转到png\u创建\u信息\u结构\u失败;
}
/*设置错误处理*/
if(setjmp(png_jmpbuf(png_ptr))){
去巴布亚新几内亚失败;
}
/*设置图像属性*/
png\U set\U IHDR(png\U ptr,
信息,
位图->宽度,
位图->高度,
深度
PNG颜色类型灰色,
PNG\u交错\u无,
PNG\压缩\类型\默认值,
PNG_过滤器_类型_默认值);
/*初始化PNG的行*/
printf(“填充图像…\n”);
png_init_io(png_ptr,fp);
png_write_info(png_ptr,info_ptr);
行指针=png_malloc(png_ptr,sizeof(pixel_t*);
row=png\u malloc(png\u ptr,sizeof(pixel\u t)*位图->宽度*像素大小);
对于(y=0;yheight;++y){
对于(x=0;xwidth;++x){
pixel_t*pixel=pixel_at(位图,x,y);
行[x-1]=像素->颜色;
}
png_写入_行(png_ptr,行);
}
png_免费(png_ptr,行指针);
/*将图像数据写入“fp”*/
printf(“写入图像…\n”);
png_write_end(png_ptr,info_ptr);
/*例程已成功写入文件,因此我们设置
“状态”设置为表示成功的值*/
状态=0;
/*
对于(y=0;yheight;y++){
png_-free(png_-ptr,行指针[y]);
}
png_免费(png_ptr,行指针);
*/
png_故障:
png\u创建\u信息\u结构\u失败:
png_destroy_write_struct(&png_ptr和&info_ptr);
png\u创建\u写入\u结构\u失败:
fclose(fp);
fopen_失败:
返回状态;
}
/*给定“值”和“最大值”,我们期望的最大值为“值”
取0到255之间的整数,该整数与
“值”除以“最大值”*/
静态整数像素(整数值,整数最大值)
{
如果(值<0)
返回0;
返回值(整数)(256.0*((双倍)(值)/(双倍)最大值));
}
int main()
{
水果;
int x;
int-y;
/*创建一个图像*/
果宽=37000;
果高=37000;
fruit.pixels=calloc(sizeof(pixel_t),fruit.width*fruit.height);
printf(“创建图像…\n”);
对于(y=0;y颜色=0;
}
其他的
{
像素->颜色=255;
}
}
}
/*将图像写入文件“fruit.png”*/
将_png_保存到_文件(&fruit,“fruit.png”);
返回0;
}

我认为BMP文件必须有一个头部()Hi brain。是的,我的大小似乎低于最大大小:he
/***********************************************************
This is a modification of the original code taken from 
http://www.lemoda.net/c/write-png/
***********************************************************/

#include "png.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>

/* A coloured pixel. */

typedef struct {
    char color;
} pixel_t;

/* A picture. */

typedef struct  {
    pixel_t *pixels;
    size_t width;
    size_t height;
} bitmap_t;

/* Given "bitmap", this returns the pixel of bitmap at the point 
   ("x", "y"). */

static pixel_t * pixel_at (bitmap_t * bitmap, int x, int y)
{
    return bitmap->pixels + bitmap->width * y + x;
}

/* Write "bitmap" to a PNG file specified by "path"; returns 0 on
   success, non-zero on error. */

static int save_png_to_file (bitmap_t *bitmap, const char *path)
{
    FILE * fp;
    png_structp png_ptr = NULL;
    png_infop info_ptr = NULL;
    size_t x, y;
    png_byte ** row_pointers = NULL;
    png_byte *row = NULL;
    /* "status" contains the return value of this function. At first
       it is set to a value which means 'failure'. When the routine
       has finished its work, it is set to a value which means
       'success'. */
    int status = -1;
    /* The following number is set by trial and error only. I cannot
       see where it it is documented in the libpng manual.
    */
    int pixel_size = 1;
    int depth = 8;

    fp = fopen (path, "wb");
    if (! fp) {
        goto fopen_failed;
    }

    png_ptr = png_create_write_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
    if (png_ptr == NULL) {
        goto png_create_write_struct_failed;
    }

    info_ptr = png_create_info_struct (png_ptr);
    if (info_ptr == NULL) {
        goto png_create_info_struct_failed;
    }

    /* Set up error handling. */

    if (setjmp (png_jmpbuf (png_ptr))) {
        goto png_failure;
    }

    /* Set image attributes. */

    png_set_IHDR (png_ptr,
                  info_ptr,
                  bitmap->width,
                  bitmap->height,
                  depth,
                  PNG_COLOR_TYPE_GRAY,
                  PNG_INTERLACE_NONE,
                  PNG_COMPRESSION_TYPE_DEFAULT,
                  PNG_FILTER_TYPE_DEFAULT);

    /* Initialize rows of PNG. */
    printf("Filling image...\n");
    png_init_io (png_ptr, fp);
    png_write_info(png_ptr, info_ptr);

    row_pointers = png_malloc (png_ptr, sizeof (pixel_t *));
    row = png_malloc (png_ptr, sizeof (pixel_t) * bitmap->width * pixel_size);
    for (y = 0; y < bitmap->height; ++y) {
        for (x = 0; x < bitmap->width; ++x) {
            pixel_t * pixel = pixel_at (bitmap, x, y);
            row[x-1] = pixel->color;
        }
        png_write_row(png_ptr, row);
    }
    png_free (png_ptr, row_pointers);

    /* Write the image data to "fp". */
    printf("Writing image...\n");
    png_write_end(png_ptr, info_ptr);

    /* The routine has successfully written the file, so we set
       "status" to a value which indicates success. */

    status = 0;
    /*
    for (y = 0; y < bitmap->height; y++) {
        png_free (png_ptr, row_pointers[y]);
    }
    png_free (png_ptr, row_pointers);
    */
 png_failure:
 png_create_info_struct_failed:
    png_destroy_write_struct (&png_ptr, &info_ptr);
 png_create_write_struct_failed:
    fclose (fp);
 fopen_failed:
    return status;
}

/* Given "value" and "max", the maximum value which we expect "value"
   to take, this returns an integer between 0 and 255 proportional to
   "value" divided by "max". */

static int pix (int value, int max)
{
    if (value < 0)
        return 0;
    return (int) (256.0 *((double) (value)/(double) max));
}

int main ()
{
    bitmap_t fruit;
    int x;
    int y;

    /* Create an image. */

    fruit.width = 37000;
    fruit.height = 37000;

    fruit.pixels = calloc (sizeof (pixel_t), fruit.width * fruit.height);
    printf("creating image...\n");
    for (y = 0; y < fruit.height; y++) {
        for (x = 0; x < fruit.width; x++) {
            pixel_t * pixel = pixel_at (& fruit, x, y);
            if((x+y)%2)
            {
                pixel->color = 0;
            }
            else
            {
                pixel->color = 255;
            }
        }
    }

    /* Write the image to a file 'fruit.png'. */

    save_png_to_file (& fruit, "fruit.png");

    return 0;
}