annotate src/flac-1.2.1/src/test_grabbag/picture/main.c @ 20:ab7c38c4c577

Ranlib
author Chris Cannam
date Mon, 25 Mar 2013 16:28:19 +0000
parents 05aa0afa9217
children
rev   line source
Chris@1 1 /* test_picture - Simple tester for picture routines in grabbag
Chris@1 2 * Copyright (C) 2006,2007 Josh Coalson
Chris@1 3 *
Chris@1 4 * This program is free software; you can redistribute it and/or
Chris@1 5 * modify it under the terms of the GNU General Public License
Chris@1 6 * as published by the Free Software Foundation; either version 2
Chris@1 7 * of the License, or (at your option) any later version.
Chris@1 8 *
Chris@1 9 * This program is distributed in the hope that it will be useful,
Chris@1 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@1 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@1 12 * GNU General Public License for more details.
Chris@1 13 *
Chris@1 14 * You should have received a copy of the GNU General Public License
Chris@1 15 * along with this program; if not, write to the Free Software
Chris@1 16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Chris@1 17 */
Chris@1 18
Chris@1 19 #if HAVE_CONFIG_H
Chris@1 20 # include <config.h>
Chris@1 21 #endif
Chris@1 22
Chris@1 23 #include <string.h>
Chris@1 24 #include "FLAC/assert.h"
Chris@1 25 #include "share/grabbag.h"
Chris@1 26
Chris@1 27 typedef struct {
Chris@1 28 const char *path;
Chris@1 29 const char *mime_type;
Chris@1 30 const char *description;
Chris@1 31 FLAC__uint32 width;
Chris@1 32 FLAC__uint32 height;
Chris@1 33 FLAC__uint32 depth;
Chris@1 34 FLAC__uint32 colors;
Chris@1 35 FLAC__StreamMetadata_Picture_Type type;
Chris@1 36 } PictureFile;
Chris@1 37
Chris@1 38 PictureFile picturefiles[] = {
Chris@1 39 { "0.gif", "image/gif" , "", 24, 24, 24, 2, FLAC__STREAM_METADATA_PICTURE_TYPE_FRONT_COVER },
Chris@1 40 { "1.gif", "image/gif" , "", 12, 8, 24, 256, FLAC__STREAM_METADATA_PICTURE_TYPE_BACK_COVER },
Chris@1 41 { "2.gif", "image/gif" , "", 16, 14, 24, 128, FLAC__STREAM_METADATA_PICTURE_TYPE_OTHER },
Chris@1 42 { "0.jpg", "image/jpeg", "", 30, 20, 8, 0, FLAC__STREAM_METADATA_PICTURE_TYPE_FRONT_COVER },
Chris@1 43 { "4.jpg", "image/jpeg", "", 31, 47, 24, 0, FLAC__STREAM_METADATA_PICTURE_TYPE_FRONT_COVER },
Chris@1 44 { "0.png", "image/png" , "", 30, 20, 8, 0, FLAC__STREAM_METADATA_PICTURE_TYPE_FRONT_COVER },
Chris@1 45 { "1.png", "image/png" , "", 30, 20, 8, 0, FLAC__STREAM_METADATA_PICTURE_TYPE_FRONT_COVER },
Chris@1 46 { "2.png", "image/png" , "", 30, 20, 24, 7, FLAC__STREAM_METADATA_PICTURE_TYPE_FRONT_COVER },
Chris@1 47 { "3.png", "image/png" , "", 30, 20, 24, 7, FLAC__STREAM_METADATA_PICTURE_TYPE_FRONT_COVER },
Chris@1 48 { "4.png", "image/png" , "", 31, 47, 24, 0, FLAC__STREAM_METADATA_PICTURE_TYPE_FRONT_COVER },
Chris@1 49 { "5.png", "image/png" , "", 31, 47, 24, 0, FLAC__STREAM_METADATA_PICTURE_TYPE_FRONT_COVER },
Chris@1 50 { "6.png", "image/png" , "", 31, 47, 24, 23, FLAC__STREAM_METADATA_PICTURE_TYPE_FRONT_COVER },
Chris@1 51 { "7.png", "image/png" , "", 31, 47, 24, 23, FLAC__STREAM_METADATA_PICTURE_TYPE_FRONT_COVER },
Chris@1 52 { "8.png", "image/png" , "", 32, 32, 32, 0, 999 }
Chris@1 53 };
Chris@1 54
Chris@1 55 static FLAC__bool debug_ = false;
Chris@1 56
Chris@1 57 static FLAC__bool failed_(const char *msg)
Chris@1 58 {
Chris@1 59 if(msg)
Chris@1 60 printf("FAILED, %s\n", msg);
Chris@1 61 else
Chris@1 62 printf("FAILED\n");
Chris@1 63
Chris@1 64 return false;
Chris@1 65 }
Chris@1 66
Chris@1 67 static FLAC__bool test_one_picture(const char *prefix, const PictureFile *pf, const char *res, FLAC__bool fn_only)
Chris@1 68 {
Chris@1 69 FLAC__StreamMetadata *obj;
Chris@1 70 const char *error;
Chris@1 71 char s[4096];
Chris@1 72 if(fn_only)
Chris@1 73 #if defined _MSC_VER || defined __MINGW32__
Chris@1 74 _snprintf(s, sizeof(s)-1, "%s/%s", prefix, pf->path);
Chris@1 75 #else
Chris@1 76 snprintf(s, sizeof(s)-1, "%s/%s", prefix, pf->path);
Chris@1 77 #endif
Chris@1 78 else
Chris@1 79 #if defined _MSC_VER || defined __MINGW32__
Chris@1 80 _snprintf(s, sizeof(s)-1, "%u|%s|%s|%s|%s/%s", (unsigned)pf->type, pf->mime_type, pf->description, res, prefix, pf->path);
Chris@1 81 #else
Chris@1 82 snprintf(s, sizeof(s)-1, "%u|%s|%s|%s|%s/%s", (unsigned)pf->type, pf->mime_type, pf->description, res, prefix, pf->path);
Chris@1 83 #endif
Chris@1 84
Chris@1 85 printf("testing grabbag__picture_parse_specification(\"%s\")... ", s);
Chris@1 86 if(0 == (obj = grabbag__picture_parse_specification(s, &error)))
Chris@1 87 return failed_(error);
Chris@1 88 if(debug_) {
Chris@1 89 printf("\ntype=%u (%s)\nmime_type=%s\ndescription=%s\nwidth=%u\nheight=%u\ndepth=%u\ncolors=%u\ndata_length=%u\n",
Chris@1 90 obj->data.picture.type,
Chris@1 91 obj->data.picture.type < FLAC__STREAM_METADATA_PICTURE_TYPE_UNDEFINED?
Chris@1 92 FLAC__StreamMetadata_Picture_TypeString[obj->data.picture.type] : "UNDEFINED",
Chris@1 93 obj->data.picture.mime_type,
Chris@1 94 obj->data.picture.description,
Chris@1 95 obj->data.picture.width,
Chris@1 96 obj->data.picture.height,
Chris@1 97 obj->data.picture.depth,
Chris@1 98 obj->data.picture.colors,
Chris@1 99 obj->data.picture.data_length
Chris@1 100 );
Chris@1 101 }
Chris@1 102 if(obj->data.picture.type != (fn_only? FLAC__STREAM_METADATA_PICTURE_TYPE_FRONT_COVER : pf->type))
Chris@1 103 return failed_("picture type mismatch");
Chris@1 104 if(strcmp(obj->data.picture.mime_type, pf->mime_type))
Chris@1 105 return failed_("picture MIME type mismatch");
Chris@1 106 if(strcmp((const char *)obj->data.picture.description, (const char *)pf->description))
Chris@1 107 return failed_("picture description mismatch");
Chris@1 108 if(obj->data.picture.width != pf->width)
Chris@1 109 return failed_("picture width mismatch");
Chris@1 110 if(obj->data.picture.height != pf->height)
Chris@1 111 return failed_("picture height mismatch");
Chris@1 112 if(obj->data.picture.depth != pf->depth)
Chris@1 113 return failed_("picture depth mismatch");
Chris@1 114 if(obj->data.picture.colors != pf->colors)
Chris@1 115 return failed_("picture colors mismatch");
Chris@1 116 printf("OK\n");
Chris@1 117 FLAC__metadata_object_delete(obj);
Chris@1 118 return true;
Chris@1 119 }
Chris@1 120
Chris@1 121 static FLAC__bool do_picture(const char *prefix)
Chris@1 122 {
Chris@1 123 FLAC__StreamMetadata *obj;
Chris@1 124 const char *error;
Chris@1 125 size_t i;
Chris@1 126
Chris@1 127 printf("\n+++ grabbag unit test: picture\n\n");
Chris@1 128
Chris@1 129 /* invalid spec: no filename */
Chris@1 130 printf("testing grabbag__picture_parse_specification(\"\")... ");
Chris@1 131 if(0 != (obj = grabbag__picture_parse_specification("", &error)))
Chris@1 132 return failed_("expected error, got object");
Chris@1 133 printf("OK (failed as expected, error: %s)\n", error);
Chris@1 134
Chris@1 135 /* invalid spec: no filename */
Chris@1 136 printf("testing grabbag__picture_parse_specification(\"||||\")... ");
Chris@1 137 if(0 != (obj = grabbag__picture_parse_specification("||||", &error)))
Chris@1 138 return failed_("expected error, got object");
Chris@1 139 printf("OK (failed as expected: %s)\n", error);
Chris@1 140
Chris@1 141 /* invalid spec: no filename */
Chris@1 142 printf("testing grabbag__picture_parse_specification(\"|image/gif|||\")... ");
Chris@1 143 if(0 != (obj = grabbag__picture_parse_specification("|image/gif|||", &error)))
Chris@1 144 return failed_("expected error, got object");
Chris@1 145 printf("OK (failed as expected: %s)\n", error);
Chris@1 146
Chris@1 147 /* invalid spec: bad resolution */
Chris@1 148 printf("testing grabbag__picture_parse_specification(\"|image/gif|desc|320|0.gif\")... ");
Chris@1 149 if(0 != (obj = grabbag__picture_parse_specification("|image/gif|desc|320|0.gif", &error)))
Chris@1 150 return failed_("expected error, got object");
Chris@1 151 printf("OK (failed as expected: %s)\n", error);
Chris@1 152
Chris@1 153 /* invalid spec: bad resolution */
Chris@1 154 printf("testing grabbag__picture_parse_specification(\"|image/gif|desc|320x240|0.gif\")... ");
Chris@1 155 if(0 != (obj = grabbag__picture_parse_specification("|image/gif|desc|320x240|0.gif", &error)))
Chris@1 156 return failed_("expected error, got object");
Chris@1 157 printf("OK (failed as expected: %s)\n", error);
Chris@1 158
Chris@1 159 /* invalid spec: no filename */
Chris@1 160 printf("testing grabbag__picture_parse_specification(\"|image/gif|desc|320x240x9|\")... ");
Chris@1 161 if(0 != (obj = grabbag__picture_parse_specification("|image/gif|desc|320x240x9|", &error)))
Chris@1 162 return failed_("expected error, got object");
Chris@1 163 printf("OK (failed as expected: %s)\n", error);
Chris@1 164
Chris@1 165 /* invalid spec: #colors exceeds color depth */
Chris@1 166 printf("testing grabbag__picture_parse_specification(\"|image/gif|desc|320x240x9/2345|0.gif\")... ");
Chris@1 167 if(0 != (obj = grabbag__picture_parse_specification("|image/gif|desc|320x240x9/2345|0.gif", &error)))
Chris@1 168 return failed_("expected error, got object");
Chris@1 169 printf("OK (failed as expected: %s)\n", error);
Chris@1 170
Chris@1 171 /* invalid spec: standard icon has to be 32x32 PNG */
Chris@1 172 printf("testing grabbag__picture_parse_specification(\"1|-->|desc|32x24x9|0.gif\")... ");
Chris@1 173 if(0 != (obj = grabbag__picture_parse_specification("1|-->|desc|32x24x9|0.gif", &error)))
Chris@1 174 return failed_("expected error, got object");
Chris@1 175 printf("OK (failed as expected: %s)\n", error);
Chris@1 176
Chris@1 177 /* invalid spec: need resolution for linked URL */
Chris@1 178 printf("testing grabbag__picture_parse_specification(\"|-->|desc||http://blah.blah.blah/z.gif\")... ");
Chris@1 179 if(0 != (obj = grabbag__picture_parse_specification("|-->|desc||http://blah.blah.blah/z.gif", &error)))
Chris@1 180 return failed_("expected error, got object");
Chris@1 181 printf("OK (failed as expected: %s)\n", error);
Chris@1 182
Chris@1 183 printf("testing grabbag__picture_parse_specification(\"|-->|desc|320x240x9|http://blah.blah.blah/z.gif\")... ");
Chris@1 184 if(0 == (obj = grabbag__picture_parse_specification("|-->|desc|320x240x9|http://blah.blah.blah/z.gif", &error)))
Chris@1 185 return failed_(error);
Chris@1 186 printf("OK\n");
Chris@1 187 FLAC__metadata_object_delete(obj);
Chris@1 188
Chris@1 189 /* test automatic parsing of picture files from only the file name */
Chris@1 190 for(i = 0; i < sizeof(picturefiles)/sizeof(picturefiles[0]); i++)
Chris@1 191 if(!test_one_picture(prefix, picturefiles+i, "", /*fn_only=*/true))
Chris@1 192 return false;
Chris@1 193
Chris@1 194 /* test automatic parsing of picture files to get resolution/color info */
Chris@1 195 for(i = 0; i < sizeof(picturefiles)/sizeof(picturefiles[0]); i++)
Chris@1 196 if(!test_one_picture(prefix, picturefiles+i, "", /*fn_only=*/false))
Chris@1 197 return false;
Chris@1 198
Chris@1 199 picturefiles[0].width = 320;
Chris@1 200 picturefiles[0].height = 240;
Chris@1 201 picturefiles[0].depth = 3;
Chris@1 202 picturefiles[0].colors = 2;
Chris@1 203 if(!test_one_picture(prefix, picturefiles+0, "320x240x3/2", /*fn_only=*/false))
Chris@1 204 return false;
Chris@1 205
Chris@1 206 return true;
Chris@1 207 }
Chris@1 208
Chris@1 209 int main(int argc, char *argv[])
Chris@1 210 {
Chris@1 211 const char *usage = "usage: test_pictures path_prefix\n";
Chris@1 212
Chris@1 213 if(argc > 1 && 0 == strcmp(argv[1], "-h")) {
Chris@1 214 printf(usage);
Chris@1 215 return 0;
Chris@1 216 }
Chris@1 217
Chris@1 218 if(argc != 2) {
Chris@1 219 fprintf(stderr, usage);
Chris@1 220 return 255;
Chris@1 221 }
Chris@1 222
Chris@1 223 return do_picture(argv[1])? 0 : 1;
Chris@1 224 }