/* * The Npic library * * Copyright (C) 2003 Edouard Thiel * * This library is free software under the terms of the * GNU Lesser General Public License (LGPL) version 2.1. */ /* DO NOT EDIT !!! Generated by npic-templa from "files_print.ct" */ /* * files_print.c - 28/01/2003 * * Print image as text */ #include /*--------------------- P U B L I C - I N T E R F A C E ----------------------*/ /* * Print whole image to stdout, without or with border pixels. * * Return NPIC_SUCCESS on success, else return error code. Verbose. */ int NpicPrintImage (Npic_image *np) { return NpicPrintImageF (np, stdout); } int NpicPrintImageB(Npic_image *np) { return NpicPrintImageBF (np, stdout); } /* * Print whole image to a file f, without or with border pixels. * The file must be opened before calling, and closed after. * * Return NPIC_SUCCESS on success, else return error code. Verbose. */ int NpicPrintImageF (Npic_image *np, FILE *f) { if (NpicImageIsOK (np, __func__) != NPIC_SUCCESS) return NPIC_ERR_NOT_OK; if (f == NULL) return NpicError (__func__, NPIC_ERR_NULL_PTR, ""); switch (np->type) { case NPIC_IMAGE_2C : case NPIC_IMAGE_2L : case NPIC_IMAGE_2D : case NPIC_IMAGE_2Q : return NpicPrintImageF_YX (np, f, 0, np->gen.ymax - 1, 0, np->gen.xmax - 1); default : return NpicError (__func__, NPIC_ERR_UNEX_NPIC, ""); } } int NpicPrintImageBF (Npic_image *np, FILE *f) { if (NpicImageIsOK (np, __func__) != NPIC_SUCCESS) return NPIC_ERR_NOT_OK; if (f == NULL) return NpicError (__func__, NPIC_ERR_NULL_PTR, ""); switch (np->type) { case NPIC_IMAGE_2C : case NPIC_IMAGE_2L : case NPIC_IMAGE_2D : case NPIC_IMAGE_2Q : return NpicPrintImageF_YX (np, f, - np->gen.ybor, np->gen.ymax + np->gen.ybor - 1, - np->gen.xbor, np->gen.xmax + np->gen.xbor - 1); default : return NpicError (__func__, NPIC_ERR_UNEX_NPIC, ""); } } /* * The same for a bounding box y1 <= y <= y2 and x1 <= x <= x2. * * Return NPIC_SUCCESS on success, else return error code. Verbose. */ int NpicPrintImageF_YX (Npic_image *np, FILE *f, int y1, int y2, int x1, int x2) { if (NpicImageIsOK (np, __func__) != NPIC_SUCCESS) return NPIC_ERR_NOT_OK; if (f == NULL) return NpicError (__func__, NPIC_ERR_NULL_PTR, ""); if (!( -np->gen.ybor <= y1 && y1 <= y2 && y2 < np->gen.ymax + np->gen.ybor && -np->gen.xbor <= x1 && x1 <= x2 && x2 < np->gen.xmax + np->gen.xbor )) return NpicError (__func__, NPIC_ERR_BAD_SIZE, ""); switch (np->type) { case NPIC_IMAGE_2C : { Npic_image_2c *p1 = NpicCastImage(np); int x, y, min, max, n; /* Compute min,max and deduce format */ min = max = p1->pix[y1][x1]; for (y = y1; y <= y2; y++) for (x = x1; x <= x2; x++) if (p1->pix[y][x] < min) min = p1->pix[y][x]; else if (p1->pix[y][x] > max) max = p1->pix[y][x]; n = NpicMinMaxFormat (min, max); /* Write in f */ for (y = y1; y <= y2; y++) { for (x = x1; x <= x2; x++) fprintf (f, " %*d", n, p1->pix[y][x]); fprintf (f, "\n"); } return NPIC_SUCCESS; } case NPIC_IMAGE_2L : { Npic_image_2l *p1 = NpicCastImage(np); int x, y, min, max, n; /* Compute min,max and deduce format */ min = max = p1->pix[y1][x1]; for (y = y1; y <= y2; y++) for (x = x1; x <= x2; x++) if (p1->pix[y][x] < min) min = p1->pix[y][x]; else if (p1->pix[y][x] > max) max = p1->pix[y][x]; n = NpicMinMaxFormat (min, max); /* Write in f */ for (y = y1; y <= y2; y++) { for (x = x1; x <= x2; x++) fprintf (f, " %*" NPIC_PL, n, p1->pix[y][x]); fprintf (f, "\n"); } return NPIC_SUCCESS; } case NPIC_IMAGE_2D : { int x, y; Npic_image_2d *p1 = NpicCastImage(np); /* Write in f */ for (y = y1; y <= y2; y++) { for (x = x1; x <= x2; x++) fprintf (f, " %8.3f", p1->pix[y][x]); fprintf (f, "\n"); } return NPIC_SUCCESS; } case NPIC_IMAGE_2Q : { int x, y; Npic_image_2q *p1 = NpicCastImage(np); int min_a, min_b, min_c, min_d, max_a, max_b, max_c, max_d, n_a, n_b, n_c, n_d; /* Compute min,max and deduce format */ min_a = max_a = p1->pix[y1][x1].a; min_b = max_b = p1->pix[y1][x1].b; min_c = max_c = p1->pix[y1][x1].c; min_d = max_d = p1->pix[y1][x1].d; for (y = y1; y <= y2; y++) for (x = x1; x <= x2; x++) { if (p1->pix[y][x].a < min_a) min_a = p1->pix[y][x].a; else if (p1->pix[y][x].a > max_a) max_a = p1->pix[y][x].a; if (p1->pix[y][x].b < min_b) min_b = p1->pix[y][x].b; else if (p1->pix[y][x].b > max_b) max_b = p1->pix[y][x].b; if (p1->pix[y][x].c < min_c) min_c = p1->pix[y][x].c; else if (p1->pix[y][x].c > max_c) max_c = p1->pix[y][x].c; if (p1->pix[y][x].d < min_d) min_d = p1->pix[y][x].d; else if (p1->pix[y][x].d > max_d) max_d = p1->pix[y][x].d; } n_a = NpicMinMaxFormat (min_a, max_a); n_b = NpicMinMaxFormat (min_b, max_b); n_c = NpicMinMaxFormat (min_c, max_c); n_d = NpicMinMaxFormat (min_d, max_d); /* Write in f */ for (y = y1; y <= y2; y++) { for (x = x1; x <= x2; x++) { fprintf (f, " %*d", n_a, p1->pix[y][x].a); fprintf (f, " %*d", n_b, p1->pix[y][x].b); fprintf (f, " %*d", n_c, p1->pix[y][x].c); fprintf (f, " %*d", n_d, p1->pix[y][x].d); if (x < x2) fprintf (f, " |"); } fprintf (f, "\n"); } return NPIC_SUCCESS; } default : return NpicError (__func__, NPIC_ERR_UNEX_NPIC, ""); } } /*-------------------- P R I V A T E - F U N C T I O N S ---------------------*/ /* * Compute number of chars needed to fit min and max ; called by NpicPrintImage*(). * Silent. */ int NpicMinMaxFormat (int min, int max) { char buf[256]; int n1, n2; sprintf (buf, "%d", min); n1 = strlen (buf); sprintf (buf, "%d", max); n2 = strlen (buf); return n1 > n2 ? n1 : n2; }