]> granicus.if.org Git - postgis/commitdiff
Add ST_AsPNG
authorBborie Park <bkpark at ucdavis.edu>
Mon, 16 May 2011 19:54:11 +0000 (19:54 +0000)
committerBborie Park <bkpark at ucdavis.edu>
Mon, 16 May 2011 19:54:11 +0000 (19:54 +0000)
- added SQL functions for ST_AsPNG
- added regression tests

Associated ticket is #342

git-svn-id: http://svn.osgeo.org/postgis/trunk@7158 b70326c6-7e19-0410-871a-916f4a2858ee

raster/rt_pg/rtpostgis.sql.in.c
raster/test/regress/Makefile.in
raster/test/regress/rt_aspng.sql [new file with mode: 0644]
raster/test/regress/rt_aspng_expected [new file with mode: 0644]

index cb2c8cbad9922322ba7986ce6a51841dc93304e9..4b3b0158d41a3b9cca4348b491622b8501fbb9f5 100644 (file)
@@ -1328,6 +1328,93 @@ CREATE OR REPLACE FUNCTION st_asjpeg(rast raster, nband int, quality int)
        AS $$ SELECT st_asjpeg($1, ARRAY[$2], $3) $$
        LANGUAGE 'SQL' IMMUTABLE STRICT;
 
+-----------------------------------------------------------------------
+-- ST_AsPNG
+-----------------------------------------------------------------------
+-- Cannot be strict as "options" can be NULL
+CREATE OR REPLACE FUNCTION st_aspng(rast raster, options text[])
+       RETURNS bytea
+       AS $$
+       DECLARE
+               num_bands int;
+               i int;
+               pt text;
+       BEGIN
+               num_bands := st_numbands($1);
+
+               -- PNG only allows 1 or 3 bands
+               IF num_bands > 3 THEN
+                       RAISE NOTICE 'The PNG format only permits one or three bands.  The first three bands will be used.';
+                       rast := st_band(rast, ARRAY[1, 2, 3]);
+                       num_bands := st_numbands(rast);
+               ELSEIF num_bands > 1 THEN
+                       RAISE NOTICE 'The PNG format only permits one or three bands.  The first band will be used.';
+                       rast := st_band(rast, ARRAY[1]);
+                       num_bands := st_numbands(rast);
+               END IF;
+
+               -- PNG only supports 8BUI and 16BUI pixeltype
+               FOR i IN 1..num_bands LOOP
+                       pt = st_bandpixeltype(rast, i);
+                       IF pt != '8BUI' AND pt != '16BUI' THEN
+                               RAISE EXCEPTION 'The pixel type of band % in the raster is not 8BUI or 16BUI.  The PNG format can only be used with 8BUI and 16BUI pixel types.', i;
+                       END IF;
+               END LOOP;
+
+               RETURN st_asgdalraster($1, 'PNG', $2, NULL);
+       END;
+       $$ LANGUAGE 'plpgsql' IMMUTABLE;
+
+CREATE OR REPLACE FUNCTION st_aspng(rast raster)
+       RETURNS bytea
+       AS $$ SELECT st_aspng($1, NULL::text[]) $$
+       LANGUAGE 'SQL' IMMUTABLE STRICT;
+
+CREATE OR REPLACE FUNCTION st_aspng(rast raster, nbands int[], options text[])
+       RETURNS bytea
+       AS $$ SELECT st_aspng(st_band($1, $2), $3) $$
+       LANGUAGE 'SQL' IMMUTABLE STRICT;
+
+CREATE OR REPLACE FUNCTION st_aspng(rast raster, nbands int[])
+       RETURNS bytea
+       AS $$ SELECT st_aspng(st_band($1, $2), NULL::text[]) $$
+       LANGUAGE 'SQL' IMMUTABLE STRICT;
+
+CREATE OR REPLACE FUNCTION st_aspng(rast raster, nband int)
+       RETURNS bytea
+       AS $$ SELECT st_aspng(st_band($1, $2)) $$
+       LANGUAGE 'SQL' IMMUTABLE STRICT;
+
+CREATE OR REPLACE FUNCTION st_aspng(rast raster, nbands int[], compression int)
+       RETURNS bytea
+       AS $$
+       DECLARE
+               options text[];
+       BEGIN
+               IF compression IS NOT NULL THEN
+                       IF compression > 9 THEN
+                               compression := 9;
+                       ELSEIF compression < 1 THEN
+                               compression := 1;
+                       END IF;
+
+                       options := array_append(options, 'ZLEVEL=' || compression);
+               END IF;
+
+               RETURN st_aspng(st_band($1, $2), options);
+       END;
+       $$ LANGUAGE 'plpgsql' IMMUTABLE STRICT;
+
+CREATE OR REPLACE FUNCTION st_aspng(rast raster, nband int, options text[])
+       RETURNS bytea
+       AS $$ SELECT st_aspng(st_band($1, $2), $3) $$
+       LANGUAGE 'SQL' IMMUTABLE STRICT;
+
+CREATE OR REPLACE FUNCTION st_aspng(rast raster, nband int, compression int)
+       RETURNS bytea
+       AS $$ SELECT st_aspng($1, ARRAY[$2], $3) $$
+       LANGUAGE 'SQL' IMMUTABLE STRICT;
+
 -----------------------------------------------------------------------
 -- MapAlgebra
 -----------------------------------------------------------------------
