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

Associated ticket is #340

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

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

index 52abfe38dc35840110d196ddb84225c6db3e29c8..cb2c8cbad9922322ba7986ce6a51841dc93304e9 100644 (file)
@@ -1242,6 +1242,92 @@ CREATE OR REPLACE FUNCTION st_astiff(rast raster, nbands int[], compression text
        END;
        $$ LANGUAGE 'plpgsql' IMMUTABLE STRICT;
 
+-----------------------------------------------------------------------
+-- ST_AsJPEG
+-----------------------------------------------------------------------
+-- Cannot be strict as "options" can be NULL
+CREATE OR REPLACE FUNCTION st_asjpeg(rast raster, options text[])
+       RETURNS bytea
+       AS $$
+       DECLARE
+               num_bands int;
+               i int;
+       BEGIN
+               num_bands := st_numbands($1);
+
+               -- JPEG only supports 8BUI pixeltype
+               FOR i IN 1..num_bands LOOP
+                       IF st_bandpixeltype(rast, i) != '8BUI' THEN
+                               RAISE EXCEPTION 'The pixel type of band % in the raster is not 8BUI.  The JPEG format can only be used with the 8BUI pixel type.', i;
+                       END IF;
+               END LOOP;
+
+               -- JPEG only allows 1 or 3 bands
+               -- we only use the first
+               IF num_bands > 3 THEN
+                       RAISE NOTICE 'The JPEG 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 JPEG 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;
+
+               RETURN st_asgdalraster($1, 'JPEG', $2, NULL);
+       END;
+       $$ LANGUAGE 'plpgsql' IMMUTABLE;
+
+CREATE OR REPLACE FUNCTION st_asjpeg(rast raster)
+       RETURNS bytea
+       AS $$ SELECT st_asjpeg($1, NULL::text[]) $$
+       LANGUAGE 'SQL' IMMUTABLE STRICT;
+
+CREATE OR REPLACE FUNCTION st_asjpeg(rast raster, nbands int[], options text[])
+       RETURNS bytea
+       AS $$ SELECT st_asjpeg(st_band($1, $2), $3) $$
+       LANGUAGE 'SQL' IMMUTABLE STRICT;
+
+CREATE OR REPLACE FUNCTION st_asjpeg(rast raster, nbands int[])
+       RETURNS bytea
+       AS $$ SELECT st_asjpeg(st_band($1, $2), NULL::text[]) $$
+       LANGUAGE 'SQL' IMMUTABLE STRICT;
+
+CREATE OR REPLACE FUNCTION st_asjpeg(rast raster, nband int)
+       RETURNS bytea
+       AS $$ SELECT st_asjpeg(st_band($1, $2)) $$
+       LANGUAGE 'SQL' IMMUTABLE STRICT;
+
+CREATE OR REPLACE FUNCTION st_asjpeg(rast raster, nbands int[], quality int)
+       RETURNS bytea
+       AS $$
+       DECLARE
+               options text[];
+       BEGIN
+               IF quality IS NOT NULL THEN
+                       IF quality > 100 THEN
+                               quality := 100;
+                       ELSEIF quality < 10 THEN
+                               quality := 10;
+                       END IF;
+
+                       options := array_append(options, 'QUALITY=' || quality);
+               END IF;
+
+               RETURN st_asjpeg(st_band($1, $2), options);
+       END;
+       $$ LANGUAGE 'plpgsql' IMMUTABLE STRICT;
+
+CREATE OR REPLACE FUNCTION st_asjpeg(rast raster, nband int, options text[])
+       RETURNS bytea
+       AS $$ SELECT st_asjpeg(st_band($1, $2), $3) $$
+       LANGUAGE 'SQL' IMMUTABLE STRICT;
+
+CREATE OR REPLACE FUNCTION st_asjpeg(rast raster, nband int, quality int)
+       RETURNS bytea
+       AS $$ SELECT st_asjpeg($1, ARRAY[$2], $3) $$
+       LANGUAGE 'SQL' IMMUTABLE STRICT;
+
 -----------------------------------------------------------------------
 -- MapAlgebra
 -----------------------------------------------------------------------
index 51843861c0462cdeb9c845585bb763f538c059f5..0a972b26c0306128d5fde3bf013e190409e9371f 100644 (file)
@@ -44,6 +44,7 @@ TEST_FUNC = \
        rt_band.sql \
        rt_asgdalraster.sql \
        rt_astiff.sql \
+       rt_asjpeg.sql \
        $(NULL)
 
 TEST_PROPS = \
diff --git a/raster/test/regress/rt_asjpeg.sql b/raster/test/regress/rt_asjpeg.sql
new file mode 100644 (file)
index 0000000..17f1405
--- /dev/null
@@ -0,0 +1,81 @@
+SELECT md5(
+       ST_AsJPEG(
+               ST_AddBand(ST_MakeEmptyRaster(200, 200, 10, 10, 2, 2, 0, 0, -1), 1, '8BSI', 123, NULL)
+       )
+);
+SELECT md5(
+       ST_AsJPEG(
+               ST_AddBand(ST_MakeEmptyRaster(200, 200, 10, 10, 2, 2, 0, 0, -1), 1, '8BUI', 123, NULL)
+       )
+);
+SELECT md5(
+       ST_AsJPEG(
+               ST_AddBand(ST_MakeEmptyRaster(200, 200, 10, 10, 2, 2, 0, 0, -1), 1, '8BSI', -123, NULL)
+       )
+);
+SELECT md5(
+       ST_AsJPEG(
+               ST_AddBand(ST_MakeEmptyRaster(200, 200, 10, 10, 2, 2, 0, 0, -1), 1, '8BUI', 254, NULL)
+       )
+);
+SELECT md5(
+       ST_AsJPEG(
+               ST_AddBand(
+                       ST_AddBand(
+                               ST_AddBand(
+                                       ST_MakeEmptyRaster(200, 200, 10, 10, 2, 2, 0, 0, -1)
+                                       , 1, '8BUI', 1, 255
+                               )
+                               , 2, '8BUI', 11, 0
+                       )
+                       , 3, '8BUI', 111, 127
+               )
+       )
+);
+SELECT md5(
+       ST_AsJPEG(
+               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['QUALITY=90','PROGRESSIVE=ON']
+       )
+);
+SELECT md5(
+       ST_AsJPEG(
+               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[3,1],
+               50
+       )
+);
+SELECT md5(
+       ST_AsJPEG(
+               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, '8BUI', 111, -1
+               ),
+               ARRAY[3],
+               10
+       )
+);
diff --git a/raster/test/regress/rt_asjpeg_expected b/raster/test/regress/rt_asjpeg_expected
new file mode 100644 (file)
index 0000000..e84e5bb
--- /dev/null
@@ -0,0 +1,9 @@
+ERROR:  The pixel type of band 1 in the raster is not 8BUI.  The JPEG format can only be used with the 8BUI pixel type.
+f0de16a21de438249ec0bbd28eafe63c
+ERROR:  The pixel type of band 1 in the raster is not 8BUI.  The JPEG format can only be used with the 8BUI pixel type.
+83b6012757444ff7786b5e8473e6cea3
+NOTICE:  The JPEG format only permits one or three bands.  The first band will be used.
+bca07d85db735e4eaf2334969a548b10
+ERROR:  The pixel type of band 1 in the raster is not 8BUI.  The JPEG format can only be used with the 8BUI pixel type.
+ERROR:  The pixel type of band 1 in the raster is not 8BUI.  The JPEG format can only be used with the 8BUI pixel type.
+d687ffc7b8dcb3ef05cd4fe99656e45d