@@ -1020,7 +1020,7 @@ package body GID.Decoding_JPG is
1020
1020
y_val_8 : U8;
1021
1021
begin
1022
1022
for ymb in flat'Range (2 ) loop
1023
- exit when y0 + Integer_32 (ymb) >= image.height;
1023
+ exit when y0 + Integer_32 (ymb) >= image.height or else x0 >= image.width ;
1024
1024
Set_X_Y (Integer (x0), Integer (image.height - 1 - (y0 + Integer_32 (ymb))));
1025
1025
for xmb in flat'Range (1 ) loop
1026
1026
exit when x0 + Integer_32 (xmb) >= image.width;
@@ -1291,7 +1291,7 @@ package body GID.Decoding_JPG is
1291
1291
declare
1292
1292
info : JPEG_Defs.Info_per_Component_A
1293
1293
renames image.JPEG_stuff.info (c);
1294
- block_x, block_y, delta_x, delta_y : Natural_32;
1294
+ block_x, block_y, x_delta_x, y_delta_y : Natural_32;
1295
1295
new_bit : Integer;
1296
1296
begin
1297
1297
-- (x, y) coordinates, on the image, of current MCU's corner
@@ -1314,14 +1314,16 @@ package body GID.Decoding_JPG is
1314
1314
-- Coordinates of the block on the current MCU
1315
1315
block_x := Natural_32 (block_count mod info.ups.samples_hor);
1316
1316
block_y := Natural_32 (block_count / info.ups.samples_hor);
1317
- delta_x := 8 * block_x;
1318
- delta_y := 8 * block_y;
1317
+ x_delta_x := x + 8 * block_x;
1318
+ y_delta_y := y + 8 * block_y;
1319
1319
if refining then
1320
1320
-- Refining scan for the DC values
1321
1321
new_bit := Get_Bits (1 );
1322
- image_array (x + delta_x, y + delta_y, compo_idx) :=
1323
- image_array (x + delta_x, y + delta_y, compo_idx) +
1324
- new_bit * (2 ** bit_position_low);
1322
+ if x_delta_x <= image_array'Last (1 ) and then y_delta_y <= image_array'Last (2 ) then
1323
+ image_array (x_delta_x, y_delta_y, compo_idx) :=
1324
+ image_array (x_delta_x, y_delta_y, compo_idx) +
1325
+ new_bit * (2 ** bit_position_low);
1326
+ end if ;
1325
1327
else
1326
1328
-- Decode DC value
1327
1329
Get_VLC
@@ -1332,17 +1334,18 @@ package body GID.Decoding_JPG is
1332
1334
info_B (c).dc_predictor := dc_value;
1333
1335
-- Store the partial DC value on the image array
1334
1336
-- Note that dc_value can be (and is often) negative.
1335
- image_array (x + delta_x, y + delta_y, compo_idx) :=
1336
- dc_value * (2 ** bit_position_low);
1337
+
1338
+ if x_delta_x <= image_array'Last (1 ) and then y_delta_y <= image_array'Last (2 ) then
1339
+ image_array (x_delta_x, y_delta_y, compo_idx) := dc_value * (2 ** bit_position_low);
1340
+ end if ;
1337
1341
end if ;
1338
1342
if full_trace then
1339
1343
Put_Line
1340
1344
(dump_file,
1341
1345
dump_sep & dump_sep & dump_sep &
1342
- Integer_32'Image (x + delta_x) & dump_sep &
1343
- Integer_32'Image (y + delta_y) & dump_sep &
1344
- image_array
1345
- (x + delta_x, y + delta_y, compo_idx)'Image);
1346
+ Integer_32'Image (x_delta_x) & dump_sep &
1347
+ Integer_32'Image (y_delta_y) & dump_sep &
1348
+ image_array (x_delta_x, y_delta_y, compo_idx)'Image);
1346
1349
end if ;
1347
1350
end loop ;
1348
1351
end ;
@@ -1756,7 +1759,7 @@ package body GID.Decoding_JPG is
1756
1759
-- for the upsampling, which gives a bit more complexity, especially
1757
1760
-- since it depends on settings *per component*...
1758
1761
x_image_array, y_image_array :
1759
- array (Component) of Integer_32 := (others => 0 );
1762
+ array (Component) of Natural_32 := (others => 0 );
1760
1763
1761
1764
qt_zz : array (Component) of JPEG_Defs.Quantization_Table;
1762
1765
@@ -1799,17 +1802,20 @@ package body GID.Decoding_JPG is
1799
1802
declare
1800
1803
block : Block_8x8 renames mb (c, sbx, sby);
1801
1804
qt_local : JPEG_Defs.Quantization_Table renames qt_zz (c);
1805
+ x_base : constant Natural_32 := x_image_array (c) + Integer_32 (8 * (sbx - 1 ));
1806
+ y_base : constant Natural_32 := y_image_array (c) + Integer_32 (8 * (sby - 1 ));
1802
1807
begin
1803
- -- Copy block data:
1804
- for yb in reverse 0 .. 7 loop
1805
- for xb in reverse 0 .. 7 loop
1806
- block (xb + yb * 8 ) :=
1807
- image_array
1808
- (x_image_array (c) + Integer_32 (8 * (sbx - 1 ) + xb),
1809
- y_image_array (c) + Integer_32 (8 * (sby - 1 ) + yb),
1810
- c_idx);
1808
+ if x_base + 7 <= image_array'Last (1 ) and then y_base + 7 <= image_array'Last (2 ) then
1809
+ -- Copy block data:
1810
+ for yb in reverse 0 .. 7 loop
1811
+ for xb in reverse 0 .. 7 loop
1812
+ block (xb + yb * 8 ) :=
1813
+ image_array (x_base + Natural_32 (xb), y_base + Natural_32 (yb), c_idx);
1814
+ end loop ;
1811
1815
end loop ;
1812
- end loop ;
1816
+ else
1817
+ block := (others => 0 );
1818
+ end if ;
1813
1819
-- Undo quantization on the block:
1814
1820
for i in reverse 0 .. 63 loop
1815
1821
block (i) := block (i) * qt_local (i);
0 commit comments