index 0a972b26c0306128d5fde3bf013e190409e9371f..b7eae7402c4ad80cd60cd08c139f46ca565f4e21 100644 (file)
@@ -45,6 +45,7 @@ TEST_FUNC = \
        rt_asgdalraster.sql \
        rt_astiff.sql \
        rt_asjpeg.sql \
+       rt_aspng.sql \
        $(NULL)
 
 TEST_PROPS = \
diff --git a/raster/test/regress/rt_aspng.sql b/raster/test/regress/rt_aspng.sql
new file mode 100644 (file)
index 0000000..898c410
--- /dev/null
@@ -0,0 +1,94 @@
+SELECT md5(
+       ST_AsPNG(
+               ST_AddBand(ST_MakeEmptyRaster(200, 200, 10, 10, 2, 2, 0, 0, -1), 1, '8BSI', 123, NULL)
+       )
+);
+SELECT md5(
+       ST_AsPNG(
+               ST_AddBand(ST_MakeEmptyRaster(200, 200, 10, 10, 2, 2, 0, 0, -1), 1, '8BUI', 123, NULL)
+       )
+);
+SELECT md5(
+       ST_AsPNG(
+               ST_AddBand(ST_MakeEmptyRaster(200, 200, 10, 10, 2, 2, 0, 0, -1), 1, '8BSI', -123, NULL)
+       )
+);
+SELECT md5(
+       ST_AsPNG(
+               ST_AddBand(ST_MakeEmptyRaster(200, 200, 10, 10, 2, 2, 0, 0, -1), 1, '8BUI', 254, NULL)
+       )
+);
+SELECT md5(
+       ST_AsPNG(
+               ST_AddBand(
+                       ST_AddBand(
+                               ST_AddBand(
+                                       ST_MakeEmptyRaster(200, 200, 10, 10, 2, 2, 0, 0, -1)
+                                       , 1, '8BSI', 1, -1
+                               )
+                               , 2, '8BSI', 11, -1
+                       )
+                       , 3, '8BSI', 111, -1
+               ),
+               ARRAY['ZLEVEL=1']
+       )
+);
+SELECT md5(
+       ST_AsPNG(
+               ST_AddBand(
+                       ST_AddBand(
+                               ST_AddBand(
+                                       ST_MakeEmptyRaster(200, 200, 10, 10, 2, 2, 0, 0, -1)
+                                       , 1, '8BSI', 1, -1
+                               )
+                               , 2, '8BSI', 11, -1
+                       )
+                       , 3, '8BSI', 111, -1
+               ),
+               ARRAY['ZLEVEL=9']
+       )
+);
+SELECT md5(
+       ST_AsPNG(
+               ST_AddBand(
+                       ST_AddBand(
+                               ST_AddBand(
+                                       ST_MakeEmptyRaster(200, 200, 10, 10, 2, 2, 0, 0, -1)
+                                       , 1, '8BSI', 1, 1
+                               )
+                               , 2, '8BSI', 11, 1
+                       )
+                       , 3, '8BSI', 111, 1
+               ),
+               ARRAY['ZLEVEL=9']
+       )
+);
+SELECT md5(
+       ST_AsPNG(
+               ST_AddBand(
+                       ST_AddBand(
+                               ST_MakeEmptyRaster(200, 200, 10, 10, 2, 2, 0, 0, -1)
+                               , 1, '8BUI', 1, 1
+                       )
+                       , 2, '8BUI', 11, 1
+               ),
+               ARRAY[1],
+               6
+       )
+);
+SELECT md5(
+       ST_AsPNG(
+               ST_AddBand(
+                       ST_AddBand(
+                               ST_AddBand(
+                                       ST_MakeEmptyRaster(200, 200, 10, 10, 2, 2, 0, 0, -1)
+                                       , 1, '8BUI', 1, 1
+                               )
+                               , 2, '8BUI', 11, 1
+                       )
+                       , 3, '8BUI', 111, 1
+               ),
+               ARRAY[3,1],
+               6
+       )
+);
diff --git a/raster/test/regress/rt_aspng_expected b/raster/test/regress/rt_aspng_expected
new file mode 100644 (file)
index 0000000..460bb9d
--- /dev/null
@@ -0,0 +1,13 @@
+ERROR:  The pixel type of band 1 in the raster is not 8BUI or 16BUI.  The PNG format can only be used with 8BUI and 16BUI pixel types.
+55279950e29968bcf36b2c11ce8bf88b
+ERROR:  The pixel type of band 1 in the raster is not 8BUI or 16BUI.  The PNG format can only be used with 8BUI and 16BUI pixel types.
+24188762b5745acda4aa7b92776e7280
+NOTICE:  The PNG format only permits one or three bands.  The first band will be used.
+ERROR:  The pixel type of band 1 in the raster is not 8BUI or 16BUI.  The PNG format can only be used with 8BUI and 16BUI pixel types.
+NOTICE:  The PNG format only permits one or three bands.  The first band will be used.
+ERROR:  The pixel type of band 1 in the raster is not 8BUI or 16BUI.  The PNG format can only be used with 8BUI and 16BUI pixel types.
+NOTICE:  The PNG format only permits one or three bands.  The first band will be used.
+ERROR:  The pixel type of band 1 in the raster is not 8BUI or 16BUI.  The PNG format can only be used with 8BUI and 16BUI pixel types.
+dfff54767aad7aa842f45e5b83326f0b
+NOTICE:  The PNG format only permits one or three bands.  The first band will be used.
+58168aa11f7ccdf095e5ed02bde1e676