int res_create_theme_display_surface(const char* name, const char* themename, gr_surface* pSurface) {
    gr_surface surface = NULL;
    int result = 0;
    png_structp png_ptr = NULL;
    png_infop info_ptr = NULL;
    png_uint_32 width, height;
    png_byte channels;

    *pSurface = NULL;

    result = open_theme_png(name, themename, &png_ptr, &info_ptr, &width, &height, &channels);
    if (result < 0) return result;

    surface = init_display_surface(width, height);
    if (surface == NULL) {
        result = -8;
        goto exit;
    }

    unsigned char* p_row = malloc(width * 4);
    unsigned int y;
    for (y = 0; y < height; ++y) {
        png_read_row(png_ptr, p_row, NULL);
        transform_rgb_to_draw(p_row, surface->data + y * surface->row_bytes, channels, width);
    }
    free(p_row);

    *pSurface = surface;

    exit:
    png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
    if (result < 0 && surface != NULL) free(surface);
    return result;
}
int
res_create_display_surface(const char *name, gr_surface *pSurface)
{
	int result = 0;
	unsigned int y;
	unsigned char *p_row;
	gr_surface surface = NULL;
	png_structp png_ptr = NULL;
	png_infop info_ptr = NULL;
	png_uint_32 width, height;
	png_byte channels;
	FILE *fp = NULL;

	*pSurface = NULL;

	result = open_png(name, &png_ptr, &info_ptr, &fp, &width, &height,
			  &channels);
	if (result < 0)
		return result;

	if (!(surface = init_display_surface(width, height))) {
		result = -8;
		goto exit;
	}

	/* TODO: check for error */
	p_row = malloc(width * 4);
	for (y = 0; y < height; y++) {
		png_read_row(png_ptr, p_row, NULL);
		transform_rgb_to_draw(p_row,
				      surface->data + y * surface->row_bytes,
				      channels, width);
	}

	free(p_row);

	*pSurface = surface;

exit:
	close_png(&png_ptr, &info_ptr, fp);
	if (result < 0 && surface != NULL)
		free(surface);

	return result;
}
Exemple #3
0
int res_create_surface_png(const char* name, gr_surface* pSurface) {
    GGLSurface* surface = NULL;
    int result = 0;
    png_structp png_ptr = NULL;
    png_infop info_ptr = NULL;
    png_uint_32 width, height;
    png_byte channels;
    FILE* fp;

    *pSurface = NULL;

    result = open_png(name, &png_ptr, &info_ptr, &width, &height, &channels, &fp);
    if (result < 0) return result;

    surface = init_display_surface(width, height);
    if (surface == NULL) {
        result = -8;
        goto exit;
    }

    unsigned char* p_row = malloc(width * 4);
    unsigned int y;
    for (y = 0; y < height; ++y) {
        png_read_row(png_ptr, p_row, NULL);
        transform_rgb_to_draw(p_row, surface->data + y * width * 4, channels, width);
    }
    free(p_row);

    if (channels == 3)
        surface->format = GGL_PIXEL_FORMAT_RGBX_8888;
    else
        surface->format = GGL_PIXEL_FORMAT_RGBA_8888;

    *pSurface = (gr_surface) surface;

  exit:
    fclose(fp);
    png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
    if (result < 0 && surface != NULL) free(surface);
    return result;
}
int res_create_theme_multi_display_surface(const char* name, const char* themename, int* frames, gr_surface** pSurface) {
    gr_surface* surface = NULL;
    int result = 0;
    png_structp png_ptr = NULL;
    png_infop info_ptr = NULL;
    png_uint_32 width, height;
    png_byte channels;
    int i;

    *pSurface = NULL;
    *frames = -1;

    result = open_theme_png(name, themename, &png_ptr, &info_ptr, &width, &height, &channels);
    if (result < 0) return result;

    *frames = 1;
    png_textp text;
    int num_text;
    if (png_get_text(png_ptr, info_ptr, &text, &num_text)) {
        for (i = 0; i < num_text; ++i) {
            if (text[i].key && strcmp(text[i].key, "Frames") == 0 && text[i].text) {
                *frames = atoi(text[i].text);
                break;
            }
        }
        printf("  found frames = %d\n", *frames);
    }

    if (height % *frames != 0) {
        printf("bad height (%d) for frame count (%d)\n", height, *frames);
        result = -9;
        goto exit;
    }

    surface = malloc(*frames * sizeof(gr_surface));
    if (surface == NULL) {
        result = -8;
        goto exit;
    }
    for (i = 0; i < *frames; ++i) {
        surface[i] = init_display_surface(width, height / *frames);
        if (surface[i] == NULL) {
            result = -8;
            goto exit;
        }
    }

    unsigned char* p_row = malloc(width * 4);
    unsigned int y;
    for (y = 0; y < height; ++y) {
        png_read_row(png_ptr, p_row, NULL);
        int frame = y % *frames;
        unsigned char* out_row = surface[frame]->data +
                                 (y / *frames) * surface[frame]->row_bytes;
        transform_rgb_to_draw(p_row, out_row, channels, width);
    }
    free(p_row);

    *pSurface = (gr_surface*) surface;

    exit:
    png_destroy_read_struct(&png_ptr, &info_ptr, NULL);

    if (result < 0) {
        if (surface) {
            for (i = 0; i < *frames; ++i) {
                if (surface[i]) free(surface[i]);
            }
            free(surface);
        }
    }
    return result;
}
int
res_create_multi_display_surface(const char *name, int *frames,
				 gr_surface **pSurface)
{
	int i, result = 0, num_text;
	unsigned int y;
	unsigned char *p_row;
	gr_surface *surface = NULL;
	png_structp png_ptr = NULL;
	png_infop info_ptr = NULL;
	png_uint_32 width, height;
	png_byte channels = 0;
	png_textp text;
	FILE *fp = NULL;

	*pSurface = NULL;
	*frames = -1;

	result = open_png(name, &png_ptr, &info_ptr, &fp, &width, &height,
			  &channels);
	if (result < 0)
		return result;

	*frames = 1;
	if (png_get_text(png_ptr, info_ptr, &text, &num_text)) {
		for (i = 0; i < num_text; i++)
			if (text[i].key && !strcmp(text[i].key, "Frames") &&
			    text[i].text) {
				*frames = atoi(text[i].text);
				break;
			}

		printf("  found frames = %d\n", *frames);
	}

	if (height % *frames != 0) {
		printf("bad height (%ld) for frame count (%d)\n",
		       (long)height, *frames);
		result = -9;
		goto exit;
	}

	if (!(surface = malloc(*frames * sizeof(gr_surface)))) {
		result = -8;
		goto exit;
	}

	for (i = 0; i < *frames; i++) {
		surface[i] = init_display_surface(width, height / *frames);
		if (!surface[i]) {
			result = -8;
			goto exit;
		}
	}

	/* TODO: Check for error */
	p_row = malloc(width * 4);
	for (y = 0; y < height; y++) {
		int frame = y % *frames;
		unsigned char *out_row;

		png_read_row(png_ptr, p_row, NULL);
		out_row = surface[frame]->data + (y / *frames) *
			  surface[frame]->row_bytes;
		transform_rgb_to_draw(p_row, out_row, channels, width);
	}

	free(p_row);

	*pSurface = (gr_surface *)surface;

exit:
	close_png(&png_ptr, &info_ptr, fp);

	if (result < 0)
		if (surface) {
			for (i = 0; i < *frames; i++)
				if (surface[i])
					free(surface[i]);

			free(surface);
		}

	return result;
}